Cache¶
Tool-result and model-response caches, keys and provider prompt caches. Concepts: Cache.
agent_adapter.cache¶
Caching: backends, keys, envelopes, the tool and LLM caches, and the prompt layout.
Provider prompt-cache plug-ins live in agent_adapter.cache.providers.
KEY_VERSION = 1
module-attribute
¶
CachePort
¶
Bases: Protocol
Byte cache. Implementations log backend failures and act as a miss; never raise.
Envelope
dataclass
¶
A decoded cache entry.
LLMResponseCache
¶
Stores normalised LLMResponses under llm_key (provider, model, request, fields).
PromptLayout
dataclass
¶
Static prefix (system prompt, tool catalogue, few-shots) plus per-request text.
messages(user=None, *, history=())
¶
System turn (cache breakpoint), history, then dynamic + user as one user turn.
prefix_digest()
¶
Digest of system_text(); changes exactly when the cacheable prefix changes.
system_text()
¶
static then every non-empty section, separated by blank lines.
with_section(name, text)
¶
A copy with section name set (replaced in place, or appended).
with_static(text)
¶
A copy with static replaced.
RedisCache
¶
Byte cache on Redis; RedisError is logged at WARNING and treated as a miss.
Raises:
| Type | Description |
|---|---|
ConfigError
|
not exactly one of |
MissingDependency
|
the |
aclose()
async
¶
Close the running loop's client (a new one is made on next use).
delete(key)
async
¶
Remove key; a Redis error is logged and ignored.
get(key)
async
¶
The value, or None on a miss or a Redis error.
set(key, value, ttl=None)
async
¶
Store with ex=ceil(ttl); ttl <= 0 deletes the key; inf / nan never expire.
ToolResultCache
¶
Stores tool results under tool_key, scoped to a run, a thread or globally.
canonical_json(obj)
¶
Deterministic JSON (sorted keys, no spaces, UTF-8).
Raises:
| Type | Description |
|---|---|
TypeError
|
|
decode(data, kind)
¶
The envelope in data; None for bad JSON, another version or another kind.
digest(obj)
¶
First 32 hex characters of the SHA-256 of canonical_json(obj).
encode(kind, payload)
¶
Wrap payload as {"v", "kind", "stored_at", "payload"} JSON bytes.
Raises:
| Type | Description |
|---|---|
TypeError
|
|
gcache_key(namespace, model, prefix_digest)
¶
Name under which a Gemini explicit cache for one static prompt prefix is stored.
llm_key(namespace, request, *, provider, model, fields)
¶
Key for an LLM response; fields is extra key material from the port and plug-ins.
Raises:
| Type | Description |
|---|---|
TypeError
|
|
tool_key(namespace, tool, args, *, scope, run_id, thread_id)
¶
Key for a tool result; "" when the call cannot be cached (no run or thread id).
Raises:
| Type | Description |
|---|---|
TypeError
|
|
ToolUsageError
|
unknown |
Also exported here, documented under the canonical path: MemoryCache.