Every tool call an agent makes is more expensive than it looks: the tool description and the full response both fly into the context, and you pay for each such chunk. The most important architectural innovation of 2026 is not a new model but a new way of calling tools. Code Mode in Claude Code: the model does not fire tools one by one — it writes JavaScript that calls the tools itself inside a REPL, and only the result returns to the context; according to measurements, that is a 29% cut in task cost and half the input tokens. This guide covers the mechanics, the economics, and the pitfalls.
A Paradigm Shift: from Calls to Code
The classic agent: each tool gets a separate call with a full description and a full response in the context. Ten steps over data — ten bloated fragments from which the model needs only the conclusions. Code Mode changes the shape of the work: the model writes a function that calls the needed tools in a loop, processes the results, and returns a summary. Schematically:
// instead of a chain of a dozen calls — one function
const rows = await search({ source: "orders", limit: 10000 });
const big = rows.filter(r => r.amount > 5000);
return {
count: big.length,
total: big.reduce((s, r) => s + r.amount, 0),
top: big.slice(0, 5).map(r => r.id)
};Instead of a thousand raw records, the context receives an aggregate of a few lines: the model sees the outcome — the very thing the task was launched for. The savings come from two parts: fewer input tokens (responses are not hauled in whole) and fewer calls (the loop lives inside a single code fragment).
Why is the classic approach so expensive? Each call carries the tool’s schema description, the parameters, and the full response in the context, and all of it lives on in the dialog: subsequent steps pay for other steps’ responses as input tokens every time. A response that has already served its purpose is dragged along by the agent until the end of the session. That is why ten steps over data in the classic setup cost noticeably more than ten lines of REPL code.
The Cloudflare Lesson: Two Tools Instead of 2600
A historical lesson from the same series is Cloudflare: 2600 MCP tools replaced by two (search + execute); descriptions shrank from 1.1 million tokens to about 1000. The approach was adopted by Codex, OpenCode, Prime Agents, and DataDog — the “search+execute” pattern became an industry standard. The essence matches Code Mode: instead of dragging the descriptions and responses of all connectors into the context, the agent first finds the needed tool and then executes it. The mechanics of MCP connectors do not change — the way they are called does.
The lesson is useful even if you have a dozen connectors rather than hundreds: every server with verbose descriptions inflates the system prompt, and that weight is paid for at every step of the session. The “search first, then execute” scheme removes the problem from both sides: descriptions are not needed all at once, and tool responses are processed by code instead of piling into the dialog.
Measurements: 29% Off the Cost and Half the Input Tokens
| Metric | Classic | Code Mode |
|---|---|---|
| Input tokens | baseline | ~50% |
| Task cost | baseline | ~71% (−29%) |
| Opus on the benchmark | 65% | 71% (same model) |
| Browser-use rewritten | — | quality +50%, tasks 2–5x cheaper |
The key conclusion is the row with the flagship Opus: quality grew without changing the model, purely from the harness. “Geniuses managed poorly” is how the author of the analysis put it: model limitations often sit in the harness, not in the capabilities. From this comes a practical rule: before changing the model, check whether you are losing quality to the way tools are called. Every row of the table compares against the baseline on the classic harness with the same model: no new engine, no provider change, only the calling mechanics. The measurements are collected from practice and are current as of fall 2026.
REPL Mechanics: State, Errors, Subagents
- State between calls. Variables in a REPL persist: 10,000 records are processed by code in chunks, and only the aggregate goes into the context. A classic agent would have to keep the data in context between steps.
- Errors as data. An exception in the code is information for the next iteration, not a task failure: the agent reads the traceback, fixes the function, and continues. The debugging loop closes inside the REPL without inflating the dialog.
- Programmatic control of subagents. Functions for creating contexts were found inside the Claude Code binary — a move toward RLM: dialog history and documents become variables, and the model itself decides what to keep in context and what to offload.
- The prompt cache trap. Editing the context breaks the cache — even at 10% of the price for cached tokens, the economics break on recalculation. For background tasks use poll: checking status on a schedule instead of spamming “how’s it going?” messages.
The most far-sighted item on the list is programmatic control of subagents. If the context becomes a variable, it can be created, discarded, and replaced from code: the model assembles a group of subagents, gives each its own context, and merges the results. That is the RLM direction: the context stops being one long string and turns into a data structure under program control.
The hygiene rule is simple: while a background task is running, do not write anything into the same session that edits earlier context. Cache discipline is covered in the article on prompt cache discipline. Background polling looks schematically like this:
// poll instead of spam: status is checked on a timer
while (job.status !== "done") {
await sleep(interval);
job = await checkStatus(job.id);
}Economics on Limits and Choosing a Harness
On paid tiers Code Mode lowers the bill directly; on free pools it stretches the quota: a 29% cost reduction means almost a third more tasks within the same daily limit. Quota benchmarks are in the free tier limits matrix, and the before-and-after counting method is in the article on token accounting: without separate counting of input and cached tokens, you simply will not see the Code Mode effect.
You can verify the effect on your own stack in one evening: take a typical routine task, run it on the classic harness, and record the counters; then enable Code Mode and repeat the same task. The difference between the counters is your personal share of that 29% saving — the more tool calls in your typical scenario, the more visible it is.
Watching agent architectures is now more interesting than watching new models: +29–50% from changing the harness without changing the engine. When choosing a harness, the question “does it have Code Mode or a REPL?” has become one of the main ones — alongside skills and hooks; the general selection framework is in the article on LLM agent harnesses. Architectures are moving toward RLM: “compaction may become obsolete” — the context will become programmatically managed rather than a string that gets trimmed.
Common Questions
Is this only in Claude Code? The pattern transfers: the Cloudflare approach has been adopted by all the major harnesses, and REPL mechanics are in recent versions of Claude Code. When choosing a tool, the question is whether your installed version supports the mechanics: it is young and changes quickly, so check the changelog before updating.
Security of agent code? The REPL is a harness-level sandbox: code runs in a controlled environment with no direct access to the system. Destructive actions are still closed off by hooks — the result is two-layer protection: the sandbox limits the code, hooks limit the agent.
Does it work on the free tier? Token savings matter twice as much on limits: Code Mode on free models delivers the same 29% quota saving — more tasks per day without upgrading the plan.
What is next? RLM: context as variables, the model managing memory itself — compaction fades into history. The first steps are already visible in the context-creation functions inside Claude Code. Practitioners should watch this direction: programmatic context management changes both the economics of long sessions and the mechanics of compaction itself.
The main takeaway: Code Mode is proof of the thesis “the harness matters more than the model”: one mechanism delivered a third off the cost, a quality gain, and not a single engine change. Concrete action: check whether your agent version has a REPL or Code Mode, enable it on routine tasks, and measure token spending before and after — the numbers will convince you faster than any reviews. The measurements in this article are as of fall 2026.
