Sitemap
Sonam G.

Sonam G.

Passionate Developer Advocate, Experienced Data Scientist, recent Doctorate

Predict Churn Before Customers Leave

Build a Python app with Telnyx AI Inference that turns customer activity signals into churn risk, recommended actions, and retention next steps.

4 min readJun 29, 2026

--

Press enter or click to view image in full size

Customer churn usually does not come out of nowhere. The signals are often already there: usage is dropping, support tickets are increasing, payment issues are showing up, renewal is getting close, or a customer who used to be active has gone quiet.

The hard part is turning those signals into something a customer success, support, or sales team can act on quickly.

I built a small Python example that uses Telnyx AI Inference to analyze customer activity patterns and return a structured churn-risk assessment with recommended interventions.

Code example: https://github.com/team-telnyx/telnyx-code-examples/tree/main/ai-customer-churn-predictor-python

What the app does

The app exposes a Flask API where you send customer signals like:

- monthly call volume trends
- monthly message volume trends
- recent support ticket count
- account age
- days until renewal
- last login recency
- payment issues

The app sends those signals to Telnyx AI Inference using the chat completions endpoint and asks the model to return structured JSON.

The response can include:

- churn risk: high, medium, or low
- probability
- risk factors
- recommended actions
- urgency
- estimated revenue at risk

Instead of just saying “this customer looks risky,” the app gives you a structured response your system can route, store, display, or use to trigger the next step.

Why this pattern is useful

A lot of AI apps do not need to start as huge platforms.

Sometimes the useful version is a small decision-support layer that helps a team make sense of operational signals faster.

For churn, that might mean pulling together customer data from your CRM, usage database, billing system, support tool, or communications stack.

Then an inference model can help summarize:

- who needs attention first
- why the account looks risky
- what action might help
- how urgent the next step is

That turns scattered data into something closer to an operator-ready workflow.

How the example works

The app has four endpoints:

- `POST /predict`
- `POST /predict/batch`
- `GET /predictions`
- `GET /health`

The main flow happens in `/predict`.

You send customer data to the endpoint. The app builds a prompt from the customer’s activity signals, sends it to Telnyx AI Inference, and asks for JSON with churn risk, probability, risk factors, recommended actions, urgency, and estimated revenue at risk.

Get Sonam G.’s stories in your inbox

Join Medium for free to get updates from this writer.

The example currently uses:

AI_MODEL=moonshotai/Kimi-K2.6 # You can pick any model from Telnyx inference

You can change that in .env if you want to test another model supported by Telnyx AI Inference. The app also includes a small JSON parsing helper. That matters because even when you ask an LLM for JSON, your application should still treat model output like external input and parse it carefully before using it.

Example request

curl -X POST http://localhost:5000/predict \
-H "Content-Type: application/json" \
-d '{
"customer_id": "CUST-123",
"call_volumes": [120, 105, 80, 55],
"message_volumes": [450, 420, 300, 190],
"support_tickets": 6,
"account_age_months": 18,
"renewal_days": 21,
"last_login_days": 14,
"payment_issues": 1
}'

A response might look like:

{
"churn_risk": "high",
"probability": 0.82,
"risk_factors": [
"Declining communication volume",
"Multiple support tickets in the last 90 days",
"Renewal date is approaching"
],
"recommended_actions": [
"Schedule a customer success check-in this week",
"Review unresolved support tickets",
"Offer renewal assistance or usage optimization guidance"
],
"urgency": "this_week",
"estimated_revenue_at_risk": "unknown",
"customer_id": "CUST-123",
"predicted_at": "2026-06-29T00:00:00Z"
}

Batch prediction

The example also includes a batch endpoint:

curl -X POST http://localhost:5000/predict/batch \
-H "Content-Type: application/json" \
-d '{
"customers": [
{
"customer_id": "CUST-123",
"call_volumes": [120, 105, 80, 55],
"support_tickets": 6,
"last_login_days": 14
},
{
"customer_id": "CUST-456",
"call_volumes": [80, 82, 85, 90],
"support_tickets": 1,
"last_login_days": 1
}
]
}'

That makes it easier to test the same pattern across a small group of accounts and quickly see how many are high risk.

Try it locally

Clone the repo:

git clone https://github.com/team-telnyx/telnyx-code-examples.git
cd telnyx-code-examples/ai-customer-churn-predictor-python

Set up your environment:

cp .env.example .env
pip install -r requirements.txt
python app.py

Add your Telnyx API key to .env:

TELNYX_API_KEY=your_telnyx_api_key
AI_MODEL=moonshotai/Kimi-K2.6
HOST=127.0.0.1

Then call /predict with customer activity data.

What you could build next

This example keeps storage in memory so the core AI workflow is easy to understand.

For a production version, you could connect it to:

  • a CRM for account details
  • a product database for usage trends
  • a helpdesk for support ticket history
  • a billing system for payment issues
  • a data warehouse for account health metrics

Resources

Code: https://github.com/team-telnyx/telnyx-code-examples/tree/main/ai-customer-churn-predictor-python
Telnyx AI skills and toolkits: https://github.com/team-telnyx/ai
Telnyx AI Inference docs: https://developers.telnyx.com/docs/inference
Chat Completions API: https://developers.telnyx.com/api/inference/chat-completions
Telnyx Portal: https://portal.telnyx.com

--

--