How to track and cap what your AI agent spends
An agent that loops can spend in an hour what a chatbot spends in a month. A leaked key can do worse: in the October 2026 OpenCode Console breach, users reported unauthorized charges of up to about $1,700 (The Hacker News). You need three layers: tracking, so you know which agent spent what; caps that actually block requests; and limits inside the agent loop.
1. Track cost per agent
- LiteLLM is an open-source (MIT) gateway you host yourself, with one OpenAI-compatible API for 100+ providers. Give each agent its own virtual key, and LiteLLM tracks spend per key, user, and team. The open-source gateway is free, and the Enterprise annual license adds features like SSO and audit logs.
- Helicone logs every request with its cost. Tag requests with a
Helicone-User-Idheader orHelicone-Property-*headers (for example, an agent name) to break costs down by agent, feature, or customer. It's free for 10,000 requests a month, and Pro is $79/mo. - Langfuse tracks tokens and cost on every trace, so you can see which step of a run cost the most. Its Hobby plan is free.
2. Hard spend caps that actually block requests
- OpenAI: under Project settings → Limits, you can enforce a hard monthly spend limit. After that, requests return a 429 with
project_spend_limit_exceeded(OpenAI notes a small overage is possible while settings update). - Anthropic: each Claude Console workspace can have its own monthly spend limit and alerts. API keys belong to one workspace, so give each agent its own workspace.
- OpenRouter: each API key can have a USD credit limit that resets daily, weekly, monthly, or never. Once it's reached, requests are rejected. OpenRouter recommends a limit on every key, because a key with no limit "lets a leaked key or a runaway agent spend your entire balance."
- LiteLLM: set
max_budgetandbudget_durationon a virtual key (for example $10 every 30 days). Requests fail once the key goes over, and the counter resets at the end of each period.
3. Loop and runaway protection
- Cap turns: the OpenAI Agents SDK's
max_turnsraisesMaxTurnsExceededwhen a run goes past the limit, or you can use an error handler to return a controlled answer instead. Whatever framework you use, set a turn or step limit. - Cap time and tokens per run in your own code, and stop the run when either is exceeded.
- Rate-limit per key: LiteLLM virtual keys support RPM, TPM, and parallel-request limits.
- Alert on spikes: a run that costs ten times the median is usually stuck in a loop. Observability tools can flag it (see how to debug AI agents).
4. Route to cheaper models
Not every step needs your most expensive model. LiteLLM's router handles fallbacks, and it can send a key to a fallback model once its budget for one model runs out. OpenRouter's openrouter/auto picks a model for each prompt within a cost tier you choose. Portkey is another gateway with routing. A common setup is a small model for classification and tool selection, with the large model only for the hard steps.
Checklist
- Give each agent its own key, project, or workspace.
- Tag every request with the agent and the customer.
- Set a hard cap at the provider and a budget at the gateway.
- Set turn, time, and token limits on every agent run.
- Set alerts on daily spend and on cost per run.
- Route easy steps to cheaper models (an LLM gateway makes this easy; see OpenRouter vs LiteLLM vs Portkey).
- Keep real keys out of the agent (see API access without your keys).