orq.aiAdvent of RoutingDocs

Door 15 · still sealed

Hands off the wax, elf. This door opens on Dec 15.

-days--hours--min--sec

Rudolph is still polishing this one. Back to the calendar

Door 15 · Dec 15 Response & prompt cache

Don't reread the same letter

Ask twice, run the model once: an exact repeat comes back from the cache, and a long shared prompt bills cheaper on every repeat.

Mid-morning in the letter room. The same bicycle wish keeps arriving word for word, and the reply elves are writing the same answer for the third time before tea.

  1. Sprocket

    Another bicycle letter! The model wrote this exact reply ten minutes ago. Total déjà Yule.

  2. Ledger

    And billed us twice. Worse, every reply rereads the whole naughty-or-nice rulebook first. Same rulebook, paid for on every single letter.

  3. Sprocket

    I'll put a dict in front of the model, keyed on the letter text! How hard can cache invalidation be?

  4. Tinsel

    Both are built in. cache returns the stored reply to an exact repeat, and cache_control makes the rulebook cheaper to reread.

  5. Ledger

    Rereads at a tenth of the price or less, first write at 1.25x. We're ahead from the second letter. I may frame this invoice.

clientYour apporq.aiResponse cacheexact_match · ttlcacheStored replyno model callprovideropenai/gpt-6.2-solreply stored for ttlhitmiss
  1. clientYour app
  2. orq.aiResponse cacheexact_match · ttl
  3. cacheStored replyno model call
    provideropenai/gpt-6.2-solreply stored for ttl
Letter #42 arrives again, word for word, so the router hands back the stored reply, while a new letter goes to the model and its reply is filed for next time.

How it works

Tinsel’s fix is two caches, and they solve different problems. The response cache sits in the AI Router and skips the model. Prompt caching happens at the provider and makes the model cheaper.

  1. Cache key. When a request carries cache, the router builds a key from the model, the input and all parameters.
  2. Hit. If that key is stored and not expired, the router returns the stored response. The model is not called.
  3. Miss. Otherwise the request goes to the model as usual, and the response is stored for ttl seconds (default 3600, minimum 1, maximum 259200, which is 3 days).
  4. Prompt cache. For a long prefix that repeats across different questions, the provider caches the prefix. The model still runs on every request, but the cached part is billed at a reduced rate.

You can use both together.

Try it with curl

Send the same request twice and compare the replies and the times:

for i in 1 2; do
  curl -s -w "\ncall $i: %{http_code} %{time_total}s\n" \
    -X POST https://my.orq.ai/v3/router/responses \
    -H "Authorization: Bearer $ORQ_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-6.2-sol",
      "input": "Write a two-line reply to letter #42",
      "cache": {"type": "exact_match", "ttl": 3600}
    }'
done

The same cache object works on /v3/router/chat/completions. From the OpenAI SDK, pass it as extra_body={"cache": {...}}.

Prompt caching from the OpenAI SDK

Prompt caching on Claude is opt-in. A workspace admin can also turn on Automatic prompt caching under Settings > Organization > General. For Claude requests that set no caching fields, the gateway then adds a top-level cache_control with a 5-minute TTL. That suits multi-turn chats. For one-off questions that share a long system prompt, mark the system prompt yourself:

from openai import OpenAI
import os

client = OpenAI(
    api_key=os.environ["ORQ_API_KEY"],
    base_url="https://my.orq.ai/v3/router",
)

# Long, stable text reused on every call.
# Must exceed the model's minimum token count.
RULEBOOK = "..."

response = client.responses.create(
    model="anthropic/claude-sonnet-5-5",
    input=[
        {"role": "system", "content": [{
            "type": "input_text",
            "text": RULEBOOK,
            "cache_control": {"type": "ephemeral"},
        }]},
        {"role": "user", "content": "Ate Santa's cookies: naughty?"},
    ],
)
print(response.output_text)
print(response.usage)

On a repeat with the same prefix, usage.input_tokens_details.cached_tokens shows how much was read from the cache.

Which cache to use

Response cache Prompt cache
Matches The whole request, exactly A shared prefix
Model called on a hit No Yes
Turn it on cache field or a routing rule cache_control on Claude, or the workspace Automatic prompt caching switch; automatic on OpenAI and Gemini
Good for FAQs, lookups, test loops Long system prompts, documents, tool lists

TTL values from the docs: 86400 for FAQ answers, 3600 for content generation, 1800 for data analysis, 300 for development.

Exact means exact

The response cache has one type, exact_match. It is case sensitive, and it does no semantic matching: a similar request is a miss. If your hit rate is low, check that temperature, max_tokens and the other parameters are identical, and that the ttl is not too short.

Prompt caching costs

On Claude, cache writes cost extra: 1.25x the input price for the default 5-minute TTL and 2x for "ttl": "1h". The docs give reads at 0.1x of the input price or less; check the model catalog for each model (claude-sonnet-5-5 lists $0.10 per million cached tokens against $2 uncached). So a prefix used only once costs more than no caching. Anthropic allows at most four breakpoints per request, and content below a model-specific minimum token count is not cached at all.

Nice list

  • Cache repeated, deterministic requests: FAQ answers, product lookups, and the same fixture your test suite sends on every run.
  • Pick ttl by how fresh the answer must be: the docs suggest 86400 for FAQ answers and 300 while you develop.
  • Put long, stable text first and mark it with cache_control on Claude. Writes cost 1.25x, so with the 5-minute TTL it pays off from the second request.

Naughty list

  • Caching personalised or time-sensitive answers, like a delivery status. Everyone who sends the same request gets the same stored reply until ttl runs out.
  • Changing temperature or max_tokens between calls and expecting a hit. Any parameter change creates a new cache key.
  • Putting a top-level cache_control on unrelated questions. It marks the user message, so no two requests share a prefix and each pays the write premium.
  • Adding cache_control for an OpenAI model and expecting a change. It is ignored: OpenAI caches prompts over 1,024 tokens automatically.

Elf's challenge

Break the cache with one keystroke: run the curl loop above, then send it once more with Write changed to write. Does the lower-case letter get the stored reply?

Stuck? Ask Tinsel for a hint

No. The cache is exact match and case sensitive, so Write and write make different cache keys. That call goes to the model, and its reply is stored under its own key.

Christmas crackerWhy is the response cache the best re-gifter in the workshop?Pull it for the punchline

Same request, same reply, same bow. Nobody has to call the model.