Build · intermediate
Measure whether a 12-hour prompt cache is worth its write cost
Use Responses API usage data to compare 30-minute and 12-hour prompt caching on a real workload before changing the default.
If your agent returns to the same instructions after lunch, a 30-minute prompt cache may have expired. OpenAI added a 12-hour option to the Responses API on October 20, 2026. It can keep a reusable prefix eligible for cache reads across longer gaps, but the write costs twice the default rate. That is a workload question, not a setting to turn on everywhere.
This tutorial gives you a short way to answer it: compare the input-token cost from a real workload under both lifetimes, then keep the cheaper setting only if it still fits your retention and latency requirements.
Know what you are paying for
The prompt caching guide
describes caching as reuse of a matching prompt prefix. For supported Responses
API models from GPT-5.6 onward, "30m" is the default lifetime, with a cache
write billed at 1.25 times ordinary input. "12h" is a minimum lifetime with
a write billed at 2.5 times ordinary input. Reads have the same rate for both
lifetimes: 0.1 times ordinary input on most models and 0.05 times on GPT-6.1
Sol. Those write rates are total rates for written tokens, not extra fees added
on top of ordinary input.
For example, at the documented GPT-6.1 Sol standard rate, one million tokens written for 30 minutes costs $2.50 and a million cached tokens read costs $0.10. A 12-hour write costs $5.00. The current pricing table is model- and service-tier-specific, so use your actual rates when estimating.
Inspect a representative workload
Pick a flow that has a stable prefix, such as shared instructions and tool definitions, followed by changing user or tool content. Keep a sample of request timing and the Responses API usage fields for that flow. For each request, record:
input_tokens_details.cached_tokensinput_tokens_details.cache_write_tokens_details.ttl_30m_tokensinput_tokens_details.cache_write_tokens_details.ttl_12h_tokens
The usage reference
explains these fields. Ordinary input tokens are total input tokens minus
cached reads and cache writes. Do not add aggregate cache_write_tokens on
top of the two lifetime-specific write counts; the detailed counts already sum
to that total.
Now replay the same request sequence in a representative test environment,
first with "30m", then with "12h". Keep model, input order, tool
definitions, processing tier, and request schedule fixed. Calculate input cost
for each replay from your model’s rates:
ordinary input × input rate
+ cached reads × cached-input rate
+ 30-minute writes × 1.25 × input rate
+ 12-hour writes × 2.5 × input rate
For a replay that uses only one lifetime, set that lifetime in the request:
const response = await client.responses.create({
model: "gpt-6.1-sol",
prompt_cache_options: { ttl: "12h" },
input: "Your representative request goes here",
});
console.log(response.usage.input_tokens_details);
This example sets the request-wide implicit cache lifetime. If you use explicit breakpoints, you can set a different lifetime for each stable or changing prefix. The guide documents that pattern and the matching request fields.
Compare at the gap that matters
Use the delay between repeated requests from your own workload. If the common gap is ten minutes, a 12-hour write premium buys little. If requests often return after several hours, the default cache may need to be written again; the higher 12-hour write can then be offset by more reads at the cheaper cached input rate.
For a quick estimate, write down W, the number of full-prefix writes, and
R, the number of full-prefix cache reads. In units of the ordinary input
price, approximate the cost as:
30 minutes: 1.25 × W + read_rate × R
12 hours: 2.50 × W + read_rate × R
This rough comparison assumes full-prefix matches and ignores partial writes, uncached suffixes, model output, and any nonstandard service tier. Use the usage breakdown above for the actual calculation. A minimum lifetime does not guarantee that a cache entry is reused on every request, and it is not a data deletion deadline.
Make the rollout observable
Keep the current setting as a baseline and compare cost per completed task, not just cost per request. Also watch cache reads, cache writes, task quality, and latency. Do not prewarm a cache unless your measured request pattern shows the extra write pays back; a write made only to prepare for a possible read is still billed.
The takeaway: try "12h" on stable context that your workload revisits hours
later, then keep it only when measured reuse pays for the more expensive write.
Sources
- OpenAI API Changelog, 12-hour prompt caching, October 20, 2026.
- OpenAI API Docs, Prompt caching, including usage fields and cache lifetimes.
- OpenAI API Docs, Pricing, current rates by model and service tier.