Prompt caching
On this page
Prompt caching reuses a matching prompt prefix across requests so repeat system instructions, tool schemas and reference docs are billed at reduced cache-read rates instead of fresh input. Put static context first, append changing user turns last, and verify cache hits in Activity.
Organize your prompt
- Stable prefix
- ReuseKeep shared instructions, tool definitions, and reference material at the beginning in a consistent order.
- Changing suffix
- AppendPut the current question and changing context after the reusable prefix.
- Measure
- Cache readsInspect reported usage and Activity. A successful response does not necessarily mean there was a cache hit.
Changing models, routes, or prompt prefixes can affect reuse. Do not assume cache state is shared across every provider or that a short test prompt meets the cache threshold.
Anthropic cache breakpoint
For a model supporting Anthropic prompt caching, a cache_control marker identifies the end of a reusable content block. This Messages API example is a request-shape example; replace the short reference text with your actual reusable material. It does not guarantee a cache hit.
import os
from anthropic import Anthropic
client = Anthropic(
base_url="https://api.straitly.ai",
api_key=os.environ["STRAITLY_API_KEY"],
)
response = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=256,
system=[{
"type": "text",
"text": "Answer using this reference: <your reusable reference>",
"cache_control": {"type": "ephemeral"},
}],
messages=[{"role": "user", "content": "Summarize the reference."}],
)
print(response.usage)
for block in response.content:
if block.type == "text":
print(block.text)Use the provider's current documentation for minimum token thresholds, breakpoint limits, and supported TTLs. OpenAI-format requests do not use this exact Anthropic content-block contract.
Read cache usage
The first eligible request may write cache entries; a later matching request may read them. Cache reads and cache writes are different billing buckets.
Inspect cache-read, cache-write, and fresh-input tokens in Activity. When using the Anthropic SDK, inspect the usage fields returned by your selected model; do not infer hits from latency alone.
Cache costs and misses
- Cache writes can cost more than fresh input, while cache reads can cost less. Check all token rates, not only input and output.
- 5-minute and 1-hour cache writes can have different prices. Use the applicable rate when estimating a workload.
- On a miss, check model support, prompt size, expiry, and exact prefix changes. Caching is not a general persistence guarantee.
- Do not use caching as a data-retention promise. Ask support about the handling requirements of sensitive workloads.
Why did my cache miss?
A cache hit needs the same model, a long enough prompt, and a prefix that matches the earlier request byte for byte while the cache entry is still alive. If cache_read_input_tokens stays at 0 while cache_creation_input_tokens keeps growing, one of these is usually the cause:
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.straitly.ai/v1",
api_key=os.environ["STRAITLY_API_KEY"],
)
response = client.chat.completions.create(
model="claude-sonnet-5-5",
prompt_cache_key="conversation-123",
messages=[
{"role": "system", "content": "<your long, unchanging instructions>"},
{"role": "user", "content": "The changing question goes last."},
],
)
print(response.usage)- Something early in the prompt changes on every call: a timestamp, a request id, a random order of tools, or a user name in the system prompt. Move anything that changes after the reusable part.
- The prompt is shorter than the model's minimum cacheable length. Short test prompts often never cache. Check the provider's threshold for that model.
- The cache entry expired. A 5-minute write is gone if the next matching call comes later than that. Use a 1-hour write for slower traffic, which is priced differently.
- You switched models. Each model keeps its own cache, so a fallback to a second model starts cold.
- On OpenAI-format requests, send the same prompt_cache_key on every turn of one conversation. It helps related requests reach a warm cache. Your own value is passed through as you send it.
- The request moved to another source for the same model after an error. Caches are not shared between sources, so the next call may write the cache again.
Read cache_read_input_tokens and cache_creation_input_tokens in usage, or the cache buckets in Activity, to confirm a hit. Faster replies alone do not prove one.
Common questions
How does prompt caching work on Straitly?
Prompt caching reuses a matching prompt prefix across requests to cut repeat input token cost. Put static system instructions, tool schemas and reference documents at the start of the prompt and append changing user turns at the end.
How do I enable prompt caching for Claude vs OpenAI models?
On Anthropic Messages requests, add cache_control: { type: "ephemeral" } to the end of the reusable content block. OpenAI and other providers use their own automatic or model-specific caching rules, and Straitly passes those cache parameters through unchanged.
How do I verify a prompt cache hit and its cost?
Inspect the returned usage fields or open Activity in the console to see fresh input, cache-read, 5-minute cache-write and 1-hour cache-write token buckets separately. Do not infer cache hits from latency alone.