← Guides

How to track and cap what your AI agent spends

By Jacob Holm · October 20, 2026

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

2. Hard spend caps that actually block requests

3. Loop and runaway protection

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

Options at a glance

ToolWhat it doesPricingLicense
LiteLLMOpen-source AI gateway: one OpenAI-compatible API for 100+ providers, with virtual keys, spend tracking, budgets, rate limits and fallbacksFree (MIT; self-hosted); Enterprise annual licensePartly open source
HeliconeLLM observability and gatewayFree tier; paid from $79/moOpen source
LangfuseOpen-source LLM observability and evalsFree tier; paid from $29/moOpen source
OpenRouterOne OpenAI-compatible API for 600+ models from 80+ providers, with fallback routing, budgets and BYOKFree tier (free models, 50 req/day); pay as you go at provider list price plus 5.5% fee on credit purchasesProprietary
PortkeyAI gateway with fallbacks, load balancing, caching, guardrails, virtual keys and observabilityFree tier (10k logs/mo); Production $49/mo; open-source gateway free to self-hostOpen source

We list what each vendor's own site says. Pricing changes often, so check it on the vendor's site before you commit.

Liked this guide? Get one new agent-infra guide a week.

One short email with a new guide and the tools worth knowing about. Free, and you can unsubscribe anytime.