Skip to main content

KV Cache Simulator

kvcache-simulator runs the KVCache.AI hit-rate simulator locally on JSONL traces. It uses the same model accounting formulas as the KV Cache Size Calculator and the same prefix-aware hit-rate semantics as the KV Cache Hit Rate Simulator.

Installation

pip install kvcache-simulator

The default simulation backend compiles and runs a bundled C++ replay core on first use. You need a local C++ compiler such as c++ or clang++. If a compiler is not available, use --backend python.

Quick Start

kvcache-simulator run \
  --trace trace.jsonl.gz \
  --model glm-5.2 \
  --kv-precision fp8_int8 \
  --indexer-precision fp4_int4

The default output is a readable table. Use --format json when another script needs to consume the result.

kvcache-simulator run \
  --trace trace.jsonl.gz \
  --model deepseek-v4-pro \
  --kv-precision fp8_int8 \
  --indexer-precision fp4_int4 \
  --format json \
  --output result.json

List supported model ids:

kvcache-simulator list-models

python -m kvcache_sim ... also works, but the installed CLI command is preferred.

run is the main command. It evaluates the selected trace across a set of KV cache memory budgets. sweep is kept as an alias for users who prefer benchmark terminology.

Input Trace Format

Input is JSONL or JSONL.GZ, one request per line. The minimal accepted format is:

{"block_size":64,"hash_ids":[2001,2002],"input_length":128}

Required fields:

  • hash_ids: cache block identities in request-prefix order.
  • input_length: prefill input token count for this request.
  • block_size: source-native block size. It can be omitted only when --block-size is provided.

Optional fields:

  • timestamp: ignored by the simulator. Requests are replayed in file order, so sort production traces by timestamp before running the command.
  • output_length: ignored by the hit-rate denominator. Generated output matters only if it appears later in another request's hash_ids.
  • block_tokens: advanced field for exact per-block token weights. If present, it must be a positive integer list with the same length as hash_ids.

--block-size is only a fallback for records that omit block_size. If any record declares block_size, the trace-declared value is used and overrides the CLI fallback for the whole trace.

Options

Option Meaning
--trace PATH JSONL/JSONL.GZ trace path, or - for stdin.
--model ID Model id from the bundled KV Cache Size Calculator model catalog. Use kvcache-simulator list-models to list ids.
--kv-precision ID KV cache precision: usually bf16_fp16, fp8_int8, or fp4_int4. Defaults follow the web calculator.
--indexer-precision ID Indexer cache precision for models with an indexer cache, such as DeepSeek V4 / GLM / MiniMax M3.
--include-draft-kv-cache Include draft/MTP KV layers when the selected model defines them. Default is off.
--block-size N Fallback block size when trace records omit block_size; trace-declared block_size overrides it.
--estimate-tokens N Override the token count used for token-dependent bytes/token formulas. By default the trace average input length is used.
--budgets-gib A,B,C Comma-separated KV cache memory budgets in GiB. Default matches the web budget sweep: 1,2,4,...,16384.
--policies fifo,lru,optimal Eviction policies to simulate. Defaults to all three.
--backend cpp|python Simulation backend. Default is cpp; use python for debugging or machines without a compiler.
--jobs N Number of worker processes for the Python backend. The C++ backend runs one batch process and ignores this option.
--no-progress Disable terminal progress output. Progress is written to stderr only when stderr is interactive, so JSON stdout stays valid.
--format table|json Output format. Default is table.
--output PATH Write output to a file. Default - prints to stdout.
--max-records N Debug/testing limit: stop after N valid requests.
--max-events N Debug/testing limit: stop after N trace blocks.

Output Semantics

  • Hit rate is measured over the last 50% of requests.
  • Budget points that do not fill the cache before that measurement window are omitted, because they are not under memory pressure yet.
  • Hit tokens count only the longest continuous cached prefix of each request. If a middle block misses, later blocks in that same request do not count as prefill hits even if their ids are already cached.
  • speedup is an ideal prefill-only upper bound: 1 / (1 - hit_rate). 1.0x means no-cache prefill throughput where every prefill input token is computed. It does not include decode, KV lookup, network, batching, scheduling, or memory bandwidth overhead.

Performance Notes

The default C++ backend runs all cache-budget points in one batch after loading the trace and building the prefix trie once. This is usually faster than the Python backend, especially on large traces.

--jobs applies only to the Python backend. It parallelizes independent (policy, cache budget) simulation tasks. More jobs are not always faster: the default budget sweep has only a small number of budget points, tasks have uneven runtimes, and large traces can become memory-bandwidth limited.

Limits

The browser version caps uploads to protect the UI. This local package does not use that browser cap, but the C++ backend currently stores trace events and prefix node indexes in 32-bit arrays, so it supports at most 2^32 - 1 block events. In practice, memory and runtime are usually the real limits before that.

Development

The package bundles two files under src/kvcache_sim/resources/: models.yaml, a copy of data/kv_cache_calculator/models.yaml, and kv-cache-lab-native-sim.cc, the batch C++ core the cpp backend compiles on first use (it takes --policy batch --capacities ... --policies ...; the single-budget scripts/kv-cache-lab-native-sim.cc serves the site's precompute pipeline). After changing the catalog run scripts/sync-simulator-resources.sh; the test suite checks that the bundled copy matches.

pip install -e ./packages/kvcache-simulator
pytest packages/kvcache-simulator

Metadata

Release files for kvcache-simulator 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kvcache-simulator 0.1.3
File Size Uploaded Provenance
kvcache_simulator-0.1.3.tar.gz 41.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for kvcache-simulator 0.1.3
File Interpreter ABI Platform
kvcache_simulator-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 81.8 kB

Release files / kvcache_simulator-0.1.3.tar.gz

Download URL kvcache_simulator-0.1.3.tar.gz
Size 41.9 kB
Tags Source
SHA-256 checksum
How to use checksums
db9bf73ef5952b3cfe04e163334614ee9d087817d4965a44d8497300f4c91ceb
BLAKE2b-256 checksum
How to use checksums
89ae582846ca5c1bfdbce6e0896f6187361cf542fe68fd10a508b93b81a90e4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Release files / kvcache_simulator-0.1.3-py3-none-any.whl

Download URL kvcache_simulator-0.1.3-py3-none-any.whl
Size 40.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1ef4b36581a5a63633ebf0e19327296183f872fd387a8ff84bc221fcf4645e5a
BLAKE2b-256 checksum
How to use checksums
124f55110a0776b25bf615a6685f8a36c05104dd932d7a36cd4cf321c28b4b28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.14

Security for kvcache-simulator 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page