Caching LLM responses only works if logically identical requests produce the same key, and different requests never collide.
Implement cache_key(request) for a request dict with "model", "messages" (a list of {"role", "content"}) and optional generation parameters:
model, the normalised messages and these parameters if present: temperature, max_tokens, top_p, stop, tools. Ignore everything else (like user or request_id).json.dumps(..., sort_keys=True, separators=(",", ":")) and return the SHA-256 hex digest.Then implement is_cacheable(request): only requests with temperature of 0 (or missing, treated as 1.0, so not cacheable) can be safely cached.