In an agentic workflow, tool outputs are injected directly into the Large Language Model's (LLM) context window. Poorly designed outputs cause "context poisoning" through verbosity or force unnecessary tool calls due to missing data. Optimizing tool output is a critical engineering task to ensure agent reliability and cost-efficiency.
The output must provide the maximum amount of actionable information in the minimum number of tokens.
status and id fields.--verbose flag for deep inspection.Agents parse structured data more reliably than prose.
key: value).+---+), decorative headers, or conversational "preambles" ("I found the following results...").Never return a success code (e.g., HTTP 200) with an error message in the body.
Error: File not found at /src/main. Suggestion: Use 'list_files' to verify the path.Never dump unlimited results into the context.
limit and offset (or page) parameter.Showing 10 of 450 matches. Use --page 2 to see more.For exploratory tools (like search or directory listing), return a high-level summary and the top N results.
Found 12 matching files.
Top 3:
1. index.js (Modified 2h ago)
2. styles.css (Modified 1d ago)
3. utils.js (Modified 5m ago)
Use 'read_file' for specific content.
When searching or grepping, provide a small window of context around the match so the agent can understand the surroundings without a second tool call.
File: auth.py
Line 42: # Validate session
Line 43: if session.is_expired(): <-- MATCH
Line 44: return Redirect("/login")
If a tool call is ambiguous, provide the valid options in the error message. This allows the agent to self-correct in the next turn.
Error: Invalid 'region' parameter.
Supported regions: [us-east-1, us-west-2, eu-central-1].
When implementing tools via an MCP server:
enum.description field in the tool definition to tell the agent exactly what format the output will take.\u001b[32m) in the context, confusing the model and wasting tokens.