Code Mode: let your assistant write code instead of reading everything

A step-by-step guide to understand what Code Mode is, measure the same task done with tools and done with code, and know where you already have it and when it pays off.

PublishedNovember 21, 2026
FreeDownload as PDF

Free resources

Create a free account to download

PDF guides and the other free resources are available with an account. It's free, and all you need is your email.

In the MCP guide we measured what the definitions of the tools take up. The other half is the results. Everything a tool returns passes through the model, whether it needs it or not. Code Mode is the answer to that: instead of calling tools one by one, the model writes a program that calls them, and reads only the final result.

The steps and figures were verified on macOS with Claude Code 2.1.302 and the Sonnet model (November 2026). What is quoted from Cloudflare and Anthropic is marked as theirs: we did not measure it.

What you will get

Understand what Code Mode is, measure the difference yourself with a real task, and know when to ask your assistant to write code and when it is not needed.

What you need

  • Claude Code installed and signed in.
  • Node.js (for the test server, with npx) and Python 3 (to create the test files).
  • Reading the MCP guide first helps, but it is not required.

Step 1. The problem: results pass through the model

When your assistant uses a tool, the whole result goes back to the model. If you ask for a figure that is spread over forty files, the model receives the forty complete files to keep one number.

And since the model remembers nothing between one call and the next, everything it has read travels again in every later call of that task.

Diagram: 40 files go through the MCP tools to the model, which answers 89
Direct way: the model receives the forty files to give one number.

Anthropic describes it with an example: moving a meeting transcript from a document to a customer manager forces the full text through the model twice, once to read it and once to write it.

Step 2. What Code Mode is

Code Mode is the name Cloudflare gave (September 2025) to a simple idea:

  • The tools of an MCP server become functions of a small program.
  • The model sees a single tool: "run this code".
  • The model writes a program that calls those functions, filters, counts or combines, and returns only what is needed.
  • The program runs in an isolated environment (sandbox): no open internet access and no view of the keys.
Diagram: the model sends a program to an isolated environment where the tools are functions; only 89 comes back
Code Mode: the files stay in the isolated environment. Example program.

Why it works, according to Cloudflare: models have seen a huge amount of real code and very few examples of tool calls, so they are better at writing code. Anthropic published the same idea in November 2025 as "code execution with MCP".

Step 3. Prepare the test

Create forty test meeting notes, with action items assigned to three people, and the two configuration files. On the Mac, in Terminal:

bash
mkdir -p ~/codemode-test && cd ~/codemode-test
cat > make_notes.py <<'PY'
import os, random
random.seed(12)
names = ["alex", "sam", "kim"]
topics = ["pricing page", "onboarding email", "launch checklist", "support macros", "billing export",
          "landing page", "release notes", "demo script", "search filters", "mobile layout"]
os.makedirs("meetings", exist_ok=True)
for i in range(1, 41):
    lines = [f"weekly sync {i:02d} - alex, sam, kim", "", "Discussion"]
    for k in range(18):
        lines.append(f"- {random.choice(topics)}: status reviewed, {random.choice(['on track', 'blocked', 'needs review', 'done'])}; notes from the team about scope, owners and dates for item {k+1}.")
    lines += ["", "Action items"]
    for k in range(random.randint(4, 9)):
        lines.append(f"- [{random.choice(names)}] {random.choice(['fix', 'review', 'update', 'ship', 'write'])} the {random.choice(topics)} by {random.choice(['monday', 'wednesday', 'friday'])}")
    open(f"meetings/meeting-{i:02d}.txt", "w").write("\n".join(lines) + "\n")
PY
python3 make_notes.py
cat > mcp.json <<JSON
{ "mcpServers": { "filesystem": { "command": "npx",
  "args": ["-y", "@modelcontextprotocol/server-filesystem", "$HOME/codemode-test/meetings"] } } }
JSON
echo '{ "mcpServers": {} }' > none.json

That is 40 files and about 90 KB of text. The question will be: how many action items are assigned to sam? With these files, the right answer is 89.

Terminal: the files meeting-01.txt to meeting-05.txt, 40 files and 90,302 bytes
The notes created: 40 files, about 90 KB.

Step 4. Measure the direct way

First, the way an assistant with tools only would do it: read the files with the MCP server. On the Mac, in Terminal:

bash
cd ~/codemode-test
ENABLE_TOOL_SEARCH=false claude -p "There are 40 meeting notes in the meetings folder. Across all of them, how many action items are assigned to sam? Lines of action items look like '- [sam] ...'. Reply with the number only. Use only the filesystem MCP tools: read the files with them. Do not write or run code." \
  --model sonnet --output-format json --mcp-config mcp.json --strict-mcp-config \
  --allowedTools 'mcp__filesystem__read_text_file,mcp__filesystem__read_multiple_files,mcp__filesystem__list_directory' \
  --disallowedTools 'Bash,Read,Grep,Glob,Write,Edit,Task'

In our test:

text
result: 89
num_turns: 5
duration: 18.0 s
input tokens (total): 193,641
total_cost_usd: 0.33

The answer is right, but to give it the model received the forty files in full.

Terminal: output of the direct way with result 89, 5 turns and the cache tokens
Real output of the direct way. The input tokens are the sum of the three input fields.

Step 5. Measure the same with code

Now the same question, asking it to write a program and read only its output. On the Mac, in Terminal:

bash
cd ~/codemode-test
claude -p "There are 40 meeting notes in the meetings folder. Across all of them, how many action items are assigned to sam? Lines of action items look like '- [sam] ...'. Reply with the number only. Write and run one short script that counts them, and read only its output." \
  --model sonnet --output-format json --mcp-config none.json --strict-mcp-config \
  --allowedTools 'Bash' --disallowedTools 'Read,Grep,Glob,Task'

In our test:

text
result: 89
num_turns: 2
duration: 3.9 s
input tokens (total): 40,479
total_cost_usd: 0.08

The same answer with almost five times fewer tokens, in a quarter of the time and for a quarter of the cost. And those 40 thousand tokens are nearly all what Claude Code always sends (about 20 thousand per call, two calls): the files never passed through the model.

Add the fields input_tokens, cache_creation_input_tokens and cache_read_input_tokens of the output to get the total. Your figures will vary a little.

Terminal: output of the code way with result 89, 2 turns and the cache tokens
Real output with code.
Bar chart: 193,641 input tokens the direct way and 40,479 with code
The same answer, 89, with almost five times fewer tokens.

Step 6. Where you already have it

  • Claude Code and Codex: they have a terminal, so they can already write and run a program. Just ask, as in step 5.
  • Claude and ChatGPT apps: when code execution or data analysis is on, they can process a file with a program instead of reading it all.
  • Cloudflare MCP server (https://mcp.cloudflare.com/mcp): its whole API, more than 2,500 operations, with only two tools, search and execute. According to Cloudflare it takes about 1,000 tokens; with one tool per operation it would be 1.17 million. It needs a Cloudflare account; we have not tested it.
  • If you build your own agents: the Claude API has "programmatic tool calling" and Cloudflare publishes the @cloudflare/codemode package.
Comparison: one tool per operation, 1.17 million tokens; Code Mode with search and execute, about 1,000
Figures published by Cloudflare; we have not measured them.

Step 7. When to use it and when not

Ask for code when:

  • there are many items to go through (files, rows, emails);
  • the result of a tool is large and you only need a part;
  • several calls must be chained, passing the result of one to the next.

It is not needed for a single call with a small result: reading a short file, creating an event. There the direct call is just as good and simpler.

Good practices

  • Say it in the message: "write a program that does it and read only its output".
  • Ask for the final result, not the data: a number, a short list, a summary.
  • Review what the program does before letting it change or delete anything: the code acts with your permissions.
  • Run the code in a bounded place: a working folder, not the whole disk.

Common problems

  • The assistant reads the files instead of writing the program. Ask explicitly or, in Claude Code, limit the tools with --allowedTools.
  • The program fails. The assistant sees the error and fixes it; each attempt is one more call, but it still does not read the data.
  • The number does not match. Ask the program to also print how many files it went through, and compare.

Sources

  • Cloudflare, "Code Mode: the better way to use MCP" (26 Sep 2025) and "Code Mode: give agents an entire API in 1,000 tokens" (20 Feb 2026).
  • Anthropic, "Code execution with MCP: building more efficient agents" (4 Nov 2025) and the "Programmatic tool calling" documentation.