AI Coding Agents
Cache-Friendly Prompt Structure: Order Your Prompt for Reuse
A cache-friendly prompt puts reusable content early in the model’s rendered context and keeps timestamps, request identifiers, and changing retrieval results after the reusable boundary. On the Claude API, the service orders tools before system content and messages. Preserve earlier turns when extending a conversation, and verify that your model can read a warm entry at the boundary you chose.
A cache-friendly prompt puts reusable content early in the model’s rendered context and keeps timestamps, request identifiers, and changing retrieval results after the reusable boundary. As of November 30, 2026, the Claude API orders tools before system content and messages. Preserve earlier turns when extending a conversation, and verify that your model can read a warm entry at the boundary you chose.
The rule that drives the layout
A cache entry covers the rendered prompt through a specific boundary. The Claude API builds that prefix in the order tools, system, and messages. OpenAI also requires the rendered prefix to match through the lookup boundary. A change can leave an earlier entry reusable while later tokens become cache writes or ordinary input, depending on the request’s cache settings. Put content that changes between requests after the stable block you want to reuse. See what prompt caching is for the billing categories.
Both providers say this in their own guidance. Anthropic’s prompt caching page instructs you to place static content, meaning tool definitions, system instructions, context, and examples, at the beginning of the prompt, and to place the cache_control breakpoint on the last block that stays identical across requests. OpenAI’s guide says to put stable developer instructions and shared reference material first, and to place timestamps, user-specific content, or other dynamic content at the end rather than the beginning, or move it into later conversation messages. OpenRouter’s best-practices doc gives the same advice to gateway users: keep the initial portion of message arrays consistent and push variations toward the end.
The stable-first layout, before and after
The table shows two layouts for a support-chatbot prompt. The second moves changing values later, freezes the tool list, and appends new turns without rewriting earlier ones.
| Order | Cache-unfriendly layout | Cache-friendly layout |
|---|---|---|
| 1 | System prompt with the current time, the user’s name, and today’s usage numbers | Tool definitions, sorted and frozen for the session |
| 2 | Tool definitions, rebuilt per request in collection order | System prompt: persona, policy, output rules (static text only) |
| 3 | Retrieved chunks for this specific question | Shared reference material: product docs, glossary, tenant or project facts |
| 4 | Conversation history, re-summarized in place each turn | Session context that changes rarely, if ever |
| 5 | The new user message | Append-only conversation history |
| 6 | Volatile tail: this turn’s retrieved chunks, timestamp, the new user message |
Move per-request values into the tail. Keep tool definitions, order, and schemas stable because changes to tools in the rendered prefix can prevent reuse of later blocks. When a user sends another message, append it after the earlier turns.
Keep per-request values after the cached prefix
In Anthropic’s example, blocks 1 through 5 contain static system context and block 6 contains a per-request timestamp and the user message. With the only cache_control on block 6, the prefix hash changes on each request and the lookback finds no earlier entry. Every eligible request writes the prefix again. Move the breakpoint to block 5 so subsequent requests can read that entry while it stays warm and meets the model’s minimum cacheable length. Anthropic’s automatic caching would mark the last cacheable block, which is block 6 in this example.
A few per-request values deserve special attention:
- Timestamps and request IDs belong in a later message if the model needs them. OpenAI’s archived cookbook suggests request metadata for a timestamp used only for logging or lookup.
- Put reusable instructions first. In the OpenAI Responses API on GPT-6.2 and later, top-level
instructionsfollowinput, beyond eligible cache breakpoints. The API bills them as ordinary input even on a cache hit. Put reusable instructions in a leadingdevelopermessage. Useinstructionsfor guidance that applies to the current request, such as a one-off formatting instruction on the final turn. - Retrieved chunks are per-request by nature and belong in the tail unless your retrieval is stable. See the next section.
Where retrieved documents belong
RAG has two distinct caching regimes, and they want different layouts:
Fixed or slowly changing corpus. Anthropic’s documentation explicitly supports embedding entire long-form documents in the prompt and letting users question them, and notes that the top-level document content blocks serving as citation sources can themselves be cached. Its own worked example places the knowledge base documents inside the system array as a # Knowledge Base Context block. If your product answers questions about the same manual all day, that material is part of your stable prefix and belongs before the breakpoint.
Per-query retrieval. Put changing chunks after the reusable boundary, adjacent to the user turn they serve. Keep their order and serialization stable so the same retrieval result can produce the same rendered content. See deterministic prompt serialization. If a small set of documents recurs across requests, test it as a separately marked prefix. Track whether repeat cache reads offset the writes and the input added to questions that would have retrieved fewer documents.
Replay the same set of questions against both layouts. Keep the model and cache lifetime fixed. Include the initial write and any writes after expiry in the comparison. On the Claude API, add cache reads, cache writes, and ordinary input at their respective rates for each request. A higher read count can still come with a higher input bill if every question now carries a manual that most questions would have skipped.
Append-only history is a cost decision
OpenAI recommends preserving earlier messages and tool results so later turns can reuse the shared prefix. The Claude API writes cache entries at breakpoints and looks backward for earlier writes. A final breakpoint can find the previous write within the 20-position lookback, counting the current breakpoint as the first position. A consecutive run of tool_use blocks or a run of tool_result blocks each counts as one position on the Claude API. Keep a separate breakpoint on the fixed prefix if appended blocks can push the previous write outside the window.
Keep earlier turns unchanged while extending a conversation. If you compact, expect reuse to drop from the first changed position onward. An earlier breakpoint can still serve unchanged content, and Claude Code can read the original warm prefix on the summarization request. See context compaction and prompt cache cost. When a conversation forks, preserve the inherited history and append the branch’s task as a new message. See cache-safe subagent forking.
Multi-tenant structure: where the cache boundary sits
Two providers draw the sharing boundary differently, and the choice affects how you partition prompts:
- Anthropic. Different organizations have separate caches, even with identical prompts. The Claude API, Claude Platform on AWS, and Microsoft Foundry also isolate caches by workspace. Claude Models on Google Cloud (Google managed) and Amazon Bedrock isolate them by organization only. With a separate cache boundary per tenant, each tenant has to warm its own entry for the shared global prefix.
- OpenAI: on models before GPT-5.6,
prompt_cache_keyseparates cache reuse between groups of requests and steers routing, so requests that share a reusable prefix should carry the same stable key, and busy groups should target roughly 15 requests per minute per key with higher volume partitioned across keys by a deterministic mapping. On GPT-5.6 and later, routing is automatic; the key’s documented role is separate cache accounting per customer or user, for examplesupport:customer_123versussupport:customer_456, even when their requests contain the same prefix.
On Anthropic, use an organization boundary or, where supported, a workspace boundary when tenants need separate caches. On OpenAI, organization and processing region bound reuse, and a request still needs to reach a machine holding a matching live entry. Before GPT-5.6, keep a stable key per routing group. On GPT-5.6 and later, use distinct customer keys for separate cache accounting or to reduce cross-customer cache-hit probing.
Worked example: the price of a timestamp in the wrong place
Assume a 20-turn chatbot session on claude-sonnet-5-5 with a 30,000-token shared prefix covering tools, system instructions, and documentation. At committed rates, ordinary input costs $2 per million tokens, a five-minute write costs $2.50, and a cache read costs $0.10. Assume the shared prefix stays eligible and each follow-up starts after the previous response begins and while the five-minute entry is still warm. In the timestamp layout, the timestamp changes every turn and there is no earlier cached breakpoint. Each layout sends the same tail for a given turn, so the estimates cover only the shared-prefix region.
| Layout | Shared-prefix billing across 20 turns | Estimate |
|---|---|---|
| Stable prefix, breakpoint after it | 1 write + 19 reads: 30,000 x $2.5/1M + 19 x 30,000 x $0.1/1M | $0.132 |
| Timestamp inside the marked prefix | 20 writes, 0 reads: 20 x 30,000 x $2.5/1M | $1.50 |
| Same prompt, caching not applied at all | 20 uncached sends: 20 x 30,000 x $2/1M | $1.20 |
At committed rates, the estimated $0.132 stable-prefix cost is 11% of the $1.20 uncached baseline and 8.8% of the $1.50 timestamp layout. Each five-minute write costs 1.25 times ordinary input.
What to do next
- Order content by the scope that reuses it. Start with global material, then tenant material shared across sessions, then session context and preserved history. Append each turn’s retrieved chunks and current user message after the reusable boundary.
- On the Claude API, mark the final reusable block with
cache_control. In the OpenAI Responses API on GPT-5.6 and later, setprompt_cache_options.modetoexplicitfor explicit-only caching. On a reusableinput_textblock, use the fieldprompt_cache_breakpointwith the value{ "mode": "explicit" }. Keep the changing tail after the marker. See the Anthropic breakpoints guide. - Freeze tools, keep history append-only, and put per-request instructions after the reusable boundary.
- For multi-tenant apps, choose
prompt_cache_keyor workspace boundaries deliberately per the section above; see the prompt_cache_key routing post. - Compare intended reusable tokens with
usage.cache_read_input_tokenson the Claude API orusage.input_tokens_details.cached_tokenson OpenAI Responses. Track writes and ordinary input as well, especially when the retrieval tail is large. See the provider pricing index.
Sources
- Prompt caching (Anthropic, accessed 2026-11-30)
- Prompt caching (OpenAI, accessed 2026-11-30)
- Prompt Caching 201 (OpenAI, accessed 2026-11-30)
- Tool use with prompt caching (Anthropic, accessed 2026-11-30)
- Prompt caching best practices (OpenRouter, accessed 2026-11-30)
How this note was produced
Drafted with AI assistance from committed ByteCosts pricing snapshots and deterministic cost formulas. Figures are grounded in the linked sources (last updated 2026-11-30). Read our editorial & automation policy, inspect the methodology, or report a discrepancy.
Cache-Friendly Prompt Structure: Order Your Prompt for Reuse. ByteCosts. Updated 2026-11-30. https://bytecosts.com/blog/cache-friendly-prompt-structure/