Churn prediction modeling

From CraftOS-dev/craftbot-agent-bundles · View interactive version

Churn prediction modeling is an AI agent skill from the GitHub repository CraftOS-dev/craftbot-agent-bundles. Source: CamDavidsonPilon lifelines SaaS churn notebook + saaslatestnews 2026 AI-powered churn + Andrew Chen retention

How to use this skill

Skill file

<!--

Source: CamDavidsonPilon lifelines SaaS churn notebook + saaslatestnews 2026 AI-powered churn + Andrew Chen retention

-->

Churn Prediction Modeling — lifelines Cox PH + LLM Signals SKILL

Predict per-user time-to-churn using lifelines survival analysis (Cox Proportional Hazards, Kaplan-Meier, AFT). 2026 SOTA: augment structured features with LLM-extracted signals from tickets, NPS comments, and call transcripts. Output at-risk cohorts piped to win-back skills.

When to use

Trigger phrases:

Pair: retention-curve-churn-diagnosis-j-smile (diagnose shape FIRST; predict only on J/flat shapes — decay = activation problem), win-back-campaigns (downstream action), pql-product-qualified-leads-framework (positive analog of same scoring approach).

Setup

export POSTHOG_PERSONAL_API_KEY="phx_..."
export POSTGRES_URL="postgresql://..."

# Install via cli-anything uvx (ephemeral)
# uvx --from lifelines python -c "from lifelines import CoxPHFitter; print('OK')"
pip install lifelines pandas numpy scikit-learn matplotlib

# For LLM signals
pip install anthropic
export ANTHROPIC_API_KEY="sk-ant-..."

Survival analysis canonical models

| Model | When to use | Strength | Limitation |

|---|---|---|---|

| Kaplan-Meier | Group-level survival curve | Non-parametric, no assumptions | No covariates |

| Cox Proportional Hazards | Per-feature hazard ratios | Interpretable per feature | Assumes proportional hazards |

| Accelerated Failure Time (AFT) | When PH assumption violated | Direct time-to-event predict | Distributional assumption |

| Random Survival Forests | Non-linear, many features | High predictive | Less interpretable |

| DeepSurv / Pycox | Big data, deep features | Best predictive | Black box, infra |

Default: Cox PH for interpretability + Kaplan-Meier for group curves.

Common recipes

Recipe 1: Data preparation (cohort + features + churn event)

import pandas as pd
import psycopg2

# Pull cohort with tenure + event + features
conn = psycopg2.connect(POSTGRES_URL)
df = pd.read_sql("""
  SELECT
    user_id,
    DATEDIFF('day', signup_date, COALESCE(churn_date, CURRENT_DATE)) AS tenure_days,
    CASE WHEN churn_date IS NOT NULL THEN 1 ELSE 0 END AS churned,
    -- Structured features
    feature_adoption_count,
    team_size,
    weekly_active_days_avg,
    nps_score,
    support_tickets_count,
    plan_tier,
    monthly_revenue,
    -- Time-since-last-action
    days_since_last_session,
    days_since_last_core_action,
    -- Lifecycle
    onboarding_completed,
    activation_event_hit
  FROM analytics.users
  WHERE signup_date >= now() - INTERVAL '24 months'
""", conn)

# Encode categorical
df = pd.get_dummies(df, columns=['plan_tier'], drop_first=True)
df = df.dropna()  # or fillna with median; depends on missing-data semantics

Recipe 2: Cox PH model

from lifelines import CoxPHFitter

cph = CoxPHFitter()
cph.fit(df, duration_col='tenure_days', event_col='churned')
cph.print_summary()

# Hazard ratios:
# exp(coef) > 1.0 → feature increases churn risk
# exp(coef) < 1.0 → feature decreases churn risk
# p < 0.05 → statistically significant

Interpretation example:

Recipe 3: Kaplan-Meier survival curves per segment

from lifelines import KaplanMeierFitter
import matplotlib.pyplot as plt

kmf = KaplanMeierFitter()

for tier in df['plan_tier'].unique():
    sub = df[df['plan_tier'] == tier]
    kmf.fit(sub['tenure_days'], event_observed=sub['churned'], label=tier)
    kmf.plot_survival_function()

plt.xlabel('Tenure (days)')
plt.ylabel('Survival probability')
plt.title('Survival curves by plan tier')
plt.savefig('survival_by_tier.png')

Shows: probability of being alive at day N per group.

Recipe 4: Predict at-risk cohort (30-day survival)

# For each current user, predict 30-day survival probability
df['surv_30'] = cph.predict_survival_function(df, times=[30]).T.iloc[:, 0]

# At-risk = < 70% chance of surviving 30 days
at_risk = df[df['surv_30'] < 0.70].sort_values('surv_30')

# Pipe to Customer.io / Klaviyo for win-back
at_risk[['user_id', 'surv_30', 'tenure_days']].to_csv('at_risk_users.csv', index=False)

Recipe 5: Accelerated Failure Time (when PH violated)

from lifelines import WeibullAFTFitter

aft = WeibullAFTFitter()
aft.fit(df, duration_col='tenure_days', event_col='churned')
aft.print_summary()

# Direct predicted time-to-churn
df['predicted_churn_day'] = aft.predict_median(df)

Test PH assumption:

from lifelines.statistics import proportional_hazard_test
results = proportional_hazard_test(cph, df, time_transform='rank')
results.print_summary()
# If p < 0.05 → PH violated → use AFT or stratified Cox

Recipe 6: LLM-augmented signals (2026 SOTA)

from anthropic import Anthropic
client = Anthropic()

def extract_ticket_signals(ticket_text):
    msg = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=200,
        messages=[{
            "role": "user",
            "content": f"""Extract churn-risk signals from this support ticket.
            Return JSON only:
            {{
              "frustration": 0-10,
              "competitor_mention": true/false,
              "cancellation_intent": true/false,
              "pricing_complaint": true/false,
              "feature_request_missing": true/false
            }}
            
            Ticket: {ticket_text}"""
        }]
    )
    return json.loads(msg.content[0].text)

# Apply to all recent tickets per user
df['llm_frustration'] = df['recent_ticket_text'].apply(
    lambda x: extract_ticket_signals(x)['frustration'] if x else 0
)
df['competitor_mention'] = df['recent_ticket_text'].apply(
    lambda x: int(extract_ticket_signals(x)['competitor_mention']) if x else 0
)

# Re-fit Cox PH with LLM features
cph.fit(df, duration_col='tenure_days', event_col='churned')
# LLM features typically rank top 5 in hazard contribution

Recipe 7: NPS comment signal

def extract_nps_signal(comment, score):
    if not comment:
        return {"churn_intent": 0}
    msg = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=100,
        messages=[{
            "role": "user",
            "content": f"""NPS score: {score}/10. Comment: "{comment}"
            Rate churn intent 0-10. JSON: {{"churn_intent": int}}"""
        }]
    )
    return json.loads(msg.content[0].text)

# Detractor (NPS 0-6) + cancellation language = high risk

Recipe 8: Call-transcript red flags (Otter / Gong / Fathom)

# For sales-led / CSM-led motion
def extract_call_red_flags(transcript):
    msg = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=400,
        messages=[{
            "role": "user",
            "content": f"""From this CSM call transcript, extract red flags.
            JSON:
            {{
              "champion_left_company": bool,
              "budget_cut_mentioned": bool,
              "alternative_evaluation": bool,
              "executive_buy_in_lost": bool,
              "renewal_doubt": 0-10
            }}
            Transcript: {transcript[:8000]}"""
        }]
    )
    return json.loads(msg.content[0].text)

Each flag = binary covariate in Cox PH.

Recipe 9: Handoff to win-back

# Tier at-risk users by risk level → different intervention
def tier_at_risk(surv_30):
    if surv_30 < 0.40: return 'critical'      # CSM call / phone outreach
    if surv_30 < 0.55: return 'high'           # Custom email + discount
    if surv_30 < 0.70: return 'moderate'       # Automated win-back sequence
    return 'low'                                # Monitor only

at_risk['intervention_tier'] = at_risk['surv_30'].apply(tier_at_risk)

# Pipe via Customer.io API to win-back sequences
for _, row in at_risk.iterrows():
    if row['intervention_tier'] == 'critical':
        hubspot.create_task(owner=row['csm_id'], note='Churn risk: schedule call this week')
        slack.send('#csm-alerts', f"At-risk: {row['user_email']}")
    elif row['intervention_tier'] in ('high', 'moderate'):
        customerio.add_to_segment(row['user_id'], 'win_back_high' if row['intervention_tier'] == 'high' else 'win_back_moderate')

Recipe 10: Model validation + monitoring

from lifelines.utils import concordance_index

# C-index = ranking quality (0.5 = random; 0.7+ = good; 0.8+ = excellent)
c_index = concordance_index(
    df['tenure_days'],
    -cph.predict_partial_hazard(df),  # negate because higher hazard = shorter survival
    df['churned']
)
print(f"C-index: {c_index:.3f}")

# Refit monthly; track c_index drift
# If c_index drops > 0.05 → features stale; retrain

Recipe 11: Subgroup model (when one model doesn't fit all)

# Often: SMB churn dynamics ≠ Enterprise. Fit per-segment.
models = {}
for tier in ['solo', 'team', 'enterprise']:
    sub = df[df['plan_tier'] == tier]
    cph = CoxPHFitter()
    cph.fit(sub, duration_col='tenure_days', event_col='churned')
    models[tier] = cph

Examples

Example 1: B2B SaaS, 18% annual logo churn

Sample: 1,400 users with 24-month history.

Cox PH top features (HR + p-value):

c-index = 0.79 (excellent).

Plan: monthly scoring; pipe critical (<40% 30d-surv) to CSM; high (40-55%) to Customer.io reactivation.

Example 2: Consumer subscription, 28% monthly churn

Decay-shape retention. STOP — don't predict; fix activation per activation-funnel-aha-moment skill first.

Example 3: Mid-market SaaS, hybrid PLG + sales

Subgroup models per tier (Recipe 11).

Pipe results to per-tier interventions.

Edge cases / gotchas

Sources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.