Topic 3 of 5 · 4 min read
Fixing the right layer of an agent
Goal: Classify a described agent failure by layer and pick the fix that addresses its cause.
Once you know the first wrong step, name the layer it belongs to: prompt, tool, model, context or orchestration. Then fix that layer, not the one where the failure showed up.
Check yourself · 1 of 3
A search tool returns an empty result with no message, and the agent invents an answer. Which layer holds the cause?
Why it matters
The cheapest fix is often the wrong one. Adding "be careful" to the prompt feels like progress, but if the tool returned bad data, the prompt cannot help. Each wrong patch also adds text or code that you must now maintain. Fixing the right layer once is cheaper than five patches in the wrong place.
The five layers
- Prompt. The instructions were missing, vague or in conflict. Sign: the model did something the prompt never ruled out, or two rules disagree.
- Tool. The tool's name, description, parameters or output were confusing, or the tool itself returned a wrong or unhelpful result. Sign: the model called it with bad arguments, picked the wrong tool, or could not read what came back.
- Model. The model could not do the step even with a clear prompt and a good tool, for example it fails at multi-step arithmetic or at reading a messy table. Sign: the same clear input fails across several reruns. This is the layer to blame last.
- Context. The model did not have the information, or had too much. A document was cut off, an old instruction was lost in a long history, or a stale value was still in memory. Sign: the right fact was never in the window, or was buried.
- Orchestration. The control code around the model was wrong: no stop rule, no retry limit, a handoff that dropped state, a missing human approval. Sign: the loop ran too long, ran twice or skipped a check.
How to do it
- Take the first wrong step from the transcript.
- Ask: what did the model see, and what did it do with it? If the information was wrong, missing or oversized, it is a tool or context problem. If the information was fine and the instruction unclear, it is the prompt. If both were fine, suspect the model. If the failure is about loops, state, retries or approvals, it is orchestration.
- Make the smallest fix in that layer and rerun.
Before and after
Weak: the agent keeps booking the wrong date, so the prompt gets six new lines about dates.
Better: the transcript shows the book tool takes date as a string and silently accepts "next friday". The fix is in the tool: require ISO dates and return a clear error otherwise.
What goes wrong
- Prompt patching by reflex. Each extra rule makes the prompt longer and rarely fixes a tool or context fault.
- Blaming the model too early. Most failures are at the other layers, and a bigger model hides them at higher cost.
- Fixing the layer you own. A prompt writer fixes prompts, a backend dev fixes tools. Pick the fix by cause, not by who is available.
Examples
Five failures, five layers
| Failure seen | Layer | Fix |
|---|---|---|
| Agent deletes a file the user never mentioned | Prompt | State what may be changed and require confirmation for deletes |
search returns raw HTML and the model misreads it |
Tool | Return clean text with the title and URL |
| Agent forgets the user's currency from turn 2 | Context | Keep the currency in a pinned note, not in old messages |
| Run repeats the same call 12 times | Orchestration | Add a step limit and stop on repeated identical calls |
| Clear input, good tool, fails the same way each rerun | Model | Try a stronger model or split the step |
A prompt fix that did not hold
An agent invented order numbers. The team added "never make up order numbers" to the prompt. It still happened 1 run in 5. The transcript showed the lookup tool returned an empty result with no message, and the model filled the gap. The fix was in the tool: return no matching order found explicitly.
Key points
- Name the layer of the first wrong step: prompt, tool, model, context or orchestration.
- A failed tool result or a missing fact is a tool or context problem, and no prompt wording will fix it.
- Suspect the model last: first confirm the prompt is clear, the tool is sound and the context is right.
- Orchestration faults are about loops, retries, state and approvals in the code around the model.
- Make the smallest fix in the right layer and rerun, instead of stacking prompt rules.
Common mistakes
- Adding prompt rules for failures caused by tool output or missing context.
- Upgrading to a bigger model before checking the prompt, tool and context.
- Fixing the symptom at the last step instead of the layer of the first wrong step.
- Choosing the fix by who owns the code rather than by where the cause is.
Check yourself · 2 of 3
An agent forgets the user's chosen language after twenty turns in a long chat. Which fix targets the cause?
Check yourself · 3 of 3
Which failure points to the orchestration layer?
Practice items, not counted towards your mastery.
Start here
- OpenTelemetry semantic conventions for generative AI (opens in a new tab)StandardFree
The standard names for model-call and tool-call spans and their attributes, so traces look the same across tools.
- Langfuse observability overview (opens in a new tab)ToolFree
Shows how traces, nested observations and sessions map onto an agent run, in an open-source tool.
- Tracing (OpenAI Agents SDK docs) (opens in a new tab)DocsFree
Lists the span types an agent framework records: agent, generation, function, handoff and guardrail.
More for this topic
- Building effective agents (Anthropic) (opens in a new tab)ArticleFree
Describes the agent loop, tool design and why simple, inspectable designs are easier to debug.