How to lower a Claude Code or Codex API bill
On an API key you pay per token, and a coding agent sends the whole session again with every request. So the bill follows the size of the context more than the number of questions you ask. To lower it, keep sessions short, load less memory on every turn, match the model to the job, and measure the same task before and after each change.
Updated 2026-11-16
What you are billed for#
A request is made of four kinds of token. Fresh input is text the model has not seen in this session. A cache write stores the start of the request so the next one can reuse it. A cache read is that reused part, billed at a small fraction of the input price. Output is what the model writes back, its thinking included, and it costs the most per token.
In an agent session the cache lines carry most of the volume, because the same session is read back on every turn. That is why two people asking the same question can pay very different amounts: one asks it in a fresh session, the other at the end of a long one.
Where a high bill usually comes from#
Anthropic’s cost docs name the two usual causes of unexpectedly high spend on an API plan: long sessions that were never cleared, and the largest model left as the default. The rest of the list is the same in any coding agent:
- Memory files that grew. CLAUDE.md and AGENTS.md are sent with every request, whether the task needs them or not.
- Long tool output. A full log read into context is paid for again on every later turn.
- Cache misses. The first message after a long break processes the whole context again at the write price.
- Parallel agents. Subagents and agent teams each send their own requests.
- Unused MCP servers. Each one adds to what the model carries.
The levers, in each tool#
| Lever | Claude Code | Codex |
|---|---|---|
| Start fresh between tasks | /clear; /rename first to come back with /resume | Start a new session |
| Shrink the history | /compact with what to keep | A short handoff note into a new session |
| Use a smaller model | /model | Pick the model per task; avoid the fast modes for routine work |
| Lower reasoning effort | /effort | Lower the reasoning effort |
| Shorten memory | CLAUDE.md under 200 lines; workflows into skills | A shorter AGENTS.md, nested per folder |
| Drop unused servers | /mcp, and /context to see what fills the window | Disable the MCP servers you do not use |
| See the numbers | /usage | /status |
Asking for shorter answers helps in both.
For a team on the Claude API, the Claude Console has workspace spend limits and per-member cost reporting, so one runaway session does not become a surprise at the end of the month.
A measured before and after#
Memory is the lever that pays on every turn, so it is the one we measured. We asked the same 30 questions in Claude Code 2.1.325 on Claude Sonnet 5.5, three full sessions a side: once with a test memory in the layout Claude Code itself writes, as it was, and once after MinniMemoryMCP 1.6.0 sorted it, with its server and hook on.
| Token kind | Before | After | Change |
|---|---|---|---|
| Fresh input | 490 | 256 | -47.8% |
| Cache write | 134,041 | 96,034 | -28.4% |
| Cache read | 4,133,084 | 2,152,150 | -47.9% |
| Output | 46,520 | 28,142 | -39.5% |
Measured 15 November 2026: at API rates, a Claude Code session on our test memory cost $0.47 before MinniMemoryMCP 1.6.0 and $0.29 after on average, 37.7% less, for the same 30 questions (each of three sessions 33.9% to 40.0% less). A blind judge found 80 of 87 answers correct before and 81 after. The method, the date and the build are on the Evidence page.
The change was made by MinniMemoryMCP, an MCP server for Claude Code and Codex. It sorts your memory files into a small part that loads on every turn and files the agent opens only when a task needs them. Every line is kept, and undo puts the originals back.
$ npm i -g minnimemory
$ minnimemory installMeasure your own#
Pick one task you do often. Run it in a fresh session, note the token counts and cost, change one thing, and run it again. In Claude Code the Session block of /usage lists input, output, cache read and cache write tokens with an estimated cost. Comparing two different days of work tells you nothing; the same task twice does.
More detail per tool: How to reduce Claude Code token usage, How to reduce Codex token usage and Why Claude Code sessions get expensive.