How should tool results be formatted before they go back into the model's context?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
Tool results are the largest uncontrolled source of context bloat in agents, and they are the part engineers most often leave as whatever the upstream API returned. A search tool that returns full documents can consume more of the window than the system prompt, the history, and the user's question combined.
Full worked answer with diagram
Tool results are the largest uncontrolled source of context bloat in agents, and they are the part engineers most often leave as whatever the upstream API returned. A search tool that returns full documents can consume more of the window than the system prompt, the history, and the user's question combined.
The governing rule: return what the model needs to decide the next step, not the whole payload.
- Project, do not dump. Field allowlist, flatten nesting, drop nulls and internal ids the model will never reference. A REST response with 60 fields where the model uses 4 is 15x waste on every call.
- Paginate explicitly. "Showing results 1-20 of 480. Call again with offset=20 for more." This gives the model agency instead of silently hiding data, which is what a bare truncation does.
- Truncate with a visible marker that says it was truncated and how to get the rest. Silent truncation makes the model confidently reason over half a table.
- Make errors actionable.
400: field 'date' must be ISO-8601, got '12/03/26'lets the model self-correct. A raw stack trace teaches nothing, costs hundreds of tokens, and pollutes the context for every subsequent turn. - Compact formatting. Pretty-printed JSON is whitespace you are paying for. Markdown tables or minimal JSON usually win for tabular data.
- Be consistent. The same tool should always return the same shape. Models learn the shape within a session, and shape changes read as new information.
For genuinely large results, offload: write the payload to a file or artifact store and return a handle plus a short summary, letting the agent re-read on demand. This keeps the window clean and turns context into addressable storage.
The security point that belongs here: tool results are untrusted content. A web-fetch or email-read result can contain text aimed at your model. Delimit it, mark it as data, and remember the delimiter is a mitigation, not a boundary.
Worth sketching. It shows a formatting layer between the API and the model, which is the thing most teams simply do not have.
flowchart LR
A["Raw API payload"] --> B["Allowlist fields,<br/>flatten, drop nulls"]
B --> C{"Still large?"}
C -->|"no"| D["Return compact result"]
C -->|"tabular"| E["Page 1 of n plus<br/>how to fetch the rest"]
C -->|"blob"| F["Write to store, return<br/>a handle and a summary"]
G["Tool error"] --> H["Actionable message:<br/>field, expected, got"]
What a good answer covers
- Tool results are the largest uncontrolled source of context bloat in agents, and they are the part…
- A search tool that returns full documents can consume more of the window than the system prompt, the…
- The governing rule: return what the model needs to decide the next step, not the whole payload
- Project, do not dump
- Paginate explicitly
Key terms: Project, do not dump · Paginate explicitly · Truncate with a visible marker · Make errors actionable · Compact formatting · Be consistent
Interviewer follow-ups to expect
How would you decide the truncation limit for a given tool?
Why is a paginated result better than a truncated one for an agent, given both hide data?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep