Sub topic 15
Reading a trace to find out why an agent went wrong
This chapter assumes you know what a message trace is (the ordered list of system, user, assistant, and tool messages), how the agent loop in the earlier chapters works, and how tool results come back as text. You should be comfortable reading JSON and short Python.
What a trace shows
A trace is the full list of messages the harness sent to the model, in order. The model has no other memory, so everything that went wrong is in the trace or was cut out of it. When an agent fails, read the trace from the top before you change the prompt.
Each step has three parts: what the model saw, what it asked for, and what the harness returned. A failure usually sits where one of these does not match the others.
A worked trace
Suppose the task is "make the failing test in test_math.py pass". The trace below is trimmed to the key lines.
trace = [
{"role": "user", "content": "Fix the failing test in test_math.py"},
{"role": "assistant", "content": "", "tool_calls": [
{"id": "1", "name": "run_command", "arguments": {"command": "pytest"}}]},
{"role": "tool", "tool_call_id": "1",
"content": "...[truncated]... FAILED test_math.py::test_add - assert 3 == 4"},
{"role": "assistant", "content": "", "tool_calls": [
{"id": "2", "name": "read_file", "arguments": {"path": "math.py"}}]},
{"role": "tool", "tool_call_id": "2", "content": "error: [Errno 2] No such file or directory: 'math.py'"},
{"role": "assistant", "content": "", "tool_calls": [
{"id": "3", "name": "read_file", "arguments": {"path": "math.py"}}]},
{"role": "tool", "tool_call_id": "3", "content": "error: [Errno 2] No such file or directory: 'math.py'"},
]
for i, m in enumerate(trace):
print(i, m["role"], str(m.get("tool_calls") or m["content"])[:80])Two problems show up. The test output was truncated from the front, so the model never saw which file failed. Then the model repeated the same failing call with the same arguments. Printing one line per message makes both easy to spot.
Common failure patterns
| Symptom in the trace | Likely cause | Fix |
|---|---|---|
| Same tool call repeated with same arguments | Error text gives no new information | Return a clearer error, such as the list of valid paths |
| Model ignores a rule from the start | Rule is far back in a long context, or was compacted away | Move the rule into the system prompt; check compaction |
| Model calls a tool with wrong argument names | Vague tool description or schema | Tighten the description and mark required fields |
| Answer is based on a file it never read | The model guessed instead of calling `read_file` | Instruct it to read before editing; check the available tools |
| Model follows text found in a file or web page | Tool output treated as instructions | Treat tool output as data; require approval for risky actions |
| Run ends with "step limit reached" | Loop without progress, or task too large | Add progress checks; split the task |
Diagnosing step by step
- Find the last good step, the point after which the model's actions stop making sense.
- Look at what the model saw just before it. Was a result truncated, an error vague, or an instruction missing?
- Check the tool call itself. Are the name and arguments valid against the schema?
- Check the context size at that step. A trace near the window limit may have lost early messages.
- Change one thing, such as the error message, the truncation, or the prompt. Then rerun and compare traces.
A small trace checker
You can automate the most common check, repeated identical calls.
def find_repeats(trace, limit=2):
seen = {}
for i, m in enumerate(trace):
for call in m.get("tool_calls") or []:
key = (call["name"], str(sorted(call["arguments"].items())))
seen.setdefault(key, []).append(i)
return {k: v for k, v in seen.items() if len(v) >= limit}
for key, positions in find_repeats(trace).items():
print("repeated", key[0], "at messages", positions)Run against the trace above, it prints repeated read_file at messages [3, 5].
Exercise
In the trace above, the agent calls read_file with math.py twice and gets the same error. Name two changes to the harness that would help the model recover.
Show answer
Return a more useful error, for example list the files in the workspace or suggest the closest match such as test_math.py. Also stop the run or warn the model when the same call with the same arguments repeats. Truncating test output from the end instead of the front, so the failing file name is kept, would also help.
Exercise
A rule in the system prompt is followed for 10 steps and then ignored. Which two parts of the trace do you check first?
Show answer
Check the context size at the step where the behavior changed, and check whether compaction or truncation removed or summarized the rule or the messages that reinforced it.