26 Tool Calls, One Script, $0.02 — The Token-Saving Power of 'Code Mode'
Agent Swarm measures what happens when an AI agent bundles 26 API calls into a single script: a 99.2% reduction in context size, 30x faster execution, and the cost drops from $2.44 to two cents.
The 60-second version
Agent Swarm measured the Code Mode pattern — letting scripts handle bulk API calls instead of making agents do individual tool calls — and found a 99.2% reduction in context size and cost.
Key points
- A workflow-triage script making 26 internal calls passes only 25,811 characters to the agent, versus ~3.26 million for sequential tool calls — a 126x reduction.
- Wall-clock time dropped from 2–6.5 minutes (estimated) to 13.12 seconds — roughly 30x faster.
- Cost fell from $2.44 (floor, one-turn pricing) to $0.02 — though the real raw-path cost would be higher since context accumulates across turns.
- The pattern aligns with Anthropic's (98.7%) and Cloudflare's (99.9%) published Code Mode benchmarks, suggesting a convergent standard.
Verdict. Code Mode — bundling API calls inside scripts so only the distilled result reaches the model — is an essential optimization that pays for itself in the first multi-call task. The numbers speak for themselves.
The ProblemWhen Every Tool Call Pollutes the Context
Every time an AI agent calls a tool — whether it's fetching a list of workflows, checking a schedule, or reading API results — the full raw JSON response lands in the model's context window. And it stays there. Every subsequent turn re-sends it. On a task that needs 26 sequential calls, that's 26 payloads accumulating in memory, each one consuming tokens and money.
It's a problem the industry has been circling for months. Anthropic published a piece in November 2025 showing how giving an agent a sandboxed script environment with a generated API could reduce a 150,000-token task to just 2,000 — a 98.7% reduction, because the raw data never has to pass through the model's context at all. Cloudflare launched the same idea under the name 'Code Mode' in September 2025, later reporting a reduction from 1.17 million tokens to about 1,000 across 2,500+ API endpoints.
But those were vendor benchmarks. Agent Swarm wanted to measure it on their own production data, with their own workflow — and share every number, including the ones that required honest footnotes.
The ExperimentWorkflow Triage — One Script vs 26 Sequential Calls
Agent Swarm's test subject is workflow-triage, a script that runs across their entire production swarm — all workflows and cron schedules — and flags which ones are dead, failing, or fine. Under the hood, it makes 26 separate API calls: one workflow_list, one schedule_list, and one workflow_listRuns per workflow (24 of them).
The Script Path
The script runs inside its own sandboxed subprocess, makes all 26 calls internally, and returns only one summary object — 25,811 characters — to the agent. The raw data never touches the model's precious context window. Wall-clock time: 13.12 seconds.
The Raw Sequential Path
An agent doing the same work by hand would make 26 individual tool calls. Every raw JSON response stays in context for the rest of the conversation. Agent Swarm measured the actual payload sizes by calling the same SDK methods directly: workflow_list returned 16,304 characters, schedule_list returned 72,105 characters, and across 4 sampled workflows (312 runs total), the average workflow_listRuns response was 3,447 characters per run. Applied to the real total of 920 runs across all 24 workflows, that's about 3.17 million characters just for the runs.
Estimated total for the raw path: roughly 815,000 tokens, taking anywhere from 2 to 6.5 minutes of wall-clock time.
The Results99.2% Reduction — Priced in Real Dollars
The script's own numbers are 100% measured — durationMs: 13120 and the 25,811-character output came directly from a live run. The raw-path character count is measured-then-extrapolated: they called the exact same SDK methods the script calls, sampled the smallest and largest workflows, and applied the measured average to the swarm's own recorded run counts.
The TechniqueCode Mode and Script Workflows
Agent Swarm's approach goes back further than the blog post. Every claude/codex/opencode session in the swarm ships a rubric — system.agent.context_mode — that tells the agent: 10+ items or a bulk fan-out means reach for a script, not N individual tool calls. This same machinery powers their Script Workflows feature, where entire automation pipelines run as composable scripts outside the agent's context.
The key insight is simple: instead of the agent calling 26 tools and filling context with raw JSON, the agent calls one tool — the script executor — and the script does the 26 calls internally. The model sees only the distilled result. It's the same principle that makes database queries cheaper than row-by-row fetches: batch the work, transfer only the answer.
Why It MattersBeyond One Benchmark
These numbers aren't specific to Agent Swarm's setup. The same pattern applies anywhere an agent needs to make multiple API calls: fetch a user's data across services, poll multiple endpoints, aggregate results from a paginated list, run health checks across a cluster. Any time the raw data is larger than the analysis, moving the processing inside a script saves context — and therefore saves money and time.
Anthropic's original 98.7% figure, Cloudflare's 99.9% reduction across 2,500 endpoints, and now Agent Swarm's 99.2% on a production workload — the pattern is convergent. Code Mode, by whatever name, is becoming a standard architectural pattern for efficient AI agents.