Saving tokens in Codex: a checklist for working through the API
Nine habits and settings that cut Codex spend when you pay per token: from AGENTS.md and task size to model choice and cache.
6 min · · экономия · агенты
When Codex works through the API rather than a subscription, every agent step becomes a line on your bill. Spend grows not with the number of your messages but with the size of the context the agent resends at every step. Below is a nine-item checklist: what to configure in Codex and which habits reduce token use. Setting and command names were checked against Codex documentation on October 18, 2026; versions change, so where they differ, trust the tool's own documentation.
Where the spend comes from
The agent loop looks like this: the model asks to read a file, gets its contents, asks to run a test, gets the output, edits the code. Every request carries the entire accumulated history. If the initial context is 10,000 tokens (instructions, AGENTS.md, first files) and each step adds another 2,000, then over 30 steps the input adds up to:
30 × 10,000 + 2,000 × (0 + 1 + ... + 29) = 300,000 + 2,000 × 435 = 1,170,000 tokensThat is about 1.2M input tokens for 30 steps. The growth is quadratic: the longer the session, the more each next step costs. Almost every item below follows from this.
Connection: it affects spend too
Zukko connects to Codex through ~/.codex/config.toml with wire_api = "responses", and the key goes into ~/.codex/auth.json as OPENAI_API_KEY. This scheme is described in the Codex docs. An example config with all the parameters from the checklist:
model = "gpt-6.1-sol"
model_provider = "zukko"
model_reasoning_effort = "medium"
model_context_window = 1000000
model_auto_compact_token_limit = 200000
tool_output_token_limit = 10000
[model_providers.zukko]
name = "zukko"
base_url = "https://api.zukko.pro/v1"
wire_api = "responses"
requires_openai_auth = trueThe numbers in the example are a starting point, not a recommendation: tune your own. For GPT models the gateway sees cache reads in this format, so cheap cached tokens reach the bill immediately.
The checklist
1. Cut tasks into short sessions
From the formula above: three sessions of 10 steps cost less than one of 30. Same assumptions: for 10 steps the input is 10 × 10,000 + 2,000 × 45 = 190,000, so three sessions give 570,000 against 1,170,000. When a logical piece of work is done and committed, start a new chat with /new (or /clear if you also want to wipe the screen). The context you need later lives in the repository and in AGENTS.md, not in the history.
2. Compress long history
If a task cannot be split, the /compact command summarizes history and frees tokens. Automatic compaction is enabled by the model_auto_compact_token_limit parameter: a token threshold at which Codex compresses history itself. If no threshold is set, the model default applies; an explicit threshold gives you control over history size, while a 1M-token window lets a session grow for a very long time and you pay for every turn of it.
3. Keep AGENTS.md short
The file is read at the start of a session and goes into every request. Each extra line is multiplied by the number of steps. Write build and test commands, code conventions, prohibitions. Do not write project history or long explanations. The reading limit is set by project_doc_max_bytes. More: AGENTS.md for Codex.
4. Phrase the task with paths
"Fix the auth bug" makes the agent search the whole project, while "fix the test tests/test_auth.py::test_refresh in app/auth/tokens.py" cuts the recon to a couple of reads. Name the files, the check command and the done criterion.
5. Limit command output
A 5,000-line test log eats context and stays in it until the session ends. Ask for tests in quiet mode with only the tail shown (pytest -q | tail -40), and save long results to a file. The tool_output_token_limit parameter sets the token budget for one tool output in history.
6. Pick the model for the task
Zukko prices on October 18, 2026, per 1M tokens:
| Model | Input / output | Cache read | Strength index |
|---|---|---|---|
gpt-6.1-sol | $0.27 / $1.32 | 0.013 | 52 |
gpt-6-sol | $0.13 / $0.63 | 0.013 | 49 |
gpt-6-luna | $0.04 / $0.21 | 0.038 | 38 |
A session of 1.2M input tokens and 24,000 output tokens without cache costs: gpt-6.1-sol is 1.2 × 0.27 + 0.024 × 1.32 = $0.324 + $0.0317 = about $0.356; gpt-6-sol is 1.2 × 0.13 + 0.024 × 0.63 = $0.156 + $0.0151 = about $0.171; gpt-6-luna is 1.2 × 0.04 + 0.024 × 0.21 = $0.048 + $0.005 = about $0.053. gpt-6-luna has no image input. A strength index does not guarantee a result on your task: run the same edit on different models and compare. You can switch models in a session with /model or with the --model flag at launch.
7. Tune the reasoning level
The model_reasoning_effort parameter (values such as low, medium, high; the set depends on the model) determines how much the model "thinks" before answering. Reasoning is generally counted as output tokens, and output costs more than input (1.32 versus 0.27 for gpt-6.1-sol). For routine edits try a lower level, raise it for hard debugging. Which level you need is shown only by testing on your tasks; the usage field in the response shows token counts. More: reasoning and cost.
8. Do not break the cache
Cache works on the request prefix: a matching beginning is read at a reduced price. For GPT in Zukko only cache reads are counted; writes are not billed separately, and the first request is charged at the full input price. To keep the cache hitting, do not change the model mid-session, do not edit AGENTS.md while working and do not shuffle the start of the prompt. A calculation: suppose 75% of the 1.2M input came from cache (0.9M) and 0.3M is fresh. Then on gpt-6.1-sol: 0.3 × 0.27 + 0.9 × 0.013 + 0.024 × 1.32 = $0.081 + $0.0117 + $0.0317 = about $0.124 instead of $0.356. The real cache share depends on your session; this is an assumption for illustration. How cache works: key limits and cache.
9. Set a limit and watch spend
A separate key for Codex with a daily limit in the dashboard stops a faulty loop. Inside a session the /status command shows the current session state, and the dollar charge history is in the "Usage" section of the dashboard or via GET /v1/usage.
Summary
| Habit | What it reduces |
|---|---|
Short sessions, /new | input growth over steps |
/compact and the auto-compaction threshold | size of accumulated history |
Short AGENTS.md | the constant part of every request |
| Paths and criteria in the task | volume of recon |
| Quiet tests and an output limit | clutter in context |
| Model by task | the token price |
| Moderate reasoning | output tokens |
| Stable prefix | share of fresh input |
| Key limit | risk of a runaway loop |
FAQ
How do I find out how many tokens Codex spent?
Open /status in a session, and check the charge history in the "Usage" section of the dashboard or via GET /v1/usage.
Does the context window size affect the price?
The window is a ceiling, and you pay for the tokens actually sent. But a large window lets a session balloon, so keep history in check with compaction and short sessions.
Is one model or two more economical?
One option is a strong model for hard steps and a cheap one for routine. Test on your tasks whether the cheap one is enough, and switch through /model.
How do I start?
Create a key in the dashboard, set a daily limit, connect Codex following the instructions above and compare spend before and after these settings.
zukko
Pay 25×
less
for Claude Code & ChatGPT
One fetch_* key and base URL api.zukko.pro — for Cursor, Claude Code, Codex, and any SDK.
Related articles
- opencode agents: setting up roles, models and toolsA custom agent in opencode is a role, a model and a set of permissions. A working config where a cheap model handles routine work and a stronger one edits code.
- Plan mode in Claude Code: plan first, then edit the codeIn plan mode Claude Code reads your code and proposes a plan but edits nothing until you agree. How to enable it, use it and what it costs.
- How much an AI agent costs: a step-by-step model spend calculationAgent cost is set by the number of steps, context growth, cache and the model. A formula, three examples with arithmetic and a calculator you can plug your own numbers into.
- Saving tokens in opencode: a checklist of settings and habitsopencode spend comes from habits and a few lines of config. A checklist you can verify: what to turn on, what to remove, and how to see the result in the dashboard.