{
  "id": "semantic-caching",
  "title": "Semantic caching",
  "url": "https://redis.io/docs/latest/integrate/google-adk/semantic-caching/",
  "summary": "Cache LLM responses and tool results using semantic similarity with Redis.",
  "tags": [
    "docs",
    "integrate",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-04-15T13:31:09-05:00",
  "page_type": "content",
  "content_hash": "26e0dcaef2697778ec1ea5169d9f4042ae9c32ac9dad4428570dc6b38b986a54",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "adk-redis provides semantic caching at two levels: LLM response caching and tool result caching, both backed by Redis. Caching uses ADK's callback system, so enabling it requires no changes to your agent's core logic."
    },
    {
      "id": "how-it-works",
      "title": "How it works",
      "role": "content",
      "text": "Before each LLM call (or tool execution), the cache checks whether a semantically similar prompt already exists in Redis. If so, the cached response is returned immediately. If not, the call proceeds and the response is stored for future lookups."
    },
    {
      "id": "cache-providers",
      "title": "Cache providers",
      "role": "content",
      "text": "Two backends are available:\n\n| Provider | Embeddings | Setup | Best for |\n|----------|-----------|-------|----------|\n| `RedisVLCacheProvider` | Local (you provide vectorizer) | Self-managed Redis | Full control |\n| `LangCacheProvider` | Server-side (managed) | API key from Redis Cloud | Zero embedding overhead |"
    },
    {
      "id": "redisvl-provider-local-embeddings",
      "title": "RedisVL provider (local embeddings)",
      "role": "content",
      "text": "[code example]"
    },
    {
      "id": "langcache-provider-managed",
      "title": "LangCache provider (managed)",
      "role": "content",
      "text": "No local vectorizer needed. Embeddings are generated server-side.\n\n[code example]"
    },
    {
      "id": "llm-response-cache",
      "title": "LLM response cache",
      "role": "content",
      "text": "Intercepts model calls through ADK's `before_model_callback` and `after_model_callback`.\n\n[code example]"
    },
    {
      "id": "configuration-notes",
      "title": "Configuration notes",
      "role": "configuration",
      "text": "- **`first_message_only=True`** caches only the first message in a session. Later messages depend on conversation context, making cache hits unreliable.\n- Function call responses and errors are automatically excluded from caching.\n- **`distance_threshold`** (set on the provider) controls how similar two prompts need to be for a cache hit. `0.0` = exact match only. `0.1` = small phrasing variations. Higher values risk returning wrong cached responses."
    },
    {
      "id": "tool-result-cache",
      "title": "Tool result cache",
      "role": "content",
      "text": "Caches tool executions using `before_tool_callback` and `after_tool_callback`.\n\n[code example]\n\nThe `tool_names` set specifies which tools to cache. Not all tools are idempotent: cache `get_weather` but not `send_email`."
    },
    {
      "id": "more-info",
      "title": "More info",
      "role": "content",
      "text": "- [semantic_cache example](https://github.com/redis-developer/adk-redis/tree/main/examples/semantic_cache): Local caching with RedisVL\n- [langcache_cache example](https://github.com/redis-developer/adk-redis/tree/main/examples/langcache_cache): Managed caching with LangCache"
    }
  ],
  "examples": [
    {
      "id": "redisvl-provider-local-embeddings-ex0",
      "language": "python",
      "code": "from redisvl.utils.vectorize import HFTextVectorizer\nfrom adk_redis.cache import RedisVLCacheProvider, RedisVLCacheProviderConfig\n\nprovider = RedisVLCacheProvider(\n    config=RedisVLCacheProviderConfig(\n        redis_url=\"redis://localhost:6379\",\n        name=\"my_cache\",\n        ttl=3600,\n        distance_threshold=0.1,\n    ),\n    vectorizer=HFTextVectorizer(model=\"redis/langcache-embed-v1\"),\n)",
      "section_id": "redisvl-provider-local-embeddings"
    },
    {
      "id": "langcache-provider-managed-ex0",
      "language": "python",
      "code": "from adk_redis.cache import LangCacheProvider, LangCacheProviderConfig\n\nprovider = LangCacheProvider(\n    config=LangCacheProviderConfig(\n        cache_id=\"your-cache-id\",\n        api_key=\"your-api-key\",\n        ttl=3600,\n    )\n)",
      "section_id": "langcache-provider-managed"
    },
    {
      "id": "llm-response-cache-ex0",
      "language": "python",
      "code": "from adk_redis.cache import (\n    LLMResponseCache,\n    LLMResponseCacheConfig,\n    create_llm_cache_callbacks,\n)\n\nllm_cache = LLMResponseCache(\n    provider=provider,\n    config=LLMResponseCacheConfig(\n        first_message_only=True,\n        include_app_name=True,\n        include_user_id=True,\n    ),\n)\n\nbefore_cb, after_cb = create_llm_cache_callbacks(llm_cache)\n\nagent = Agent(\n    name=\"cached_agent\",\n    model=\"gemini-2.0-flash\",\n    instruction=\"You are a helpful assistant.\",\n    before_model_callback=before_cb,\n    after_model_callback=after_cb,\n)",
      "section_id": "llm-response-cache"
    },
    {
      "id": "tool-result-cache-ex0",
      "language": "python",
      "code": "from adk_redis.cache import (\n    ToolCache,\n    ToolCacheConfig,\n    create_tool_cache_callbacks,\n)\n\ntool_cache = ToolCache(\n    provider=provider,\n    config=ToolCacheConfig(\n        tool_names={\"web_search\", \"get_weather\"},\n    ),\n)\n\nbefore_tool_cb, after_tool_cb = create_tool_cache_callbacks(tool_cache)",
      "section_id": "tool-result-cache"
    }
  ]
}
