Systems Library / Sales Automation / How to Build an AI Deal Stage Predictor
Sales Automation crm pipeline

How to Build an AI Deal Stage Predictor

Predict which deals will close using AI analysis of pipeline data.

Jay Banlasan

Jay Banlasan

The AI Systems Guy

Every sales pipeline has deals that look alive but are already dead. An ai deal stage prediction system catches them before your team wastes another week on pipeline forecasting that never matches reality. I built this for a client who was losing 30% of forecasted revenue to ghost deals.

What You Need Before Starting

Step 1: Pull Your Deal Data

Get your historical deals out of the CRM. You need closed-won and closed-lost deals with timestamps for each stage change.

import requests
import pandas as pd
from datetime import datetime

def fetch_deals(api_key, base_url):
    headers = {"Authorization": f"Bearer {api_key}"}
    deals = []
    offset = 0

    while True:
        response = requests.get(
            f"{base_url}/deals",
            headers=headers,
            params={"limit": 100, "offset": offset,
                    "properties": "dealstage,amount,closedate,createdate"}
        )
        batch = response.json().get("results", [])
        if not batch:
            break
        deals.extend(batch)
        offset += len(batch)

    return pd.DataFrame([d["properties"] for d in deals])

Step 2: Build Feature Vectors

Each deal needs features the model can learn from. Days in current stage, total deal value, number of activities, and stage progression speed all matter.

def build_features(df):
    df["days_in_stage"] = (
        pd.Timestamp.now() - pd.to_datetime(df["last_stage_change"])
    ).dt.days
    df["activity_count"] = df["emails_sent"] + df["calls_made"] + df["meetings_booked"]
    df["velocity"] = df["stages_completed"] / df["days_since_created"].clip(lower=1)
    features = df[["days_in_stage", "amount", "activity_count", "velocity"]]
    return features

Step 3: Train the Predictor

Use your historical closed deals to train a classifier. The model learns which patterns lead to wins vs losses.

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

historical = deal_data[deal_data["dealstage"].isin(["closedwon", "closedlost"])]
X = build_features(historical)
y = (historical["dealstage"] == "closedwon").astype(int)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)

print(f"Prediction accuracy: {model.score(X_test, y_test):.1%}")

Step 4: Score Active Deals

Run every open deal through the model daily. Flag anything below 40% probability.

open_deals = deal_data[~deal_data["dealstage"].isin(["closedwon", "closedlost"])]
open_features = build_features(open_deals)

predictions = model.predict_proba(open_features)[:, 1]
open_deals["win_probability"] = predictions

at_risk = open_deals[open_deals["win_probability"] < 0.4]
print(f"At-risk deals: {len(at_risk)}")
print(at_risk[["dealname", "amount", "win_probability"]].to_string())

Step 5: Automate the Daily Alert

Schedule this to run every morning. Send the at-risk list to Slack or email so your team sees it before their first call.

import smtplib
from email.mime.text import MIMEText

def send_risk_alert(at_risk_deals, recipient):
    body = "Deals needing attention today:\n\n"
    for _, deal in at_risk_deals.iterrows():
        body += f"- {deal['dealname']}: ${deal['amount']:,.0f} "
        body += f"({deal['win_probability']:.0%} win probability)\n"

    msg = MIMEText(body)
    msg["Subject"] = f"Pipeline Risk Alert: {len(at_risk_deals)} deals"
    msg["To"] = recipient

    with smtplib.SMTP("smtp.gmail.com", 587) as server:
        server.starttls()
        server.login("[email protected]", "app-password")
        server.send_message(msg)

What to Build Next

Add the model output back into your CRM as a custom field. That way reps see the risk score on every deal without leaving their pipeline view.

Related Reading

Want this system built for your business?

Get a free assessment. We will map every system your business needs and show you the ROI.

Get Your Free Assessment

Related Systems