AIF-40Recommended
Put the stable part of the prompt first
Order every prompt from most stable to least stable, with instructions, tool definitions and shared documents first and user input last. Turn on the provider's prompt caching, and measure the hit rate.
How to do it
Providers can reuse the work on a prompt prefix they have seen recently, which lowers cost and latency on long prompts. A cache only matches an identical prefix, so a date or user name near the top breaks it. Sort keys when you serialise JSON into a prompt, and keep the tool list in a fixed order. OpenAI caches prompts of 1,024 tokens or more by default on GPT-5.6 and later. Anthropic needs cache_control, either on the request or on the last block of the shared part. Put the marker at the end of the shared part when each request ends in unique text, such as retrieved chunks. Anthropic, and OpenAI from GPT-5.6, bill a cache write at 1.25 times the input price, so a prefix used only once costs more. Read the cached and written token counts from the usage fields and chart them per feature. OpenAI reports input_tokens_details.cached_tokens and input_tokens_details.cache_write_tokens. Anthropic reports cache_read_input_tokens and cache_creation_input_tokens. Count the saving in the spend per call from Price every model call and cap the spend.
Examples
TypeScript example
const response = await anthropic.messages.create({
model: config.model,
max_tokens: 4096,
system: [{ type: 'text', text: STATIC_INSTRUCTIONS + '\n\n' + productCatalogue, cache_control: { type: 'ephemeral' } }],
messages: [{ role: 'user', content: `Today is ${today}. Customer question:\n${question}` }],
});
const { input_tokens, cache_read_input_tokens, cache_creation_input_tokens } = response.usage;
const totalInput = input_tokens + (cache_read_input_tokens ?? 0) + (cache_creation_input_tokens ?? 0);
metrics.gauge('ai.cached_input_share', (cache_read_input_tokens ?? 0) / totalInput, { feature });
Python example
response = openai_client.responses.create(
model=settings.model,
instructions=STATIC_INSTRUCTIONS + "\n\n" + product_catalogue,
input=f"Today is {today}. Customer question:\n{question}",
)
usage = response.usage
metrics.gauge("ai.cached_input_share", usage.input_tokens_details.cached_tokens / usage.input_tokens, tags={"feature": feature})
How to check
The share of input tokens served from cache is on the dashboard per feature. For a feature whose shared prefix is over 4,096 tokens, it is above half during busy hours. A test renders the prompt twice for two users and finds the same bytes up to the first user-specific text.
Sources
Related rules
- AIF-42 Price every model call and cap the spend in AI features
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/ai-features/latency-cost-and-fallbacks/prompt-caching">Agent Wranglers AI features playbook</a>. In code you write, add a comment next to it with the same link.