Skip to content

"cached_tokens": 0

Cause: The start of your prompt changed between calls or was too short to cache, so the provider found no matching cached prefix and billed the full input again.

Copy-paste fix

Put everything that stays the same first and byte-identical (instructions, tools, examples), put the changing parts last, and log the cache fields on every call.

STATIC = open("system_prompt.txt").read()  # same bytes every call: no dates or IDs

resp = client.chat.completions.create(
    model=MODEL,
    messages=[{"role": "system", "content": STATIC},  # stable prefix first
              *history,
              {"role": "user", "content": question}],  # changing part last
)
u = resp.usage
details = getattr(u, "prompt_tokens_details", None)
cached = (details.cached_tokens or 0) if details else 0
log.info("prompt_tokens=%s cached_tokens=%s", u.prompt_tokens, cached)

# Claude API: resp.usage.cache_read_input_tokens and
#             resp.usage.cache_creation_input_tokens

Compare model input prices on TokenCenter to see what a cache miss costs you

What the error means

OpenAI reports cache hits in usage.prompt_tokens_details.cached_tokens. Its docs say reuse needs the whole prefix to match and the prefix must meet the model's minimum cacheable length.

On the Claude API, check cache_read_input_tokens and cache_creation_input_tokens in usage; Anthropic's docs say that if both are 0 the prompt was not cached, and that changing tool definitions invalidates the whole cache.

Checklist

  1. Remove timestamps, request IDs and user names from the start of the prompt.
  2. Keep tool definitions and their order fixed between calls.
  3. Check that your prefix is above the model's minimum cacheable length.
  4. Track the cache-hit share per feature and alert when it drops.
  5. Remember caches expire; quiet periods will show misses.

Related

Sources

All guides · All errors