Anthropic just posted another banger guide.
This one is on building more efficient agents to handle more tools and efficient token usage.
This is a must-read for AI devs!
(bookmark it)
It helps with three major issues in AI agent tool calling: token costs, latency, and tool composition.
How? It combines code executions with MCP, where it turns MCP servers into code APIs rather than direct tool calls.
Here is all you need to know:
1. Token Efficiency Problem: Loading all MCP tool definitions upfront and passing intermediate results through the context window creates massive token overhead, sometimes 150,000+ tokens for complex multi-tool workflows.
2. Code-as-API Approach: Instead of direct tool calls, present MCP servers as code APIs (e.g., TypeScript modules) that agents can import and call programmatically, reducing the example workflow from 150k to 2k tokens (98.7% savings).
3. Progressive Tool Discovery: Use filesystem exploration or search_tools functions to load only the tool definitions needed for the current task, rather than loading everything upfront into context. This solves so many context rot and token overload problems.
4. In-Environment Data Processing: Filter, transform, and aggregate data within the code execution environment before passing results to the model. E.g., filter 10,000 spreadsheet rows down to 5 relevant ones.
5. Better Control Flow: Implement loops, conditionals, and error handling with native code constructs rather than chaining individual tool calls through the agent, reducing latency and token consumption.
6. Privacy: Sensitive data can flow through workflows without entering the model's context; only explicitly logged/returned values are visible, with optional automatic PII tokenization.
7. State Persistence: Agents can save intermediate results to files and resume work later, enabling long-running tasks and incremental progress tracking.
8. Reusable Skills: Agents can save working code as reusable functions (with SKILL .MD documentation), building a library of higher-level capabilities over time.
This approach is complex and it's not perfect, but it should enhance the efficiency and accuracy of your AI agents across the board.
anthropic. com/engineering/code-execution-with-mcp