↓ Skip to main content

Cutting the Cost of Agent Swarms: Isolate Context, Cache the Manual

·2406 words·12 mins·Mathusuthan

In a multi-agent swarm, several AI agents work together on one engineering task, such as fixing a failed build. The cost does not come from one long answer. It comes from context: every agent reads the code, the logs and the other agents’ work again and again. This post shows what that costs, and how isolating each agent and caching what they share cuts the bill.

What kind of application is this ?
#

This archetype is a team of specialised agents that work on software engineering tasks. A lead agent, the orchestrator, splits the task into steps. Other agents find the cause of a failure, write the code, and review it for security problems. At the end, the lead agent decides if the change is ready to merge. Together they form a multi-agent swarm.

This is the most autonomous archetype in the deck. The agents work across large codebases, logs and test suites, and no person checks each step. A person usually sees only the final pull request.

The deck gives three use cases. The NovaMart lab built one incident for each. The prompts and outputs below come from recorded lab runs. Long text is shortened (…).

1

Autonomous feature development

Take a feature request. Write the code, make the tests pass and open a pull request.

Input

A flash-sale feature must reserve stock in three fulfilment centres at once. The acceptance tests fail: "Concurrent reverse-order carts timed out in AB-BA lock deadlock".

Prompt (patch author agent)

Role: Swarm Code & Patch Author. Rewrite ONLY the target Python module to satisfy 100% of the referenced standard … Preserve EVERY dataclass name, field name, exception class, and function/method signature verbatim. … Return the FULL self-contained repaired Python file inside ONE fenced ```python block.

Output

"""Multi-FC Split-Cart Inventory Reservation
Manager (CONC-STD-07).
Deadlock-free, atomic, idempotent split-cart
reservations across fulfillment centers using
canonical sorted lock ordering …
"""

2

Security threat hunting

Find a security hole, reproduce the attack and fix the code.

Input

A payment webhook fails a red-team test: "Replayed valid webhook from 900s ago was ACCEPTED". It also compares signatures in a way that leaks timing.

Prompt (security critic agent)

Role: Swarm Security & Test Critic. Review the diff and deterministic `PR-SANDBOX-GATE-01` (10 checks: 4 CI Unit + 4 Adversarial + 2 Static AST). Confirm there are no DPDP PII leaks, timing side channels, paise rounding leaks, or lock-order deadlocks (<=150 words).

Output

2. Constant-time comparison (CWE-208, AST-SEC-01): Satisfied. The only signature check is hmac.compare_digest(expected, provided_hex). …

3

CI/CD failure triage

Read a failed build. Find the broken lines and tell the patch author what the fix must do.

Input

The checkout tax build fails: "taxable_subtotal reduced by wallet 500.00 (expected 2400.00, got 1900.00)".

Prompt (CI triager agent)

Role: Swarm CI/CD & Runtime Triager. Map the exact failing assertions or exploit paths to the defects in the target Python file and list exact contractual invariants the Patch Author must satisfy (<=160 words). Do not emit full code.

Output

4. wallet_used computed before/inside taxable … Contract: taxable = gross − discount only; wallet is a post-GST tender …

How I tested it. I built a “self-healing” swarm for NovaMart’s checkout and pricing code. NovaMart is a made-up retailer, and all of its data is synthetic. Each run has five agent turns: the orchestrator plans, the CI triager finds the cause, the patch author writes the fix, the security critic reviews it, and the orchestrator decides if it can merge. There were three incidents, one for each use case above. Each one is a fix to a single file.

A sandbox checks every patch with 10 automatic checks: 4 unit tests, 4 attack tests and 2 code-structure checks. A run counts as a verified fix only if the patch passes all 10. I ran four set-ups, five times on each incident. That is 60 runs. The tasks are small, so read the results as what I saw in one lab, not as a benchmark.

The token profile and cost dynamics
#

The deck gives this profile for the archetype:

INPUT PER RUN
100k to 1M+ tokens
Codebases, logs, traces, agent state
OUTPUT PER RUN
10k to 50k+ tokens
Multi-file changes, tests, security fixes
VOLUME
1.5M to 10M+ tokens a day
For each developer, the deck’s benchmark
DYNAMICS
Parallel agents multiply load
Each agent reads its own copy of the context

“Parallel agents multiply load” means this. Every agent needs context to do its job. If each agent gets the full codebase, the logs and the whole conversation so far, you pay for that context once for each agent, at each turn. Five agents that each read the same 40,000 tokens cost you 200,000 input tokens. And when the agents share one thread, each answer is added to it, so the next agent reads even more.

Here is a real example from the NovaMart lab. A nightly check found that checkout under-charged tax. The build now fails four tests. All five agents share one growing thread. The thread starts with the ticket, the failing tests and the source file. It also carries the full repository manual, the code of three unrelated services, a raw build log and 135 lines of production telemetry.

Example: one incident on a shared thread (Sonnet 5.5 on every turn)

What went into the first prompt (share of the text)

What each agent turn read and wrote

The five agents read 214,894 input tokens and wrote only 3,939. Input was 92% of the cost of this run. About two-thirds of the text in the first prompt was not needed for this task, and every later agent read it again.

How this compares with the deck’s profile:

  • Input. The shared thread used 213,000 to 217,000 input tokens per run, inside the deck’s range of 100,000 to 1 million. With the lever (below), a run used 56,000 to 58,000.
  • Output. All 60 runs wrote 3,800 to 6,600 tokens, below the deck’s 10,000 to 50,000. My tasks were small fixes to one file. Larger multi-file changes would write more.
  • Daily volume. I could not test the per-developer daily figure from single runs. For scale: 1.5 million to 10 million tokens a day is about 7 to 46 shared-thread runs, or about 24 to 160 runs with the lever.

The architectural lever
#

The deck’s lever for this archetype is: “Isolate sub-agent contexts, cache shared repo prefixes, and measure ROI by Cost per Verified Outcome.” It has three parts.

1. Isolate each agent’s context
#

Give each agent a fresh, narrow thread with only what its role needs. This is context isolation. In the lab:

  • The orchestrator gets the ticket and the failing tests.
  • The CI triager gets the orchestrator’s plan, the failing tests and the source file.
  • The patch author gets the triager’s list of what the fix must do, and the source file.
  • The security critic gets the patched file and the sandbox result.
  • The merge gate gets the sandbox result and the critic’s verdict.

No agent gets the unrelated services, the raw build log or the telemetry. No agent reads the full conversation. In the lab, the input at full price fell from about 214,000 to about 10,000 tokens per run.

2. Cache the shared repository prefix
#

All agents need the same repository manual: the engineering standards for tax, security and stock locking. In the lab this manual is 9,337 tokens. You put it at the start of every agent’s prompt, word for word the same, and you mark it for context caching.

The first agent writes the cache. That costs 1.25 times the normal input price. Every later agent reads the manual from the cache at a large discount: 95% off on Sonnet 5.5 and 90% off on Haiku 5.5.

There are two ways to price this:

  • Cold cache. The run writes the cache itself, as a single run starting from nothing. Every lab run started cold. See cold cache.
  • Warm cache. The manual is already in the cache, because another run wrote it a moment ago. This is the case in a busy team. I calculated the warm prices from the token counts; I did not run them.

One catch: each model keeps its own cache. If two models are in one run, the manual is written twice. The lab’s mixed set-up wrote 18,675 cache tokens per run, not 9,337.

3. Measure cost per verified outcome
#

Cost per run is not enough. A cheap run that gives a broken patch has no value, and somebody must fix it later. So divide the total spend by the number of runs whose result passes verification. This is cost per verified outcome. For example, a design that costs $0.05 a run but passes only half the time costs $0.10 per verified fix.

In the lab, a verified fix is a patch that passes all 10 sandbox checks.

4. Use a small model for the simple roles
#

This part is not in the deck’s lever, but I tested it as a second model set-up. Four of the five roles plan, triage, review and summarise. Haiku 5.5 does these roles. Only the patch author, which writes the code, stays on Sonnet 5.5. This is a form of model routing.

Compare the two designs on the same incident. Choose a model set-up, then look at what each agent turn read and what it cost. “Haiku agents” means Haiku 5.5 on four roles and Sonnet 5.5 for the patch author.

Same incident, two designs

Without One shared thread

What each agent reads

The whole thread: the ticket, failing tests, source file, full manual, three unrelated services, a raw build log, 135 lines of telemetry, and every earlier agent's answer. Nothing is cached.

With Isolated agents, cached manual

What each agent reads

The cached manual, plus only the inputs its role needs: for example, the triager's list and the source file for the patch author, or the patched file and sandbox result for the critic.

Recorded runs from the lab: the checkout tax incident, first of five runs. "Cached" is input read from or written to the cache. Cost is for this one run; warm is calculated.

On average over 15 runs, the design with the lever cost 79% less on Sonnet, from a cold start. Two numbers went up, not down. The agents wrote more output: 5,325 tokens per run against 4,079. And the model time per run was longer: 38.8 seconds against 30.8 on Sonnet, and 28.4 seconds against 25.6 with Haiku agents.

The chart shows where the tokens and the cost of a run go, on average over 15 runs.

Tokens and cost per run (average of 15 runs, Sonnet 5.5 on every turn)

Lower is better

"Input at full price" includes the one cache write per run, which costs 1.25 times the input price. Cost is the cold-cache cost: each run wrote its own cache.

The shared thread is input-heavy: almost all of its cost is input that it sends again at full price on every turn. With the lever, the input becomes small and cheap, and output becomes the largest part of the bill.

5. Does the quality stay the same?
#

On these tasks, yes. Every set-up fixed every incident. The chart shows the verified fixes for each set-up, out of 15 runs.

Verified fixes, of 15 runs

Higher is better

A verified fix passes all 10 sandbox checks. Every set-up passed 150 of 150 checks. No run needed its one allowed retry.

Read this result with care:

  • The tasks were easy enough for all four set-ups. When everything passes, the test cannot show a difference in quality. 15 of 15 is consistent with a true pass rate anywhere from 80% to 100%.
  • The shared thread did not break anything here. It cost more and its patches passed the same checks. On harder work, such as changes across many files or longer chains of agent turns, the set-ups could differ. I did not test that.
  • Cost per verified fix equals cost per run here. Because every run passed, the measure did not change the ranking. It matters when the pass rates differ.

The cost savings
#

The chart starts with a shared thread on Sonnet. Each step adds one change. Select a step to see what it saves.

Cost of one verified fix, step by step

Lower is better

Show as a table

Average of 15 runs per set-up, at the rate card below. Step 4 is calculated from the same runs with the cache already written; it was not run.

Isolation and caching give the largest single cut: 79% on Sonnet. The model choice and the design each give part of the saving. On the unchanged shared thread, Haiku agents alone cut the cost by 73%, to $0.125 per verified fix. Together, the design and the Haiku agents cut it by 88% from a cold start, and by 93% with a warm cache.

ModelInput ($ per million tokens)Output ($ per million)Cache write ($ per million)Cache read ($ per million)
Claude Sonnet 5.52.0010.002.500.10
Claude Haiku 5.50.100.500.1250.01

Cost Estimator
#

Use this calculator for your own swarm. It starts with Haiku 5.5 prices and the token counts I measured in the lab. Change the prices to the rates of the model that you use.

Multi-agent swarm cost estimator

Prices, $ per million tokens
Your workload
Shared thread
Isolated agents, cached manual

Defaults are lab averages with Sonnet 5.5 on every turn: 214,112 input and 4,079 output tokens per shared-thread run; 9,792 new input, a 9,337-token cached manual and 5,325 output tokens per isolated run. The calculator uses one model for every turn, so the manual is written to the cache once per run and read by every other turn. The lab did not run Haiku on every turn.