Prefix-cache hit rate on synthetic agent traces
CPU · your Macnotebook120 minrequiredGoal · A simulator of block-level prefix caching across N replicas fed by synthetic agent sessions, reporting token-level hit rate for random, session-affinity and prefix-hash routing across cache sizes.
- 1Generate 500 agent sessions, each with a 1200-token shared system prompt, a session-specific 400-token task, and 8 to 40 turns where each turn appends 300 to 900 new tokens and reuses the entire previous context.
- 2Interleave sessions in time so that at any moment about 50 sessions are active, and emit one request per turn with its full token sequence.
- 3Implement a per-replica block cache with 16-token blocks, chained block hashing and LRU eviction, parameterised by capacity in blocks.
- 4Implement three routers (random, session-affinity by session id, prefix-hash of the first 1600 tokens with least-loaded fallback) over 4 replicas.
- 5For capacities of 50k, 200k and 1M tokens per replica, run all three routers and record token-level hit rate, requests per replica (balance) and total uncached prefill tokens.
- 6Plot hit rate versus capacity per router and a bar chart of per-replica load variance.
- 7Repeat with 1 replica to get the upper bound and report how far each router sits from it.
- Deliverable
- prefix_cache_sim.ipynb with the two plots and a summary table.
- Check
- With 4 replicas and 200k-token caches, session-affinity hit rate is at least 2x the random-routing hit rate; hit rate is monotonically non-decreasing with capacity for every router; the single-replica hit rate is the highest value in the table.