Lay Out Prompts for Cache Hits

easy
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 11. Everything before it counts as cleared.

Provider prompt caches (and vLLM's prefix cache) only reuse an exact prefix. Putting a timestamp or the user's name at the top of the prompt defeats caching for everything after it.

A prompt is a list of components (name, tokens, static), where static means identical across requests. Implement:

  • cacheable_tokens(components): the token count of the leading run of static components (stop at the first dynamic one)
  • reorder(components): move every static component before every dynamic one, keeping the relative order within each group
  • cost(components, n_requests, price, cached_discount=0.9): the total input cost over n_requests identical-layout requests. The first request pays full price for everything; later requests pay price × (1 - cached_discount) for the cacheable tokens and full price for the rest. price is per token.
#prompt-caching#cost#latency2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Lay Out Prompts for Cache Hits · TechGradify