When to reach for it
- Costs must be capped or allocated.
- Agents autonomously execute loops.
- Optimization of models and patterns is required.
Tokens and dollars, per run, per agent, per node.
Token consumption, model costs, and tool-related expenses are measured per run, agent, or workflow. The metric is only useful if something acts on it: a kill switch (a hard budget ceiling that terminates a runaway run) and caching of intermediate results turn the measurement into a control. Variable execution paths make cost forecasting hard, and a single edge case that triggers a retry chain can cost many times a normal run — so the ceiling is a safeguard, not an optimization.
In practiceAn autonomous research agent emits token and cost telemetry on each LLM call, and a hard per-run budget cap terminates the run before a self-initiated retry storm triples the daily bill.
Every model call reports its tokens, and the run adds them up as money. Before each further call the sum is compared with a ceiling, and a run that has reached the ceiling is stopped.
1 · Measure
2 · Act
| Step | USD | Total | Budget check |
|---|---|---|---|
| Plan | 0.04 | 0.04 | Continue |
| Search and read six sources | 0.38 | 0.42 | Continue |
| Write a draft | 0.21 | 0.63 | Continue |
| One source fails to parse and is retried ten times | 1.42 | 2.05 | Continue, each retry was under the ceiling when it started |
| Eleventh retry | 0.00 | 2.05 | Stopped: the ceiling is reached |
# illustrative: input, output in USD
PRICE_PER_MILLION = {"small": (0.25, 1.25), "large": (3.00, 15.00)}
class BudgetExceeded(Exception):
pass
class Budget:
def __init__(self, ceiling_usd):
self.ceiling, self.spent, self.cache = ceiling_usd, 0.0, {}
def call(self, model_name, prompt):
if prompt in self.cache:
return self.cache[prompt] # a repeated step costs nothing
if self.spent >= self.ceiling:
# the kill switch
raise BudgetExceeded(f"{self.spent:.2f} USD spent")
reply = model(prompt, model=model_name)
price_in, price_out = PRICE_PER_MILLION[model_name]
self.spent += (reply.input_tokens * price_in + reply.output_tokens * price_out) / 1_000_000
self.cache[prompt] = reply.text
return reply.textWhen to reach for it
When it backfires
Usage is computed at month end from the provider's invoice. A runaway loop has already cost a week's budget by the time anyone notices.
Countermeasure
Emit cost on every span as it happens. Set hard per-run caps at the harness; treat exceeding the cap as a correctness failure, not a billing concern.
A supervisor fans out to specialists and tools that all bill to one account. The total is visible, but you cannot tell which agent, tenant, or feature drove the spend, so you cannot act on it.
Countermeasure
Tag every model call with run, agent, and tenant identifiers on the span, and aggregate cost along those dimensions — not just a single global counter.
Keep going
Search patterns, frameworks, and pages.