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.
check
abstractmethod
async
¶
check(prompt: str, **kwargs: Any) -> Optional[CacheEntry]
store
abstractmethod
async
¶
Store a prompt-response pair in the cache.
clear
abstractmethod
async
¶
CacheEntry
dataclass
¶
CacheEntry(prompt: str, response: str, distance: Optional[float] = None, metadata: Optional[dict[str, Any]] = None)
Represents a cached entry.
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. |
Source code in src/adk_redis/cache/_provider.py
store
async
¶
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). |
{}
|
Source code in src/adk_redis/cache/_provider.py
clear
async
¶
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.
Source code in src/adk_redis/cache/_provider.py
store
async
¶
Store a prompt-response pair in the cache.
Source code in src/adk_redis/cache/_provider.py
clear
async
¶
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. |