Repository navigation
Feature Request: Agent-facing KV cache export/import — deterministic per-session cache artifacts #4279
nizovtsevnv
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Feature Request: Agent-facing KV cache export/import — deterministic per-session cache artifacts
Summary
For agentic coding workflows (pi, Claude Code, etc.) running against oMLX, long sessions accumulate a large prompt prefix. When the model/server is reloaded, or the working set is evicted, the next turn pays a full re-prefill of the entire accumulated context — minutes of compute for a 60–120k-token session.
I'd like the engine to expose an export/import API for prefix cache state, so that the agent can materialize the cached prefix into a deterministic file artifact (stored next to its own session file), and later hand that artifact back to oMLX for restore — instead of re-prefilling.
The built-in tiered KV cache (hot RAM + cold SSD) already solves the common case well; this request adds a layer of agent-owned, deterministic persistence on top of it.
Why this is different from the existing SSD tier
The existing cold tier is engine-managed:
ssd_cache_max_size), evicted under pressure,An agent-owned artifact adds:
~/.pi/agent/sessions/--<project>--/<session>/kv.omlxalongside the JSONL transcript). It survives cache clears, evictions, and oMLX upgrades; the agent deletes it explicitly.Suggested API shape
Consistent with the existing admin router (e.g.
POST /api/cache/probe):Notes on the contract:
cached_tokens), so the agent can verify the hit and fall back honestly.exportshould say so (the client can retry right after a turn completes).What already exists to build on
Most of the machinery is in the tree:
omlx/cluster/prompt_snapshot_cache.py— SSD-backed prompt-cache snapshots keyed byhash(model, prefix tokens), atomic manifest, boundary chains where each boundary is an independent restore point, deterministic bounded LRU, and serialization of all cache types (KV slabs, rotating windows, GDN recurrent state) via mlx-lm'ssave_prompt_cache/load_prompt_cache. Today it is wired to the distributed rank only; a single-node export/import API would be a generalization of this store.omlx/cache/paged_ssd_cache.py— block-based safetensors persistence, CoW, prefix sharing (its own format already references the same mlx-lm save/load machinery).POST /api/cache/probe— client can already ask where a given prompt's blocks live (hot/warm/cold) without paying prefill; useful for deciding when to export/import.I read this as ~80% of the feature already existing; what's missing is a client-facing surface plus a portable format/manifest contract.
Correctness constraints worth calling out
Concrete workflow (pi coding agent)
~/.pi/agent/sessions/....exportwith the exact messages it sent → storesmanifest + blobnext to the transcript (few GB for a 60–120k context on a 27B hybrid with 8-bit KV; light zstd at most).importwith the stored artifact → engine validates digest → first turn starts from restored state; the usage field confirmsrestored_tokens ≈ prompt_tokens.Expected win: restarting a 100k-token session drops from "minutes of prefill" to "seconds of NVMe read" (modulo #3064).
Out of scope
Open questions
ssd_cache_max_sizebudget, or a separate one?format_versionwith a stability policy (for the cross-machine portability use case)?Happy to help with a prototype (the pi-agent side and a reference client) once the API shape is settled.
All reactions