---
title: "Caching LLM calls without fooling yourself"
url: https://eldan.ai/writing/cache-llm-calls-honestly
canonical: https://eldan.ai/writing/cache-llm-calls-honestly
locale: en
language: en
published: 2026-11-06
updated: 2026-11-23
author: "Eldan Abdrashim"
cite_as: "Eldan Abdrashim. \"Caching LLM calls without fooling yourself.\" Eldan.ai, 6 November 2026, updated 23 November 2026. https://eldan.ai/writing/cache-llm-calls-honestly."
---

# Caching LLM calls without fooling yourself

> Provider prompt caching and response caching solve different problems. Know which one you use, build cache keys from everything that shapes the answer, let prompt and model changes invalidate them, and keep response caches out of the evals that decide releases.

A team changes a prompt, reruns the eval suite and gets exactly the same score as last week, to the decimal. They take it as good news: no regression. A month later someone notices that the eval harness caches model responses by input text. The new prompt was never called. The suite measured last week’s answers twice.

Caching LLM calls is worth doing. Calls are slow and cost money, and many of them repeat. But “caching” names two different mechanisms with different risks, and the second one can quietly make your measurements meaningless.

## Two caches with one name

**Provider prompt caching** happens on the provider’s side. When the beginning of your prompt matches a recent request exactly, the provider reuses the work it already did on that prefix. The model still generates a fresh answer every time. You pay less for the cached part of the input and wait less for the first token.

The details differ by provider. On [Anthropic’s API](https://platform.claude.com/docs/en/build-with-claude/prompt-caching) you mark where the cacheable prefix ends with `cache_control`. An entry lives five minutes by default, refreshed each time it is read, and a one-hour lifetime is available. Writing to the cache costs more than a normal input token (1.25 times for the five-minute lifetime) and reading costs a fraction. [OpenAI](https://developers.openai.com/api/docs/guides/prompt-caching) applies prompt caching automatically to prompts above a minimum length. Both require an exact match of the prefix, and both report cache hits in the usage block of the response: `cache_read_input_tokens` on Anthropic, `cached_tokens` under the input token details on OpenAI.

The practical rule follows from the exact match: stable content first, variable content last. Instructions, tool definitions and examples go at the top. The user’s message, retrieved documents and anything with a timestamp go at the end.[^1] Then read the usage fields to confirm you are actually getting hits.

**Response caching** happens in your code. You store the model’s output and return it when the same request comes again. It saves the whole call, not just part of the input, and it changes behaviour: the user gets an old answer instead of a new one.

That is correct only when the same request should get the same answer and a stale answer is acceptable. Classifying a fixed document, extracting fields from a file that does not change, embedding a chunk of text: good candidates. A conversation, an answer that depends on today’s data, or anything personal to the user: poor ones.

## Keys: everything that shapes the answer

A response cache is only as honest as its key. The key must include every input that can change the output. In practice that means the model, the fully rendered messages, the tool definitions, the output schema and the sampling parameters.

```python
import hashlib
import json

PROMPT_VERSION = "support-answer@7"
SCHEMA_VERSION = 3

def cache_key(model: str, messages: list[dict], tools: list[dict], params: dict) -> str:
    material = {
        "model": model,  # a pinned, dated model version, not an alias
        "prompt_version": PROMPT_VERSION,
        "schema_version": SCHEMA_VERSION,
        "messages": messages,  # fully rendered, system prompt included
        "tools": tools,
        "params": {k: params.get(k) for k in ("temperature", "top_p", "max_tokens")},
    }
    canonical = json.dumps(material, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
    return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
```

Hash the rendered messages, not a template name plus the user’s input. Someone will edit the template and forget to bump the version, and the rendered text catches that. The version numbers are still worth having, because they make keys readable in logs and let you invalidate on purpose.

## Invalidation comes from the key

With a key like this, most invalidation takes care of itself. Change the prompt, the schema or the model, and every key changes, so every lookup misses. You do not need a “clear cache” button that someone forgets to press.

Three gaps remain. First, model aliases: if you call a name the provider moves to newer versions, your key stays the same while the model changes underneath it, so pin a dated version wherever the provider offers one. Second, data that changes outside the request: if the answer depends on a document that can be edited, either include the document’s content or version in the key or give entries a lifetime. Third, per-user context: if two users with different permissions can send the same text, the key must know the difference.

## Why cached evals lie

Evals are where caching does the most damage, because a wrong number looks like a right one.

**Stale keys.** The story at the top: a cache keyed on input alone returns old answers after a change, and the score does not move. Print the cache hit rate for every eval run. A high hit rate right after a prompt change is a red flag, not a speed-up.

**One frozen sample.** With temperature above zero, a model gives different answers to the same input. A cached eval scores one fixed sample per case, forever. Compare two prompts on frozen samples and part of the difference is the luck of whichever answers were stored. For decisions, run fresh samples, and on a subset run each case several times to see how much the score moves on its own.

**Warm numbers.** Prompt caching does not change answers, but it does change cost and latency. An eval suite sends hundreds of requests with the same prefix back to back, so nearly every one is a cache hit. Production traffic may arrive minutes apart, and a short-lived cache entry may have expired between them. Take cost and latency figures from production usage fields, not from a warm eval run.

There is one place where caching in evals is right: when you are working on the grader, not the system. If you are tuning a judge prompt or a scoring rule, cache the system’s outputs so that each grader change is tested on the same answers.[^2] Then turn the cache off again before you evaluate any change to the system itself.

## The rules, briefly

Know which cache you mean. For prompt caching, put stable content first and check the usage fields. For response caching, key on everything that shapes the answer, pin the model, and give changing data a lifetime. Never let a response cache sit between a change and the eval that judges it.

The glossary entry on [prompt caching](/glossary/prompt-caching) summarises the provider side. [LLM Apps in Production](/workshops/llm-apps-in-production) has a lesson on what to cache and what breaks it, and [Evals That Catch Regressions](/workshops/evals-that-catch-regressions) covers eval cost, runtime and sampling.

[^1]: The classic mistake is the current date at the top of the system prompt. It changes every day, sits inside the prefix, and turns caching off for everything after it without any error.
[^2]: A model judge is also a model call. If you cache its verdicts while tuning, the judge prompt and judge model belong in that key too, or a new rubric gets graded with old verdicts.

_Cost and latency · Evals_
