Expand description
Client-side prefix cache hit estimation.
The provider reports cache_read_tokens for real prompt-cache reads, but
there is no provider-agnostic signal for how much of the final context
prefix actually overlapped with the previous request. This module implements
a lightweight, tokenizer-free estimate:
- Serialize the final provider
Contextinto a canonical byte sequence. - Split the bytes into fixed-size chunks and hash each chunk.
- Compare the current chunk list with the previous request’s list from index 0 (longest common prefix).
- Convert overlapping bytes to tokens using the provider-reported total input token count as the byte-to-token calibration.
The estimate is intentionally approximate and only intended to explain cache trends, not to replace provider-reported cache accounting.
Structs§
- Context
Cache Tracker - Per-session, per-model prefix overlap tracker.
- Prefix
HitEstimate - Result of comparing the current context against the previous baseline.
- Prefix
HitResult - Final prefix-hit metrics after provider usage is available.
Constants§
- CONTEXT_
CHUNK_ SIZE - Chunk size for the prefix-overlap comparison.
Functions§
- canonical_
context_ bytes - Canonical, deterministic byte representation of the final provider context.