Tokenizer Caching¶
SMG provides a two-level tokenizer cache that reduces tokenization overhead for repeated content. It applies only where the gateway tokenizes: requests to gRPC and ZMQ workers, and /v1/tokenize.
How It Works¶
| Cache Level | Strategy | Best For |
|---|---|---|
| L0 (Exact Match) | Hash-based O(1) lookup for identical whole inputs | Identical prompts, such as repeated batch inputs or resent requests |
| L1 (Prefix Match) | Boundary-aligned prefix matching, tokenizes only the suffix | Multi-turn conversations, growing contexts |
On a multi-turn conversation, L1 avoids re-tokenizing the entire history — only new messages are tokenized.
Enable Caching¶
Both cache levels are disabled by default. Enable them with CLI flags:
L0 Only (Exact Match)¶
Best for workloads that send the same whole prompt many times (batch processing, resent requests):
smg \
--worker-urls grpc://worker:50051 \
--model-path meta-llama/Llama-3.1-8B-Instruct \
--tokenizer-cache-enable-l0 \
--tokenizer-cache-l0-max-entries 10000L0 + L1 (Exact + Prefix Match)¶
Best for multi-turn chat applications:
smg \
--worker-urls grpc://worker:50051 \
--model-path meta-llama/Llama-3.1-8B-Instruct \
--tokenizer-cache-enable-l0 \
--tokenizer-cache-l0-max-entries 20000 \
--tokenizer-cache-enable-l1 \
--tokenizer-cache-l1-max-memory 104857600Configuration Reference¶
L0 Cache¶
| Parameter | Default | Description |
|---|---|---|
--tokenizer-cache-enable-l0 |
false |
Enable exact match cache |
--tokenizer-cache-l0-max-entries |
10000 |
Maximum number of cached entries |
--tokenizer-cache-l0-max-memory |
268435456 (256 MiB) |
Byte budget for cached texts, token IDs, and per-entry overhead |
Each entry keeps the full input text and its token IDs, so its size grows with prompt length. With L0 enabled, both limits must be greater than 0.
L1 Cache¶
| Parameter | Default | Description |
|---|---|---|
--tokenizer-cache-enable-l1 |
false |
Enable prefix match cache |
--tokenizer-cache-l1-max-memory |
52428800 (50 MB) |
Maximum memory in bytes |
Memory Planning¶
L0 Sizing¶
L0 caps both the number of entries and their total bytes. Each entry is charged its text bytes plus 4 bytes per token plus a small fixed overhead. When a new entry would pass either bound, SMG evicts approximately least recently used entries (sampling 8 at a time). An input whose entry alone would take more than a quarter of the byte budget is not cached at all.
Size --tokenizer-cache-l0-max-entries from the number of distinct whole prompts that repeat in your traffic, and --tokenizer-cache-l0-max-memory from the memory you can spare. The budget counts the cache's contents, not process memory, so watch process memory as you raise either limit.
L1 Sizing¶
| Memory | Recommended For |
|---|---|
| 25 MB | Memory-constrained environments |
| 50 MB | Standard deployments (default) |
| 100 MB | Multi-turn conversation heavy |
| 200 MB | Long context applications |
On a miss, L1 stores entries for a selection of the input's special-token boundaries, not all of them: geometrically spaced checkpoints — each stored prefix at least doubling the token length of the one before — plus the input's deepest boundary. Each entry holds the token IDs of the whole prefix up to its boundary. On a hit it stores the prefix up to the input's own deepest boundary when that boundary lies past the match, so each turn of a conversation leaves the entry its next turn matches. When boundaries lie between the match and the deepest, it also stores a speculative entry at the boundary right after the match; eviction removes that entry ahead of matched entries unless another request matches it first.
Each entry is charged the prefix's length in bytes plus 4 bytes per token, so a miss charges at most a few times the input's own length, and hits as well as misses grow the cache toward the memory bound.
Recommended Configurations¶
For workloads that resend identical prompts:
smg \
--worker-urls grpc://worker:50051 \
--model-path meta-llama/Llama-3.1-8B-Instruct \
--tokenizer-cache-enable-l0 \
--tokenizer-cache-l0-max-entries 50000For chat applications with growing conversation history:
smg \
--worker-urls grpc://worker:50051 \
--model-path Qwen/Qwen2.5-7B-Instruct \
--tokenizer-cache-enable-l0 \
--tokenizer-cache-l0-max-entries 20000 \
--tokenizer-cache-enable-l1 \
--tokenizer-cache-l1-max-memory 104857600Moderate benefit with minimal memory (a 64 MiB L0 byte budget):
smg \
--worker-urls grpc://worker:50051 \
--model-path meta-llama/Llama-3.1-8B-Instruct \
--tokenizer-cache-enable-l0 \
--tokenizer-cache-l0-max-entries 5000 \
--tokenizer-cache-l0-max-memory 67108864Monitor Cache Activity¶
SMG exports cache counters on its Prometheus endpoint, each labeled by layer (l0 or l1): smg_tokenizer_cache_lookups_total (with a result label, hit or miss), smg_tokenizer_cache_evictions_total, and smg_tokenizer_cache_reused_bytes_total. Check each layer's hit ratio:
sum by (layer) (rate(smg_tokenizer_cache_lookups_total{result="hit"}[5m]))
/
sum by (layer) (rate(smg_tokenizer_cache_lookups_total[5m]))A low hit ratio together with a steady eviction rate means the cache is too small for the workload. See the Metrics Reference for the full definitions.
Next Steps¶
- Tokenizer Caching Concepts — Cache architecture, special token boundaries, monitoring metrics, PromQL queries
- gRPC Workers — Enable gateway-level tokenization with gRPC mode
- Load Balancing — Choose a routing policy (cache-aware routing uses tokenizer results)