Intermediate 1 concept to know first

Prompt Caching

Claude Code re-sends your whole conversation on every turn. Prompt caching lets the API reuse the unchanged start of it, so long sessions stay fast and cheap until something breaks that prefix.

The animation needs JavaScript; every step is described below.

0:15

Know these first

1 concept · basics first
  1. Context

    The context window is everything Claude can see in a session: your messages, the files it reads, tool output and auto-loaded instructions. It fills as you work, and Claude Code compacts it when it gets full.

  2. Prompt Caching · you are here

The model remembers nothing between requests. So every time you send a message, Claude Code re-sends the whole thing: the system prompt, your project context, every earlier message and tool result, then your new message.

Prompt caching is how the API avoids doing that work twice. It reuses what it already processed, bills the re-read at a much lower cached rate, and fully processes only what changed. You don’t switch it on; Claude Code manages it. What’s worth knowing is what breaks it, because a broken cache costs you one slower, more expensive turn while it rebuilds.

It matches the prefix

The API caches by matching the start of each request, the prefix, against what it recently processed. The match is exact: change anything in the prefix and everything after it is recomputed.

So Claude Code orders each request from least to most volatile:

  • System prompt and tool definitions, which rarely change.
  • Project context: CLAUDE.md and auto memory, loaded at session start.
  • The conversation, which grows every turn.

New content goes at the end, so a normal turn reuses everything before it. It’s also why editing CLAUDE.md mid-session does nothing until /clear, /compact or a restart: the copy loaded at the start is part of the prefix.

What breaks it, and what doesn’t

Each model has its own cache, so switching models with /model re-reads the whole conversation uncached. Compacting replaces your history with a summary, so the conversation layer starts over by design. Changing the tool list can invalidate everything, for example connecting an MCP server when its tools aren’t deferred by tool search. And the first session after a Claude Code upgrade usually starts cold.

Plenty is safe: editing files, changing permission mode, invoking skills and commands, and /rewind, which truncates back to a prefix that’s already cached. A subagent builds a cache of its own and leaves yours alone.

The docs’ advice: pick your model and effort level at the top of a session, and save /compact for natural breaks between tasks.

The catch: it expires

A cached prefix expires after a stretch of inactivity, and every hit resets the timer. The API offers a five-minute and a one-hour lifetime. On a Claude subscription within your plan’s usage, the main conversation gets an hour by default; with an API key or a cloud provider it gets five minutes unless you set promptCacheTtl. Step away longer and your first turn back pays full price.

Run /usage to see how you’re doing: its Prompt cache (main) line shows the session’s hit ratio. And The Duel is a 90-second game about why re-sent context is the real cost of agentic work.

Where it sits

Before this

Context
Prompt Caching

Leads to

Nothing yet.

The ideas underneath · Field Guide

Further reading from claude.dev

Posts from Anthropic's developer blog that go deeper. Summaries are ours; the posts are Anthropic's.

  • What a task costs on Opus 5.5

    Prices a typical Claude Code task turn by turn and shows why the cache hit rate moves your input bill more than any other setting.

Sources

  1. How Claude Code uses prompt caching . Claude Code docs: prefix matching, what invalidates the cache, TTLs, /usage

Verified against the official Claude Code docs on October 1, 2026 · next review by January 1, 2027. Claude Code changes quickly; the docs are the source of truth.