Debugging AI at the Customer: Read a Retrieval Log, a Span Tree and an Eval Diff Instead of the Servers
The app runs on the customer's servers and you can't log in. Three stories, three kinds of evidence: a retrieval log shows two refund policies tied at 0.45 so the old one wins; a span tree shows two timed-out CRM calls taking 93 percent of a 12,900 ms run; an eval diff shows a 5 to 4 score hiding one format change and one real error.
What you will learn
- Debug a wrong answer from a retrieval log that records ids and scores per request
- Find where the time went in a slow answer by reading a span tree and its slowest leaf
- Turn 'the new model feels worse' into an eval diff that labels each case same, fixed or regressed
- Follow a five-step checklist: symptom, evidence, first wrong step, one change, keep the evidence
Before you start
- The RAG Pipeline in One Picture
- Observability and Tracing for LLM Apps
The app is running at the customer, on servers you can't touch, and something goes wrong. You usually can't log in and poke around. So you learn to read the evidence the app leaves behind. It comes in three kinds: a retrieval log, a span tree and an eval diff. The project has one small script per story, a triage script and tests, and it runs on a stand-in test model, so no key is needed. Every output below comes from running those scripts.
Think like a detective
A detective arriving at a crime scene can't rerun the crime. All they can do is read what was left behind. At a customer, the app runs on their servers and those stay locked, so you're in the same spot.
Story one: a wrong refund answer
A customer says: "The assistant told me I have 30 days for a refund, but our policy says 14." In the index there's the old refund policy, version 1, and the new one, version 2. Somebody uploaded the new policy, but the old copy was never removed.
Here's how 01_retrieval_debug.py retrieves. The score is simply the share of the question's words found in a document. latest_only keeps just the highest version in each document family, and that's going to be the fix.
def retrieve(question, docs, k=2):
ranked = [(round(len(words(question) & words(d["text"])) / len(words(question)), 2), d) for d in docs]
ranked.sort(key=lambda pair: -pair[0])
return ranked[:k]
def latest_only(docs):
newest = {}
for d in docs:
if d["family"] not in newest or d["version"] > newest[d["family"]]["version"]:
newest[d["family"]] = d
return list(newest.values())
answer_and_log writes one JSON line per request with the ids, the scores and the answer. That one line is the evidence, so before you change anything, make sure every request leaves a line like it. The run:
before {"request": "req-1042", "question": "How many days do I have to ask for a refund?", "retrieved": [{"id": "refund-v1", "score": 0.45}, {"id": "refund-v2", "score": 0.45}], "answer": "Refund window: customers can ask for a refund within 30 days of purchase."}
after {"request": "req-1043", "question": "How many days do I have to ask for a refund?", "retrieved": [{"id": "refund-v2", "score": 0.45}, {"id": "password-v1", "score": 0.18}], "answer": "Refund window: customers can ask for a refund within 14 days of purchase."}
Both refund copies score 0.45 (five of the question's eleven words). It's a tie, and when two scores are equal the sort keeps the order they were listed in, so the old copy comes first. The model was handed the old document and answered 30 days from it. Keep only the newest version before ranking and the same question gets 14 days. The model was fine. The wrong document was handed to it. A retrieval problem like this one is covered more broadly in Where Naive RAG Fails.
Story two: a slow answer
Another customer says the assistant takes forever on an order question. A slow answer is a job for a span tree. A span is a small block that notes when a step started and how long it took. Spans can sit inside spans, and that makes it a tree. 02_tool_timeout_trace.py uses a simulated clock (wait), so the numbers are the same every time and nothing here measures a real system:
def run_ticket(timeout_ms, cached_fallback):
t = Tracer()
with t.span("agent.run", ticket="T-88"):
with t.span("llm.plan"):
t.wait(400)
with t.span("tool.lookup_order", order="1042"):
for attempt in (1, 2):
with t.span("http.crm_orders", attempt=attempt, timeout_ms=timeout_ms) as s:
t.wait(timeout_ms)
s["status"] = "timeout"
if cached_fallback:
with t.span("cache.order_status"):
t.wait(5)
with t.span("llm.answer"):
t.wait(500)
return t.roots[0]
agent.run 12900 ms
llm.plan 400 ms
tool.lookup_order 12000 ms
http.crm_orders 6000 ms <-- timeout
http.crm_orders 6000 ms <-- timeout
llm.answer 500 ms
slowest leaf: http.crm_orders 6000 ms of 12900 ms
agent.run 2905 ms
llm.plan 400 ms
tool.lookup_order 2005 ms
http.crm_orders 1000 ms <-- timeout
http.crm_orders 1000 ms <-- timeout
cache.order_status 5 ms
llm.answer 500 ms
slowest leaf: http.crm_orders 1000 ms of 2905 ms
The model steps, 400 and 500 ms, are tiny. The two calls to the customer's order system take 12,000 of the 12,900 ms, 93 percent of the run. The fix is a one-second timeout and a cached order status as a fallback, so the customer still gets an answer: 2,905 ms. The rule: find the widest bar at the bottom of the tree and look there first. Tracing tools such as LangSmith show the same kind of tree for a real app; see Observability and Tracing and Reading an Agent Trace.
Story three: worse after a model change
The customer upgraded to a newer model and says "it feels worse". Feelings are hard to debug, so turn them into a test. 03_model_change_eval_diff.py has six cases, each with a phrase the answer must contain. A case passes when that phrase is inside the lowercased reply, and the diff labels every case same, fixed or regressed:
def diff(before, after):
rows = []
for case in CASES:
b, a = before[case["id"]]["pass"], after[case["id"]]["pass"]
change = "same" if a == b else ("fixed" if a else "regressed")
rows.append({"id": case["id"], "expect": case["expect"], "before": b, "after": a, "change": change})
return rows
refund-days pass -> FAIL regressed
password-reset pass -> pass same
order-status pass -> pass same
cancel-plan FAIL -> pass fixed
unknown-topic pass -> FAIL regressed
polite-tone pass -> pass same
score: 5 -> 4 of 6
The score, 5 to 4 of 6, tells you very little. The table tells you more: two cases got worse and one got fixed, so the new model isn't simply worse, it's different. In refund-days the new reply says "fourteen days", written as a word. The fact is right but the check looks for the digits, so that one is only a format change. In unknown-topic the new model says "Yes, we offer lifetime refunds" when it should have said it's not sure. That's a wrong answer, and it's the real problem. Both show up as the same drop in the score, and only the diff tells them apart. For upgrade testing in general, see Model Pinning and Upgrade Evals.
The checklist as code
04_triage.py reads the three evidence files and says, for each one, what it found and what to try first:
retrieval log: old copy of a document ranks level with the new one
next: keep only the newest version before ranking
span tree: 2 timed-out calls to http.crm_orders took 93 percent of the run
next: shorter timeout and a cached fallback
eval diff: 2 cases got worse after the change: refund-days, unknown-topic
next: fix the prompt or the checks for those cases, then re-run
The first look at new evidence takes a second, not an afternoon. The tests in test_debugging.py hold each story in place (the tie and its fix, the slowest leaf, the two regressions and the triage findings), and because the stand-in model gives scripted replies they run without a key. To use a real model, swap the stand-in class for your provider's client.
The debugging checklist
- Say the symptom in one line, for example "the refund answer is wrong".
- Ask the customer for evidence, not access: a log, a trace or eval results.
- Find the first wrong step: here, the tie, the wide bar and the flipped case.
- Change one thing, then run the same input again. Change three things and you won't know which one helped.
- Keep the evidence with the fix, before and after, side by side. That's what you show the customer.
Recap
| Symptom | Evidence | What it showed | Fix |
|---|---|---|---|
| Wrong answer | Retrieval log | refund-v1 and refund-v2 tied at 0.45 | Keep only the newest version before ranking |
| Slow answer | Span tree | Two CRM timeouts, 93% of 12,900 ms | 1 s timeout and a cached fallback: 2,905 ms |
| Worse after upgrade | Eval diff | 5 to 4 of 6: one format change, one real error | Fix the check, fix the prompt, re-run |
Try it yourself
Make the timeout longer, say 3,000 ms, and read the tree again. Which line is the slowest leaf now, and what share of the run does it take?
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
Reading a Business Requirement Like an Architect: Function, Quality, Constraints and Assumptions
One sentence from an insurer's claims director becomes four buckets (function, quality, constraints, assumptions), two testable targets and five questions back to the business, before any design starts.
Discovery for an AI Project: Running the First Conversation, Five Question Groups and Three Red Flags
An insurance company says 'we would like an AI assistant'. That one sentence could be three different projects. Five question groups, asked to the right people, turn it into a one-page problem statement, and three red flags show what still blocks the design.
Communicating AI Decisions, Trade-offs and Risk to Executives: The Decision Card, the Risk Matrix and One Ask
A week before launch the CEO asks: is it safe to launch? Answer with a decision card that says what each option costs, a risk matrix where controls move a risk but never erase it, a one-page brief that ends with one ask, and an honest answer to 'can you guarantee it?'.
Feedback Loops, Expectations and SLAs for an AI Service: Find the Promise the Architecture Can't Keep
Two weeks after launch, the head of claims says 'I thought it never makes mistakes'. A weekly feedback loop, expectation gaps closed in writing, and a review of the draft SLA find the one clause nothing can measure before anyone signs it.
