cadrumo.adapters.persistence.llm.cache module¶
Encrypted content-addressed cache for LLM responses.
Each CachedEntry is stored under
LLM_CACHE_NAMESPACE as an encrypted
secure object with SensitivityClass
DIAGNOSTIC classification so operator-identifying inputs are redacted
before persistence.
- class LLMCache(root_dir=None)[source]¶
Bases:
objectPersist LLM responses through encrypted secure objects.
The cache derives
CacheKeyvalues fromLLMRequestcontent and persistsLLMResponsepayloads throughsecure_object_repository_for_active_bucket().- Parameters:
root_dir (
Path|None) – Optional logical cache partition override.
- build_key(request, provider, model)[source]¶
Derive a
CacheKeyfrom the request.- Parameters:
request (
LLMRequest) – StructuredLLMRequest.provider (
LLMProvider) – EffectiveLLMProvider.model (
str) – Effective model for the request.
- Return type:
- Returns:
Deterministic
CacheKeycomponents.
- read(request, provider, model)[source]¶
Read a cached response, if present.
- Parameters:
request (
LLMRequest) – StructuredLLMRequest.provider (
LLMProvider) – EffectiveLLMProvider.model (
str) – Effective model for the request.
- Return type:
LLMResponse | None
The decoded entry is bound back to the key that was asked for before it is returned. The object key is derived from the request, but until that check existed nothing tied the DECRYPTED payload’s own provider / model / prompt hash / args hash to it: a valid entry re-encrypted under another entry’s row was returned verbatim, so a caller asking one provider-and-model could be answered by another’s text with
cache_hitset. Successful decryption proves custody of the bucket key, never that the row holds what was requested.An entry whose response carries a redaction placeholder is evicted and reported as a miss: it predates the write-side refusal to store a redacted response, and replaying it would substitute a hash for the value the model returned.
- Return type:
- Returns:
Cached
LLMResponsewhen present, otherwiseNone.- Raises:
LLMCacheError – When the cached
payload is present but cannot be parsed, or when it decodes to –
an entry belonging to a different cache key. –
- Parameters:
request (LLMRequest)
provider (LLMProvider)
model (str)
- write(request, response)[source]¶
Write a response to the cache and return the stored entry.
The serialised payload is routed through the substrate’s
redact_structured()helper atSensitivityClassDIAGNOSTICclass before persistence (the CACHE-class default policy has an empty rule set because most caches are public reference data; the LLM cache carries identity-bearing inputs and therefore adopts the DIAGNOSTIC rule set, mirroring the run-trace sink’s discipline). The redacted payload is stored as an encrypted SQL secure object rather than a materialized JSON file.A response the redaction altered is not stored at all. A cache hit stands in for the response, and a redacted one is a different response: an invoice read whose tax identifier came back as its hash fails validation on replay, so a re-read of the same document refused what the first read accepted.
- Parameters:
request (
LLMRequest) – StructuredLLMRequest.response (
LLMResponse) – PublicLLMResponseto persist.
- Return type:
- Returns:
Persisted
CachedEntrymodel.- Raises:
LLMCacheError – When redaction
produces a non-dict result or the storage write fails with an –
OS-level error. –
- stats()[source]¶
Return encrypted cache counts as a
CacheStats.- Return type:
- Returns:
CacheStatswith aggregate entry count and total decrypted JSON byte size for this logical cache partition.
- prune(*, retention_days=None, max_records=None)[source]¶
Delete cached entries older than the retention window or beyond the count cap.
Two-stage bound mirroring
prune(): entries older thanretention_days(measured against the current time) are removed, then – if more thanmax_recordsremain – the oldest excess entries beyond the cap are removed too. Both bounds default to the centralizedcadrumo_llm_cache_retention_days/cadrumo_llm_cache_max_recordssettings, giving the response cache a declared retention lifecycle instead of unbounded growth.- Return type:
- Returns:
Number of removed cache objects.
- Raises:
LLMCacheError – When a cache
entry cannot be parsed during iteration. –
- Parameters: