By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Caching an LLM answer sounds impossible until you separate the deterministic parts from the creative ones. Policy retrieval is deterministic for a given policy snapshot. A draft for the same exception, prompt version and model alias is stable enough to cache for a short window. What must never be cached is the approval decision or the execution of a chargeable action.
The cache key is the entire design. It must include the tenant, the feature, the prompt version, the model alias, the tool policy version and a canonical hash of the input payload. Forget one of them and the cache will happily serve an answer produced by a prompt you retired last week.
import hashlib
import json
def canonical_key(
feature: str,
tenant_id: str,
prompt_version: str,
model_alias: str,
payload: dict,
) -> str:
body = {
"feature": feature,
"tenant_id": tenant_id,
"prompt_version": prompt_version,
"model_alias": model_alias,
"payload": payload,
}
encoded = json.dumps(body, sort_keys=True, separators=(",", ":")).encode()
return hashlib.sha256(encoded).hexdigest()
STORE: dict[str, str] = {}
def draft_update(payload: dict, prompt_version: str = "wp-update@v7") -> tuple[str, bool]:
key = canonical_key(
"customer_update", "northline", prompt_version, "waypoint-standard", payload
)
if key in STORE:
return STORE[key], True
text = "Update drafted for EXC-4821: Northline delay credit applies."
STORE[key] = text
return text, False
payload = {"exception_id": "EXC-4821", "carrier": "northline"}
first_text, first_hit = draft_update(payload)
second_text, second_hit = draft_update(payload)
_, bumped_hit = draft_update(payload, prompt_version="wp-update@v8")
print("first hit:", first_hit)
print("second hit:", second_hit)
print("hit after prompt bump:", bumped_hit)
print("cached text matches:", first_text == second_text)Caching avoids repeated work. Idempotency avoids repeated side effects, which is a stronger and more important promise once money moves. Every chargeable request from the client carries an Idempotency-Key. The gateway stores the key with the request fingerprint, the current status and the eventual result. A retry with the same key returns the stored result instead of executing again.
| Store | Key contains | TTL | Risk if wrong |
|---|---|---|---|
| Exact result cache | Feature, tenant, prompt version, model alias, payload hash | Minutes to hours | Stale answer after a prompt change |
| Retrieval snapshot | Policy id, policy version, query | Until policy version changes | Citations to a retired clause |
| Semantic cache | Embedding similarity above a threshold | Minutes | A similar-but-different exception gets the wrong answer |
| Idempotency record | Client idempotency key plus request fingerprint | At least the retry window | Duplicate refund or duplicate reroute |
Key takeaways
Cache the deterministic parts: retrieval snapshots and short-window drafts, never approvals.
The cache key must include tenant, feature, prompt version, model alias and a canonical payload hash.
Idempotency keys make retries safe: reserve, record in-progress, store the terminal result.
Idempotency records must outlive the retry window; caches should expire quickly.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.