A Cache Hit Is Not Correctness in AI Systems
A cache hit proves that two requests shared an identity rule. It does not prove that reusing the result is correct.
That distinction matters more in AI systems than in ordinary web caching. An HTTP response usually has a resource, a method, and explicit variation dimensions. An agent call can depend on a model snapshot, system policy, tool schemas, retrieved documents, tenant authority, clock-sensitive facts, and the state of the world behind every tool. If the cache key ignores one of those dimensions, the system can return a fast answer from the wrong execution universe.
I think AI caching needs to be designed as a contract, not added as a performance flag.
Two caches that should not be confused
“Prompt caching” often means reusing model-side prefix computation. OpenAI documents exact-prefix matching and requires identical tools and images for a hit. Anthropic describes a prefix ordered as tools, system content, then messages, with changes invalidating the affected prefix and everything after it. vLLM makes the mechanism concrete: it hashes full token blocks together with parent hashes and additional identity inputs such as LoRA identifiers, multimodal hashes, and optional salts.
This cache avoids repeated prefill computation. It does not reuse the generated answer. A hit changes the cost or latency of processing the input prefix; generation still happens again.
An application-level “semantic cache” does something stronger. It may decide that a new question is close enough to an old one and return the old answer without executing the model, retrieval, or tools again. That is not merely an optimization. It is an execution decision.
The two layers therefore need different correctness models:
| Layer | Reused artifact | Safe identity basis | Main failure |
|---|---|---|---|
| Prefix cache | model KV/prefill state | exact rendered prefix plus model/runtime isolation | missed isolation or invalidation |
| Result cache | completed answer or action proposal | full execution contract plus freshness policy | stale or unauthorized result |
A team can enable provider prompt caching with little semantic risk because the provider still generates a fresh continuation. The same team cannot safely enable fuzzy answer reuse with the same confidence.
The cache identity vector
For result reuse, I prefer a cache identity vector rather than a single clever string:
{
"request": "sha256:canonical-user-intent",
"model": "provider/model/snapshot",
"policy": "sha256:system-and-guardrails",
"tools": "sha256:ordered-tool-schemas",
"knowledge": "sha256:retrieval-snapshot",
"authority": "tenant:role:scope",
"environment": "region:runtime:feature-set",
"freshness": "price-data@2026-07-20T14:25Z"
}
The final key may be a hash of this object, but the object should remain inspectable. A bare hash is operationally useless when engineers need to explain why two requests did or did not share a result.
Each field answers a separate question.
Request identity: Are we asking the same normalized question, or merely questions that look similar in embedding space?
Model identity: Did the model, adapter, tokenizer, sampling policy, or structured-output mode change?
Policy identity: Did the system instruction, safety rule, approval boundary, or output contract change?
Tool identity: Are the same tools available with the same schemas and behavior versions? A renamed parameter can change what an agent is capable of doing even when the user text is identical.
Knowledge identity: Was the answer grounded in the same document set and revisions? This is where context must remain separate from durable memory: retrieval produces an execution package, not timeless truth.
Authority identity: Is the caller allowed to see and do the same things? Tenant and permission scope must never be inferred from semantic similarity.
Freshness identity: How long can this class of fact remain reusable? A compiler explanation and a deployment status do not share a sensible TTL.
This resembles the role of Vary in HTTP caching, but AI systems have more variation dimensions and fewer mature defaults. The safest default is not “cache everything.” It is “declare what may vary.”
Canonicalize before hashing
A cache contract fails if equivalent inputs produce different bytes or different inputs collapse accidentally. The identity vector therefore needs deterministic serialization. Field order, Unicode normalization, absent versus null values, number formatting, and schema version must have one representation.
That is why I treat canonical JSON as an intermediate representation, not a formatting preference. The cache key should be derived from a typed, versioned object after normalization—not from concatenated prompt strings.
A useful pattern is:
raw request
-> typed execution request
-> canonical representation
-> component hashes
-> cache policy decision
-> lookup
The policy decision belongs before lookup. It can declare no-store for actions, personal data, one-time credentials, volatile state, or any request whose authority cannot be represented safely. Not every deterministic key deserves a cache entry.
Similarity is a candidate generator, not permission
Semantic caches often begin with an embedding threshold: if two questions are close enough, reuse the previous answer. This is attractive and incomplete.
“Can I deploy this service?” and “Can this service be deployed?” may be close in vector space. The first can be an authority-sensitive request; the second can be an architectural question. “What is the current incident status?” can be almost identical five minutes later while the correct answer has changed.
Similarity can nominate candidates. A deterministic gate must still check:
- compatible intent class;
- identical authority scope;
- compatible tool and policy fingerprints;
- knowledge revision and freshness budget;
- output schema version;
- whether the previous result was an answer, a proposal, or an executed effect.
This is the same separation I want elsewhere in agent architecture: the model proposes; the runtime permits. A vector search can propose a reusable result. The cache policy decides whether reuse is legal.
Cache receipts make reuse observable
Every result hit should emit a small receipt:
{
"cache": "result-v3",
"decision": "hit",
"entry": "sha256:…",
"matched": ["request", "model", "policy", "tools", "knowledge", "authority"],
"fresh_until": "2026-07-20T14:35:00Z",
"created_by_run": "run_01…"
}
The receipt is not bureaucracy. It is the difference between “the system answered instantly” and “we can prove which prior execution it reused.” It should join the same trace as retrieval, model invocation, tool calls, and validation. If the output schema changes, schema evolution must remain explicit; silently reading an old cache artifact as a new type is another form of hidden migration.
Receipts also make cache quality measurable without pretending that hit rate equals success. Useful operational questions include:
- Which identity dimension causes most misses?
- Which entries are invalidated by policy or tool changes?
- How often does a candidate fail the authority or freshness gate?
- Can every served result be traced to its originating execution?
I am deliberately not claiming that this vector is universally complete. An agent controlling physical devices, financial operations, or regulated data will need domain-specific dimensions and stricter no-store rules. The mechanism is a design frame: make reuse dependencies explicit, typed, and auditable.
Performance comes after identity
AI systems will cache more aggressively because context is expensive and repeated prefixes are common. That pressure is healthy. It forces architecture to reveal what an execution actually depends on.
But a fast stale answer is not an optimization. A cross-tenant hit is not efficiency. A reused proposal under changed permissions is not determinism.
The engineering order should be simple:
identity -> eligibility -> freshness -> reuse -> receipt
When that order is reversed, caching hides state. When it is preserved, caching becomes something better than a speed trick: a precise statement about which parts of an AI execution are stable enough to reuse.