{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreidhgcjahdqcurmjpxxtq5iy4amfv3y4pkeivioodrmftpn6vmcobe",
    "uri": "at://did:plc:25rdn5elo5izoxrmtis34zuk/app.bsky.feed.post/3mpjyqact47a2"
  },
  "coverImage": {
    "$type": "blob",
    "ref": {
      "$link": "bafkreibkgb6ulhyutrgjybw42hqilnln7vemsnunarlvs4halgvq6iz4c4"
    },
    "mimeType": "image/webp",
    "size": 51942
  },
  "path": "/sonam_50a41a4ced7e6b4f3fa/predict-churn-before-customers-leave-36m6",
  "publishedAt": "2026-06-30T21:48:29.000Z",
  "site": "https://dev.to",
  "tags": [
    "telnyx",
    "ai",
    "devrel",
    "https://github.com/team-telnyx/telnyx-code-examples/tree/main/ai-customer-churn-predictor-python",
    "https://github.com/team-telnyx/ai",
    "https://developers.telnyx.com/docs/inference",
    "https://developers.telnyx.com/api/inference/chat-completions",
    "https://portal.telnyx.com/"
  ],
  "textContent": "Subtitle: Build a Python app with Telnyx AI Inference that turns customer activity signals into churn risk, recommended actions, and retention next steps.\n\nMost customer churn is only surprising because the signals were scattered.\n\nUsage dropped in one place. Support tickets went up somewhere else. A renewal date got closer. A login did not happen for two weeks. Payment issues started showing up. None of those signals alone proves a customer is leaving, but together they usually tell a story.\n\nThat is the workflow I wanted to make easier to build: take customer activity data, pass it through an inference model, and return a structured churn assessment that a product or customer success team can actually use.\n\nThe example is here:\n\nhttps://github.com/team-telnyx/telnyx-code-examples/tree/main/ai-customer-churn-predictor-python\n\nIt is a small Flask app using Telnyx AI Inference through the chat-completions API.\n\n##  The App Shape\n\nThe app exposes a few routes:\n\n  * `POST /predict` for one customer\n  * `POST /predict/batch` for up to 20 customers\n  * `GET /predictions` for recent in-memory predictions\n  * `GET /health` for app health\n\n\n\nThe current default model is set in `.env.example`:\n\n\n\n    AI_MODEL=moonshotai/Kimi-K2.6\n\n\nUnder the hood, the app calls:\n\n\n\n    POST https://api.telnyx.com/v2/ai/chat/completions\n\n\nThe prompt asks the model to behave like a customer success analyst and return JSON only. That is the important part. This is not a chatbot. It is an application endpoint that produces structured output.\n\n##  What Goes In\n\nA request can look like this:\n\n\n\n    curl -X POST http://localhost:5000/predict \\\n      -H \"Content-Type: application/json\" \\\n      -d '{\n        \"customer_id\": \"CUST-123\",\n        \"call_volumes\": [120, 105, 80, 55],\n        \"message_volumes\": [450, 420, 300, 190],\n        \"support_tickets\": 6,\n        \"account_age_months\": 18,\n        \"renewal_days\": 21,\n        \"last_login_days\": 14,\n        \"payment_issues\": 1\n      }'\n\n\nThose fields are deliberately simple. The point is to show the pattern, not to pretend this is a full enterprise churn model.\n\nThe model gets the trend data, support context, account age, renewal window, login recency, and payment issues. Then it returns risk, probability, risk factors, recommended actions, urgency, and revenue-at-risk context.\n\nThat response can feed a dashboard, trigger a customer success task, create a review queue, or become part of a larger retention workflow.\n\n##  Why I Like This Pattern\n\nA lot of AI product ideas start with a blank chat box.\n\nBut many useful AI apps are not blank chat boxes. They are small decision endpoints with clear inputs and clear outputs.\n\nFor churn prediction, the output shape matters:\n\n\n\n    {\n      \"churn_risk\": \"high\",\n      \"probability\": 0.82,\n      \"risk_factors\": [\n        \"Usage declined over the last four months\",\n        \"Support tickets increased\",\n        \"Renewal date is approaching\"\n      ],\n      \"recommended_actions\": [\n        \"Schedule an account review this week\",\n        \"Investigate open support ticket themes\"\n      ],\n      \"urgency\": \"this_week\"\n    }\n\n\nThat is easier to build with than a paragraph. You can sort it, store it, validate it, route it, or show it in a product.\n\n##  The Small Detail That Matters\n\nThe app asks for JSON, but it still treats model output carefully.\n\nIt includes a helper that strips markdown fences and extracts the JSON object before parsing. That is the sort of thing you want in AI apps. Prompts help, but validation still matters.\n\nThe sample keeps the implementation readable so developers can inspect the whole flow in one file.\n\n##  What I Would Add Next\n\nThe example uses in-memory storage because it is meant to be easy to run locally. In production, I would add:\n\n  * Persistent storage for predictions\n  * Auth on the API routes\n  * A real customer data source\n  * Strict schema validation\n  * Batch processing through a queue\n  * Alerting for high-risk accounts\n  * Human review before customer-facing interventions\n\n\n\nThe repo is also structured to be agent-readable. That means your coding agent can inspect the README, API reference, guide, environment file, and app code, then help you extend it. You can ask it to add persistence, write tests, connect the output to a CRM, or turn the batch endpoint into a scheduled workflow.\n\n##  Try It\n\nCode:\nhttps://github.com/team-telnyx/telnyx-code-examples/tree/main/ai-customer-churn-predictor-python\n\nTelnyx AI skills and toolkits:\nhttps://github.com/team-telnyx/ai\n\nTelnyx AI Inference docs:\nhttps://developers.telnyx.com/docs/inference\n\nChat Completions API:\nhttps://developers.telnyx.com/api/inference/chat-completions\n\nTelnyx Portal:\nhttps://portal.telnyx.com/",
  "title": "Predict Churn Before Customers Leave"
}