New Tool Radar

AI Concepts

How to Reuse a Cached Prompt Prefix With Prompt Caching

Put the stable content first, mark the breakpoint at the end of the stable prefix, and check the next request reads from cache.

Schematic with three steps: Put stable content first; Mark the breakpoint at the end of; Send a second request and check for.

Prompt caching lets the API resume from specific prefixes in your prompts, which significantly reduces processing time and costs for repetitive tasks or prompts with consistent elements. The cache has a 5-minute lifetime by default, refreshed at no extra cost each time the cached content is used. The rule that matters is where you put the breakpoint: place cache_control on the last block whose prefix is identical across the requests you want to share a cache, not on a block that changes every request.

What you need

  • A prompt with a large stable prefix, for example system instructions and background
  • The API parameter for cache control on your provider
  • Two requests that share the same prefix to test with

Step 1: Put stable content first

Order the prompt so the content that never changes comes first: tools, then system instructions, then stable context. Put the per-request content, such as a timestamp or the user message, after the breakpoint.

Step 2: Mark the breakpoint at the end of the stable prefix

Place the cache control marker on the last block that stays the same across requests. A breakpoint on a block that changes every request writes a new cache entry each time and never gets a read.

Step 3: Send a second request and check for a cache hit

Send the same prefix again with a new question and check the usage report shows cached tokens read. If no cached tokens appear, the prefix changed somewhere before the breakpoint.

Worked example: synthetic prompt caching check

The table below is a synthetic example. It shows the expected result of reusing a cached prefix; no request was sent.

Request Prefix Cache result
Request 1 System plus background Cache written
Request 2, same prefix System plus background Cache hit – cheap read
Request 3, timestamp in prefix Prefix changed No hit – fresh write
Fix Breakpoint before timestamp Cache hit again

Expected result (UNTESTED). the second request reads from cache, the request with a changed prefix does not, and moving the breakpoint before the timestamp restores the hit. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • The default cache lifetime is 5 minutes, and a 1-hour cache duration is available at additional cost.
  • The minimum cacheable prompt length is 1,024 tokens on some models and 512 on others; check your model.
  • Cache writes cost more than base input tokens, while cache reads cost a fraction, so caching only pays off when the prefix is reused.
  • Changing any block at or before the breakpoint produces a different hash, so no earlier entry matches.

Sources

See all