Iaroslav Kozlov Русский

LLM integrations Code Mode

Code Mode

A way to use MCP servers: their tools become a typed API, the model writes a short program against it, a sandbox runs the program, and by default only its output, its return value and its errors go back to the model.

My work

I use Code Mode in place of long lists of MCP tools, to spend fewer tokens; the layer that runs the model’s code is partly ready-made and partly mine.

How it is built

A generalised layout from Cloudflare’s and Anthropic’s descriptions, not my installation. The numbers are the vendors’ own, each with the setup it was measured on.

data control My part

While no sandbox can be started

unavailable

  • The code tool returns an error to the model, as any tool that fails does: no program ran, so nothing was half done.
  • The MCP servers are untouched. Where the host also lists their tools, the model can call them one by one, and pays again for the definitions and the results.
  • This is the part Code Mode adds that can fail: Anthropic counts sandboxing, resource limits and monitoring as overhead that direct tool calls avoid. 2

Components

Press a component to read what it does.

Model writes code

The language model in its host application. In Code Mode it writes a few lines of code instead of calling the tools one by one.

Typed API read on demand

The tools’ schemas turned into TypeScript definitions, which the model looks up as it needs them, not all up front.

Code tool runs programs

The tool the model hands its program to: it starts a sandbox for the program and passes the sandbox’s calls on to the servers. Finding the definitions can be a tool of its own, as search() is at Cloudflare.

Sandbox no own network

Runs the program. By default its only way out is through the bindings it is given; the keys stay with the runtime.

MCP servers the same tools

The servers are not changed: Code Mode runs on top of MCP and calls the same tools.

Databases, logs read-only

What the tools read. Their limits are set where the data is: a read-only role, a statement timeout, a cap on returned rows.

Output console output

What the program logs or returns, and its errors: all of the run that reaches the model.

  1. Where the tokens go

    With plain tool calls, most clients fill the context with two things: the definition of every tool, loaded before the task starts, and every intermediate result, which passes through the model.

    In detail

    Anthropic names both. Most MCP clients load every tool definition into the context up front, and each tool’s result goes back through the model before it can be passed to the next call: in its example, a two-hour meeting transcript copied from one system to another passes through the context twice, about 50,000 tokens more. Its answer is to present the servers as code: the agent lists a tree of files and reads only the definitions it needs. 2 34

    Where it breaks Anthropic later shipped the first half of this as an API feature, tool search: tools marked to load on demand are found when needed. Its own internal figures: about 77,000 tokens up front without it, about 8,700 with it. Cloudflare’s objection: the search itself has to be maintained, and each tool it finds still costs tokens.

  2. Tools as an API

    Code Mode turns the tools into a TypeScript API and gives the model a tool that runs code: the model writes a short program instead of calling the tools one by one.

    My part I use Code Mode in place of long lists of MCP tools, to spend fewer tokens.

    In detail

    In Cloudflare’s Code Mode, the Agents SDK fetches each MCP server’s schema and generates TypeScript interfaces with doc comments taken from it; the agent is presented with one tool, which executes TypeScript. Cloudflare’s reasoning: models have seen far more real code than tool calls, so they may write code against an API better than they call tools. Its MCP server for the whole Cloudflare API applies the same idea on the server’s side: two tools, search() and execute(), in front of more than 2,500 endpoints. 1 4

    Where it breaks Code Mode is not part of the MCP specification and does not replace MCP: the API is generated from the servers’ own schemas, and the servers stay as they are.

  3. The sandbox

    In Cloudflare’s executor the program runs in an isolate started for it, by default with no network of its own: fetch() and connect() fail, and the keys stay outside the code.

    My part The layer that runs the model’s code: part of it ready-made, part written by me.

    In detail

    Cloudflare runs each snippet in a fresh V8 isolate through its Dynamic Workers, in open beta since 24 March 2026. The DynamicWorkerExecutor of its codemode package blocks outbound fetch() and connect() by default and stops a program after 60 seconds; an outbound Fetcher, if one is given, receives every request and can add the credentials itself, so the secret is never in the code. The package was rewritten in February 2026 to take any sandbox through one interface, Executor. 5 68

    Where it breaks Cloudflare itself says isolates have a broader attack surface than virtual machines, and adds layers of its own. Its plain code tool leaves out the tools that need a person’s approval; its durable runtime pauses the program before such a call and, once it is approved, resumes it by replay.

  4. Calls through bindings

    A call the program makes goes through a binding to the runtime, and the runtime calls the MCP server: loops, joins and filters run in the code.

    In detail

    In Cloudflare’s Code Mode the servers are reached through bindings, and the runtime adds the access tokens. Anthropic’s example of what this buys: a sheet of 10,000 rows is filtered in the sandbox, the code prints a count and the first five matches, and the model sees five rows instead of 10,000; a loop that polls until a condition holds is a few lines of code, not a run of tool calls each answered by the model. Datadog’s MCP server does the same over its own APIs: in its example, one script finds the services with the most error logs in a time window, queries span latency for each, joins the two and returns a compact object, and every API call inside is authorized with the user’s own permissions. ClickHouse’s MCP server can run EXPLAIN ESTIMATE before a query: it estimates the parts, rows and marks the query would read from MergeTree-family tables, and covers no other engines. 1 2910

    Where it breaks Filtering in the sandbox saves the model’s context, not the database: the rows are still read and sent. For a database the filter and the aggregate belong in the query, and the limits where the data is: a read-only role and a statement timeout; a cap on returned rows does not bound what the query reads. A sandbox does not make a write tool read-only. Once its default 30-second timeout passes, ClickHouse’s MCP server only tries to cancel the query with KILL QUERY.

  5. What comes back

    By default the model sees only what the program prints or returns, and its errors; the intermediate data stays where the code ran.

    In detail

    In Cloudflare’s description the results come back as the program’s console output. Anthropic adds that data not meant for the model can pass from one tool to another without entering its context at all, and that working code can be saved as a function and, with a SKILL.md beside it, become a skill. 1 27

    Where it breaks What the program prints is still read by the model: a program that prints the whole table brings back the cost it was meant to save. Pydantic reports that one model printed much of the data back into the conversation, which limited its token savings at 200 items.

  6. What was measured

    Each figure is one setup measured by the vendor: a worked example, one large API, a benchmark. On short tasks, Code Mode can cost more than plain calls.

    In detail

    In Anthropic’s example, reading only the definitions the task needs took 150,000 tokens down to 2,000. Cloudflare measured what its server puts into the context, with tiktoken: about 1,000 tokens for two tools against 1.17 million for a tool per endpoint. Anthropic’s programmatic tool calling took the average on complex research tasks from 43,588 tokens to 27,297, in its internal testing. Pydantic graded 5,276 runs of eight models with and without Code Mode: on long tasks the cost fell by more than half; with five items it rose on three tasks of five. It also found that models already batch independent calls, up to 200 in one response: what Code Mode cuts is what each request carries. 2 3457

    Where it breaks For a short task plain calls can be cheaper, as Pydantic’s five-item runs showed: it is worth measuring both on the real work. Running a program again can repeat its writes: in Pydantic’s benchmark, three of one model’s failures on the long tasks created the same records twice. When Cloudflare’s durable runtime resumes a paused execution, it reuses the recorded results, except for calls marked to run again, and matches them by order: in a program that can pause, connector calls should run one after another.

The whole diagram

Code Mode

Every component and step of the figure, drawn at once; the page lists them in text.

data control My part

Next story

Backstage: a model of services

Backstage is an open-source framework for developer portals, made at Spotify: its software catalog keeps services, APIs and databases as entities with owners and relations, and its templates create new services from a skeleton.

How the catalog works LLM integrations