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 with | How you pay | Native 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 key | Per token at API prices | Organization 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:
- Open Project settings → Limits.
- Under Spend, choose Edit spend limit and enter a monthly amount.
- 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:
- Spend alerts don’t stop anything. Keep them for early warning (say 50% and 80%), with the hard limit as the actual cap.
- Enforcement isn’t instant. OpenAI says recorded spend can go slightly over while the limit propagates.
- It’s monthly. One runaway night can use the whole month before it trips.
- A separate project also makes Codex’s spend easy to see on its own in the usage dashboard.
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:
- Check your remaining limits inside the CLI with
/status, or in the Codex usage dashboard. - Credits are managed in Settings → Usage in ChatGPT (or Usage & Billing in the Codex app). If you don’t want to pay past your plan, don’t keep a credit balance.
- If you use automatic reload, set a maximum monthly spend. OpenAI applies that cap to automatic reloads only, not to one-time credit purchases (OpenAI help).
- Business workspaces manage credits and spend controls in workspace billing settings, as an owner.
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
- Pick the model for the task. OpenAI notes that switching to a smaller model makes plan limits last much longer; on an API key, it is also cheaper per token.
- Mind speed and reasoning. Fast mode (
/fast) uses included limits and credits faster, and a highermodel_reasoning_effortproduces more output tokens. - Keep context small.
/compactreplaces earlier turns with a summary; starting a new session for a new task is often cheaper still.
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:
- Past the session or daily cap, or after a $10 jump within 5 minutes, every tool call is denied with a reason such as
LoopGuard: today's spend across agents $40.12 reached daily cap $40.00. Stopping.Codex hooks have no “end session” field, so repeated denial is how the session stops making progress. - The same tool call 5 times within 10 minutes is denied if nothing changed in between: the git working tree and the command’s output (from Codex’s
PostToolUsehook) both stayed the same (loop.maxRepeats,loop.windowSec). loopguard pause 15lifts enforcement for 15 minutes;loopguard uninstall --agent codex --restoreputs your originalhooks.jsonback.
Limits worth knowing
- Costs are estimates at API list prices from a built-in table. Newer model names may fall back to a related model’s price, so if your numbers matter, set your actual rates in
~/.loopguard/pricing.json. On a ChatGPT plan you aren’t billed per token; the figure is a consistent measure of how hard a session is pushing your limits. - Checks happen when Codex calls a tool or finishes a turn, so one long response isn’t cut off midway. Hosted tools such as web search don’t go through local hooks.
- Hooks only see Codex’s own tool calls. A script the agent writes that calls an API in a loop is outside their reach, which is why the hard project limit and the key hygiene above still matter.
- LoopGuard fails open: if it has an internal error, Codex keeps working and the error goes to
~/.loopguard/errors.log.
Related
- How to stop runaway Claude Code, Codex and Cursor agent loops
- How to track AI coding agent token costs per session
- How to set a spending limit for Claude Code
- How to limit Cursor agent usage costs
- Claude Code usage limit reached: why it happens and how to monitor it
- How to set a hard budget for AI coding agents in CI
- Claude Code stuck in a loop: how to break it and stop it repeating the same command
- Codex usage limit reached: 5-hour and weekly limits explained
- Cursor agent stuck in a loop: how to stop it, roll back, and keep it from repeating
- Why Claude Code uses so many tokens, and how to use fewer
- How to set a budget limit for Aider
- GitHub Copilot agent mode stuck in a loop: how to stop it and keep it from repeating
- Cline stuck in a loop: how to stop it, roll back, and cap what it spends
- Gemini CLI usage limits: daily quotas, the June 2026 change, and keeping costs in check
- Codex stuck in a loop: how to stop it, undo it, and keep it from repeating