prompt-cache-long-system-prompt-claude-code · EN · 2026-11-03

Designing a Stable System Prompt Prefix for Claude Code Sessions So Cache Hits Survive the Whole Run

Mid-session edits to the system prompt or injected context break prompt caching for Claude Code sessions, forcing full-price recomputation on every turn. This article explains why cache hits depend on byte-stable prefixes, identifies common triggers that mutate the prefix, and shows how to structure instructions so the cacheable portion survives the entire coding session.

Why prompt caching matters in long coding sessions

Claude Code sessions involve many rounds of tool calls, file reads, edits, and reasoning. Each request resends the system prompt and conversation prefix. With prompt caching, the model provider can reuse the cached prefix instead of reprocessing it every turn, cutting latency and cost on repeated context. But caching only works when the prefix is byte-stable: the exact same sequence of tokens must appear at the start of each request.

If anything in that prefix changes—even a single character—the cache is invalidated for that portion and everything after it. For a long session, a single mid-session edit can force the model to reprocess all prior context at full price, erasing the savings you expected.

The cacheable prefix: what actually gets cached

Most providers cache the longest common prefix of your request that was also present in a recent request. For Claude Code, that typically includes:

  • The system prompt (including tool definitions and instructions)
  • The initial conversation turns and any context injected early
  • Large file contents or repository context included at the start

The conversation history grows over time, but the cacheable prefix is the portion that remains unchanged. Once a change occurs at position X, the cached prefix up to X-1 can still be reused; everything from X onward is new.

How mid-session edits kill reuse

Any edit to the system prompt or early context shifts the token sequence. Common triggers include:

  • Appending instructions mid-session: adding “remember to run tests” or “use TypeScript” after the first turn.
  • Dynamic context injection: inserting timestamps, environment variables, or user-specific data into the system prompt.
  • Reordering tools or instructions: even if content is identical, a different order changes the token sequence.
  • Formatting changes: whitespace, line endings, or JSON serialization order can alter bytes.
  • Conditional system prompt sections: toggling optional blocks based on session state.

Each change invalidates the cached prefix from the point of mutation onward. If the change happens near the start, nearly the entire cache is lost.

Designing for byte-stability

To keep cache hits alive for the whole run, treat the system prompt and early context as immutable once the session starts.

Keep the system prompt static

  • Finalize all instructions before the first request.
  • Avoid appending or modifying system-level rules mid-session.
  • If you must adapt behavior, do it through user messages or tool outputs, not by editing the system prompt.

Separate static and dynamic content

  • Put stable content (role, tools, coding standards) in the system prompt.
  • Put dynamic content (current task, file paths, user input) in user messages or tool results after the cacheable prefix.
  • Never inject timestamps, session IDs, or environment details into the system prompt.

Normalize serialization

  • Use deterministic formatting for tool definitions and structured context.
  • Avoid pretty-printing that may vary between runs.
  • Ensure line endings and whitespace are consistent.

Avoid reordering

  • Keep tool definitions and instruction blocks in a fixed order.
  • If you generate the system prompt programmatically, sort keys and lists deterministically.

Front-load large context

  • Include repository context, file contents, or long documentation early in the conversation.
  • Once included, do not modify those early turns; append new context as later messages.

Handling necessary updates

Sometimes you need to update context mid-session—for example, when the user changes a requirement. Instead of editing the system prompt, add the update as a new user or tool message. This preserves the cached prefix up to that point. The cache will extend to include the new message on subsequent turns, so the cost of the update is paid once, not repeatedly.

Monitoring cache effectiveness

Most providers expose cache usage in the response (e.g., cachecreationinputtokens and cachereadinputtokens). Track these metrics per session:

  • High cache_read relative to total input tokens indicates good reuse.
  • Sudden drops in cache_read after a turn suggest a prefix mutation.
  • If cache_creation spikes mid-session, something changed in the prefix.

Use these signals to debug which part of your prompt is unstable.

Practical checklist for Claude Code integrations

  • [ ] System prompt and tool definitions are finalized before the first request.
  • [ ] No dynamic values (time, IDs, env) are embedded in the system prompt.
  • [ ] All instructions are ordered deterministically.
  • [ ] Large context is front-loaded and not edited later.
  • [ ] Mid-session updates are appended as new messages, not injected into the prefix.
  • [ ] Cache metrics are logged and monitored for unexpected invalidation.

Conclusion

Stable caching in Claude Code sessions is mostly a discipline problem: treat the system prompt and early context as append-only, keep serialization deterministic, and route dynamic data through later messages. When the cacheable prefix stays byte-stable, you get consistent cache hits for the entire run—reducing latency and token costs without changing how the model behaves.