lpgcast.llm.cache¶
On-disk cache of model responses, keyed by the logical request.
The key covers everything that changes a response — the messages, the JSON Schema asked for, and every model setting that reaches the provider — and nothing that does not, so a rerun of the same casts hits and a changed prompt misses. Entries are written atomically, one JSON file per key.
Attributes¶
CACHE_FORMAT_VERSION = 1
module-attribute
¶
LLM_CACHE_SUBDIR = 'llm'
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
CacheMissError
¶
CachedResponse
¶
Bases: BaseModel
One stored response.
Source code in lpgcast/llm/cache.py
Attributes¶
content = Field(description='Response text as received.')
class-attribute
instance-attribute
¶
finish_reason = Field(default=None, description='Provider stop reason.')
class-attribute
instance-attribute
¶
mode = Field(description='Response format the request was sent with.')
class-attribute
instance-attribute
¶
model = Field(default=None, description='Model the endpoint reported.')
class-attribute
instance-attribute
¶
refusal = Field(default=None, description="The model's refusal message, when it refused.")
class-attribute
instance-attribute
¶
usage = Field(default=None, description='Token counts, replayed on a hit.')
class-attribute
instance-attribute
¶
LLMCache
¶
Response cache under <dir>/llm/<key>.json.
Source code in lpgcast/llm/cache.py
Attributes¶
dir = Path(base).expanduser() / LLM_CACHE_SUBDIR
instance-attribute
¶
enabled
property
¶
require_hit
property
¶
settings = settings or CacheSettings()
instance-attribute
¶
writable
property
¶
Methods:¶
__init__(settings=None)
¶
Source code in lpgcast/llm/cache.py
aget(key)
async
¶
aset(key, entry)
async
¶
clear()
¶
Delete every entry; returns how many were removed.
get(key)
¶
The cached response for key, or None on a miss or unreadable entry.
Source code in lpgcast/llm/cache.py
path(key)
¶
set(key, entry)
¶
Store entry atomically; a no-op when the cache is not writable.
Source code in lpgcast/llm/cache.py
Functions:¶
cache_key(messages, json_schema, settings)
¶
SHA-256 of the logical request.
Keyed on the messages the caller passed, not on the wire request: a
json_object fallback adds a schema message, and a replay must hit
whichever format served the original run.
Source code in lpgcast/llm/cache.py
cache_key_config(settings)
¶
The model settings that take part in the key — the single definition.
Source code in lpgcast/llm/cache.py
default_cache_dir()
¶
./.test_cache under pytest; else the user cache directory.