Agentic AI
Pi Agent Slashes MCP Token Costs With Codemode Tool Discovery
Pi's creator resisted the MCP protocol because it consumed 18,000 tokens before doing anything useful. The solution: route tools through a sandboxed JavaScript layer instead.
The New Stack
When Mario Zechner measured the cost of connecting MCP servers to Pi's coding agent, he found a problem that nearly killed the idea altogether. The Chrome DevTools MCP server alone consumed roughly 18,000 tokens—about 9% of a typical 200,000-token context window—just loading its tool definitions, before the model had accomplished a single task.
The MCP Token Problem#
For most of 2024, Pi deliberately avoided the Model Context Protocol standard, even as it became the norm across developer tools. The economics were brutal. Playwright MCP needed 13,700 tokens to describe just 21 tools, or roughly 7% of a standard context window. Add multiple servers together, and the overhead became untenable.
Beyond raw token consumption, Zechner objected to another inefficiency: MCP results had to flow back through the model's context window before being saved to disk or combined with other outputs. This meant every operation tied up additional context real estate. "Results returned by an MCP server have to go through the agent's context to be persisted to disk or combined with other results," he noted.
His initial response was pragmatic—he built CLI-based browser tools using Bash scripts. These required only 225 tokens to document and could pipe output directly between commands without taxing the model's attention.
A Second Approach to MCP#
When Earendil acquired Pi earlier this year, the new parent company revisited the MCP question. Rather than abandon the protocol, Earendil found a way to keep it without repeating Zechner's original mistake. The key insight: MCP didn't need direct exposure to the language model. Instead, Pi could route MCP tool calls through Codemode, a sandboxed JavaScript execution layer that already handled tool discovery and invocation for Pi's own capabilities.
"The reason we brought MCP into the core is not just about how MCP has changed, but also because we found that the changes it would require were generally useful," Earendil explained.
How Codemode Reduces Context Cost#
Codemode operates in a restricted QuickJS sandbox—no Node APIs, file system access, network calls, or timers. Within those constraints, it can invoke Pi's tools, run concurrent operations, and filter results before returning anything to the model.
By default, MCP tool definitions never appear in the model's context. Instead:
- The system prompt receives only a one-line description of each MCP server

