Problem: teams add an AI chatbot to sales and cannot tell which leads it dropped, because nobody tracked what happened to each lead in the first place. Result: a small tracking core that lists overdue follow-ups, shows per-source conversion to booking and counts loss reasons. Stack: Python 3.10+ standard library (tested on 3.15.2). Boundaries: storage and capture are out of scope; in a real setup an AI step fills the lead records from forms, messages and call summaries, and this code reads them
Why tracking comes first
AI does best in sales where data in and data out are clear. Conversations with customers are the opposite: endless branches, and every customer finds a new one. Tracking leads is the opposite of a conversation: one record per lead, a fixed set of steps, a date for the next action. That makes it the right first job for AI, and the base for measuring anything you automate later
In my own setup, website requests arrive as a card in Telegram and each client call becomes a summary, tasks with dates and an updated CRM card. About 40 calls with 15 clients have gone through it, and each one shows which step it is on
Input, processing, output
Input: a list of lead records with id, source, step, next action date and, for lost leads, a reason
Processing: find open leads whose next action date has passed; count leads per source and how many reached booked or paid; count reasons for lost leads
Output: an overdue list, a per-source line and a reasons summary, ready to send as a morning message
The example
The leads below are invented for illustration
from collections import Counter, defaultdict
from datetime import date
STEPS = ["new", "in_progress", "waiting_client", "booked", "paid", "lost"]
DONE = {"paid", "lost"}
def overdue(leads, today):
"""Open leads whose next action date has passed, oldest first."""
late = [l for l in leads if l["step"] not in DONE and l["next_action"] < today]
return sorted(late, key=lambda l: l["next_action"])
def by_source(leads):
"""Per source: how many leads arrived and how many reached booked or paid."""
total, won = Counter(), Counter()
for l in leads:
total[l["source"]] += 1
if l["step"] in ("booked", "paid"):
won[l["source"]] += 1
return {s: (won[s], total[s]) for s in total}
def lost_reasons(leads):
return Counter(l.get("reason", "unknown") for l in leads if l["step"] == "lost")
if __name__ == "__main__":
today = date(2026, 11, 12)
leads = [
{"id": 1, "source": "web_form", "step": "waiting_client", "next_action": date(2026, 11, 9)},
{"id": 2, "source": "messenger", "step": "booked", "next_action": date(2026, 11, 14)},
{"id": 3, "source": "web_form", "step": "lost", "next_action": date(2026, 11, 5), "reason": "slow reply"},
{"id": 4, "source": "messenger", "step": "paid", "next_action": date(2026, 11, 1)},
{"id": 5, "source": "phone", "step": "new", "next_action": date(2026, 11, 11)},
{"id": 6, "source": "web_form", "step": "lost", "next_action": date(2026, 11, 6), "reason": "slow reply"},
]
for l in overdue(leads, today):
print(f"overdue: lead {l['id']} ({l['source']}, {l['step']}) since {l['next_action']}")
for src, (w, t) in sorted(by_source(leads).items()):
print(f"{src}: {w} of {t} reached booking")
print("lost reasons:", dict(lost_reasons(leads)))
Output, exactly as printed when I ran it:
overdue: lead 1 (web_form, waiting_client) since 2026-11-09
overdue: lead 5 (phone, new) since 2026-11-11
messenger: 2 of 2 reached booking
phone: 0 of 1 reached booking
web_form: 0 of 3 reached booking
lost reasons: {'slow reply': 2}
Even on six invented leads the pattern is readable: the web form converts worst and both losses say "slow reply". That is a routing problem, not a chatbot problem
Design notes
Fixed steps, not free text. A step list makes every lead comparable. Free-text statuses like "talked, will call back maybe" cannot be counted
Next action date is mandatory. A lead without a next action is a lead nobody owns. The overdue list only works if every open lead has a date
Loss reasons are the most valuable column. After a month they tell you whether you lose on price, timing or response speed, which decides what to automate next
Let AI fill, let code count. The model is good at turning a message or a call into a record: source, request, next step. Counting and comparing belongs in code, so the morning numbers do not depend on how the model phrased something
Turning the output into a morning message
The three functions are meant to run on a schedule, for example once in the morning, and feed a short message to the sales owner: who is overdue, which source converts, why leads were lost. Keep that message short. A list of two overdue leads gets acted on; a dashboard with twenty numbers gets ignored
If the message is sent by an AI step, give it the computed numbers and ask it only to phrase them. Do not ask it to recount. The point of the split is that the numbers are the same no matter how the model words the message
When to add a chatbot
Only after tracking runs and you have numbers per source. Then you can measure the bot by what happens to leads, booked or paid, not by how many replies it sends. Keep it inside narrow limits: a clinic chatbot I built answers only from the clinic's documents, about 114 price items and 48 services, and passes anything else to the owner
Errors and limits
Garbage in: if the AI step mislabels a source or skips a lead, the counts are wrong. Check a sample of records against the original messages every week at first
Small numbers lie. Conversion on a handful of leads is noise; look at trends over weeks
Dates and time zones: store dates in one zone. A follow-up due "today" in one zone can be overdue in another
Reproduce it
- Save the code as
leads.py - Run
python3 leads.py - Compare with the output above
- Change lead 1 to step "booked" and run again: it should drop from the overdue list and web_form should show 1 of 3
A system with lead tracking, call summaries, CRM updates and reminders takes me about 2–3 weeks to launch. The non-technical version of this is on my site: AI in sales: where to start
This article was created with the help of AI.



Top comments (0)