Skip to content
mentoor

Topic 6 of 6 · 6 min read

Debugging a bad answer from its trace

Goal: Read a trace and name the span where a wrong or slow answer started, and the fix it points to.

Debug a bad answer by finding its trace, then walking the spans in order and stopping at the first one whose output is already wrong; that step, not the final model call, is usually the cause.

Check yourself · 1 of 3

In a trace, the retrieval span returned zero chunks and the model answered confidently. Where did the problem start?

Choose one answer

Why it matters

When an answer is wrong, the instinct is to blame the model or rewrite the prompt. Often neither is the problem. The retrieval returned the wrong page, the context was cut off, a tool failed and the model improvised, or an alias moved to a different snapshot. A trace shows which. It turns a debate about quality into a short reading exercise.

How to do it

  1. Find the trace. Use the request id from the ticket. If there is none, filter by session, tenant and time, plus a thumbs-down score.
  2. Read the root span. Total time, status, release and prompt version. Check that this is the version you think it is.
  3. Walk the children in order. At each span, ask: was its input right, and was its output right? Stop at the first span where the output is wrong.
  4. Check the usual suspects at each type.
    • Retrieval: zero or few chunks, low scores, wrong index, a stale document.
    • Prompt build: token count near the model's context limit, a context section missing or cut.
    • Model call: finish reason length (truncated), content_filter, a response model that differs from the one you tested, a large input.
    • Tool call: error status, empty result, wrong arguments the model chose.
    • Agent turns: a loop that repeats the same tool call, or ends without a final answer.
  5. Name the fix. Retrieval miss: fix the query, the index or the chunking. Truncation: shorten context or raise the output limit. Tool error: fix the tool or its error message. Wrong model: pin the snapshot.
  6. Save the trace as a test case. Add the input and the expected answer to your evaluation set so the bug stays fixed.

Before and after

Before, the complaint is "the bot said refunds take 30 days." The team edits the prompt to say "be accurate" and the bug returns.

After, the trace shows the retrieval span returned the 2022 policy page with score 0.41 and the current page was not in the top results. The model answered correctly from a stale source. The fix is to remove the old page from the index and add a freshness filter, then add the case to the eval set.

What goes wrong

  • Starting at the model span. You read the last step first and blame it for earlier errors.
  • Not checking the finish reason. A truncated answer looks like a model that does not know.
  • Fixing one trace without a test. The same failure returns after the next prompt or index change.

Examples

A truncated answer

chat model-a   in=15,900 out=1,024 finish=length

The answer ends mid-sentence. The finish reason is length: the output limit was reached. The fix is a higher output limit or a shorter answer format, not a new model.

A tool error the model hid

A lookup_order span has status error and an empty result. The next model span answers with a plausible order status anyway. The fix is in the tool and in its error handling: return a clear error and instruct the model to say it could not look the order up.

Key points

  • Find the trace by request id, then walk the spans in order and stop at the first wrong output.
  • Check the root span first: confirm the release, prompt version and model are the ones you expect.
  • Each span type has usual suspects: empty retrieval, truncated context, finish reason length, tool errors, loops.
  • Name the fix that matches the failing step, such as index, chunking, limits, tool or pinned model snapshot.
  • Save the failing trace as an eval test case so the fix stays fixed.

Common mistakes

  • Rewriting the prompt before reading the trace.
  • Reading spans from the end instead of from the first wrong output.
  • Ignoring finish reason and the response model.
  • Fixing the case but not adding it to the eval set.

Check yourself · 2 of 3

A model span shows finish reason length and the answer stops mid-sentence. What is the likely cause?

Choose one answer

Check yourself · 3 of 3

After fixing a bad answer found in a trace, what should you do so it does not return?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic