Prompt caching and compaction
How long Claude Code and Codex keep the prompt cache, what coming back to a big idle session costs, when compacting pays, and how Ottto shows it.
This guide explains the prompt cache in Claude Code and Codex, why a big session costs more after a break, and when compacting saves money. Vendor facts were checked against the linked vendor pages on November 30, 2026. Prices are list prices. On a Claude or ChatGPT plan, read them as plan usage, not cash.
What the prompt cache is
Section titled “What the prompt cache is”A coding agent sends the whole conversation with every request: instructions, tools, files it read, and every earlier turn. One message from you is often several requests.
The provider keeps the start of the conversation it has already processed. When the next request starts the same way, that part is read from the cache instead of being processed again.
| Part of a request | What you pay |
|---|---|
| Read from the cache | 2.5% to 10% of the normal input price, depending on the model |
| Written to the cache | Claude: 1.25x input (5-minute cache) or 2x input (1-hour cache). OpenAI API: 1.25x input (30-minute cache) on GPT-5.6 and later; none on earlier models. Codex on ChatGPT credits: no write charge |
| New input, not cached | The normal input price |
| Output | The normal output price |
Two things follow:
- A big context costs on every request. Even from the cache, each request pays to read all of it. On Claude Opus 5.5 that is about $0.02 per request at 100K tokens, $0.06 at 300K and $0.10 at 500K.
- A lost cache costs a rebuild. If the cache has expired, the next request processes or writes the whole conversation again. On Claude, a 1-hour cache write costs 40 times a read on Opus 5.5 and 80 times on Fable 5.2.
There is no storage fee. A big cache costs money only because every request reads it and every rebuild writes it.
A write is not a rebuild
Section titled “A write is not a rebuild”Cache writes show up on almost every request, and that is normal. Each request writes the part that is new since the last one, usually your latest message and the tool results, so the next request can read it. A rebuild is one request after the cache expired or the start of the conversation changed: it writes the whole conversation again. Small writes on every turn are the price of caching. Large writes after a pause or a change are rebuilds.
Sources: Anthropic prompt caching pricing, How Claude Code uses prompt caching, OpenAI prompt caching, Codex rate card.
How long the cache lasts
Section titled “How long the cache lasts”The cache lifetime (TTL) is the minimum time a cached prefix stays available after its last use. Each read restarts the timer. The lifetime depends on the app, the kind of request and how you pay.
Claude Code
Section titled “Claude Code”Claude Code puts each request in one of two groups. Anthropic calls them:
- Main conversation: your interactive turns,
-pruns and Agent SDK turns, plus the helpers that run inline with them. - Everything else: subagents, workflows, in-process teammates, forks, compaction and session titles.
| How you pay | Main conversation | Everything else |
|---|---|---|
| Claude subscription, within the plan’s included usage | 1 hour | 5 minutes (a few helper requests that Anthropic controls get 1 hour) |
| Claude subscription, drawing on usage credits | 5 minutes by default | 5 minutes |
| API key | 5 minutes | 5 minutes |
| Amazon Bedrock, Google Cloud, Microsoft Foundry | 5 minutes | 5 minutes |
| Claude apps gateway | 5 minutes; an administrator can turn on 1 hour in gateway policy | Same |
Both lifetimes exist in one session, and often in one request. On a subscription, the main conversation is cached for 1 hour while its subagents use 5 minutes. The compaction request is the common mixed case: it runs in the 5-minute group, but while the main conversation is still cached it reads that 1-hour cache and writes only its own small tail.
On usage credits you pay for each token, so Claude Code drops the main conversation to 5 minutes by default. A 1-hour lifetime you set yourself, in settings or an environment variable, still applies there. A 1-hour value in a subagent file does not: Claude Code ignores it while you draw on usage credits.
Forks read the parent’s cache on their first request. Subagents start from a different prefix and build their own cache.
Sources: Which TTL each request gets, Choose the TTL yourself, Subagents and the cache, gateway policy.
Moving between 5 minutes and 1 hour
Section titled “Moving between 5 minutes and 1 hour”A session can change lifetime halfway, for example when a subscription starts or stops drawing on usage credits, or when you change the setting.
- From 1 hour to 5 minutes: the next request still reads the warm cache. Only new writes use the 5-minute lifetime.
- From 5 minutes back to 1 hour: the first 1-hour request can write most of the conversation again at the 1-hour rate (2x input). Anthropic does not count a hit on a 5-minute entry when a later 1-hour cache marker misses, so everything after the last 1-hour entry is written again. A very short 5-minute stretch, one or two requests, often costs little; a longer one usually costs a near-full rewrite.
To avoid paying that rewrite, choose the lifetime before a long session rather than in the middle of it. If you often draw on usage credits and pause 5 to 60 minutes, set the main conversation to 1 hour so it does not switch back and forth.
Source: Mixing different TTLs.
Claude Code settings
Section titled “Claude Code settings”Each value is 5m or 1h; any other value is ignored. When more than one is set, the first match in this order wins.
| Setting | What it does | Applies to |
|---|---|---|
FORCE_PROMPT_CACHING_5M=1 (environment variable) |
5 minutes for every request | Both groups |
CLAUDE_CODE_PROMPT_CACHE_TTL (environment variable, Claude Code 2.1.242 or later) |
Sets the lifetime | Main conversation |
CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL (environment variable, 2.1.242 or later) |
Sets the lifetime | Everything else |
promptCacheTtl (settings file, 2.1.242 or later) |
Sets the lifetime | Main conversation |
subagentPromptCacheTtl (settings file, 2.1.242 or later) |
Sets the lifetime | Everything else |
cacheTtl under experimental in a subagent file (2.1.248 or later) |
Sets the lifetime for that subagent type; 1h is ignored while drawing on usage credits |
One subagent type |
ENABLE_PROMPT_CACHING_1H=1 (environment variable) |
1 hour for every request | Both groups |
Pick the scope
Section titled “Pick the scope”- One run or session: set
CLAUDE_CODE_PROMPT_CACHE_TTLorCLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTLin the shell that starts it. Agent SDK callers can pass the same variables throughoptions.env. - A project, all your projects, or an organization:
promptCacheTtlandsubagentPromptCacheTtlwork in any settings file: project, local, user or managed. An administrator can also put the environment variables in theenvblock of managed settings. - One subagent type: add an
experimentalmap to the subagent’s file in.claude/agents/or~/.claude/agents/, or in the agents folder of managed settings.
---name: long-builddescription: Waits on long buildsexperimental: cacheTtl: 1h---The subagent-type setting has limits:
- It goes inside
experimental, not at the top level of the frontmatter. - It is read only from subagent files. Subagents passed as
--agentsJSON ignoreexperimental. CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTLandsubagentPromptCacheTtlcome first. If either is set, the file’s value is not used.- A built-in type has no setting of its own. A project or user subagent with the same name replaces the built-in, including its prompt and tools; Anthropic documents this for
Explore. Otherwise all built-in types followsubagentPromptCacheTtl. - Workflow agents follow
subagentPromptCacheTtl.
Check which lifetime you got
Section titled “Check which lifetime you got”Run claude -p "hello" --output-format json and look at usage.cache_creation: 1-hour writes are under ephemeral_1h_input_tokens and 5-minute writes under ephemeral_5m_input_tokens. In a session, /usage shows a Prompt cache (main) line with the lifetime in effect for the main conversation (Claude Code 2.1.251 or later).
When to change it
Section titled “When to change it”- The main conversation on an API key, cloud provider or usage credits, and you often pause 5 to 60 minutes: try
"promptCacheTtl": "1h". One reuse after 5 minutes replaces a 1.25x rewrite with a cheap read, which pays for the 2x write. On Amazon Bedrock, 1-hour support depends on the model and region. - A subagent type that waits 5 to 60 minutes inside its own run, for example on a long build or a review: give that type
cacheTtl: 1hin its file. Most subagents finish without such a wait, and for them 1 hour only raises the price of every write. That is whysubagentPromptCacheTtlset to1hfor all of them rarely pays. - Scripted
-por Agent SDK runs that never pause over 5 minutes, on a subscription: setCLAUDE_CODE_PROMPT_CACHE_TTL=5mfor those runs only, so every write costs 1.25x instead of 2x. Do not set it for runs that can pause. - Interactive turns that almost always come back within 5 minutes, on a subscription:
"promptCacheTtl": "5m"writes at 1.25x instead of 2x. After a longer pause the next request rewrites the conversation, and idle compaction no longer runs.
The last two cases are about plan usage, not a bill. Anthropic doesn’t publish how cache counts against plan limits. If your plan uses tokens the way the API does, these changes would help as described.
Sources: Choose the TTL yourself, subagent frontmatter fields, subagent scope, workflows and the cache, Agent SDK cache lifetime, Anthropic: when to use the 1-hour cache.
Codex has no setting for the cache lifetime. Every request gets the provider’s default: main thread, subagents and compaction alike. A new subagent reads only the shared start of the prompt.
| How you pay | Cache lifetime | Cache write charge |
|---|---|---|
| Sign in with ChatGPT | Not published. In practice it usually lasts through short pauses and fades over hours; the longer the pause, the more likely it is gone | On credits: none, so a lost cache costs the normal input price. Within a plan’s included usage: OpenAI doesn’t say how writes count |
| OpenAI API key, GPT-5.6 and later | At least 30 minutes; OpenAI may keep it longer. Each read restarts the 30 minutes | 1.25x input. The API also offers a 12-hour lifetime at 2.5x, but Codex has no setting to ask for it |
On an API key, a request with more than 272K input tokens pays a higher rate for the whole request on eligible models. The Codex rate card for ChatGPT sign-in lists no such step.
Since you cannot change the lifetime, what you control in Codex is how long you pause in a big thread and when it compacts. See Coming back to a big idle session and While you work: when to compact.
Sources: OpenAI prompt caching, OpenAI pricing, Codex rate card, Codex configuration reference.
Routes at a glance
Section titled “Routes at a glance”| Route | Cache lifetime | How the cache is priced |
|---|---|---|
| Claude API key or cloud provider | 5 minutes; you can set 1 hour | Writes 1.25x (5 minutes) or 2x (1 hour) input; reads 2.5% to 10% |
| Claude usage credits | Main conversation 5 minutes by default; you can set 1 hour, but not in a subagent file | Standard API rates |
| Claude plan, included usage | Main conversation 1 hour, the rest 5 minutes; you can change both | Not published |
| Codex with an OpenAI API key | At least 30 minutes; no setting | Writes 1.25x input; reads 5% to 10%; more above 272K input tokens |
| Codex on ChatGPT credits | Not published; no setting | Credits per token; no write charge |
| Codex on a ChatGPT plan, included usage | Not published; no setting | Not published |
Neither Anthropic nor OpenAI publishes how cache reads and writes count against plan limits. If your plan uses tokens the way the API does, the advice in this guide applies to your plan usage the same way. Anthropic does list long context and cache misses among the reasons plan usage climbs.
Sources: Manage usage credits, Why usage climbs in a long session, Codex pricing.
Does the length of a pause matter?
Section titled “Does the length of a pause matter?”Only against the cache’s own lifetime. For a Claude subscription’s main conversation, a 2-minute pause and a 50-minute pause are the same: the cache is still there. After the hour, usually only the shared start (system prompt and tools) is still cached. On a 5-minute route, a 10-minute pause already loses the conversation. Codex has no sharp edge; the chance of a loss grows with the pause.
Coming back to a big idle session
Section titled “Coming back to a big idle session”Why it costs more
Section titled “Why it costs more”When you come back after the cache expired, two things cost money:
- The rebuild. The first request writes the whole conversation into the cache again, at 1.25x or 2x the input price on Claude.
- Every later request. Each one reads the whole big conversation again. Over the next tens of messages this is usually the larger cost.
So the question is not only “what does the first message cost” but “how big is the conversation I am about to keep reading”.
Warm and cold compaction
Section titled “Warm and cold compaction”Compaction replaces the history with a summary so later requests carry less. The summary is a model request of its own.
| Warm compaction | Cold compaction | |
|---|---|---|
| When | The cache is still there | The cache has expired |
| What the summary request pays | Reads the history from the cache | Processes the history as new input, at the normal input price |
| Cost per 100K of history, Claude Opus 5.5 list prices | About $0.07 | About $0.39 |
Even a cold compaction is usually cheaper than just typing into a big expired session. Just typing writes the whole history again at the write price and then keeps re-reading it. On Claude’s 1-hour route, /compact first is cheaper from the first message once the history is above roughly 150K to 200K tokens.
After any compaction the next request writes the shorter prompt into the cache. A compacted Claude Code session does not start empty: system prompt, tools, instruction files, the summary and files the agent reads again often add up to about 100K tokens in a heavy setup. Codex keeps a compacted summary plus recent messages and usually restarts much smaller.
Source: Compacting the conversation.
Idle compaction (Claude Code)
Section titled “Idle compaction (Claude Code)”Idle compaction is on by default. Claude Code prepares a summary of an eligible session before the 1-hour cache expires, while the summary request can still read the cache. In practice the summary is prepared a little under an hour after the last request.
- Terminal: the summary is applied while you are idle. The notice starts with “Compacted while idle, before the prompt cache expired”.
- VS Code and the desktop app’s Code tab: the summary is used only when you next send a message after the cache expired. If something wakes the session before that, the cache was still warm, so the summary is not used.
It can run only when all of these hold:
| Condition | Detail |
|---|---|
| Interactive session | The terminal, the VS Code chat panel, or the desktop Code tab, on your machine or over SSH |
| Auto-compact is on | Turning auto-compact off also turns idle compaction off |
| The conversation is on the 1-hour cache | Default on a subscription within included usage, or set with promptCacheTtl. On a cloud provider or a gateway it runs once the 1-hour cache is on |
| The context is big enough | 200,000 tokens by default. Set it with CLAUDE_CODE_IDLE_COMPACT_MIN_TOKENS (lowest 100,000) |
| About an hour has passed | Since the last request, with the computer awake |
It cannot run:
- in non-interactive runs (
-p, scripts, Agent SDK); - on a 5-minute cache, which is the default for API keys, cloud providers and usage credits;
- when the computer sleeps through the hour;
- when
"idleCompaction": falseis set, orDISABLE_AUTO_COMPACT=1.
Codex has no idle compaction.
Sources: Settings reference: idleCompaction, Claude Code changelog.
Worked example
Section titled “Worked example”A modeled Claude Code session on Claude Opus 5.5, list prices per million tokens: input $4, 1-hour cache write $8, cache read $0.20, output $20. The session is 475K tokens. You come back after 2.5 hours and send 50 more messages, about three requests each.
| What you do | First message | Total for 50 messages |
|---|---|---|
| Just type | About $3.70 | About $22 |
Run /compact first (cold) |
About $2.40 | About $9.50 |
| Idle compaction ran while the cache was warm | About $0.80 | About $8 |
The first message explains under $3 of the gap. The rest is later requests reading a context about four times larger. After any compaction, sessions of every size cost about the same, because the compacted session is about the same size.
On a 5-minute route the order is the same. Idle compaction does not run there, so /compact before you step away from a big session is the only warm option.
What to do when you come back
Section titled “What to do when you come back”| Situation | Claude Code | Codex |
|---|---|---|
| Big session, cache expired, work continues | Run /compact first, then type |
On ChatGPT sign-in, compacting first costs about the same as just typing; do it if you will keep working. On an API key it pays back within a few calls above about 300K |
| The task is done | /clear (free) or a new session, not a cold /compact |
Start a new thread |
| You need the exact earlier detail | Keep the history and accept the rebuild | Same |
While you work: when to compact
Section titled “While you work: when to compact”Each request reads the whole context, so a bigger context costs more on every request. A compaction has a roughly fixed price: the summary request plus writing the smaller prompt back into the cache, about $0.60 on Claude Opus 5.5 when warm. Most of what compaction saves, often 90% or more, comes from the cheaper requests that follow it.
Rule of thumb: compact at about two to three times the size of a freshly compacted session. Being 30% off this point costs only a little, so a round number is fine.
| Setup | Auto-compact limit to try | How to set it |
|---|---|---|
| Claude Code, subscription (1-hour main cache) | About 300K. 400K if exact detail matters more | /autocompact, autoCompactWindow in settings, or CLAUDE_CODE_AUTO_COMPACT_WINDOW |
| Claude Code, API key, usage credits or cloud provider (5-minute cache) | About 250K. More is at stake because every pause over 5 minutes rewrites the conversation | Same |
| Codex, ChatGPT sign-in | About 200K | model_auto_compact_token_limit in ~/.codex/config.toml |
| Codex, API key | About 200K to 272K. Staying under 272K also avoids the higher long-context rate | Same |
Defaults for comparison: Claude Code on a 1M-token model compacts at about 967K. Codex compacts at 90% of the model’s context window, which is 450K on a model with a 500K window. model_auto_compact_token_limit can only lower the Codex limit.
Codex can also compact at the end of a turn with model_post_turn_compact_threshold_percent, a share of the usable context window. It is off by default. It is the closest Codex option to compacting before you step away.
Under about 150K, compaction rarely pays. A compacted Claude Code session restarts near 100K, so compacting at 150K removes little and the context grows back within a few dozen requests. Sessions that stay under 200K gain almost nothing from any limit.
The price of compaction is detail. A summary replaces the full history, so exact errors and early instructions can be lost. In the middle of debugging, let the size limit handle it rather than compacting by hand.
When a task is finished, use /clear in Claude Code or start a new Codex thread. It is free and the next task starts from its normal startup context.
Sources: Context window and auto-compaction, Manage costs effectively, Codex configuration reference, OpenAI compaction.
Cache reuse: what normal looks like
Section titled “Cache reuse: what normal looks like”Cache reuse is the share of prompt input read from the cache. Month-scale public data for heavy coding agents gives these bands:
| App | Normal | A bit low | Low |
|---|---|---|---|
| Claude Code | 95% or more | 90% to 95% | Under 90% |
| Codex | 93% or more | 85% to 93% | Under 85% |
A low number usually has a cause you can find: a custom provider or gateway that drops cache markers, a setting or tool list that changes mid-session, or a model or effort switch.
A high number does not mean there is nothing to save. A rebuild is a small share of tokens but a large share of cost: on Claude Opus 5.5 a 1-hour write costs 40 times a read. Sessions can sit at 98% reuse while rebuilds after long breaks make up a large part of the prompt cost. Fixing every rebuild might move reuse by less than a point. Use reuse as a health check, and look at rebuilds and session size for the money.
How Ottto shows this
Section titled “How Ottto shows this”All of these are in the Ottto web app under Optimize.
Prompt caching tab
| Part | What it shows |
|---|---|
| Bottom line | When an app rebuilt its cache 20 or more times after a pause of an hour or more: the share of that app’s rebuilds that came after such a pause, counted over rebuilds that did not follow a compaction. Otherwise it shows cache reuse |
| Cache lifetime | One row per way you pay (Claude API key, cloud provider, gateway, Claude plan, Codex on an API key or on ChatGPT sign-in): its share of cost, the cache lifetime its requests used, and whether that lifetime fits your pauses of 5 to 60 minutes. Open a row to see the main chat, headless runs and each subagent type, and the exact setting to change, with a Copy button. Codex rows have nothing to change, because OpenAI sets the lifetime; they link to lowering Codex’s automatic compaction limit instead |
| Big sessions woken after the cache expired | Claude Code wake-ups after a pause of an hour or more, by the size of the request that woke the session, from 200K tokens. When conversations over 300K were written again after such a pause, it says to compact before you step away, or to leave idle compaction on, and links to the Compaction plan |
| When the cache was rebuilt | Rebuilds by pause (Under 5 minutes, 5 to 60 minutes, An hour or more, Pause unknown) and by recorded reason (After a compaction, Model switch, Settings change, Other or none). Counts, not dollars |
| Sessions that rebuilt the most | The top 5, 10 or 20 sessions, each linking to its session page |
| What cache reuse saved | By app, model or subscription: Read from cache, Read savings, Extra write cost and Net saving |
The Cache lifetime panel suggests a change only when it is worth at least 5% of that route’s cost and $1.50 a day. On a plan, amounts are API-equivalent: what the same tokens cost on the API. It needs pause counts from the Ottto Mac app, which start with sessions uploaded after the app’s next update. Until then it shows how many sessions are counted so far.
A pause is the time between two recorded requests, not proven idle time. The pause rows are fixed at 5 and 60 minutes. On a 5-minute route, rebuilds in the 5 to 60 minutes row are cache expiries too. When some sessions have partial records, Ottto says “at least”.
Opportunities tab. When an app rebuilt its cache 20 or more times after pauses of an hour or more in the last 7 days, an item says so and links to the Prompt caching tab. A second item shows when an app rebuilt it 20 or more times after a model switch. A third, Prompt cache lifetime: 1 hour pays (or 5 minutes pays), shows when the Cache lifetime panel suggests a change for the last 7 days.
Context growth tab
| Part | What it shows |
|---|---|
| When sessions compacted | How compactions started: While idle, Automatic, Manual or Not recorded |
| Compaction plan | Replays the last 7 days of Claude Code and Codex sessions as if they had compacted at a lower limit, with the saving at API list prices (API-equivalent on a plan) and the setting to change |
| Above the 272K price step | The extra cost, at API list prices, of requests above 272K input tokens. It applies when Codex uses an API key |
Ottto does not read your settings files. The Cache lifetime panel shows which lifetime your requests used, not what you set. For your compaction limit, check your own settings files.
Glossary
Section titled “Glossary”| Term | Meaning |
|---|---|
| Prompt cache | The provider’s saved copy of the start of a conversation it has already processed |
| Cache read | Input served from the cache, at 2.5% to 10% of the input price depending on the model |
| Cache write | Saving input into the cache so later requests can read it. Happens on almost every request for the new part of the conversation. Claude charges 1.25x or 2x input; the OpenAI API charges 1.25x on GPT-5.6 and later; Codex on ChatGPT credits has no write charge |
| Cache lifetime (TTL) | The minimum time a cached prefix stays available after its last use: 5 minutes or 1 hour on Claude, at least 30 minutes on the OpenAI API |
| Prefix | The unchanged start of a request. Only a matching prefix can be read from the cache |
| Shared start | The system prompt and tools that many sessions share. It often stays cached when the rest has expired |
| Warm | The cache is still there |
| Cold | The cache has expired, so the next request rebuilds it |
| Rebuild | A request that writes the whole context again instead of reading it, after the cache expired or the start of the conversation changed |
| Route | How a request is paid for: API key, cloud provider, usage credits, or a plan’s included usage. It decides the default cache lifetime and the price |
| Subagent type | A named kind of subagent, built in (such as Explore) or defined in a subagent file. A file can set its own cache lifetime |
| Main conversation | Claude Code’s group for your turns, -p runs, Agent SDK turns and their inline helpers |
| Everything else | Claude Code’s group for subagents, workflows, in-process teammates, forks, compaction and session titles |
| Subscription within plan | Claude Code usage covered by a Claude plan’s included usage |
| Usage credits | Paid usage a Claude subscription draws on past its included usage, billed at standard API rates. Claude Code uses 5-minute caches here unless you set 1 hour |
| API key | Paying the provider directly per token |
| Compaction | Replacing a conversation’s history with a summary so later requests carry less |
| Auto-compact limit | The context size at which the app compacts on its own |
| Warm compaction | A compaction whose summary request reads the history from the cache |
| Cold compaction | A compaction after the cache expired, which pays the history as new input |
| Idle compaction | Claude Code compacting an eligible idle session before its 1-hour cache expires |
/compact |
Compacts the conversation now |
/clear |
Drops the history for free and starts the next task fresh |
| Cache reuse | Cache reads divided by all prompt input |
| Long-context price step | On eligible OpenAI models with an API key, a higher rate for the whole request above 272K input tokens |
| List price | The provider’s published price. On a plan, read it as plan usage, not cash |