MCP Explainers
How do you reduce MCP token usage with code execution?
MCP code execution has the model write and run code that calls your MCP tools, instead of calling each tool directly and reading back every result. Anthropic measured one case where this cut a task from 150,000 tokens to 2,000.
Mark
Head of Marketing, MCPOrbit
- Published
- Updated
- · Updated
- Read time
- · 6 min read
Model Context Protocol (MCP) code execution has the model write a short program that calls your MCP tools. The program runs in a sandbox, a locked-down place to run code safely, then calls tools directly and sends back only a short summary. Anthropic measured one real case where this cut a task from 150,000 tokens to 2,000.
That is a 98.7% drop in one documented example. This post explains how the pattern works, what it actually saves, and when it is worth the extra setup.
What is code execution with MCP?
Code execution with MCP is a pattern where an AI assistant writes a program instead of calling tools one at a time. That program runs inside a sandbox, a locked-down environment built to run untrusted code safely. Inside the sandbox, the program calls your MCP tools directly, the same way any other MCP client would.
A normal MCP tool call is like asking someone to read an entire spreadsheet out loud so you can find one number. Code execution is like asking them to open the spreadsheet themselves, find the number, and just tell you that. The model never has to sit through the rest of the sheet.
Anthropic built this pattern into its own API as a code execution tool. Some agent platforms, like Claude Code, already run tool calls this way by default. It is not yet a feature every MCP client ships.
Why do MCP tool calls use so many tokens?
Two separate things eat tokens, and code execution targets both of them.
- Tool definitions. Every connected MCP server registers its tools and their schemas before any work starts. Connect ten servers and all ten tool lists sit in context, whether you use them or not.
- Tool results. Every result a tool returns gets inserted into the conversation in full. A tool that returns 10,000 rows puts all 10,000 rows in front of the model, even if it only needed one.
Normal tool calling has no way to filter a result before the model sees it. Code execution does, because the filtering happens in code, before anything reaches the model's context window.
How much does code execution actually save?
Anthropic published the clearest measured example. An assistant connected to a document tool and a spreadsheet-style tool. Copying one transcript between them used about 150,000 tokens with normal tool calls. The same task, done through generated code that called the tools directly and only returned what was needed, used about 2,000 tokens.
Approach Tokens used What reaches the model
Direct tool calls 150,000 Every full tool result, unfiltered
Code execution 2,000 A short, filtered summary onlyThat 98.7% figure is one measured case, not a universal rate. An independent benchmark from AIMultiple found a smaller, still real gain of around 77% fewer total tokens. It also noted that code execution uses more output tokens, because the model has to write the code itself. Treat the Anthropic number as a ceiling for a favorable case, not a default.
What does a code execution setup actually involve?
The model does not call your MCP tools from its own context. It writes code, hands that code to a sandbox, and the sandbox runs it. Inside the sandbox, an MCP client loads only the tool definitions the task needs and calls them directly.
Here is the shape of that generated code, simplified:
# Runs inside the sandbox, not in the model's own context.
from mcp_client import load_tool
search = load_tool("crm", "search_records")
get_doc = load_tool("docs", "get_document")
# Call the tools directly. Full results stay inside the sandbox.
records = search(query="open deals this quarter")
doc = get_doc(id=records[0]["doc_id"])
# Only this short line goes back to the model.
return f"{records[0]['name']}: {doc['summary'][:200]}"Only the last line, the return value, enters the model's context. The full records list and the full document never do.
When is code execution worth the setup?
Code execution pays off when you connect many MCP servers at once. It also helps when a tool regularly returns large results, like full database rows or long documents. It is overkill for a task that calls one tool once and reads a short answer.
It also is not free. A sandbox that safely runs model-written code is a real piece of infrastructure, not a setting you flip on. The generated code can have bugs. It needs limits on network access and run time. Debugging a failure now means reading code the model wrote, not just a tool call log.
If your context problem comes from too many connected servers rather than large results, a smaller fix might be enough. Limiting how many tools you connect at once or paginating large results solves a different part of the same problem, with far less setup.
Frequently asked questions
Frequently asked questions
- Does code execution replace MCP tool calls?
- No. Code running in the sandbox still calls MCP tools through the Model Context Protocol (MCP). What changes is where the result gets processed. Filtering happens in code before the model sees anything, instead of the full result landing straight in its context.
- Do I need to change my MCP server to support code execution?
- No. Code execution happens entirely on the client side, inside the assistant's sandbox. Your MCP server keeps exposing tools exactly as it already does. It does not know or care whether the caller is a sandbox or the model itself.
- Is code execution the same as the MCP Apps extension?
- No. MCP Apps let a server render its own interface inside a client. Code execution changes how tool calls are made and filtered, not what gets displayed. The two solve different problems and can be used together.
- Does code execution always cut tokens by 98.7%?
- No. That figure is from one documented Anthropic test with large intermediate results. A separate benchmark measured a smaller, still real gain of around 77%. Your savings depend on how large your tool results are and how many tools you call.
- What do I need to set up code execution with MCP?
- You need a sandbox that can safely run model-generated code, plus an MCP client inside it. You also need limits on what the generated code can do, like network access and run time. It is an engineering project, not a configuration flag.
Code execution routes MCP tool calls through generated code, not the model's own context. It can cut token usage sharply when results are large. Before deciding a server needs it, look at what that server's tools actually return.
About the author
Mark
Head of Marketing, MCPOrbit
Mark leads marketing at MCPOrbit, the free desktop client for the Model Context Protocol. He writes the build-it and reliability guides, and the code in them is run before it ships.


