cache¶
cache
¶
Semantic caching for ADK agents using Redis.
LLMResponseCache
¶
LLMResponseCache(provider: BaseCacheProvider, config: Optional[LLMResponseCacheConfig] = None)
Cache service for LLM responses.
This service caches LLM responses using semantic similarity matching. It can be configured to only cache first messages in a session to reduce API costs for common initial queries.
Initialize the LLM response cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
BaseCacheProvider
|
The cache provider to use for storage. |
required |
config
|
Optional[LLMResponseCacheConfig]
|
Configuration for caching behavior. |
None
|
Source code in src/adk_redis/cache/llm_cache.py
before_model_callback
async
¶
before_model_callback(callback_context: CallbackContext, llm_request: LlmRequest) -> Optional[LlmResponse]
Check cache before making LLM call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback_context
|
CallbackContext
|
The callback context with session info. |
required |
llm_request
|
LlmRequest
|
The LLM request being made. |
required |
Returns:
| Type | Description |
|---|---|
Optional[LlmResponse]
|
LlmResponse if cache hit, None to proceed with LLM call. |
Source code in src/adk_redis/cache/llm_cache.py
after_model_callback
async
¶
after_model_callback(callback_context: CallbackContext, llm_response: LlmResponse) -> Optional[LlmResponse]
Store response in cache after LLM call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
callback_context
|
CallbackContext
|
The callback context with session info. |
required |
llm_response
|
LlmResponse
|
The LLM response to cache. |
required |
Returns:
| Type | Description |
|---|---|
Optional[LlmResponse]
|
None to pass through the original response. |
Source code in src/adk_redis/cache/llm_cache.py
LLMResponseCacheConfig
¶
Bases: BaseModel
Configuration for LLM response caching.
Attributes:
| Name | Type | Description |
|---|---|---|
first_message_only |
bool
|
Only cache first message in session. |
include_app_name |
bool
|
Include app name in cache key. |
include_user_id |
bool
|
Include user ID in cache key. |
include_session_id |
bool
|
Include session ID in cache key. |
BaseCacheProvider
¶
Bases: ABC
Abstract base class for cache providers.
Entry identity contract
Providers surface a stable per-entry identifier when the backend exposes one. check() populates CacheEntry.entry_id on hits and store() returns the identifier of the newly written entry. Both LangCacheProvider and RedisVLCacheProvider provide backend entry IDs; a backend that cannot provide an identifier must leave CacheEntry.entry_id as None and return None from store(). Identifiers from check() and store() are interchangeable inputs to delete_by_id() for the same provider instance.
check
abstractmethod
async
¶
check(prompt: str, **kwargs: Any) -> Optional[CacheEntry]
Check if a semantically similar prompt exists in the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt to look up. |
required |
**kwargs
|
Any
|
Provider-specific options. |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[CacheEntry]
|
A CacheEntry on a hit (with entry_id populated when the backend |
Optional[CacheEntry]
|
provides one), None on a miss. |
Source code in src/adk_redis/cache/_provider.py
store
abstractmethod
async
¶
store(prompt: str, response: str, metadata: Optional[dict[str, Any]] = None, **kwargs: Any) -> Optional[str]
Store a prompt-response pair in the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt text. |
required |
response
|
str
|
The response text. |
required |
metadata
|
Optional[dict[str, Any]]
|
Optional metadata to store alongside the entry. |
None
|
**kwargs
|
Any
|
Provider-specific options. |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The identifier of the newly written entry when the backend |
Optional[str]
|
provides one, otherwise None. The identifier can be passed to |
Optional[str]
|
delete_by_id() to retire exactly this entry. |
Source code in src/adk_redis/cache/_provider.py
delete_by_id
abstractmethod
async
¶
Delete exactly one entry from the cache by its identifier.
Unlike clear(), which removes every entry in the cache, this targets a single entry, identified by the entry_id obtained from check() or store(). Deleting an identifier that no longer exists is a no-op.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entry_id
|
str
|
The identifier of the entry to delete, as returned by check() (CacheEntry.entry_id) or store() on this provider. |
required |
**kwargs
|
Any
|
Provider-specific options. |
{}
|
Source code in src/adk_redis/cache/_provider.py
clear
abstractmethod
async
¶
Clear all entries from the cache.
For removing a single entry, use delete_by_id() instead.
CacheEntry
dataclass
¶
CacheEntry(prompt: str, response: str, distance: Optional[float] = None, metadata: Optional[dict[str, Any]] = None, entry_id: Optional[str] = None)
Represents a cached entry.
Attributes:
| Name | Type | Description |
|---|---|---|
prompt |
str
|
The prompt text that was matched. |
response |
str
|
The cached response text. |
distance |
Optional[float]
|
The vector distance of the match, if available. |
metadata |
Optional[dict[str, Any]]
|
Optional metadata stored alongside the entry. |
entry_id |
Optional[str]
|
Stable backend identifier for the matched entry, if the backend provides one. For LangCacheProvider this is the LangCache entry ID; for RedisVLCacheProvider this is the RedisVL entry ID. It can be passed to delete_by_id() to retire exactly this entry. None when the backend does not expose an identifier. |
LangCacheProvider
¶
LangCacheProvider(config: LangCacheProviderConfig)
Bases: BaseCacheProvider
Cache provider using Redis LangCache (managed semantic cache service).
LangCache is a managed semantic caching service that handles embedding generation, storage, and retrieval. Unlike RedisVLCacheProvider, it does not require a local vectorizer; embeddings are handled server-side.
Requires redisvl>=0.11.1 with LangCache support.
Initialize the LangCache cache provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
LangCacheProviderConfig
|
Configuration for the LangCache provider. |
required |
Raises:
| Type | Description |
|---|---|
ImportError
|
If redisvl>=0.11.1 is not installed. |
Source code in src/adk_redis/cache/_provider.py
check
async
¶
check(prompt: str, **kwargs: Any) -> Optional[CacheEntry]
Check for a semantically similar prompt in LangCache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt to check. |
required |
**kwargs
|
Any
|
Additional keyword arguments (e.g., distance_threshold). |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[CacheEntry]
|
A CacheEntry if a cache hit is found, None otherwise. entry_id is |
Optional[CacheEntry]
|
the LangCache entry ID of the matched entry and can be passed to |
Optional[CacheEntry]
|
delete_by_id(). |
Source code in src/adk_redis/cache/_provider.py
store
async
¶
store(prompt: str, response: str, metadata: Optional[dict[str, Any]] = None, **kwargs: Any) -> Optional[str]
Store a prompt-response pair in LangCache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt text. |
required |
response
|
str
|
The response text. |
required |
metadata
|
Optional[dict[str, Any]]
|
Optional metadata to store alongside the entry. |
None
|
**kwargs
|
Any
|
Additional keyword arguments (e.g., ttl). |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The LangCache entry ID of the newly written entry. It can be |
Optional[str]
|
passed to delete_by_id() to retire exactly this entry. |
Source code in src/adk_redis/cache/_provider.py
delete_by_id
async
¶
Delete exactly one LangCache entry by its entry ID.
Unlike clear(), which removes every entry, this deletes only the entry identified by the LangCache entry ID obtained from check() or store().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entry_id
|
str
|
The LangCache entry ID of the entry to delete. |
required |
**kwargs
|
Any
|
Additional keyword arguments (unused). |
{}
|
Source code in src/adk_redis/cache/_provider.py
clear
async
¶
Clear all entries from the LangCache.
For removing a single entry, use delete_by_id() instead.
close
async
¶
LangCacheProviderConfig
¶
Bases: BaseModel
Configuration for LangCache (managed semantic cache) provider.
LangCache is a managed semantic caching service provided by Redis. It requires a cache_id and api_key from the LangCache service.
RedisVLCacheProvider
¶
RedisVLCacheProvider(config: RedisVLCacheProviderConfig, vectorizer: Any)
Bases: BaseCacheProvider
Cache provider using RedisVL's SemanticCache.
Initialize the RedisVL cache provider.
Source code in src/adk_redis/cache/_provider.py
check
async
¶
check(prompt: str, **kwargs: Any) -> Optional[CacheEntry]
Check for a semantically similar prompt in the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt to look up. |
required |
**kwargs
|
Any
|
Additional keyword arguments (unused). |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[CacheEntry]
|
A CacheEntry on a hit, None on a miss. entry_id is the RedisVL entry |
Optional[CacheEntry]
|
ID of the matched entry and can be passed to delete_by_id(). |
Source code in src/adk_redis/cache/_provider.py
store
async
¶
store(prompt: str, response: str, metadata: Optional[dict[str, Any]] = None, **kwargs: Any) -> Optional[str]
Store a prompt-response pair in the cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt text. |
required |
response
|
str
|
The response text. |
required |
metadata
|
Optional[dict[str, Any]]
|
Optional metadata (unused by this provider). |
None
|
**kwargs
|
Any
|
Additional keyword arguments (unused). |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The RedisVL entry ID of the newly written entry. It can be passed to |
Optional[str]
|
delete_by_id() to retire exactly this entry. |
Source code in src/adk_redis/cache/_provider.py
delete_by_id
async
¶
Delete exactly one cache entry by its RedisVL entry ID.
Unlike clear(), which removes every entry, this drops only the entry identified by the RedisVL entry ID obtained from check() or store().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entry_id
|
str
|
The RedisVL entry ID of the entry to delete. |
required |
**kwargs
|
Any
|
Additional keyword arguments (unused). |
{}
|
Source code in src/adk_redis/cache/_provider.py
clear
async
¶
Clear all entries from the cache.
For removing a single entry, use delete_by_id() instead.
close
async
¶
Close the cache provider and release resources.
Source code in src/adk_redis/cache/_provider.py
RedisVLCacheProviderConfig
¶
Bases: BaseModel
Configuration for RedisVL cache provider.
ToolCache
¶
ToolCache(provider: BaseCacheProvider, config: Optional[ToolCacheConfig] = None)
Cache service for tool call results.
This service caches tool results using semantic similarity matching on the tool name and arguments. It can be configured to only cache specific tools.
Initialize the tool cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
BaseCacheProvider
|
The cache provider to use for storage. |
required |
config
|
Optional[ToolCacheConfig]
|
Configuration for caching behavior. |
None
|
Source code in src/adk_redis/cache/tool_cache.py
before_tool_callback
async
¶
before_tool_callback(tool: BaseTool, args: Dict[str, Any], tool_context: ToolContext) -> Optional[Dict[str, Any]]
Check cache before executing tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
The tool being called. |
required |
args
|
Dict[str, Any]
|
The arguments for the tool call. |
required |
tool_context
|
ToolContext
|
The tool context with session info. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
Dict if cache hit (skips tool execution), None to proceed. |
Source code in src/adk_redis/cache/tool_cache.py
after_tool_callback
async
¶
after_tool_callback(tool: BaseTool, args: Dict[str, Any], tool_context: ToolContext, tool_response: Dict[str, Any]) -> Optional[Dict[str, Any]]
Store tool result in cache after execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
The tool that was called. |
required |
args
|
Dict[str, Any]
|
The arguments used for the tool call. |
required |
tool_context
|
ToolContext
|
The tool context with session info. |
required |
tool_response
|
Dict[str, Any]
|
The result from the tool execution. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Dict[str, Any]]
|
None to pass through the original response. |
Source code in src/adk_redis/cache/tool_cache.py
ToolCacheConfig
¶
Bases: BaseModel
Configuration for tool result caching.
Attributes:
| Name | Type | Description |
|---|---|---|
tool_names |
Optional[Set[str]]
|
Set of tool names to cache. None means cache all tools. |
include_app_name |
bool
|
Include app name in cache key. |
include_user_id |
bool
|
Include user ID in cache key. |
include_session_id |
bool
|
Include session ID in cache key. |
create_llm_cache_callbacks
¶
create_llm_cache_callbacks(cache: LLMResponseCache) -> Tuple[BeforeModelCallback, AfterModelCallback]
Create callback functions for LLM response caching.
This factory function wraps the LLMResponseCache methods into standalone callback functions compatible with ADK's Agent constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache
|
LLMResponseCache
|
The LLMResponseCache instance to use. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[BeforeModelCallback, AfterModelCallback]
|
A tuple of (before_model_callback, after_model_callback) functions. |
Example
Source code in src/adk_redis/cache/callbacks.py
create_tool_cache_callbacks
¶
create_tool_cache_callbacks(cache: ToolCache) -> Tuple[BeforeToolCallback, AfterToolCallback]
Create callback functions for tool result caching.
This factory function wraps the ToolCache methods into standalone callback functions compatible with ADK's Agent constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache
|
ToolCache
|
The ToolCache instance to use. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[BeforeToolCallback, AfterToolCallback]
|
A tuple of (before_tool_callback, after_tool_callback) functions. |