Stop Routing Data Through Your LLM

Stop Routing Data Through Your LLM

Here's a pattern worth stealing if you're building agents that connect to more than a handful of tools: code execution with MCP.

The default way most agent frameworks use the Model Context Protocol is to load every connected tool's full definition into context up front, then pass every intermediate result back through the model as it chains calls together. That works fine with a handful of tools. But this goes out of control with dozens or hundreds that show up once you wire up Drive, Salesforce, Slack, a database, and a few internal APIs, it becomes the bottleneck: bloated context, higher latency, and a token bill that scales with the data moving through the workflow, not the task's actual complexity.

Anthropic's engineering team described a fix that's simple once you see it: expose MCP tools as a filesystem of callable functions (./servers/google-drive/getDocument.ts, and so on) instead of a wall of tool schemas. The agent writes ordinary code to orchestrate the work, and only the parts of the result that matter come back into the model's context.

Their example: pulling a meeting transcript from Google Drive and attaching it to a Salesforce record. The naive approach pipes the full transcript through the model twice. The code-execution version looks like this:

javascript

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

Token usage on that workflow dropped from 150,000 to 2,000, a 98.7% reduction. The same approach lets you filter a 10,000-row dataset down to the 5 rows you need entirely in code, and keeps sensitive data out of the model's context altogether when it doesn't need to see it.

If you're building agents and cost or latency has become a problem, this is worth checking before you reach for a bigger model: the bottleneck might just be how much data you're asking the model to shuttle around.

Source: Code execution with MCP: building more efficient AI agents — Anthropic Engineering