DEV Community

Volodymyr Nehir
Volodymyr Nehir

Posted on

What Is Code Mode: How to Cut AI Agent Context by 99% Using Code Instead of JSON-RPC

If you have built autonomous AI agents over the past year, you have almost certainly embraced MCP (Model Context Protocol). It has quickly become the open industry standard for giving language models access to external tools, databases, and APIs.

However, as production agent architectures scale, a glaring flaw in raw tool calling has emerged: schema bloat, context rot, and skyrocketing inference costs.

When an agent connects to multiple MCP servers exposing dozens or hundreds of endpoints, your context window degrades before the user even finishes typing their prompt.

The industry's answer—pioneered and adopted by teams at Cloudflare, TanStack, Arize AI, Goose, and across the open-source ecosystem—is Code Mode.

In this deep dive, we’ll explore what Code Mode is, why traditional JSON-RPC tool calling breaks down at scale, and how treating code as the execution interface slashes token overhead by up to 99.9%.


The Bottleneck: Why Raw Tool Calling Doesn't Scale

In traditional tool calling, the LLM is treated as an RPC router. For every tool available, you pass an exhaustive JSON schema describing its name, description, and every nested parameter type.

This model encounters three critical failure points:

1. Schema Bloat & Context Rot

  • Small scale: 50 tools across 4 internal servers consume around 9,400 tokens purely on schema definitions in every single turn.
  • Enterprise scale: Exposing an entire enterprise surface—such as Cloudflare’s 2,500+ REST API endpoints—via traditional JSON schemas would require ~1.17 million tokens. That exceeds the context window of almost every production model, leaving zero room for conversation history or reasoning.

2. The N+1 Network Round-Trip Tax

Imagine asking an agent: "Find our top 20 customers, fetch their latest orders, and check shipment status."

Under raw tool calling, the agent must:

  1. Call getTopCustomers() → wait for model inference → parse response.
  2. Trigger 20 sequential or batched tool calls for getOrders() → wait → re-ingest all JSON.
  3. Trigger another batch for getShipmentStatus().

Every intermediate payload—megabytes of raw JSON—is dumped directly into the conversation history. You pay for these tokens on every subsequent inference turn, and response latency balloons from seconds to minutes.

3. Arithmetic Hallucinations & Logic Failures

LLMs predict tokens; they do not compute. When raw tool calling forces the model to sum, filter, or join arrays returned from separate endpoints, calculation errors are virtually guaranteed.


What Exactly Is Code Mode?

> Code Mode is an architectural pattern where an agent interacts with tools not through declarative JSON-RPC schema arrays, but by writing and executing programs (typically TypeScript, JavaScript, or Python) against a typed client library inside a secure sandbox.

Instead of registering hundreds of individual function schemas with the model provider, the system exposes a minimal meta-interface—often just two tools:

  • search(query): Dynamically discover available types and endpoints on demand (Progressive Disclosure).
  • execute(code): Run a complete script in an isolated runtime (V8 isolate, QuickJS, or WASM).
Traditional Tool Calling:
[User Prompt] -> [LLM] -> [Tool Call Request] -> [Client Executes API] -> [Raw JSON to Context] -> [LLM] ... (Repeated N times)

Code Mode:
[User Prompt] -> [LLM Writes Typed Script] -> [Secure Sandbox Executes Loops/Fetches/Logic] -> [Pruned Summary back to LLM]

Enter fullscreen mode Exit fullscreen mode

Why Models Excel at Code Mode

  1. Pre-training bias: Modern frontier models have ingested hundreds of billions of lines of real-world code featuring loops, async handling (Promise.all), conditionals, and array manipulations. They are fundamentally better at writing idiomatic TypeScript than navigating bespoke JSON tool-call structures.
  2. Context Pruning at the Edge: Filtering, pagination, sorting, and mathematical aggregations happen directly inside the execution sandbox. Only the final computed result is returned to the context window.

Code Comparison: The N+1 Problem in Action

Let’s look at a concrete workflow: "Fetch top 5 products and calculate their average review rating."

The Raw Tool Calling Way

Requires 7 conversational round trips, injects dozens of raw rating objects into context, and relies on the LLM to mentally compute averages.

The Code Mode Way (e.g., TanStack AI / Cloudflare Pattern)

The model generates and runs a single programmatic workflow:

// The agent writes this entire execution block at once
const topProducts = await storeApi.getTopProducts({ limit: 5 });

// Concurrent data retrieval directly inside the runtime
const ratingsData = await Promise.all(
  topProducts.map((product) => storeApi.getProductRatings({ productId: product.id }))
);

// Deterministic filtering and exact math executed in JS engine
return topProducts.map((product, idx) => {
  const scores = ratingsData[idx].ratings.map((r) => r.score);
  const avg = scores.reduce((acc, curr) => acc + curr, 0) / (scores.length || 1);

  return {
    id: product.id,
    name: product.name,
    totalSales: product.totalSales,
    averageRating: Number(avg.toFixed(2)), // 100% mathematical precision
  };
});

Enter fullscreen mode Exit fullscreen mode

The difference:

  • Model turns: 1 instead of 7+.
  • Token footprint: Only the final 4-line summary enters the conversation context.
  • Accuracy: Exact floating-point math handled natively by the JavaScript engine.

The Real-World Numbers: Token Reductions

Architecture / Real-World Benchmark Raw Tool Calling Code Mode Token Savings
Cloudflare Ecosystem (2,500+ APIs) ~1,170,000 tokens (Unusable) ~1,000 tokens 99.9%
StackOne Integration Benchmarks ~14,000 tokens ~500 tokens 96.4%
Goose Agent (Bug fix & PR workflow) 33,648 tokens 23,339 tokens ~30.6%

Sandboxing & Security: Running Untrusted Agent Code

Allowing an LLM to generate dynamic code introduces immediate security requirements. Production Code Mode implementations address this through zero-trust runtime environments:

  1. Lightweight Isolates (V8 / Cloudflare Dynamic Workers): Start in under 5ms without container startup overhead.
  2. WebAssembly & QuickJS: Lightweight runtimes (like quickjs-emscripten) running directly in-process or in edge functions with strict RAM and CPU execution deadlines.
  3. Capability Bindings over Raw Secrets: Scripts never receive raw API keys or database connection strings. Instead, the sandbox provides pre-authenticated proxy client instances where network requests are routed and validated by the host environment.
  4. State Persistence Across Turns: Advanced runtimes (such as OpenClaw) allow scripts to store memory pointers and references across turns, letting agents interact with multi-megabyte datasets without repassing data into the LLM context.

When to Use Which Architecture

Scenario Raw MCP / Native Tool Calling Code Mode
Small tool catalog (< 15 tools) ✅ Best fit ⚠️ Architectural overkill
Single-step lookup queries ✅ Fast & simple ⚠️ Unnecessary step
Large API catalog (> 50 endpoints) ❌ Context bloat ✅ Essential (99% savings)
Data filtering, loops, joins, and math ❌ Slow & error-prone ✅ Native execution
Strict air-gapped data operations ❌ Leaks raw payloads to LLM ✅ Data pruned in sandbox

Final Thoughts

Code Mode is not a replacement for the Model Context Protocol; it is the architectural layer that makes MCP viable at scale.

Instead of forcing language models to act as fragile, latency-heavy RPC brokers, Code Mode meets LLMs where they are strongest: generating clear, typed, expressive code that executes safely in an isolated runtime.

Top comments (3)

Collapse
 
feamine709byte profile image
AMINE FE •

The "MCP Context Tax" is the elephant in the room right now. Pre-loading entire OpenAPI/JSON schemas upfront on every turn via tools/list turns autonomous agents into token incinerators before any real inference begins.

In Star Protocol, we solved this with a two-tier JIT Schema Hydration:

  1. The model only receives lean tool signatures (name + 1-sentence descriptor) in the base prompt.
  2. Full parameter schemas are injected dynamically only when tool invocation is predicted.
  3. We preserve prompt cache hits via Deterministic Canonical Ordering so the dynamic tool additions don't break Anthropic/OpenAI KV caches: github.com/feamine709-byte/star-pr...

Moving tool discovery to a JIT protocol gateway rather than dumping raw JSON schemas cuts baseline agent prompt overhead by up to 85%.

Collapse
 
paul_irolla_6b5ae261d0224 profile image
Paul Irolla •

Worth saying what the 99% covers: the tool definitions only, or the history each step resends too?

Collapse
 
volodymyr_nehir profile image
Volodymyr Nehir •

Great question! That 99% figure applies specifically to tool/schema definitions upfront, but Code Mode actually tackles token consumption and context degradation on two distinct levels:

  1. The 99.9% reduction: Static Schema Overhead In traditional tool calling / raw MCP, you must pass full JSON schemas for every endpoint upfront.
  2. For an enterprise catalog like Cloudflare's (2,500+ endpoints), dumping those schemas into system prompts takes ~1.17 million tokens before the user says a single word.
  3. With Code Mode (using a typed client + progressive disclosure via search and execute), agents discover what they need dynamically. That initial footprint drops to ~1,000 tokens—which is where the 99.9% number comes from.
  4. The 90%+ reduction: Dynamic Multi-Turn History (Context Pruning) This is arguably the even bigger win for runtime cost and latency:
  5. In raw tool calling, if an agent needs to paginate or fetch 20 items in a loop, it takes 20 round-trips. Every single raw JSON response gets appended to the message history and re-sent to the LLM on every subsequent turn (O(N²) context accumulation).
  6. In Code Mode, that entire loop runs in the local sandbox (V8/QuickJS). The raw data is filtered, mapped, and aggregated in memory, and only the final clean result enters the LLM conversation context. In real-world benchmarks (like StackOne workflows), ~14,000 tokens of raw API payloads were distilled into a ~500-token summary in context (~96% reduction in conversational payload).

So the short answer: The 99% headline is upfront schemas, but sandbox execution simultaneously eliminates the exponential compounding of multi-turn history.