Cutting MCP Token Costs by Up to 92% with Code Mode
MCP token costs scale with the number of tools you connect, not with the work an agent actually does. In a controlled benchmark across 508 tools and 16 MCP servers, classic MCP consumed 75.1 million input tokens where Code Mode consumed 5.4 million for the same query set, a 92.8% reduction, with both configurations passing 65 of 65 test queries. Bifrost, the open-source AI gateway built in Go by Maxim AI, implements this as Code Mode on any MCP client it manages. This post covers where MCP token costs come from, what the benchmarks show at three different tool counts, how the mechanism works, and when enabling it is the wrong call.
Why do MCP token costs grow with every server you connect?
Two patterns drive MCP token costs, and both scale with the size of your tool catalog rather than the complexity of the task. Anthropic's engineering team documented both when proposing code execution as an alternative to direct tool calls:
- Tool definitions overload the context window. Most MCP clients load every tool definition upfront, on every turn. Connect 10 servers with 150 tools and the model rereads that catalog before doing any work, on turn one and on turn six.
- Intermediate results pass through the context. Chaining tools means each tool's output returns to the model, which then passes it into the next call. A large document, a query result set, or a list of records is paid for twice: once arriving, once departing.
The compounding effect is the problem. A six-turn workflow across 100 tools pays the full tool-definition cost six times, plus every intermediate payload, before producing an answer that might be three lines long. Since the Model Context Protocol has become the standard way to connect agents to systems, the number of connected tools in a typical deployment has grown faster than the work those agents perform.
What is Code Mode?
Code Mode is an execution pattern where the model writes code to orchestrate tools instead of calling them one at a time, so tool definitions load on demand and intermediate results stay outside the model's context. In Bifrost, Code Mode replaces the full tool catalog with four generic meta-tools:
listToolFilesdiscovers which MCP servers are available.readToolFileloads compact Python stub signatures for a server or a single tool, with optional partial reads.getToolDocsreturns detailed documentation for one tool.executeToolCoderuns Python in a sandbox with bindings to every connected Code Mode server.
The model sees four tools regardless of whether five servers are connected or fifty. Everything else is reachable, but only loaded when the model decides it needs it. Code Mode is enabled per MCP client in the MCP gateway, so heavy servers can run in Code Mode while small utilities stay as direct tools.
How much does Code Mode cut MCP token costs?
Bifrost benchmarked Code Mode against classic MCP across three rounds with increasing MCP footprint, running the same query set with the feature off and on:
| Round | MCP footprint | Input tokens, classic | Input tokens, Code Mode | Token change | Est. cost, classic | Est. cost, Code Mode | Pass rate, Code Mode |
|---|---|---|---|---|---|---|---|
| 1 | 96 tools / 6 servers | 19.9M | 8.3M | -58.2% | $104.04 | $46.06 | 64/64 (100%) |
| 2 | 251 tools / 11 servers | 35.7M | 5.5M | -84.5% | $180.07 | $29.80 | 65/65 (100%) |
| 3 | 508 tools / 16 servers | 75.1M | 5.4M | -92.8% | $377.00 | $29.00 | 65/65 (100%) |
Three things in this table matter more than the headline number.
The savings scale with tool count, not with usage. At 96 tools the reduction is 58.2%; at 508 tools it reaches 92.8%. Classic MCP input tokens grew from 19.9M to 75.1M across the rounds while Code Mode stayed roughly flat, between 5.4M and 8.3M. Classic MCP cost tracks the size of your catalog; Code Mode cost tracks what the model actually reads.
Accuracy did not degrade. Code Mode passed every query in all three rounds, including the round where classic MCP dropped one. Token reduction that costs correctness is not a saving, so this is the column to check first.
Cost follows tokens closely. Estimated cost fell 92.2% in round three, from $377.00 to $29.00 for the same workload. At around 500 tools, average input tokens per query dropped roughly 14x, from 1.15M to 83K. Execution also ran about 40% faster, because fewer round trips means less time waiting on the model.
The full methodology and per-query data are published in the benchmark report, and the MCP gateway writeup walks through what the numbers mean for cost governance at scale.
How does Code Mode work?
A Code Mode request follows a different shape from a classic MCP tool execution request. Rather than the model selecting one tool per turn and receiving its output, the sequence is discover, write, execute:
- The model calls
listToolFilesand sees a directory of virtual.pyistub files, one per server or per tool depending on binding granularity. - It calls
readToolFileon the servers relevant to the task, loading compact function signatures rather than full JSON schemas for everything. - It writes a short orchestration script and submits it to
executeToolCode. - Bifrost runs that script in a Starlark interpreter, a Python subset, with each connected Code Mode server exposed as a global object. Tool calls inside the sandbox are synchronous.
- Intermediate results stay in the sandbox. Only the value assigned to
resultreturns to the model.
A script that would take five classic MCP turns collapses into one execution:
results = youtube.search(query="AI news", maxResults=5)
titles = [item["snippet"]["title"] for item in results["items"]]
print("Found", len(titles), "videos")
result = {"titles": titles, "count": len(titles)}
The sandbox is deliberately narrow. There are no import statements, no file I/O, no network access, and no classes, and each execution runs in its own isolated context with a default 30-second timeout. That constraint is what makes running model-written code at the gateway defensible: the code can call the tools you connected and nothing else.
When should you enable Code Mode?
Code Mode is not a universal improvement, and the benchmark rounds show why. At 96 tools the reduction is real but moderate; the pattern earns its keep as the catalog grows.
Enable Code Mode when:
- Three or more MCP servers are connected to the same client.
- Workflows are multi-step and tools need to feed each other.
- Token cost or latency on agentic traffic is a current concern.
Keep classic MCP when only one or two small servers are connected, or when tool calls are simple and direct. The two can also be mixed: run Code Mode on heavy servers such as web search, document stores, and databases, and leave small utilities exposed directly. Enabling it is a per-client flag:
{
"name": "youtube",
"connection_type": "http",
"connection_string": "<http://localhost:3001/mcp>",
"tools_to_execute": ["*"],
"is_code_mode_client": true
}
Cost control and access control belong together here. Because Bifrost brokers the connection to every server, MCP tool filtering restricts which tools a virtual key can reach, and MCP tool groups assemble curated collections that attach to keys, teams, and customers and are enforced at request time. A smaller permitted catalog is both a governance control and a token saving, which is the argument for running centralized MCP tool governance rather than configuring servers per client.
Frequently asked questions
Does Code Mode reduce output tokens as well as input tokens?
The benchmarked reduction is on input tokens, which is where the tool catalog cost lives. Output savings come indirectly, through fewer model turns: the benchmark rounds showed roughly 3-4x fewer LLM round trips.
Is running model-written code at the gateway safe?
The Starlark sandbox has no file system access, no network access, and no imports, and each execution is isolated with a timeout. The model can call the MCP tools you connected through their bindings, which means the security boundary is the set of tools you permitted, not the code itself.
Does Code Mode work with autonomous tool execution?
Yes. Agent Mode handles autonomous execution with configurable auto-approval, and the two combine: the model writes orchestration code and executes it without a manual approval step per call.
Do clients need to change to use Code Mode?
No. Code Mode is configured on the MCP client inside Bifrost, and applications keep calling the same OpenAI-compatible endpoint. Bifrost can also be exposed as an MCP server to external clients, which inherit the same configuration.
Getting started with Code Mode
MCP token costs are a catalog problem before they are a usage problem, and Code Mode addresses the catalog directly: four meta-tools instead of hundreds of definitions, stubs loaded on demand, and orchestration executed in a sandbox so intermediate results never enter the context. The benchmark data shows the effect growing from 58.2% to 92.8% as tool count rises from 96 to 508, with no loss in pass rate. Teams running large MCP footprints can review the MCP gateway resource page for how tool access, authentication, and cost governance fit together.
To see what Code Mode would do to MCP token costs on your own server mix, book a demo with the Bifrost team.