◉ANTIGRAVITY LABJP
Articles/Agents & Manager
◈ Agents & Manager/2026-06-01Advanced

Capping Parallel Agents With a Token Budget — Designing a Guard That Stops Runaway Cost

Running many agents in parallel quietly inflates your token bill. This is not about shrinking prompts — it is about a governance layer that meters spend in real time and cuts it off at a budget. Full design and TypeScript implementation, drawn from my own overnight jobs.

antigravity464multi-agent50tokens3cost-control5budgetgovernanceoperations33

✦ Premium Article

One morning I opened the billing dashboard and found the previous month's token cost was more than triple the usual. The cause was not obvious. There was no record of which agent, on which task, had spent how much. I was running close to ten agents in parallel overnight, and one of them had slipped into an unexpected loop, repeatedly throwing an enormous context at the model.

I moved the overnight batch onto agents because keeping article generation, link audits, and routine app-side chores in my own hands had stopped being sustainable for a solo developer. Running them in parallel did give me what I wanted: the work is finished when I wake up. But it also handed me a new kind of accident — cost that swells before you notice. I had traded manual effort for an invisible one.

That overnight job ran an internal link audit across several repositories. Normally it finishes quietly in about an hour. That day, one agent got stuck mid-audit in a small loop — "link not found, re-search, still not found" — and each pass reloaded nearly the entire article index into context. The input tokens for a single call ballooned to dozens of times the norm, repeated hundreds of times. When you watch everything alone as an indie developer, this kind of quietly swelling cost is easy to catch late, precisely because it runs in the middle of the night.

This note is the record of the governance layer I built so it would never happen again. It is not about tuning token counts down. It is about watching consumption in units of budget and automatically stopping when a limit is crossed. I hope it helps anyone else running several agents in parallel who feels uneasy about cost opacity.

Optimizing token counts does not stop the bleeding

The first thing I tried was the "use fewer tokens" direction: shorter prompts, compressed context, cheaper models. That is worthwhile, and it works. Reworking the prompts for routine tasks alone dropped average consumption per task by about 20%.

But that lowers the unit price during normal operation; it does not stop a runaway. The accident I hit was "one agent used 50x the expected tokens." Cutting the unit price by 20% does nothing when a 50x spike sends the bill to 40x. Optimization shrinks the denominator; it does not build a ceiling for when the numerator jumps.

Put another way, optimization improves the average; governance contains the worst case. An indie budget is thin, so a single worst case each month easily eats up the average gains. What I overpaid in the accident month exceeded, in a single night, what months of optimization had saved. So the correct priority is to build the ceiling first, then lower the unit price.

What I needed was not everyday thrift but abnormal-state cutoff. In engineering terms, not tuning to make code faster, but a watchdog that detects an infinite loop and kills it. That shift in framing became the starting point of the design.

Thinking about governance in three layers

When I laid it out, the problem of controlling cost split into three layers with different natures.

The first layer is allocation: deciding in advance who may spend how much on what. You assign ceilings per task, per agent, or for an entire run. Without allocation, neither measurement nor cutoff has a reference point.

The second layer is metering: accumulating actual consumption at near-real-time granularity. An after-the-fact billing dashboard is too slow. If you cannot tell "how much have I spent in total right now" while a run is in flight, you cannot make the call to stop.

The third layer is cutoff: actually acting when the metered value approaches or exceeds the allocated ceiling. If you make this an immediate hard stop, you end up killing tasks that were about to finish — defeating the purpose. So degradation is staged.

I implemented these three layers as a thin wrapper that can be retrofitted onto Antigravity's parallel agents. The code below is a minimal TypeScript implementation that runs as-is.

✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦Get the full TypeScript implementation of a TokenLedger that allocates a per-task budget and accumulates real spend in real time
✦Learn the BudgetGuard design that degrades gracefully — downgrade, then halt — and why a ceiling checked against settled spend is overshot by every call still in flight
✦See the operating rules that pulled an inflated monthly token bill back to its usual level
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Antigravity Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

◈ Agents & Manager2026-10-13
Turning Gut-Feeling Judgments into Rules an Agent Can Actually Follow
A small, repeatable way to turn the judgment calls that live only in your head into written rules an Antigravity agent can apply, with a quick check against past cases.
◈ Agents & Manager2026-09-06
With scheduled agents, I now look for runs that never happened before I look for failures
My run ledger showed a 100% success rate while the evening slot had not fired for two weeks. Here is the reconciliation I now run against an expected-fire table, with measured notes on cron expansion, exit codes, and how the ledger itself gets written.
◈ Agents & Manager2026-09-04
Snapshot Your Agents' Effective Scope So You Notice the Day a Default Changes
Antigravity 1.1.25 made Markdown-defined custom agents inherit the surrounding skills, rules, and subagents by default. Here is the snapshot-and-diff routine I now use to check whether an agent I meant to keep narrow quietly got wider after an update.