{"slug": "claude-code-context-deep-dive-3-why-prompt-caching-shapes-the-agent", "title": "Claude Code Context Deep Dive (3): Why Prompt Caching Shapes the Agent", "summary": "A developer's research note on Claude Code v2.1.220 analyzes how the Claude API's prompt caching, which orders prefixes as tools → system → messages, shapes agent request layout and cost. The note describes a four-breakpoint allocation in that version and an internal marker called SYSTEM_PROMPT_DYNAMIC_BOUNDARY, while cautioning that current Claude Code docs describe caching in three conceptual layers rather than fixed cache_control positions. The practical guidance is to place stable content early, append rather than edit history, and mark breakpoints only where content is actually reused.", "body_md": "**Editor's note:** This is an AI-assisted English adaptation of a research note on Claude Code v2.1.220. The four-breakpoint layout and named internals come from that version-specific note, not from a current public guarantee. Where the current Claude Code docs differ, this article calls out the difference.\n\nIn [Part 2](https://dev.to/_94be737e156beb4d74df2/claude-code-context-deep-dive-2-three-rules-for-the-message-array-15a6), we looked at the message array an agent sends on each turn. Here’s why its layout matters: if a request changes near the top, every prefix extending past that point changes too. If the agent only appends a new exchange, most of the previous request can still be reused.\n\nThat distinction explains why a skill can arrive as a new message, why a fork can start warm, and why a change to the tool list can have a much larger cost than a new tool result.\n\nThe [Claude API prompt caching guide](https://platform.claude.com/docs/en/build-with-claude/prompt-caching) describes a cumulative prefix ordered as `tools → system → messages`. A cache write at a marked block stores the prefix **through** that block. The marker is not the start of a new independent segment.\n\nSuppose you mark two positions: one at the end of your tool definitions, another after a large project brief. If the brief changes, an existing entry ending at the tool definitions can still match, assuming it hasn’t expired and the other cache conditions are met. If a tool definition changes, neither prefix matches. There’s no separate cache entry for the brief alone.\n\nAppending new history is different from editing old history. Suppose you cached a request through block 10, then appended five blocks and moved the breakpoint to block 15. The API can look back and reuse the write at block 10. If the previous write falls outside the API’s 20-position lookback, it won’t be found from that breakpoint. The lookback searches for **existing writes**; it doesn’t create entries at unmarked boundaries.\n\nThe practical rules: put stable content early, append new information where possible, and mark a reusable boundary when a section will change independently.\n\nThe API allows up to four breakpoints. The v2.1.220 research note described this particular allocation:\n\n| Position in the note | Prefix preserved when later content changes | \n|---|---|\n| End of the tools section | Tool definitions | \n| End of the stable system section | Tools plus core system instructions | \n| End of a configuration-related system section | The preceding prefix plus that configuration | \n| Near the end of the submitted conversation | The reusable history sent so far | \n\nTreat this as a historical model, not a recipe Claude Code follows on every request today. The current [Claude Code caching docs](https://code.claude.com/docs/en/prompt-caching) explain the product in three conceptual layers: system prompt and tools, project context, and conversation. Those layers describe what changes together; they don’t specify four fixed `cache_control` positions.\n\nThis also explains why four is a limit, not a target. One breakpoint can suffice for a large common prefix. Add another when it protects something you actually reuse, not simply because there’s an unused slot.\n\nThe older note described an internal marker named `SYSTEM_PROMPT_DYNAMIC_BOUNDARY`, used to distinguish reusable system content from content that might change between requests. The current public docs don’t guarantee that marker, its placement, or the serialization that goes with it.\n\nThe implementation idea is useful without the marker. If you’re building your own agent, represent core instructions and per-request context as separate blocks. Place your breakpoint based on which blocks remain stable, rather than guessing from line counts or assuming everything under the heading “system prompt” has the same lifetime.\n\nIt’s tempting to treat `CLAUDE.md` as product-level system text. But those rules belong to the user or project, and they can change independently of the core agent. The old note described moving them into message context with an internal reminder wrapper. The wrapper is not a public contract.\n\nThe current docs say that project-root and user-level `CLAUDE.md` files are read at the start and held in memory. Editing one mid-session doesn’t immediately replace the old version; reload via `/clear`, `/compact`, or a restart. Directory-scoped rules can load later. The key is that editing a file on disk and rewriting text that was already sent are two different events.\n\nA skill catalog is useful, but repeatedly rebuilding a catalog early in the request can make later prefixes harder to reuse. The historical note described a set of already-announced skill names and a policy of sending only additions. Its post-compaction behavior and token estimates weren’t independently verified for the current release.\n\nWhat is documented now: invoking a skill or command appends its instructions to the conversation. It doesn’t rewrite the prior turns. A skill that switches models is an important exception: the new model has its own cache.\n\nThe old note cited a fixed placeholder in a fork-related path. It would be misleading to generalize that into “all inherited tool results are always replaced”: current docs say a fork can reuse the parent’s cache by inheriting the same system prompt, tools, and history. A regular subagent with a different prompt and tool set starts with a different prefix.\n\nFor a fan-out, the transferable idea is to start with a shared prefix and put each worker’s instructions after it. Don’t replace evidence the worker needs with placeholders just to save tokens. Also, equal prefixes don’t guarantee a read: a request first has to write the entry, and the entry must still be available.\n\nThe note reported an older environment snapshot that didn’t follow every shell directory change. It interpreted the behavior as a cache trade-off. The current docs confirm that startup environment and repository details can affect cache sharing, but not the note’s exact refresh rules or its claims about every tool’s working directory.\n\nThe useful distinction is between context that helps the model orient itself and state that must be read at execution time. A startup snapshot can describe the project. A file or shell operation should use the current path, not infer it from an old prompt.\n\nThe old note described a separate date-change reminder. Treat the exact notice and trigger as historical. The design lesson is simple: if the date matters, supply the current value where it’s needed. Replacing a date near the start of a large shared prefix breaks matches for everything after it; appending an update can leave the old prefix intact.\n\nAn MCP server that finishes connecting later may bring new instructions. The historical note described adding instructions as a delta rather than rewriting the system section. Appending new text can preserve earlier cache entries. But if the change adds, removes, or modifies a definition in the request’s early tool list, the cache consequences are different.\n\nThe current Claude Code docs distinguish cases involving deferred tool loading, reconnecting with the same definitions, and deliberately removing a tool. Don’t infer a cache hit just because the change originated in MCP. Check whether the actual request prefix changed.\n\n“Static” has two meanings here. A project rule may be static in purpose but change in the request when you switch repositories. A date is dynamic in purpose, yet you can represent it with an append-only update. Cache behavior follows what was actually sent, not the label you give it.\n\nThe benefit depends on reuse. A single cold request pays for a write without getting a later read. Two requests can already save on the shared portion if the second hits and the read discount outweighs the write premium. Expiry, short prefixes below the model’s caching threshold, and early content that changes on every call all reduce the upside. Retrieval-augmented generation isn’t inherently a poor fit: a varying retrieval result can go after stable instructions or examples.\n\nNor is caching limited to repeated calls by the same end user. The API documents organization isolation and, on some providers, workspace isolation. Sharing within the applicable scope still depends on a matching prefix and other cache conditions. Different providers and APIs make different guarantees.\n\nThe quickest way to validate a design is to compare `cache_creation_input_tokens` and `cache_read_input_tokens` across representative turns. If one change repeatedly turns a read into a write, find where that change enters the request before adding more breakpoints.\n\nNext: compaction, where preserving old history verbatim is no longer the main objective.", "url": "https://wpnews.pro/news/claude-code-context-deep-dive-3-why-prompt-caching-shapes-the-agent", "canonical_source": "https://dev.to/_94be737e156beb4d74df2/claude-code-context-deep-dive-3-why-prompt-caching-shapes-the-agent-5ha7", "published_at": "2026-11-25 02:04:21+00:00", "updated_at": "2026-11-25 02:14:57.747561+00:00", "lang": "en", "topics": ["large-language-models", "ai-agents", "ai-tools", "developer-tools"], "entities": ["Claude Code", "Claude API", "Anthropic", "SYSTEM_PROMPT_DYNAMIC_BOUNDARY"], "also_reported_by": [], "takeaway": "Because Claude's prompt cache stores cumulative prefixes ordered tools → system → messages, editing a tool definition invalidates every cached prefix while appending new history can still reuse an earlier write within the 20-position lookback.", "recommendation": {"advice": "Order agent requests with stable tool and system blocks first, append rather than edit history, and add cache breakpoints only where a section changes independently instead of filling all four slots.", "playbook": "llm-cost", "playbook_url": "https://wpnews.pro/playbooks/llm-cost"}, "alternates": {"html": "https://wpnews.pro/news/claude-code-context-deep-dive-3-why-prompt-caching-shapes-the-agent", "markdown": "https://wpnews.pro/news/claude-code-context-deep-dive-3-why-prompt-caching-shapes-the-agent.md", "text": "https://wpnews.pro/news/claude-code-context-deep-dive-3-why-prompt-caching-shapes-the-agent.txt", "jsonld": "https://wpnews.pro/news/claude-code-context-deep-dive-3-why-prompt-caching-shapes-the-agent.jsonld"}}