Guides · Updated October 22, 2026

How to set a hard budget for AI coding agents in CI

An agent in CI runs with nobody watching: no one presses Esc when it starts retrying the same failing test, and a workflow that fires on every push can run it hundreds of times a month. The fix is layers: a per-run cap inside the agent, a timeout on the job, and a hard spend limit on the key the runner uses. Here is what Claude Code, Codex CLI and Cursor CLI each offer for headless runs as of October 2026, and where the gaps are.

The four layers

LayerStopsClaude Code (claude -p)Codex CLI (codex exec)Cursor CLI (agent -p)
Per-run dollar capOne expensive run--max-budget-usdNone (experimental token budget only)None
Per-run step capEndless agentic turns--max-turnsNone documentedNone documented
Job timeoutHung or looping jobsYour CI system, e.g. timeout-minutes in GitHub Actions
Key / account limitThe monthly totalConsole workspace spend limitOpenAI project hard spend limitAccount spend limit; service-account limits on Enterprise

The key limit is the only one that is a hard dollar ceiling no matter what the agent does, so set it first. The per-run caps keep any single run from using a large share of it.

Give CI its own key with its own limit

Monthly limits are not instant (OpenAI and Cursor both note spend can briefly pass them) and they are monthly, which is why the per-run layers below still matter.

Claude Code: --max-budget-usd and --max-turns

Print mode has a real per-run cap. From the CLI reference, the run stops once estimated spend reaches the amount, subagent spend counts, and once the cap is hit Claude Code won’t start more subagents and stops background ones (v2.1.217 or later). --max-turns exits with an error after that many agentic turns.

claude -p --max-budget-usd 3.00 --max-turns 40 \
  --output-format json "Fix the failing test in src/billing" > result.json
jq '.total_cost_usd' result.json   # the run's estimated cost, for your CI logs

In the official GitHub Action (anthropics/claude-code-action), claude_args passes any CLI argument, so the same flags go there:

- uses: anthropics/claude-code-action@v1
  timeout-minutes: 20
  with:
    anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
    claude_args: "--max-turns 30 --max-budget-usd 3"

Caveats: the cap is checked against Claude Code’s own estimate, the final response can push spend past it (leave headroom), and with --continue or --resume earlier runs’ spend doesn’t count toward the new cap.

Codex CLI: no per-run cap, so lean on the project limit

codex exec has no flag that stops a run at a dollar amount. The config reference lists a token “rollout budget” (features.rollout_budget) that is under development and off by default. What you do get:

Cursor CLI: no per-run cap, and no cost in the output

Cursor’s CLI parameters have no budget or turn limit, and the documented --output-format json result reports duration but no cost. So for agent -p in CI, the account or service-account spend limit plus a job timeout are your controls. Cursor recommends restricting what the agent may do in CI with permission rules (for example denying Shell(git)) and doing pushes and comments in separate deterministic steps.

Always set a job timeout

A timeout is the one control that works the same for all three agents and costs nothing. GitHub Actions jobs default to a 360-minute limit, far longer than any agent task should take:

jobs:
  agent-fix:
    runs-on: ubuntu-latest
    timeout-minutes: 20        # whole job
    steps:
      - uses: actions/checkout@v5
      - name: Run agent
        timeout-minutes: 15    # just the agent step
        run: ...

Also watch the trigger: an agent on every push or synchronize event multiplies cost by how often people push. Run it on a label, a comment, or a failed CI run instead, and use concurrency to cancel superseded runs.

Add loop detection and caps with LoopGuard

Per-run dollar caps don’t notice that the agent has run the same failing command ten times, and Codex and Cursor don’t have them at all. LoopGuard is an open-source (MIT) CLI that installs PreToolUse/Stop hooks for Claude Code and Codex (and preToolUse/stop for Cursor) and checks every tool call against loop heuristics and session and daily dollar caps. It reads the agents’ own local logs, so it needs no proxy and no extra key. On a fresh runner:

npm i -g loopguard-cli
loopguard init --agent claude,codex     # writes ~/.claude/settings.json and ~/.codex/hooks.json
loopguard config set sessionCapUsd 3    # estimated $ per agent session
loopguard config set loop.maxRepeats 3  # repeats of the same call (with no progress) allowed within 10 minutes

claude -p --max-budget-usd 3 "..."                        # Claude Code: hooks load from ~/.claude
codex exec --dangerously-bypass-hook-trust \
  --sandbox workspace-write "..."                         # Codex: see the trust note below
loopguard report --by session                             # estimated cost per session, for the job log

What to know before relying on it in CI:

Related

LoopGuard on GitHub · Pricing