Prompt Cache Keys

easy
Unit 8 · Decoding0/3 to milestone

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:

  • Normalise each message's content: strip leading and trailing whitespace, and collapse runs of spaces and tabs within lines (but keep newlines).
  • Include only model, the normalised messages and these parameters if present: temperature, max_tokens, top_p, stop, tools. Ignore everything else (like user or request_id).
  • Serialise with 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.

#caching#llm#hashing2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Prompt Cache Keys · TechGradify