A single agent calling an LLM once per user turn is easy to budget for. A multi-agent network isn't: a coordinator agent might delegate to three worker agents, each of which calls the model two or three times to reason and respond, and if any of them loop or retry, that fan-out multiplies fast. Without a hard guardrail, a bug or an unexpectedly long task can turn one user request into hundreds of model calls before anyone notices.
Building a token/cost budget guardrail
1. Estimate tokens before dispatching a call. Use tiktoken — OpenAI's official byte-pair-encoding tokenizer — to count the tokens in the prompt you're about to send, so you know the cost before the API call happens, not after.
2. Track cumulative spend in a shared counter. In a multi-agent run, all agents should decrement from one shared budget object (or check a shared running total) rather than tracking their own local limits independently.
3. Raise, don't warn. When the budget is exceeded, the orchestration layer should stop dispatching new calls entirely — a soft warning that the agents can choose to ignore defeats the purpose of a guardrail.
# pip install tiktoken
import tiktoken
class BudgetExceeded(Exception): pass
class TokenBudget:
def __init__(self, max_tokens: int, model: str = "gpt-4o"):
self.max_tokens = max_tokens
self.spent = 0
self.encoder = tiktoken.encoding_for_model(model)
def check_and_spend(self, prompt_text: str) -> int:
cost = len(self.encoder.encode(prompt_text))
if self.spent + cost > self.max_tokens:
raise BudgetExceeded(
f"Blocked call: {self.spent + cost} would exceed budget of {self.max_tokens}")
self.spent += cost
return cost
# One shared budget object passed to every agent in the network
budget = TokenBudget(max_tokens=50_000)
| Guardrail Approach | Enforcement Point | Failure Mode if Skipped |
|---|---|---|
| Post-hoc cost logging / dashboards | After the run completes | Overspend already happened by the time anyone sees it |
| Shared token budget checked before each call | Before every model call, in orchestration code | Call is blocked and exception raised before cost is incurred |
Cost observability (logging, dashboards) and cost enforcement (hard limits) are different tools, and multi-agent systems need both — but only enforcement actually stops the overspend before it happens. Treat the budget object the same way you'd treat a rate limiter: a piece of shared, code-level state that every agent in the network must check before acting, not a suggestion baked into a prompt.
Practical Challenge
Add the TokenBudget class above to a small two-agent LangChain setup (a planner and a worker) and set the max low enough that a normal run trips BudgetExceeded — confirm the exception actually halts execution rather than just logging a warning.
Concept Check
Sources & Further Reading
- tiktoken (GitHub) — OpenAI's official byte-pair-encoding tokenizer, used here for pre-flight token counting.
- OpenAI Cookbook: How to count tokens with tiktoken — worked examples of counting tokens for different models before sending a request.
- Multi-agent — Docs by LangChain — patterns for coordinating multiple agents that this guardrail is designed to wrap.
AI