Cut Your Agent's Token Bill by 98% With Code Execution

Cut Your Agent's Token Bill by 98% With Code Execution

Here's a pattern to cut down your bill significantly and it's changing how agent builders use the Model Context Protocol (MCP): stop calling tools directly, and let the agent write code instead.

How this works?

A typical agent loads every connected MCP server's full tool schema into context before the model does anything: names, parameters, descriptions, for every tool, whether you need it in this run or not. Then, every intermediate result (a full document, a giant JSON blob, a spreadsheet dump) gets shipped back through the context window between tool calls, even when nothing but the next tool needs it. The problem is this is a linear problem , the more tools you are loading, the bigger your monthly bill will grow. Luckily there's a quick solution for this.

The fix

Treat MCP servers like importable code libraries instead of a flat list of tool calls. The agent then will explore a filesystem of available tools, read only the definitions it actually needs, then write a short script that chains calls together. This will keep the data inside the execution environment instead of round-tripping it through the model:

import * as gdrive from './servers/google-drive';
import * as salesforce from './servers/salesforce';

const transcript = (await gdrive.getDocument({documentId: 'abc123'})).content;
await salesforce.updateRecord({objectType: 'SalesMeeting', data: {Notes: transcript}});

No intermediate transcript gets pasted back into context, it just flows from one call to the next inside the code.

The impact

 On one real workflow, this dropped token usage from 150,000 to 2,000: that's a 98.7% reduction. If you're building multi-tool agents on MCP and context is growing (or costs are), this is the pattern to try first, before reaching for a bigger context window.

References

Jones, A., & Kelly, C. (2025, November 4). Code execution with MCP. Anthropic. https://www.anthropic.com/engineering/code-execution-with-mcp