← All guides

Agents · July 11, 2026 · 3 min read

Set spend caps before your AI agents run overnight

An always-on agent with a paid API key is a surprise bill waiting to happen. Two layers of spend caps bound the damage before your first overnight run.

The problem

An always-on agent with a paid API key is a surprise bill waiting to happen. A misfiring cron or a runaway reasoning loop can fire thousands of API calls while you sleep, and there are real stories of agents that quietly burned hundreds of dollars in a day. The fix is two layers of spend caps: a hard limit at the provider that nothing in the agent can touch, and a daily cap in your own code checked before every model call.

How do you stop an agent from overspending overnight?

Layer 1 lives at the provider. Set a hard monthly limit and a billing alert in your Anthropic, OpenAI, or OpenRouter dashboard. That ceiling holds even when the agent misbehaves, because it sits outside anything the agent can read, write, or reason about. It is the one limit an agent cannot talk its way around.

Layer 2 lives in your code. In the member app I run, my community RAG coach, every request passes a spend check before the model is ever called: reserve the estimated cost against a per-user daily cap and a global daily cap, then reconcile with the actual cost after the call. The reserve is atomic, it locks the member’s daily usage row, so two concurrent requests cannot both squeeze under the limit. The per-user caps are sized so a maxed-out member still costs less per month than their subscription. Copy the pattern in any stack:

Before every model call:
1. Estimate the cost of this call.
2. Atomically add the estimate to today's running total.
3. If the total would cross the daily cap, refuse and log. Do not call.
4. After the call, replace the estimate with the actual cost.

A spend cap is a safety rail you chose in advance, not a budget you expect to hit.

The spend cap checklist for your first overnight run

  1. A hard limit and a billing alert at every provider the agent can call.
  2. The daily cap check above in front of every model call your code makes.
  3. No free payment method. If a workflow genuinely needs to buy something, route it through a card with a low limit or a manual approval step.
  4. Cheap models for the grunt work. Let the top model plan and a cheaper model build: routing lowers your average cost, and the caps bound your worst case.

Do this now

  1. Open your provider’s billing page and set a hard monthly limit plus an alert below it.
  2. Put the four-line cap check in front of every model call your agent makes.
  3. If the agent can buy anything, remove the saved card and add an approval step.

Claude Code

Turn this into a Claude skill

Paste this into Claude Code. It reads this guide and builds a skill shaped to how you work.

Read the guide at https://aiwithamirthan.com/guides/set-spend-caps-before-your-agents-run-overnight. Then build me a Claude Code skill from it, personalized to how I actually work.

1. Fetch and read the full guide at the URL above.
2. Look at my setup so the skill fits me, not a generic template: skim my repos, my CLAUDE.md / AGENTS.md files, my existing skills, and my stack.
3. Create a new skill (a SKILL.md with a clear name, description, and steps) that lets me do what this guide teaches, adapted to my tools and goals.
4. Tell me where you saved it and how to invoke it.

If anything about my setup is unclear, ask me one or two questions first.

Want to build this yourself?

Join Claude Code Academy: courses, build alongs, and builders turning AI into real systems. And I post a new build on Instagram nearly every day.