Guides · Updated October 19, 2026

How to cap OpenAI Codex CLI spend

How you cap Codex CLI spend depends on how you sign in. With an API key you can set a hard monthly limit on the OpenAI platform. With a ChatGPT plan your plan limits are the ceiling, and the money risk is credits and automatic reloads. Neither gives you a dollar cap per session. Here is how to set each control, and how to add per-session and daily caps with a hook.

Which kind of Codex billing are you on?

Run codex login status to see whether the CLI is using a ChatGPT account or an API key.

Signed in withHow you payNative hard stop
ChatGPT plan (Plus, Pro, Business)Included usage in 5-hour windows (weekly limits may also apply), then credits if you buy them. Enterprise/Edu with flexible pricing is credit-based.Your plan limit, unless you have credits. Optional monthly cap on automatic reloads.
OpenAI API keyPer token at API pricesOrganization or project hard spend limit (monthly)

API key: set a hard spend limit on a dedicated project

Give Codex its own project and API key on the OpenAI platform, then cap that project. From OpenAI’s spend limits guide:

  1. Open Project settings → Limits.
  2. Under Spend, choose Edit spend limit and enter a monthly amount.
  3. Turn on Enforce a hard limit and save. Without this it is only an alert.

Once the project reaches the limit, its API requests fail with a 429 and the code project_spend_limit_exceeded, so Codex stops getting responses. You can set the same thing for the whole organization under Organization limits. Notes:

ChatGPT plan: plan limits, credits and automatic reload

On a ChatGPT plan, Codex runs against your included usage. When it runs out, Codex stops until the window resets, unless you have credits, which let you continue past the limit. So the controls that matter are about credits:

Keep live API keys away from the agent

A Codex user on a subscription described a session that hit its plan limit, then wrote its own script that called a metered API with a key from the project’s .env, and spent about $453 in one day, even though the organization had a spend limit and alerts configured. Whatever caps you set, don’t run an unattended agent in a folder with live keys it doesn’t need, and keep the keys it does need on their own capped project.

Spend less per session

The gap: no per-session dollar cap

As of this writing, Codex CLI has no setting that stops a session at a dollar amount (the config reference lists an experimental token “rollout budget” that is under development and off by default). The platform limits above are monthly and account-wide, so they won’t stop one looping session at $5 or notice that the agent has run the same failing test twenty times.

Codex does have hooks. A PreToolUse hook in ~/.codex/hooks.json runs before shell commands, apply_patch edits, MCP tools and most other local tools, and can block the call by exiting with code 2 and writing a reason to stderr. That is where a per-session cap belongs.

Per-session and daily caps with LoopGuard

LoopGuard is an open-source (MIT) CLI that installs a PreToolUse and a Stop hook for Codex. It reads the session logs Codex already writes (~/.codex/sessions/**/rollout-*.jsonl), sums tokens per turn with cached input priced separately, and checks your caps on every tool call. It works with ChatGPT sign-in or an API key.

npm i -g loopguard-cli          # puts `loopguard` on your PATH
loopguard init --agent codex    # adds hooks to ~/.codex/hooks.json (backed up first)

loopguard config set sessionCapUsd 10   # estimated $ per session (default 25)
loopguard config set dailyCapUsd 40     # estimated $ per local day, across all guarded agents (default 100)
loopguard report --agent codex --by session

Then trust the hooks, or they won’t run. Codex skips new or changed hooks until you review them. Open codex, run /hooks, and trust the three LoopGuard hooks. Until you do, codex exec skips them without any warning. Codex tracks trust by the hook’s exact definition, so if you re-run loopguard init from a different install (say, after switching from npx to a global install), trust them again. For one-off automation that already vets its hook sources, Codex also has --dangerously-bypass-hook-trust.

With the hooks trusted:

Limits worth knowing

Related

LoopGuard on GitHub · Pricing