Topic 2 of 5 · 5 min read
Compact, meaningful tool results for agents
Goal: Rewrite a bloated tool response so it fits a token budget and still gives the agent what it needs for the next step.
A tool result is the agent's working memory: return the smallest amount of information that lets it take the correct next step. Everything you return is read as tokens, paid for, and competes with the task for space in the context window.
Check yourself · 1 of 3
A search tool returns 40 records with 25 fields each, and agents run out of context. What is the best first fix?
Why it matters
APIs written for apps return every field, because a UI picks what it needs. An agent has no UI. It reads all of it. A 300-line JSON blob of timestamps, internal flags and UUIDs wastes context and buries the one value that matters. Anthropic's guide notes that agents handle natural-language names and labels better than cryptic identifiers, and that Claude Code caps tool responses at 25,000 tokens by default.
How to do it
- Return meaning, not plumbing. Prefer
"owner": "Dana Cole"over"owner_id": "9f3c...". If a later call needs the id, include it only in a detailed mode. - Offer a response mode. A
response_formatparameter with"concise"and"detailed"lets the agent ask for ids and metadata only when it needs them. Default to concise. - Paginate and cap. Take
limitandcursor, set a sensible default, and truncate long text with a marker. - Say what is missing and what to do. When you cut results, return a hint: "Showing 20 of 143. Narrow with
statusor passcursorto continue." - Use structure the agent can read. MCP tools can return
structuredContentwith anoutputSchema, and a text rendering for the model. Keep field names descriptive and consistent.
Before and after
Before, search_tickets returns 40 full records with 25 fields each. After:
{
"total": 143,
"shown": 10,
"tickets": [
{ "ref": "T-2041", "title": "Refund not received", "assignee": "Omar Reyes", "status": "open", "updated": "2026-11-28" }
],
"next": "Showing 10 of 143. Pass cursor=\"c10\" for more, or add status or assignee to narrow."
}
Short references like T-2041 are fine when the next tool accepts them.
What goes wrong
- Silent truncation. The agent assumes it has seen everything and answers from a partial list.
- Names with no way back. A concise result that drops the only identifier the next tool accepts forces a second call.
- Dumping raw logs or HTML. Strip markup and repeated boilerplate before it reaches the model.
Examples
Slack-style message search
A search tool returns channel ids, user ids, timestamps in milliseconds and reaction metadata for each hit. In concise mode it returns "#billing, Ana Ruiz, 2026-11-28: refund approved" plus a message ref. The agent asks for detailed only when it must reply in the thread and needs the ids.
Truncation with a hint
A read_log tool returns 200 lines and ends with: "Truncated at 200 of 5,812 lines. Use start_line or pattern to look further." The agent now knows what it did not see and how to continue.
Key points
- Every returned token costs context, so return only what the next step needs.
- Prefer names and short readable references over opaque UUIDs, and keep real ids available in a detailed mode.
- Paginate with a default limit and say plainly when results were cut, with a hint on how to narrow or continue.
- A concise and detailed response mode lets the agent decide how much to pay for.
- Structured output with descriptive field names is easier for an agent to use than free-form text dumps.
Common mistakes
- Passing the raw API response straight through.
- Cutting a list without saying so, so the agent treats it as complete.
- Returning only names when the next tool needs an id the agent cannot recover.
- Setting no default limit, so one broad query fills the context window.
Check yourself · 2 of 3
A tool shortens a list of 143 results to the first 10. What should it also return?
Check yourself · 3 of 3
Why might a tool return "Dana Cole" instead of a UUID for an owner field?
Practice items, not counted towards your mastery.
Start here
- Writing effective tools for AI agents (Anthropic) (opens in a new tab)ArticleFree
Anthropic's field guide: which tools to build, namespacing, token-efficient responses, prompt-engineered descriptions and eval-driven iteration.
- MCP specification: Tools (opens in a new tab)StandardFree
The protocol definition of tool listings, input and output schemas, structured content and the isError flag.
- Introducing advanced tool use (Anthropic) (opens in a new tab)ArticleFree
Techniques for large tool sets, such as loading tools on demand and calling them from code, and why they matter for context cost.