Prompt Caching Compared: Claude vs OpenAI vs DeepSeek, Kimi, GLM and Qwen, Plus Cache-Hit Discipline for Coding Agents
As of 2026-11-25, every major model vendor caches prompts by prefix match, but the switches and prices differ a lot. Anthropic's Claude needs cache_control markers (or top-level automatic caching), charges 1.25x input for a 5-minute write or 2x for a 1-hour write, and bills reads on Opus 5.5 and Sonnet 5.5 at just 5% of input. OpenAI caches automatically on GPT-5.6 and later, but now charges 1.25x for writes too. DeepSeek caches on disk by default with no write fee, and a hit costs about 2% of a miss. For a coding agent the price table is only half the story; the other half is keeping the prefix stable: a frozen system prompt and tool list, no timestamps in CLAUDE.md or AGENTS.md, and append-only history.
QCode is an independent third-party API service. It is not affiliated with Anthropic, OpenAI, DeepSeek, Moonshot, Zhipu or Alibaba. Vendor rules below come from each vendor's official docs, read on 2026-11-25. What you are actually charged on QCode is listed on /models.
If you only care about Claude Code, our Claude Code prompt caching cost page covers that tool alone, and Prompt caching cost 2026 tracks prices for three vendors. This page compares the mechanics across six vendors and lays out hit-rate habits that work in any coding agent.
Updated 2026-11-26
Key facts
- Claude: explicit
cache_control: {"type": "ephemeral"}, 5 minutes by default,"ttl": "1h"optional. Writes cost 1.25x or 2x input; reads cost $0.20 on Opus 5.5, $0.10 on Sonnet 5.5 and $0.20 on Fable 5.2 per million tokens (Anthropic pricing page). - OpenAI: automatic on GPT-5.6 and later, minimum 1,024 visible input tokens,
"30m"default lifetime and"12h"in the Responses API. Writes cost 1.25x or 2.5x; reads on GPT-6.2 Sol and Astra cost 5% of input. - DeepSeek: on by default, no code changes, best effort. Entries usually last hours to days.
deepseek-flashat peak: $0.006 per million on a hit vs $0.30 on a miss. - Kimi K3 / K3.1 Pro: implicit caching by default, TTL
5mor1h. On K3, a 5-minute write costs the normal $3 input price, a 1-hour write $6, and a hit $0.30. - GLM and Qwen: Zhipu caches implicitly and suggests prefixes of 500+ tokens. Alibaba Model Studio's implicit cache usually bills hits at 20% of input; its explicit cache usually bills creation at 125% and hits at 10%, and the two modes are mutually exclusive.
- Discipline matters: one developer reported on vc.ru that removing an auto-updated timestamp from the first line of CLAUDE.md lifted the hit rate from 41% to about 93% (author's own account, details below).
How prompt caching works
The model API remembers nothing between calls, so a coding agent re-sends the system prompt, tool definitions, project instruction files and the whole conversation on every turn. Caching means: if the start of this request (the prefix) matches a recently processed request byte for byte, the vendor reuses that work, bills it at a lower cache-read rate, and fully processes only the new tail.
Four rules hold everywhere:
- Prefix only. A change to any byte invalidates everything after it. There is no per-file or per-section cache.
- Render order sets stability. Anthropic renders
tools, thensystem, thenmessages. Tools come first, so editing one tool definition clears the whole cache. - Caches are per model. Switching models means a fresh cache, even with identical content.
- Entries expire. If nothing reads an entry within its TTL, the next turn writes it again.
Caching rules by vendor (2026-11-25)
| Vendor / model | How it's enabled | Minimum | Lifetime | Write price | Read price | Usage fields |
|---|---|---|---|---|---|---|
| Anthropic Claude | cache_control block markers or top-level automatic caching; up to 4 breakpoints |
Anthropic documents a minimum cacheable prefix; check the caching docs for your model | 5 min default; "ttl": "1h"; each hit refreshes |
1.25x (5 min), 2x (1 h) | 0.05x on Opus 5.5 / Sonnet 5.5; 0.025x on Fable 5.2; 0.1x on Haiku 5.5 | cache_creation_input_tokens, cache_read_input_tokens |
| OpenAI GPT-5.6+ | Automatic (implicit breakpoints); prompt_cache_options for explicit mode |
1,024 visible input tokens | "30m" default; "12h" in Responses API |
1.25x (30 min), 2.5x (12 h), as total rates | 0.1x; 0.05x on GPT-6.2 Sol / Astra and GPT-6.1 Sol / Terra | Responses: input_tokens_details.cached_tokens, cache_write_tokens |
| DeepSeek | On by default, no changes needed | Not stated | Not fixed; usually hours to days | No write fee mentioned | deepseek-flash peak $0.006 vs $0.30 miss; deepseek-v4-pro peak $0.044 vs $1.32 |
prompt_cache_hit_tokens, prompt_cache_miss_tokens |
| Kimi (Moonshot) | Implicit by default; set TTL via prompt_cache_options.ttl or top-level cache_control.ttl on Messages |
Block-based, block size not published | 5m (default) or 1h, locked at first write |
K3: $3 for 5 min (same as input), $6 for 1 h | K3 $0.30 (1/10 of input) | Chat: prompt_tokens_details.cached_tokens, cache_write_tokens |
| Zhipu GLM | Implicit, no switch | 500+ tokens suggested | Not stated | Cache storage "limited-time free" on Z.ai | Vendor docs describe a cache-hit discount; check the current Zhipu or Z.ai price list for your region and model | prompt_tokens_details.cached_tokens |
| Alibaba Model Studio (Qwen) | Implicit (default, can't be turned off) or explicit cache_control, one per request |
1,024 tokens | Implicit not fixed; explicit 5 min, reset on hit | Implicit 100%; explicit usually 125% | Implicit usually 20%; explicit usually 10%; some newer Qwen models list cache prices only in the Model Studio console | cached_tokens, cache_creation_input_tokens |
Sources: Anthropic pricing page and prompt caching docs; OpenAI prompt caching guide; DeepSeek KV cache guide and pricing page; Kimi platform context caching docs; Zhipu context cache docs and Z.ai pricing; Alibaba Model Studio context cache docs. All read 2026-11-25.
Differences people miss:
- Write premiums are no longer Claude-only. OpenAI (from GPT-5.6), Kimi's 1-hour TTL and Model Studio's explicit cache all charge extra to write. A cache entry nobody reads is pure overhead.
- DeepSeek's discount is the deepest. A
deepseek-flashhit costs about 2% of a miss, so each 10 points of hit rate moves your input bill more there than anywhere else. - Model Studio's two modes don't stack. Adding
cache_controlto a request switches that request to explicit caching. - GLM Coding Plan is billed differently. Zhipu's docs say the cache-hit discount applies to the standard API, not to resource packs or the GLM Coding Plan, which uses its own credit coefficients.
Caching through QCode
QCode bills official price × a service multiplier, which is ×1.0 for Anthropic and OpenAI models as of 2026-11-25. /models lists cache-read prices for Claude and GPT that match the vendors: claude-opus-5-5 $0.20, claude-sonnet-5-5 $0.10, gpt-6.2-sol $0.10 per million tokens.
- Claude: Anthropic's API supports prompt caching, and Claude Code adds
cache_controlmarkers itself. Gateways do not all forward those markers and theanthropic-betaheader used for the 1-hour TTL unchanged, so if you rely on either through a customANTHROPIC_BASE_URL, test it with a small request first and confirm hits incache_read_input_tokens. - GPT: GPT-6.x caches automatically; Codex CLI and the OpenAI SDK need no changes.
- Chinese models: caching behaviour comes from the upstream vendor; the rate you pay is the one shown on /models.
Cache-hit discipline for coding agents
1. Freeze the system prompt and tool list
Tool definitions sit at the very front of the prefix. Connecting or removing an MCP server, enabling a plugin that brings MCP servers, or denying a whole tool mid-session can force a full rebuild. Claude Code's docs explain that with tool search on (the default on supported models), servers that connect mid-session supply deferred tools and leave the cached prefix alone. Through a custom ANTHROPIC_BASE_URL gateway, tool search may be unavailable, so tools load upfront and a mid-session server change invalidates the cache. In your own agent, sort tool JSON by name and serialize it deterministically.
2. Keep volatile content out of CLAUDE.md and AGENTS.md
Project instruction files ride in the prefix every turn. Leave out timestamps, "last updated" lines, the current git branch, random IDs, generated file lists and dates.
The most cited example comes from a vc.ru article by Artemiy Miller (2026-06-19). Coding with DeepSeek V4-Pro, the author found a 41% cache hit rate. The cause was a timestamp on the first line of CLAUDE.md that a git hook rewrote every turn. After stopping the auto-update, the hit rate reached about 93% within five turns. This is the author's own report and hasn't been independently reproduced, but the mechanism matches every vendor's documentation.
One nuance: Claude Code reads the project-root and user CLAUDE.md once at session start. Editing it mid-session neither invalidates the cache nor takes effect until /clear, /compact or a restart. So in Claude Code, a timestamp that changes each time mainly makes every new session start cold, while in a harness that re-reads instruction files each turn it breaks every turn. The same applies to tools that read AGENTS.md, such as Codex.
3. Keep history append-only
Deleting or editing earlier messages, or removing a reminder you injected last turn, rewrites the prefix. On Claude Opus 5.5, thinking blocks are also bound to the model and conversation, so history edits invalidate later thinking blocks (Anthropic, "What's new in Opus 5.5"). To abandon a wrong path, /rewind to an earlier turn instead of editing; the rewound prefix is already cached.
4. Pick the model and effort at session start
Each model has its own cache, so after /model the next turn re-reads the whole history uncached. Settings like opusplan, which switch models between plan and execution, cold-start on every toggle. On effort, Claude Code's docs say that on Opus 5.5, Sonnet 5.5 and Haiku 5.5 with an API key, changing effort keeps the cache by default, though not on some cloud platforms and gateways. Don't mix vendors in one conversation either: to the second vendor, all earlier history is new.
5. Time your compaction
/compact replaces history with a summary, which by design invalidates the conversation layer. With a warm cache, the summarization request reads the prefix from cache and costs little. After a break longer than the TTL, it re-reads the full history uncached, which is the most expensive case. Compact between tasks rather than letting auto-compaction fire mid-task. Claude Code v2.1.325 fixed compaction behind third-party gateways, so keep it up to date when using QCode.
6. Match the TTL to how you work
With an API key, Claude Code's main conversation uses a 5-minute TTL by default. If you often step away for 10 to 60 minutes, set promptCacheTtl to 1h or export CLAUDE_CODE_PROMPT_CACHE_TTL=1h (Claude Code v2.1.242+). If you work in steady bursts with gaps under 5 minutes, stay on 5 minutes, because 1-hour writes cost 2x input.
# Claude Code via QCode, with a 1-hour cache for the main conversation (v2.1.242+)
export ANTHROPIC_BASE_URL=https://api.qcode.cc/api
export ANTHROPIC_AUTH_TOKEN=cr_your_key
export CLAUDE_CODE_PROMPT_CACHE_TTL=1h
claude
How to read cache hits
Each vendor reports hits in different fields. This Python helper turns all three formats into one hit rate:
def cache_hit_rate(usage: dict) -> float:
"""Pass the response's usage dict; returns cached share of input tokens."""
if "cache_read_input_tokens" in usage: # Anthropic Messages (also Kimi / Model Studio Anthropic-compatible)
read = usage.get("cache_read_input_tokens", 0)
total = read + usage.get("cache_creation_input_tokens", 0) + usage.get("input_tokens", 0)
elif "prompt_cache_hit_tokens" in usage: # DeepSeek
read = usage["prompt_cache_hit_tokens"]
total = read + usage.get("prompt_cache_miss_tokens", 0)
else: # OpenAI style (Responses / Chat Completions)
details = usage.get("input_tokens_details") or usage.get("prompt_tokens_details") or {}
read = details.get("cached_tokens", 0)
total = usage.get("input_tokens") or usage.get("prompt_tokens") or 0
return read / total if total else 0.0
OpenAI's guide documents usage.input_tokens_details.cached_tokens for the Responses API; for other OpenAI-compatible APIs, check the usage object your SDK returns. On Anthropic, input_tokens is only the uncached tail after the last breakpoint, so add all three fields for the total. In Claude Code, /usage shows a "Prompt cache (main)" line (v2.1.251+) with the session hit ratio and the likely cause of the last miss. Running claude -p "hello" --output-format json shows under usage.cache_creation whether writes used ephemeral_5m_input_tokens or ephemeral_1h_input_tokens.
A healthy long session looks like this: cache_read_input_tokens grows turn over turn, and cache_creation_input_tokens is roughly the previous output plus the new input. If every turn writes close to the full conversation size, something is rewriting the prefix.
Worked cost example: hit rate changes the bill several times over
Assume a 40-turn coding session where each turn sends 100K tokens of context, 4K of it new, and produces 2K output tokens. Claude prices are official and QCode bills Anthropic models at ×1.0; the DeepSeek figure below is the vendor's list price, for comparison. TTL is 5 minutes.
| Scenario | Sonnet 5.5 ($2 in / $0.10 read / $2.50 write / $10 out) | Opus 5.5 ($4 / $0.20 / $5 / $20) |
|---|---|---|
| Healthy cache: 96K read + 4K written per turn | ~$0.040 per turn, ~$1.58 per session | ~$0.079 per turn, ~$3.17 per session |
| Prefix rewritten every turn: 100K written | ~$0.27 per turn, ~$10.80 per session | ~$0.54 per turn, ~$21.60 per session |
| Difference | ~6.8x | ~6.8x |
The math for healthy Sonnet 5.5: 96K × $0.10/M + 4K × $2.50/M + 2K × $10/M ≈ $0.0096 + $0.010 + $0.020 = $0.0396. The same session on DeepSeek deepseek-flash at peak prices costs about $0.17 healthy and about $1.30 with every turn missing, again close to 8x. Real sessions grow turn by turn, so treat this as a model, but the conclusion holds: cache discipline often moves your bill more than switching to a cheaper model.
FAQ
Should I use Claude's 5-minute or 1-hour cache?
Look at the gap between the start of requests that share a prefix. With gaps under 5 minutes, stay on the default: writes cost only 25% extra and every hit refreshes the timer. The 1-hour TTL pays off only if you regularly pause for 10 to 60 minutes, because its writes cost 2x input.
Is OpenAI prompt caching still free?
Models before GPT-5.6 have no write surcharge. From GPT-5.6, writes cost 1.25x for the default 30-minute lifetime and 2.5x for 12 hours in the Responses API. Reads cost 10% of input, or 5% on GPT-6.2 Sol and Astra (OpenAI guide, 2026-11-25).
Do I have to turn on DeepSeek context caching?
No. DeepSeek enables disk caching for all users by default with no code changes. Check prompt_cache_hit_tokens and prompt_cache_miss_tokens; DeepSeek says caching is best effort and doesn't guarantee a 100% hit rate.
Why is my cache read always zero?
Usually something in the prefix changes: the current time in the system prompt, unsorted tool JSON, a tool set that varies per turn, or a prefix shorter than the minimum. Diff the full JSON of two consecutive requests with cache_control stripped; the first difference is where the cache breaks.
Does editing CLAUDE.md break the cache in Claude Code?
A mid-session edit neither breaks the cache nor takes effect until /clear, /compact or a restart. The real cost is content that changes every time, which makes each new session start with a cold cache, and hurts every turn in tools that re-read instruction files.
Does caching still work when I call Claude through QCode?
QCode lists cache-read prices for Claude on /models (for example $0.10 per million on Sonnet 5.5), and Claude Code reports hits in cache_read_input_tokens and /usage. Gateways can differ in which cache headers they forward, so if you rely on explicit cache_control markers or the 1-hour TTL through QCode, test it with a small request first.
Does /compact make things more expensive?
Compaction sends one summarization request with the full history. With a warm cache most of it is billed as cache reads; after the TTL has passed it is all billed uncached. Compact between tasks while the cache is still warm.
Which plan fits
The higher your hit rate, the more sessions the same daily quota covers. In the example above, a healthy 40-turn Opus 5.5 session costs about $3.17, against about $21.60 when the prefix keeps changing. QCode's Daily 35 plan gives $35 of usage per day (reset at 00:00 Beijing time; Anthropic and OpenAI models are billed at official price ×1.0), which suits developers who run Claude Code or Codex for long stretches every day. Plans start from $9 per month; see /pricing for tiers and /models for per-model and cache prices.