| title | Llama Cache |
|---|---|
| module_name | llama_cpp.llama_cache |
| source_file | llama_cpp/llama_cache.py |
| last_updated | 2026-09-17 |
| version_target | latest |
llama_cpp.llama_cache provides cache implementations for storing and restoring LlamaState objects or recurrent model state checkpoints.
The module is mainly used to speed up repeated inference workflows by reusing previously computed model state for matching token prefixes.
It defines several cache classes:
| Class | Purpose |
|---|---|
BaseLlamaCache |
Abstract base class for llama.cpp state caches. |
LlamaRAMCache |
In-memory LRU cache for LlamaState objects. |
LlamaDiskCache |
Disk-backed cache using the diskcache library. |
TrieNode |
Internal trie node used by LlamaTrieCache. |
LlamaTrieCache |
Trie-based cache optimized for fast longest-prefix lookup. |
HybridCheckpoint |
Dataclass representing one saved Hybrid/Recurrent checkpoint and its host-visible payload. |
HybridCheckpointCache |
Checkpoint manager for Hybrid/Recurrent model state snapshots, with host and device-backed modes. |
The public compatibility alias is:
LlamaCache = LlamaTrieCacheThis means that code importing LlamaCache receives the trie-based cache implementation.
Defined in: llama_cpp/llama_cache.py
Related pages: Llama, Caching and state reuse.
The cache module provides reusable storage for model runtime state.
There are two main caching strategies:
-
Token-prefix state caching
Used by:
LlamaRAMCacheLlamaDiskCacheLlamaTrieCacheLlamaCache
These caches map token sequences to
llama_core.LlamaStateobjects. RAM and disk lookup select the cached key with the longest nonempty common prefix, even if that key later diverges from the query. Trie lookup requires the complete cached key to be a prefix of the query. All return the stored snapshot without truncating it. -
Hybrid / recurrent checkpoint caching
Used by:
HybridCheckpointHybridCheckpointCache
This is designed for Hybrid or recurrent models where rollback requires saving and restoring hidden state snapshots through low-level llama.cpp state APIs.
| API | Type | Public | Description |
|---|---|---|---|
BaseLlamaCache |
Abstract class | Yes | Base interface for cache implementations. |
LlamaRAMCache |
Class | Yes | In-memory LRU cache with linear prefix lookup. |
LlamaDiskCache |
Class | Yes | Disk-backed cache using diskcache.Cache. |
LlamaTrieCache |
Class | Yes | Trie-based cache with efficient prefix lookup. |
LlamaCache |
Alias | Yes | Backward-compatible alias for LlamaTrieCache. |
HybridCheckpoint |
Dataclass | Yes | Represents one saved Hybrid/RNN checkpoint. |
HybridCheckpointCache |
Class | Yes | Manages Hybrid/RNN state checkpoints. |
TrieNode |
Class | Internal | Trie node used by LlamaTrieCache. |
BaseLlamaCache is the abstract base class for llama.cpp cache implementations.
It defines a common dictionary-like interface for storing and retrieving llama_core.LlamaState objects by token sequence.
Subclasses are expected to implement:
cache_size__getitem____contains____setitem__
Defined in: llama_cpp/llama_cache.py
BaseLlamaCache acts as the shared contract for cache implementations used by higher-level llama-cpp-python runtime code.
It is not intended to be used directly. Users should instantiate one of the concrete cache classes instead:
LlamaRAMCacheLlamaDiskCacheLlamaTrieCacheLlamaCache
def __init__(self, capacity_bytes: int = (2 << 30)):
...| Parameter | Type | Default | Required | Description |
|---|---|---|---|---|
capacity_bytes |
int |
2 << 30 |
No | Maximum cache capacity in bytes. The default is approximately 2 GiB. |
| Name | Type | Description |
|---|---|---|
capacity_bytes |
int |
Maximum allowed cache size in bytes. Concrete subclasses use this value to decide when eviction is required. |
@property
@abstractmethod
def cache_size(self) -> int:
...Returns the current cache size in bytes.
Concrete implementations define how this value is calculated.
def _find_longest_prefix_key(
self,
key: Tuple[int, ...],
) -> Optional[Tuple[int, ...]]:
...Finds the cached key with the longest token prefix matching the requested key.
In BaseLlamaCache, this method is only a placeholder and does not implement behavior.
Concrete subclasses may override it.
@abstractmethod
def __getitem__(self, key: Sequence[int]) -> "llama_core.LlamaState":
...Retrieves a cached LlamaState.
The expected behavior is longest-prefix matching rather than strict exact-key lookup.
@abstractmethod
def __contains__(self, key: Sequence[int]) -> bool:
...Returns whether the cache contains a matching token prefix for the given key.
@abstractmethod
def __setitem__(
self,
key: Sequence[int],
value: "llama_core.LlamaState"
) -> None:
...Stores a LlamaState under a token sequence.
LlamaRAMCache is an in-memory cache for llama_core.LlamaState objects.
It stores token sequences in an OrderedDict and maintains an LRU eviction policy. Lookup selects the cached key with the longest nonempty common prefix with the query; the key need not be wholly contained in the query.
Defined in: llama_cpp/llama_cache.py
LlamaRAMCache is useful when users want fast in-process caching without writing state to disk.
It keeps all cached states in Python memory. This makes retrieval simple, but memory usage can grow quickly depending on the size of saved LlamaState objects.
def __init__(self, capacity_bytes: int = (2 << 30), verbose: bool = False):
...| Parameter | Type | Default | Required | Description |
|---|---|---|---|---|
capacity_bytes |
int |
2 << 30 |
No | Maximum total size of cached states in bytes. |
verbose |
bool |
False |
No | Whether to enable verbose behavior when computing token-prefix matches. This value is passed to Llama.longest_token_prefix. |
| Name | Type | Description |
|---|---|---|
capacity_bytes |
int |
Maximum cache capacity in bytes. |
cache_state |
OrderedDict[Tuple[int, ...], llama_core.LlamaState] |
Stores cached token sequences and their corresponding LlamaState objects. The order is used for LRU eviction. |
_current_size |
int |
Current total size of cached states in bytes. |
verbose |
bool |
Passed to llama_core.Llama.longest_token_prefix during prefix comparison. |
@property
def cache_size(self):
return self._current_sizeReturns the current tracked memory usage of the cache in bytes.
def _find_longest_prefix_key(
self,
key: Tuple[int, ...],
) -> Optional[Tuple[int, ...]]:
...Finds the cached token sequence with the longest prefix match against key.
This implementation scans every key in cache_state and calls:
llama_core.Llama.longest_token_prefix(k, key, self.verbose)| Operation | Complexity |
|---|---|
| Prefix lookup | O(N * K) |
| LRU update | O(1) |
| Size tracking | O(1) |
Where:
Nis the number of cached entries.Kis the token sequence length.
def __getitem__(self, key: Sequence[int]) -> "llama_core.LlamaState":
...Returns the cached LlamaState for the longest matching token prefix.
Behavior:
- Raises
KeyError("Cache is empty")if the cache has no entries. - Converts the input key to a tuple.
- Finds the cached key with the longest nonempty common prefix.
- Raises
KeyError("Key not found")if no matching prefix exists. - Moves the matched key to the end of
cache_stateto mark it as recently used. - Returns the matched
LlamaState.
def __contains__(self, key: Sequence[int]) -> bool:
...Returns True if any cached key is a prefix match for the requested token sequence.
Returns False if the cache is empty.
def __setitem__(self, key: Sequence[int], value: "llama_core.LlamaState"):
...Stores a LlamaState in memory.
Behavior:
- Converts
keyto a tuple. - If the key already exists, deletes the old entry.
- Inserts the new
LlamaState. - Adds
value.nbytesto_current_size, falling back tovalue.llama_state_sizefor legacy state objects. - Evicts least-recently-used entries while
_current_size > capacity_bytes. - Resets
_current_sizeto0if the cache becomes empty.
Note: The current implementation increments
_current_sizeby the new value size when replacing an existing key, but it does not subtract the old value size before deletion. This may cause size tracking to overcount replaced entries.
from llama_cpp import Llama
from llama_cpp.llama_cache import LlamaRAMCache
llm = Llama(
model_path="./models/model.gguf",
cache=LlamaRAMCache(capacity_bytes=1 << 30),
)
response = llm("Q: What is llama.cpp?\nA:", max_tokens=64)
print(response["choices"][0]["text"])- Use
LlamaRAMCachewhen cache speed is more important than persistence. - Keep
capacity_bytesbelow available system memory. - Reuse the same cache instance across repeated prompts when prefix reuse is expected.
- Prefer
LlamaTrieCacheorLlamaCachewhen many cached entries are expected and prefix lookup cost matters.
LlamaDiskCache is a disk-backed cache for llama_core.LlamaState objects.
It delegates storage, size limits, and eviction behavior to the external diskcache library without explicitly selecting an eviction policy.
Defined in: llama_cpp/llama_cache.py
LlamaDiskCache is useful when cached model states should persist beyond the current Python process or when RAM usage should be limited.
Compared with LlamaRAMCache, it may reduce memory pressure but can be slower due to disk I/O.
def __init__(
self,
cache_dir: str = ".cache/llama_cache",
capacity_bytes: int = (2 << 30),
verbose: bool = False
):
...| Parameter | Type | Default | Required | Description |
|---|---|---|---|---|
cache_dir |
str |
".cache/llama_cache" |
No | Directory used by diskcache.Cache to store cached state data. |
capacity_bytes |
int |
2 << 30 |
No | Maximum disk cache size in bytes. Passed to diskcache.Cache(..., size_limit=capacity_bytes). |
verbose |
bool |
False |
No | Passed to Llama.longest_token_prefix when searching for the best prefix match. |
| Name | Type | Description |
|---|---|---|
cache_dir |
str |
Filesystem directory for the disk cache. |
cache |
diskcache.Cache |
SQLite-backed disk cache object. |
verbose |
bool |
Passed to token-prefix comparison logic. |
capacity_bytes |
int |
Maximum configured cache capacity in bytes, inherited from BaseLlamaCache. |
@property
def cache_size(self):
return self.cache.volume()Returns the current disk cache volume in bytes using diskcache.Cache.volume().
def _find_longest_prefix_key(
self,
key: Tuple[int, ...],
) -> Optional[Tuple[int, ...]]:
...Finds the cached key with the longest token-prefix match.
Behavior:
- Returns
Noneimmediately if the disk cache is empty. - Iterates over
self.cache.iterkeys(). - Uses
llama_core.Llama.longest_token_prefix(k, key, self.verbose)to compare each cached key. - Stops early if a perfect match is found.
| Operation | Complexity |
|---|---|
| Prefix lookup | O(N * K) |
| Disk iteration | Depends on diskcache and filesystem |
| Exact-match early exit | Supported |
def __getitem__(self, key: Sequence[int]) -> "llama_core.LlamaState":
...Retrieves the cached state associated with the longest matching prefix.
Behavior:
- Prints
"LlamaDiskCache.__getitem__: called"tostderr. - Raises
KeyError("Cache is empty")if no entries exist. - Converts
keyto a tuple. - Finds the longest prefix key.
- Raises
KeyError("Key not found")if no match exists. - Reads and returns the cached
LlamaState.
The read is non-destructive. Access tracking and eviction follow the effective diskcache settings; this wrapper does not explicitly enable an LRU policy.
def __contains__(self, key: Sequence[int]) -> bool:
...Returns whether the cache has any longest-prefix match for the given token sequence.
def __setitem__(self, key: Sequence[int], value: "llama_core.LlamaState"):
...Stores a LlamaState in the disk cache.
Behavior:
- Prints
"LlamaDiskCache.__setitem__: called"tostderr. - Converts
keyto a tuple. - Assigns the value to
self.cache[tuple(key)].
diskcache handles capacity checks and eviction.
from llama_cpp import Llama
from llama_cpp.llama_cache import LlamaDiskCache
cache = LlamaDiskCache(
cache_dir=".cache/llama_cache",
capacity_bytes=2 << 30,
)
llm = Llama(
model_path="./models/model.gguf",
cache=cache,
)
response = llm("Q: What is llama.cpp?\nA:", max_tokens=64)
print(response["choices"][0]["text"])- Use
LlamaDiskCachewhen cache persistence is useful. - Place
cache_diron a fast local SSD when possible. - Avoid using slow network filesystems for high-throughput inference.
- Consider
LlamaTrieCachefor workloads where many prefix lookups happen within a single process.
- Disk-backed caching can be slower than RAM caching.
- The cache depends on the third-party
diskcachepackage. - Prefix lookup still scans cached keys linearly, even though storage and eviction are handled by
diskcache. - The implementation prints debug messages to
stderron get and set operations.
TrieNode is an internal helper class used by LlamaTrieCache.
Each node represents one position in a token-prefix tree.
Defined in: llama_cpp/llama_cache.py
TrieNode is not intended to be used directly by users.
It stores:
- Child nodes keyed by token ID.
- An optional
LlamaStatewhen the node marks the end of a cached token sequence.
def __init__(self):
...The constructor takes no parameters.
| Name | Type | Description |
|---|---|---|
children |
Dict[int, TrieNode] |
Child trie nodes keyed by token ID. |
state |
Optional[llama_core.LlamaState] |
Cached state stored at this node if the node represents a complete cached token sequence. |
LlamaTrieCache is a trie-based cache implementation for llama_core.LlamaState objects.
It optimizes longest-prefix lookup by storing token sequences in a prefix tree rather than scanning all cached keys.
Defined in: llama_cpp/llama_cache.py
LlamaTrieCache is the preferred cache implementation for efficient prefix lookup.
It combines:
- A trie for
O(K)longest-prefix lookup. - An
OrderedDictforO(1)LRU tracking. - Explicit byte-size tracking through
_current_size.
The compatibility alias LlamaCache points to this class:
LlamaCache = LlamaTrieCachedef __init__(self, capacity_bytes: int = (2 << 30)):
...| Parameter | Type | Default | Required | Description |
|---|---|---|---|---|
capacity_bytes |
int |
2 << 30 |
No | Maximum cache size in bytes. Entries are evicted when tracked state size exceeds this value. |
| Name | Type | Description |
|---|---|---|
root |
TrieNode |
Root node of the token-prefix trie. |
_current_size |
int |
Current total size of cached states in bytes. |
lru_tracker |
OrderedDict[Tuple[int, ...], TrieNode] |
Tracks cached keys by recency. The value is the terminal TrieNode for that key. |
capacity_bytes |
int |
Maximum cache capacity in bytes, inherited from BaseLlamaCache. |
@property
def cache_size(self) -> int:
return self._current_sizeReturns the current total size of cached states in bytes.
This is an O(1) operation.
def _find_longest_prefix_node(
self,
key: Tuple[int, ...]
) -> Tuple[Optional[TrieNode], Optional[Tuple[int, ...]]]:
...Finds the trie node containing the longest cached prefix for the given token sequence.
Returns:
Tuple[Optional[TrieNode], Optional[Tuple[int, ...]]]The first item is the matching trie node.
The second item is the matching cached key.
- Starts at the root node.
- Checks whether the empty prefix has a cached state.
- Walks one token at a time through the trie.
- Updates the best match each time it reaches a node with a stored state.
- Stops when the token path no longer exists.
| Operation | Complexity |
|---|---|
| Prefix lookup | O(K) |
Where K is the length of the requested token sequence.
def __getitem__(self, key: Sequence[int]) -> "llama_core.LlamaState":
...Retrieves the LlamaState for the longest matching cached prefix.
Behavior:
- Converts
keyto a tuple. - Finds the longest matching trie node.
- Raises
KeyErrorif no prefix match exists. - Moves the matched key to the end of
lru_tracker. - Returns the stored
LlamaState.
def __contains__(self, key: Sequence[int]) -> bool:
...Returns True if any prefix of key is cached.
This lookup is O(K).
def _prune(self, key: Tuple[int, ...]):
...Removes a cached key from the trie and prunes empty parent nodes.
This is an internal helper used during LRU eviction.
Behavior:
- Walks the trie path for the given key.
- Returns immediately if the key does not exist.
- Removes the stored state from the terminal node.
- Subtracts the state size from
_current_size. - Walks backward through the path and removes empty trie nodes.
def __setitem__(self, key: Sequence[int], value: "llama_core.LlamaState"):
...Stores a LlamaState in the trie cache.
Behavior:
- Converts
keyto a tuple. - Creates trie nodes for each token if needed.
- If the terminal node already has a state, subtracts the old state size.
- Stores the new state.
- Adds
value.nbytesto_current_size, falling back tovalue.llama_state_sizefor legacy state objects. - Updates
lru_tracker. - Evicts least-recently-used items while
_current_size > capacity_bytes.
from llama_cpp import Llama
from llama_cpp.llama_cache import LlamaCache
llm = Llama(
model_path="./models/model.gguf",
cache=LlamaCache(capacity_bytes=2 << 30),
)
response = llm("Q: What is llama.cpp?\nA:", max_tokens=64)
print(response["choices"][0]["text"])Because LlamaCache is an alias for LlamaTrieCache, this example uses the trie-based cache.
| Cache | Prefix Lookup | LRU Tracking | Storage |
|---|---|---|---|
LlamaRAMCache |
O(N * K) |
O(1) |
RAM |
LlamaDiskCache |
O(N * K) |
Delegated to diskcache |
Disk |
LlamaTrieCache |
O(K) |
O(1) |
RAM |
Where:
Nis the number of cached entries.Kis the token sequence length.
- Prefer
LlamaCachefor general use, because it currently aliasesLlamaTrieCache. - Use
LlamaTrieCachedirectly when you want explicit control over the cache implementation. - Use a realistic
capacity_bytesvalue based on available RAM. - Use this cache when many prompts share prefixes.
- The cache still stores full
LlamaStateobjects, which may be large. - RAM and trie capacity accounting uses
LlamaState.nbytes: native state bytes plus owned token, score, and last-logit arrays. It excludes Python object and container overhead. Legacy objects fall back tollama_state_size. Disk usage instead comes fromdiskcache.Cache.volume(). TrieNodeis internal and should not be manipulated directly.- Eviction removes entries from both
lru_trackerand the trie.
LlamaCache is a backward-compatible alias for LlamaTrieCache.
LlamaCache = LlamaTrieCacheThis means users can import LlamaCache and receive the trie-based implementation.
from llama_cpp import Llama
from llama_cpp.llama_cache import LlamaCache
cache = LlamaCache(capacity_bytes=2 << 30)
llm = Llama(
model_path="./models/model.gguf",
cache=cache,
)Older code may expect LlamaCache to refer to another cache implementation.
In the current source, LlamaCache resolves to LlamaTrieCache.
When documenting or debugging cache behavior, treat LlamaCache as equivalent to:
from llama_cpp.llama_cache import LlamaTrieCache as LlamaCacheHybridCheckpoint is a dataclass representing one saved snapshot of a Hybrid or Recurrent model state.
It is used by HybridCheckpointCache.
Defined in: llama_cpp/llama_cache.py
Hybrid or recurrent models may require sequence-state rollback rather than standard KV-cache truncation.
HybridCheckpoint stores the Python token position, native backend memory-position range, prefix verification hash, sequence id, and the serialized checkpoint payload visible to Python.
Its data field has different ownership semantics depending on the cache mode:
- In host mode (
on_device=False),dataowns the host-serialized partial checkpoint; the attention prefix remains in the live context. - In device mode (
on_device=True),datacontains only the host-visible serialized portion. The large tensor payload is stored inllama_context-owned device buffers by llama.cpp, keyed byseq_id.
@dataclass
class HybridCheckpoint:
pos: int
data: bytes
hash_val: str
size: int
seq_id: int
pos_min: int = -1
pos_max: Optional[int] = None| Field | Type | Description |
|---|---|---|
pos |
int |
Token position where this checkpoint was taken. |
data |
bytes |
Serialized checkpoint payload visible to Python. In host mode this is the serialized partial state; in device mode this is only the host-visible portion. |
hash_val |
str |
SHA-256 hash prefix used to verify exact token-prefix matching. |
size |
int |
Number of bytes written by llama_state_seq_get_data_ext. |
seq_id |
int |
Sequence id used by llama.cpp sequence-state APIs. |
pos_min |
int |
Minimum native backend memory position covered by the checkpoint. |
pos_max |
Optional[int] |
Maximum native backend memory position covered by the checkpoint. None is reserved for manually created legacy checkpoints. |
HybridCheckpoint objects are normally created by HybridCheckpointCache.save_checkpoint.
Users usually do not need to instantiate this dataclass manually.
In device mode, old HybridCheckpoint Python objects may become stale if a newer checkpoint is saved for the same seq_id, because the device-side tensor payload is keyed by seq_id and may be overwritten.
HybridCheckpointCache manages Hybrid/Recurrent model state checkpoints.
It is designed for models whose memory cannot always be safely truncated like a regular Transformer KV cache. Instead, rollback is implemented by saving and restoring sequence-state snapshots through llama.cpp state APIs.
The cache supports two operating modes:
-
Host mode (
on_device=False)- Partial checkpoint payloads are materialized as Python-owned
bytes. - Multiple historical checkpoints per
seq_idcan coexist while their native prefixes remain valid. - This is the default mode and is useful for multi-turn rollback or deeper prefix reuse.
- Partial checkpoint payloads are materialized as Python-owned
-
Device mode (
on_device=True)LLAMA_STATE_SEQ_FLAGS_ON_DEVICEis forwarded to llama.cpp.- Tensor payloads are stored in
llama_context-owned device buffers. - Python keeps only the host-visible serialized portion.
- Only one active checkpoint per
seq_idis safe because device payloads are keyed byseq_id. - This mode can reduce device-to-host copy overhead during checkpoint save/restore.
Defined in: llama_cpp/llama_cache.py
HybridCheckpointCache is a specialized cache manager for Hybrid/Recurrent model rollback.
It stores host-visible checkpoint data extracted from the llama.cpp backend through low-level C API functions:
llama_state_seq_get_size_extllama_state_seq_get_data_extllama_state_seq_set_data_ext
When on_device=True, tensor payloads are not treated as Python-owned bytes. They are stored by llama.cpp in llama_context-owned device buffers, while Python keeps the host-visible serialized portion and checkpoint metadata.
It is not a drop-in replacement for LlamaRAMCache, LlamaDiskCache, or LlamaTrieCache.
def __init__(
self,
ctx: llama_cpp_lib.llama_context_p,
max_checkpoints: int = 16,
on_device: bool = False,
verbose: bool = False
):
...| Parameter | Type | Default | Required | Description |
|---|---|---|---|---|
ctx |
llama_cpp_lib.llama_context_p |
— | Yes | Borrowed low-level llama.cpp context pointer used for sequence-state save/restore. The cache does not own or free this context. |
max_checkpoints |
int |
16 |
No | Maximum number of Python-side checkpoint entries to retain. If set to 0 or below, checkpointing is disabled. |
on_device |
bool |
False |
No | Whether to request llama.cpp to store checkpoint tensor payloads in llama_context-owned device buffers via LLAMA_STATE_SEQ_FLAGS_ON_DEVICE. |
verbose |
bool |
False |
No | Enables diagnostic messages printed to stderr. |
The constructor raises ValueError if ctx is None.
If max_checkpoints <= 0, checkpointing is disabled. In verbose mode, the cache reports that rollback capabilities are turned off. This mode is intended to avoid expensive state extraction for single-turn workflows.
When on_device=True, the cache forwards LLAMA_STATE_SEQ_FLAGS_ON_DEVICE to llama.cpp. In this mode, the cache keeps only one active checkpoint per seq_id by replacing old Python-side checkpoint metadata before saving a new checkpoint for the same seq_id.
| Name | Type | Description |
|---|---|---|
_ctx |
llama_cpp_lib.llama_context_p |
Borrowed llama.cpp context pointer used for state extraction and restoration. |
on_device |
bool |
Whether LLAMA_STATE_SEQ_FLAGS_ON_DEVICE is forwarded to llama.cpp state APIs. |
verbose |
bool |
Enables debug output. |
max_checkpoints |
int |
Maximum number of Python-side checkpoint entries retained. Values less than or equal to zero disable checkpointing. |
checkpoints |
list[HybridCheckpoint] |
Python-side checkpoint registry. In host mode, entries own full checkpoint payloads. In device mode, entries own only host-visible metadata/payload portions. |
_current_size |
int |
Python-tracked host-visible checkpoint size in bytes. In device mode, this does not include llama_context-owned device tensor storage. |
_get_size_ext |
Callable | Cached reference to llama_state_seq_get_size_ext. |
_get_data_ext |
Callable | Cached reference to llama_state_seq_get_data_ext. |
_set_data_ext |
Callable | Cached reference to llama_state_seq_set_data_ext. |
_flags |
int |
Combined llama.cpp sequence-state flags, always including LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY and optionally LLAMA_STATE_SEQ_FLAGS_ON_DEVICE. |
@property
def cache_size(self) -> int:
return self._current_sizeReturns the Python-tracked host-visible checkpoint size in bytes.
In host mode, this is close to the full serialized checkpoint payload size.
In device mode, this reports only the host-visible portion returned by llama.cpp. It does not include llama_context-owned device tensor storage.
def clear(self):
...Clears Python-side checkpoint metadata and resets _current_size to 0.
If the checkpoint list is already empty, it returns immediately.
In device mode, this does not explicitly release llama_context-owned device buffers. Those buffers are managed by llama.cpp and are associated with the context.
In verbose mode, it prints:
HybridCheckpointCache(clear): cleared
def close(self):
...Releases Python-side checkpoint metadata and detaches cached references held by the cache.
Behavior:
- Calls
clear(). - Sets
_ctxtoNone. - Sets cached C API function references to
None.
This method does not free the llama.cpp context itself, because the context is borrowed rather than owned by the cache.
def __del__(self) -> None:
self.close()Finalizer that calls close.
def _hash_prefix(self, tokens: List[int], length: int) -> str:
...Computes a SHA-256 hash for the token prefix up to length.
Behavior:
- Returns
"empty"iflength <= 0. - Clamps
lengthto the actual token list length. - Converts the selected token prefix into an
array.array('i'). - Hashes the bytes with SHA-256.
- Returns the first 32 hex characters.
This hash is used to ensure checkpoints are restored only when the token prefix exactly matches.
Llama registers its hybrid cache with the owning LlamaContext through weak
references. This registration neither keeps a discarded cache alive nor extends
the native context's lifetime. Closing the context closes its registered caches
before freeing native resources.
Wrapped memory clears and state loads invalidate affected checkpoints. Sequence removal preserves an earlier checkpoint only when its recorded native range is strictly before the removed suffix; shifts, copies, and sequence retention also invalidate affected entries. Device capture invalidates older device snapshots for the same sequence across registered caches before overwriting the slot.
These notifications cover registered caches on the same context. Raw C API calls, automatic SWA eviction, and dependencies on another context's shared KV memory are not automatically tracked. Advanced callers performing these operations must coordinate cache invalidation themselves. A cache constructed from a raw context pointer is not automatically registered.
def _evict_checkpoints_if_needed(self) -> None:
...Evicts old checkpoint entries using FIFO order until len(checkpoints) <= max_checkpoints.
In host mode, this evicts full Python-owned checkpoint payloads.
In device mode, this evicts Python-side checkpoint metadata only. Device tensor payloads are owned by llama_context.
Behavior:
- Checks whether the number of checkpoints exceeds
max_checkpoints. - Pops the oldest checkpoint entry from the front of the list.
- Subtracts its size from
_current_size. - Clamps
_current_sizeto0if needed. - Prints an eviction message in verbose mode.
def find_best_checkpoint(
self,
tokens: List[int],
seq_id: int = 0
) -> Optional[HybridCheckpoint]:
...Finds the longest valid checkpoint matching the given token prefix and sequence id.
The hash check prevents restoring a checkpoint that has the same length but belongs to a different prompt/history.
Returns None if:
- Checkpointing is disabled.
- There are no checkpoints.
- No checkpoint matches the requested sequence id and token prefix.
Behavior:
- Returns immediately if
max_checkpoints <= 0or no checkpoints exist. - Skips checkpoints whose
seq_iddiffers from the requestedseq_id. - Skips checkpoints whose
posis greater than the current token length. - Verifies token-prefix integrity using
_hash_prefix. - Returns the checkpoint with the largest matching
pos.
def save_checkpoint(
self,
current_pos: int,
tokens: List[int],
seq_id: int = 0
) -> bool:
...Extracts the current Hybrid/Recurrent model state from the C++ backend and stores it as a HybridCheckpoint.
Returns True if the checkpoint was saved successfully.
Returns False if:
- Checkpointing is disabled.
- The backend reports state size
0. - State extraction writes an unexpected number of bytes.
- Returns immediately if
max_checkpoints <= 0. - In device mode, invalidates older device checkpoints for the same
seq_id, including those in other registered caches on this context. - Uses
_flagsto select partial-only state serialization, optionally withLLAMA_STATE_SEQ_FLAGS_ON_DEVICE. - Calls
_get_size_extto query the required host-visible buffer size. - Allocates a
ctypes.c_uint8buffer. - Calls
_get_data_extto extract the host-visible checkpoint data. - Copies the data into a Python
bytesobject. - Computes a hash of the token prefix.
- Appends a new
HybridCheckpoint. - Increments
_current_size. - Evicts old checkpoint entries using FIFO order if the number of entries exceeds
max_checkpoints.
The implementation intentionally bypasses checkpoint extraction when max_checkpoints <= 0.
This avoids potentially large synchronous checkpoint extraction costs for single-turn workflows.
When on_device=True, llama.cpp may keep large tensor payloads in context-owned device buffers instead of materializing them as Python-owned bytes. This can reduce device-to-host tensor copy overhead, but only one active checkpoint per seq_id is safe.
def restore_checkpoint(
self,
cp: HybridCheckpoint,
seq_id: int = 0
) -> bool:
...Restores a previously saved checkpoint into the C++ backend.
Returns True if restoration succeeds.
Returns False if:
- The checkpoint sequence id does not match the requested
seq_id. - The cache is closed, or the exact checkpoint object is no longer tracked by it, in either host or device mode.
- The current backend state size differs from the checkpoint size.
- The backend does not report the expected number of restored bytes.
- The backend cannot remove the memory suffix after the restored native
pos_max.
- Verifies
cp.seq_id == seq_id. - Rejects a closed cache or a checkpoint whose object identity is absent from its registry.
- Queries current expected host-visible state size from the backend.
- Verifies it matches
cp.size. - Copies checkpoint bytes into a ctypes buffer.
- Calls
_set_data_extto restore the state. - Removes the remaining attention-memory suffix beginning at
cp.pos_max + 1. - On success, invalidates checkpoints at or beyond the restored suffix across registered caches on the same context.
- If native restoration or suffix removal fails, invalidates affected sequence checkpoints because native state may already have changed. Exceptions from these operations are propagated. Preliminary rejection in steps 1–4 does not invalidate current checkpoints.
- Returns whether both state restoration and suffix removal succeeded. High-level callers reset or rebuild after failure.
The native memory range is stored separately from pos because token counts do not
always map one-to-one to backend positions, notably for multimodal inputs, custom
position IDs, and SWA models.
In device mode, Python does not own the full checkpoint tensor payload. The large tensor payload is stored inside llama_context device buffers keyed by seq_id.
If a newer checkpoint is saved for the same seq_id, an older HybridCheckpoint Python object may still exist outside the cache, but its device-side tensor payload may have been overwritten.
Host checkpoints also depend on attention memory that remains in the live
context: they use PARTIAL_ONLY, not a complete LlamaState snapshot.
restore_checkpoint therefore requires the exact object to remain registered
in both modes. Retaining a Python reference does not preserve its validity after
clear, eviction, invalidation, or closure. Matching serialized sizes alone does
not establish that the required prefix is still available.
HybridCheckpointCache inherits from BaseLlamaCache, but it intentionally disables the dictionary-style methods.
def __getitem__(self, key):
raise NotImplementedError(
"HybridCheckpointCache: pls use save_checkpoint or restore_checkpoint method"
)def __setitem__(self, key, value):
raise NotImplementedError(
"HybridCheckpointCache: pls use save_checkpoint or restore_checkpoint method"
)def __contains__(self, key):
raise NotImplementedError(
"HybridCheckpointCache: pls use save_checkpoint or restore_checkpoint method"
)Users should use checkpoint-specific methods instead.
from llama_cpp.llama_cache import HybridCheckpointCache
# `ctx` must be a valid llama.cpp context pointer.
checkpoint_cache = HybridCheckpointCache(
ctx=ctx,
max_checkpoints=16,
on_device=False,
verbose=True,
)
tokens = [1, 2, 3, 4]
current_pos = len(tokens)
saved = checkpoint_cache.save_checkpoint(
current_pos=current_pos,
tokens=tokens,
seq_id=0,
)
if saved:
checkpoint = checkpoint_cache.find_best_checkpoint(tokens, seq_id=0)
if checkpoint is not None:
restored = checkpoint_cache.restore_checkpoint(checkpoint, seq_id=0)
print("Restored:", restored)Host mode owns the serialized partial payload in Python bytes. Multiple historical checkpoints per seq_id can coexist while their required native prefixes remain valid.
from llama_cpp.llama_cache import HybridCheckpointCache
# `ctx` must be a valid llama.cpp context pointer.
checkpoint_cache = HybridCheckpointCache(
ctx=ctx,
max_checkpoints=16,
on_device=True,
verbose=True,
)
tokens = [1, 2, 3, 4]
current_pos = len(tokens)
saved = checkpoint_cache.save_checkpoint(
current_pos=current_pos,
tokens=tokens,
seq_id=0,
)
if saved:
checkpoint = checkpoint_cache.find_best_checkpoint(tokens, seq_id=0)
if checkpoint is not None:
restored = checkpoint_cache.restore_checkpoint(checkpoint, seq_id=0)
print("Restored:", restored)In device mode, llama.cpp owns the large tensor payload in context-owned device buffers. Python keeps only the host-visible checkpoint data and metadata.
Only one active checkpoint per seq_id is safe.
Note: These examples assume
ctxis already available from lower-level llama.cpp runtime code. Most high-level users do not manually create this cache. Instead, they configure it through theLlamaconstructor usingctx_checkpoints,checkpoint_interval, andcheckpoint_on_device.
- Use
HybridCheckpointCacheonly for Hybrid or recurrent model workflows that require hidden-state rollback. - Keep
on_device=Falsewhen you need multiple historical checkpoints for the sameseq_id. - Use
on_device=Truewhen reducing device-to-host checkpoint copy overhead is more important than keeping many historical checkpoint payloads. Retain the checkpoint object so its sequence id and native memory-position range remain available for restoration. - Set
max_checkpoints=0for single-turn workflows where rollback is not needed. - Keep
max_checkpointssmall if checkpoint states are large. - Use
find_best_checkpointbefore callingrestore_checkpoint. - Do not restore checkpoint objects removed from the cache, even if another Python reference still keeps them alive.
- Do not use dictionary-style cache access with this class.
- Passing
ctx=NoneraisesValueError. max_checkpoints <= 0disables checkpointing.- Restoring a checkpoint with the wrong
seq_idfails. - Restore fails if the current backend state size no longer matches the checkpoint size.
- Both host and device objects become stale after invalidation or eviction. Device capture additionally overwrites the previous slot for that sequence.
- In device mode,
cache_sizedoes not includellama_context-owned device tensor storage. clear()removes Python-side checkpoint metadata but does not explicitly free llama.cpp-owned device buffers.close()detaches internal references; the object should not be reused afterward.- This class is not equivalent to
LlamaCache.
LlamaCache = LlamaTrieCacheBackward-compatible alias for LlamaTrieCache.
Users can import either:
from llama_cpp.llama_cache import LlamaCacheor:
from llama_cpp.llama_cache import LlamaTrieCacheBoth refer to the trie-based cache implementation in the current source.
| Class | Storage | Prefix Lookup | Eviction | Persistence | Best For |
|---|---|---|---|---|---|
LlamaRAMCache |
RAM | O(N * K) |
LRU | No | Small in-memory caches. |
LlamaDiskCache |
Disk | O(N * K) |
Delegated to diskcache |
Yes | Persistent cache across runs. |
LlamaTrieCache |
RAM | O(K) |
LRU | No | Fast prefix lookup with many cached entries. |
HybridCheckpointCache |
RAM | Hash-verified checkpoint search | FIFO by checkpoint count | No | Hybrid/Recurrent model rollback. |
For most users:
from llama_cpp.llama_cache import LlamaCacheThis currently gives the trie-based implementation.
For explicit cache selection:
from llama_cpp.llama_cache import LlamaRAMCache
from llama_cpp.llama_cache import LlamaDiskCache
from llama_cpp.llama_cache import LlamaTrieCacheFor Hybrid/Recurrent models:
from llama_cpp.llama_cache import HybridCheckpointCache