The $4,000 Thursday
It starts as a Tuesday. Your agent works fine. Wednesday, you notice it ran longer than usual. By Thursday morning, you have a $4,200 bill on your credit card and no idea which agent did it or why.
You check the vendor dashboard. It shows total spend. It doesn't show which of your 12 agents made 40,000 calls to Claude overnight. It doesn't show that one agent hit a bug and retried every request 30 times. It doesn't show that your cost-per-task is 3x what it was a month ago.
Generic spend dashboards tell you how much. They don't tell you why, who, or when.
Real-time cost tracking for AI agents needs three things working together: granular transaction logging at the wallet level, a fast path to query current spend without polling the vendor API, and alerting that fires before you've already blown the budget.
Architecture: the three-layer stack
A real-time cost tracking system has three moving parts:
1. The wallet ledger. Every purchase goes through a wallet. The wallet is the source of truth for who spent what, when, and on which vendor. It records amount_usd, vendor, description, and timestamp atomically — no async, no eventual consistency.
2. The webhook push. When a transaction is recorded, a webhook fires to your endpoint. Your handler writes to a time-series store (or Postgres table) immediately. This is the "real time" part — you're not polling for updates, you're receiving them.
3. The query layer. An Express endpoint reads from the events table and returns aggregates. This feeds a dashboard, an alert check, or a billing report. Queries run against your own data, not the vendor's API.
Why not just use the vendor dashboard? Anthropic and OpenAI dashboards show you total spend across all API keys. If you run 10 agents, they all share the same billing account. You can't tell agent-7's research task from agent-3's summarization job. Trove's per-wallet ledger gives you per-agent cost attribution — every transaction tagged to a specific wallet and agent_id.
Step 1: Provision per-agent wallets
One wallet per agent
Give every agent its own wallet. The wallet is the cost container — everything the agent spends routes through it. This is the foundation of attribution.
Step 2: Route every dollar through the wallet
Wrap the API call in a purchase call
Before your agent calls Anthropic or OpenAI, record the expected cost through the wallet. This creates the transaction record and enforces the budget cap simultaneously.
Step 3: Store and query real-time spend
Build a live spending table
Your webhook handler writes every transaction to a Postgres table. Index on wallet_id and created_at so queries stay fast even at scale.
Step 4: Query the live dashboard
Build the queries that answer your questions
With the events table in place, you can answer any cost question in real time. Here are the queries that matter most.
Detecting a runaway loop
The most expensive AI agent failure is the infinite loop. Your agent calls an API, gets a result, loops back to try again, hits a retry handler, and makes the same call 500 times. At $0.003 per call, that's $1.50 in 10 minutes. It can hit $50 before you notice.
The tell is spend velocity — transactions per minute. Normal agents have a stable rate. Loops have a sudden spike. Here's how to detect it:
The baseline adapts to your agent's actual behavior. A research agent that normally runs at 2 tx/min will trigger an alert if it jumps to 10 tx/min. A crawler that normally runs at 20 tx/min won't false-positive on a jump to 30 tx/min. The multiplier (5x) is tunable — dial it down if you get too many false alerts, up if you're missing real loops.
Making cost data actionable
Tracking spend is useful. Acting on it is what pays the bills. Here are the three decisions cost data enables:
Right-size agent budgets. After 30 days of real-time data, you know exactly what each agent spends. Set the budget limit at 2x the 90th percentile monthly spend. If an agent normally runs $80/month but hits $120 in a bad week, you want to know at $160 — not at $500.
Route tasks to cheaper vendors. If Anthropic accounts for 70% of your spend and your summarization tasks don't need the most capable model, route them to a cheaper provider. You can only make this trade-off when you have per-vendor attribution data.
Bill clients accurately. If you build AI agents for clients and charge them for API costs, you need per-agent spend data to back up your invoices. A webhook-driven ledger gives you the auditable transaction history that makes cost-plus billing credible.
The minimum viable setup. One Postgres table, one webhook handler, one Express endpoint. That's enough to see live spend by agent and detect a runaway loop before it costs $200. Everything else — anomaly thresholds, Slack alerts, monthly rollups — is additive on top of this foundation.
Try it with a sandbox wallet
The playground gives you a sandbox wallet and API key. Make a few purchase calls, hit the balance endpoint, and see the transaction ledger populate in real time.
Open the PlaygroundFor the full API reference — balance checks, transaction pagination, budget updates — see the Trove documentation.