
Or how $1.09 of API calls made me change my title along the way.
Executive Summary
Problem: Code Mode, popularized by Cloudflare, replaces an agent’s tool list with two generic tools, search and execute, for which the model writes JavaScript. The promise: an initial context divided by a thousand on a large API. But a smaller initial context does not guarantee a cheaper task.
Protocol: Two architectures, exactly the same tools and the same data. Three tasks that require 2, 4 then 7 tools, on catalogs of 10, 30 and 100 tools, with and without prompt caching, as isolated tasks and then as a conversation. 30 runs per cell.
Result: 1,440 measurements, all successful. On 10 tools, Code Mode sends 29% fewer tokens on the first call and costs 77% more per successful task. On 100 tools without caching, it costs 43% less. On 100 tools with caching, direct tool calling is back to half the price.
What I take away: catalog size matters, caching matters more. And the size of the initial context is a poor indicator of what you will pay.
Glossary
- Direct tool calling
- the classic architecture: the definition of every tool (name, description, JSON schema) is sent to the model on every call. The model picks the tools and the harness runs them.
- Code Mode
- an architecture popularized by Cloudflare: the model only receives two tools, search and execute, and writes JavaScript to discover and then call the tools of a catalog that stays out of its context.
- Initial context
- the size of the first prompt sent to the model: tool definitions, system prompt and user request.
- Prompt caching
- a mechanism by which a provider bills the start of a prompt at a discounted rate when it is identical to that of a recent call. Depending on the provider, it is explicit or automatic.
- Cost per successful task
- the total amount billed for a series of tasks, divided by the number of tasks whose final answer is correct. This is the central metric of this benchmark.
- Sandbox
- an isolated environment where the code written by the model runs, with no access to the network or the file system.
The promise that intrigued me
When Cloudflare published its Code Mode, one number stuck: their whole API, more than 2,500 endpoints, exposed to an agent for about a thousand tokens instead of more than a million. The idea is elegant. You no longer give the model its list of tools. You give it search, to query the catalog with code, and execute, to call what it found. The catalog stays in a sandbox, and only the result of the code enters the context.
On an API with 2,500 endpoints, I have no doubt it works. But most business agents do not have 2,500 tools. They have ten, sometimes thirty. And I was watching the pattern spread as a universal best practice, always with the same argument: look how small the starting context is.
So I started with a hunch: on an ordinary business agent, the discovery steps must eat the savings. I wanted to check it, without assuming the conclusion. Good thing I did, because the conclusion is not the one I expected.
The protocol: two architectures, the same tools
The domain is deliberately mundane: sales management for a fictional company. Ten tools, from find_customer to get_credit_limit, with complete JSON schemas and deterministic data.
Direct scenario: tool definitions are sent on every model call. Independent calls go out in parallel.
Code Mode scenario: two tools, search and execute, each taking a JavaScript function, following the pattern published by Cloudflare. Results are truncated at about 6,000 tokens, as they are there.
The three tasks are about the same customer and keep the same shape: find the customer, then fire independent calls. Only the number of tools required changes: 2, 4, then 7.
The central metric is not the number of tokens in the first prompt. It is the amount actually billed, divided by the tasks whose final answer is correct.
One protocol trap cost me two trial runs. I wanted a “no cache” series to measure the raw cost. Except the provider caches on its own, without being asked, as soon as the prefix goes past 1,024 tokens. So my first “no cache” measurements were reading from the cache. I had to slip a unique identifier into every call to break the prefix. Keep that in mind if you run your own measurements: on some providers, “no cache” does not exist by default.
Ten tools: less context, bigger bill
| Isolated tasks, no caching | Direct | Code Mode |
|---|---|---|
| Initial context | 998 tokens | 705 tokens |
| Total tokens per task | 3,740 | 6,832 |
| Model calls | 3.0 | 4.3 |
| Cost per 1,000 successful tasks | $0.47 | $0.82 |
| Success rate | 100% | 100% |
The promise holds on the first row, and only on that one. Both architectures give the right answer every time. The difference is in the bill, +77%, and in the wait: the median latency of Code Mode is 32% to 55% higher depending on the task.
And the gap does not close as the task grows: +81% on the 2-tool task, +73% on the 4-tool one, +78% on the 7-tool one. I expected the opposite. The more calls there are to chain, the more turns a single script should save.
Where the savings go
The transcripts answer without ambiguity. Three mechanisms.
The model does not write the single script. Across 180 Code Mode runs, none settled everything in one execute. All of them made exactly two: a first one to find the customer, a second one for the rest. The model wants to see the identifier before going on. Direct tool calling does the same thing in two tool turns, without the search step.
What is discovered stays in the context. A search returns on average 2,800 characters of descriptions and schemas. That text is reread on every following call, exactly like the definitions Code Mode wanted to avoid sending. The savings of the first call are handed back on the second.
Two generic tools are not free. For the model to write code against a catalog, you have to describe the catalog, the types and an example. My two definitions weigh half as much as the ten classic ones.
And in a real conversation?
My first protocol was harsh: every task started from scratch, so every task paid for its discovery again. In real life, a user asks one question after another. I ran the measurement again, asking the three requests in a row, in the same thread.
| Extra cost of Code Mode, 10 tools | Isolated tasks | In a conversation |
|---|---|---|
| Without caching | +77% | +52% |
| With caching | +68% | +27% |
The gap narrows sharply. After the first question, the model only runs a new search one time in three to one time in two, and the latency of the two architectures converges to within half a second.
It stays more expensive, for a reason I had not anticipated: from the second question on, Code Mode is the one carrying the larger context, 1,789 tokens against 1,222. The code it wrote and the schemas it discovered pile up in the history.
Thirty tools, a hundred tools: that is where it flips
At that point, I had my article. “Code Mode costs more”, full stop. Except ten tools is home turf for direct tool calling, and the case for Code Mode is about large catalogs. Publishing without testing that would have been dishonest.
So I surrounded the ten business tools with plausible but useless ones: suppliers, quotes, credit notes, purchase orders, payment reminders, disputes. Some are deliberately close to the useful tools.
| Cost per 1,000 successful tasks | Direct | Code Mode | Gap |
|---|---|---|---|
| 10 tools, no caching | $0.47 | $0.82 | +77% |
| 30 tools, no caching | $0.85 | $0.97 | +14% |
| 100 tools, no caching | $2.21 | $1.25 | −43% |
| 10 tools, with caching | $0.37 | $0.62 | +68% |
| 30 tools, with caching | $0.26 | $0.69 | +171% |
| 100 tools, with caching | $0.41 | $0.82 | +100% |
Without caching, the promise holds. The initial context of direct tool calling goes from 998 to 6,802 tokens, while Code Mode stays around 730. At 100 tools, Code Mode costs 43% less. Interpolating between my two measurements puts the tipping point at around 40 tools.
So my initial hunch was wrong, or rather incomplete. But look at the last three rows.
Caching changes everything
With caching, the tipping point disappears. At 100 tools, direct tool calling costs half as much as Code Mode.
The reason is almost mechanical. Tool definitions are the ideal prefix to cache: long, identical from one call to the next, placed at the very start of the prompt. At 100 tools, direct tool calling rereads about 95% of its input at the discounted rate. The Code Mode context, on the other hand, changes with every search and caches poorly.
The detail that made me smile: with caching, direct tool calling with 30 tools costs less than with 10 tools. The small catalog sat just under the caching threshold. Adding twenty useless tools lowered the bill.
Code Mode has another, less visible cost: it becomes unpredictable. On the 4-tool task with a catalog of 100, its bill ranges from $0.94 to $2.47 per 1,000 tasks, depending on whether the model searches in a targeted way or starts by listing the whole catalog. In the same series, the cost of direct tool calling varies by less than 2%.
There’s obviously a lot more to dig into
These results hold for my setup, and I can see at least four blind spots.
My tools return a few lines. The other promise of Code Mode is to process large volumes in the sandbox without having the model read them, and that advantage had nothing to show here. It is probably where it is strongest.
I only tested one model, a compact one. A more capable model might write the single script this one never writes, and the math would change.
My provider caches automatically, with no write fee. With pricing that charges for cache writes, the break-even point would move.
And I stopped at 100 tools. Beyond a certain size, sending every definition stops being practical, whatever it costs.
What that changes for me as a practitioner
I will no longer choose between these two architectures based on the size of the starting context.
If my agent has caching and up to about a hundred tools → I stay with direct tool calling. It is cheaper, faster and predictable.
If I have no caching and more than about 40 tools → Code Mode starts to pay off.
If my tools return large volumes, or if my catalog runs into the hundreds → I test Code Mode on my own data before deciding.
If a predictable bill matters → I look at the spread of costs, not just the average.
An agent does not pay for its context once. It resends it on every call, and it does not always pay the same price for it. The only metric that matters is the full cost of a correctly completed task.
1,440 measurements, 4 series, GPT-6 Luna through OpenRouter, “medium” reasoning effort, 30 runs per cell, $1.09 in total.
Costs are the amounts actually billed for each call. All raw transcripts are kept.
AiBrain