Prompt Caching Is Not Memory

Prompt Caching Is Not Memory
The policy changes. The assistant keeps returning the old answer, quickly and cheaply.
Before deciding the model has remembered the wrong thing, I would ask whether a model ran at all. The application might simply be returning an earlier answer from its own cache.
Cache is a small word covering several different mechanisms. Mixing them together makes performance easier to discuss and stale behavior harder to diagnose.
Three operations that should keep their names
External memory stores information for later selection into a request. The application decides what to write, when to retrieve it and whether it is still appropriate.
An answer cache stores a completed result. On a hit, the application may return that result without generating a new answer. The cached result needs a scope and an invalidation policy: what must change before the old value can no longer be used?
Prompt caching reuses provider-side processing of an eligible input prefix. A prefix is the initial part of a request, such as stable instructions followed by a fixed document. Generation can still produce a new answer to a new question after that prefix.
These can coexist. A remembered preference can enter a prompt, that prompt's prefix can be cached, and the final answer can be cached separately. None of those operations should inherit the others' correctness or authorization claims.
A prefix hit doesn't remove the input
Anthropic's prompt-caching guide, checked November 26, documents exact matching of eligible prefixes and separate cache read/write usage fields. Its detailed lifetimes, minimum lengths and supported controls are provider-specific.
The important distinction is simpler: cached prefix content still occupies logical context. The provider can avoid some repeated processing without making the content disappear from the model's available input.
The request also still needs to identify the intended input according to that API's contract. A provider prompt cache is not a promise that an omitted policy will be remembered tomorrow. Managed conversation state is a separate feature with its own lifetime.
I would keep stable reusable material before changing per-question content where the provider's cache behavior benefits from that arrangement. I would not pad a prompt with irrelevant text just to improve a cache-hit metric without measuring whether that helped the actual workload.
A cached answer needs a more demanding key
For our fictional documentation assistant, the same question can have a different answer for another user, a new policy revision or a corrected memory. A cache key must represent the inputs that make the answer reusable.
Here is a deliberately small application answer cache. It does not implement provider prompt caching. SHA-256 is a standard hash function used here to form fixed-length keys; hashing is not encryption or an anonymity guarantee.
import { createHash } from 'node:crypto'; type Scope = { ownerId: string; model: string; policyRevision: string; corpusRevision: string; memoryRevision: string; }; type CachedAnswer = { text: string; sourceIds: string[] }; type CacheEntry = { createdAt: number; expiresAt: number; answer: CachedAnswer }; function answerKey(scope: Scope, question: string): string { return createHash('sha256').update(JSON.stringify([ scope.ownerId, scope.model, scope.policyRevision, scope.corpusRevision, scope.memoryRevision, question, ])).digest('hex'); } class AnswerCache { private readonly entries = new Map<string, CacheEntry>(); put(scope: Scope, question: string, answer: CachedAnswer, now: number, ttl: number): void { if (![now, ttl, now + ttl].every(Number.isSafeInteger) || ttl <= 0) { throw new Error('Invalid cache lifetime'); } if (!answer.text.trim() || answer.sourceIds.length === 0) throw new Error('Missing answer evidence'); this.entries.set(answerKey(scope, question), { createdAt: now, expiresAt: now + ttl, answer: structuredClone(answer), }); } get( scope: Scope, question: string, now: number, canRead: (sourceIds: readonly string[]) => boolean, ): CachedAnswer | undefined { if (!Number.isSafeInteger(now)) throw new Error('Invalid clock'); const entry = this.entries.get(answerKey(scope, question)); if (!entry || now < entry.createdAt || now >= entry.expiresAt) return undefined; if (!canRead([...entry.answer.sourceIds])) return undefined; return structuredClone(entry.answer); } }
The application supplies the authenticated owner and current revisions. A model cannot choose an older revision to recover an answer it prefers. The access callback uses trusted current permissions, even on a cache hit.
The key is complete only for this teaching contract. Add answer language, schema revision, tool configuration or other answer-affecting inputs if the real application uses them. Omitting a dependency is a stale-result bug waiting for that dependency to change.
Invalidate by changing the conditions
This offline fixture uses invented millisecond times and an already-checked answer:
const scope: Scope = { ownerId: 'reader-a', model: 'example-model', policyRevision: 'instructions-1', corpusRevision: 'docs-3', memoryRevision: 'memory-2', }; const cache = new AnswerCache(); cache.put(scope, 'Does production need approval?', { text: 'Human approval is required.', sourceIds: ['release-policy-v3'], }, 100, 50); const hit = cache.get(scope, 'Does production need approval?', 120, () => true); const changedDocs = cache.get( { ...scope, corpusRevision: 'docs-4' }, 'Does production need approval?', 120, () => true, );
The first lookup returns a copy. Changing the corpus revision produces a miss. The example can also test another user, an expired entry, a memory correction and revoked source access without calling any provider.
A time-to-live, or TTL, makes reuse stop after a bounded interval. It does not notice a policy update before that interval ends. Revision changes and permission checks provide different invalidation paths.
This cache is in process memory, has no capacity eviction and does not coordinate concurrent workers. Production storage needs those operational decisions. Returning undefined here means a miss, not permission to bypass unavailable authorization or invent an answer.
Concurrent misses deserve a separate test. Several callers can miss the same key and all start generation before any result is stored. Sharing one in-flight operation can reduce that duplicate work, but only within the same authorized scope. A lock keyed only by question text would reintroduce the user-isolation mistake we just avoided in the answer cache.
A fast result still needs the ordinary checks
I would inspect cache-hit behavior with the same care as a generated answer. Does a corrected memory change the key? Does current access get checked? Can one caller mutate the object another caller later receives?
Prompt-cache metrics answer a different question: how much eligible input processing was reused? A hit rate alone does not establish lower total cost after write charges, improved answer quality or correct retention behavior.
I would also keep data-handling claims tied to the selected service and agreement. A provider's prefix-cache implementation and our own stored answer can have different isolation, deletion and retention rules. Expiring one does not erase the other.
The useful outcome is not a system that remembers everything faster. It is one where reuse has a named scope, expires for a reason and stops when the conditions that justified the answer change.
Next the assistant needs to choose where evidence comes from: a table, a document, an event stream or a search index. That decision starts with the shape of the data, not with a demand that everything become a vector.
When a cached answer becomes stale, which changed input should have invalidated it? I would write that dependency into the key or the read policy before adjusting the TTL again.
A note from the author
I’m Maksym Kuzmitskyi, a senior software developer working mainly with React and TypeScript. I build frontend applications and design workflows for working with AI in software delivery. If this article connects with something you’re working on, I’d love to hear from you.
More than a blog post
I share frontend news and the reasoning behind it throughout the day. Pick the language that feels natural to you.