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 running six sites autonomously.

antigravity435multi-agent50tokenscost-control5budgetgovernance2operations25

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'm Masaki Hirokawa. I've been building iOS and Android apps solo since 2014, and I run six sites by myself — a family of wallpaper apps with 50 million cumulative downloads, plus four Lab sites and two blogs. Ever since I started handing article generation, link audits, and routine AdMob work to Antigravity's parallel agents, operations got genuinely easier. But I also took on a new kind of accident: cost that swells before you notice.

That overnight job ran article generation for four sites plus an internal link audit for each. 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 the model, then halt — when a hard cap is reached, plus the production gotchas I hit
See the operating rules that pulled my monthly token cost back down from roughly 3.2x after running six sites autonomously
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-06-17
Accounting for Which Agent Spent What: A Cost Attribution Design by Task
Your month-end bill is one number, but running multiple agents on Gemini 3.5 Flash hides which task ate the cost. Separate from a budget guard, I share a cost-attribution accounting design that maps usage to per-task and per-site cost, with a solo-operator implementation and numbers.
Agents & Manager2026-06-17
Tracing Parallel Agents After the Fact: Observability with Structured Logs and Spans
Running multiple agents in parallel on the Antigravity 2.0 desktop makes it impossible to tell which one is doing what. I share an observability design that drops tangled print debugging for run_ids and spans you can trace afterward, with a solo-operator implementation and numbers.
Agents & Manager2026-06-17
Making Managed Agent Batches Safe to Re-run: Idempotency and Checkpoints
Running overnight batches on the Antigravity 2.0 Managed Agents API makes recovery from partial failure unavoidable. Starting from a duplicate-post incident, I share the implementation of idempotency keys, a checkpoint store, and resume logic, with real numbers from solo operations.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →