redis_openai_agents.SemanticCache#

class SemanticCache(redis_url='redis://localhost:6379', similarity_threshold=0.9, ttl=None, name='llm_cache', pool=None)[source]#

Two-level semantic cache for LLM responses.

Uses Redis for persistent caching with semantic similarity matching. Level 1 uses fast O(1) hash lookup for exact matches, while Level 2 uses vector similarity search for semantic matches.

Example

>>> cache = SemanticCache(redis_url="redis://localhost:6379")
>>> cache.set(query="What is Redis?", response="Redis is a database.")
>>> result = cache.get(query="Tell me about Redis")
>>> if result:
...     print(f"Hit! {result.response}")
Parameters:
  • redis_url (str) – Redis connection URL

  • similarity_threshold (float) – Minimum similarity for semantic matches (0.0-1.0)

  • ttl (int | None) – Time-to-live in seconds (None = no expiration)

  • name (str) – Cache index name in Redis

  • pool (RedisConnectionPool | None)

Initialize the semantic cache.

Parameters:
  • redis_url (str) – Redis connection URL

  • similarity_threshold (float) – Minimum similarity for semantic matches (0.0-1.0). Higher values require closer matches.

  • ttl (int | None) – Time-to-live in seconds (None = no expiration)

  • name (str) – Cache index name in Redis

  • pool (RedisConnectionPool | None) – Optional shared connection pool

__init__(redis_url='redis://localhost:6379', similarity_threshold=0.9, ttl=None, name='llm_cache', pool=None)[source]#

Initialize the semantic cache.

Parameters:
  • redis_url (str) – Redis connection URL

  • similarity_threshold (float) – Minimum similarity for semantic matches (0.0-1.0). Higher values require closer matches.

  • ttl (int | None) – Time-to-live in seconds (None = no expiration)

  • name (str) – Cache index name in Redis

  • pool (RedisConnectionPool | None) – Optional shared connection pool

Return type:

None

Methods

__init__([redis_url, similarity_threshold, ...])

Initialize the semantic cache.

aget(query)

Async version of get() - check cache for a matching response.

aset(query, response[, metadata])

Async version of set() - store a query-response pair in the cache.

clear()

Clear all cache entries and reset statistics.

get(query)

Check cache for a matching response.

get_stats()

Get cache hit/miss statistics.

set(query, response[, metadata])

Store a query-response pair in the cache.

Attributes

name

Cache index name in Redis.

similarity_threshold

Minimum similarity score for cache hits.

ttl

Time-to-live in seconds for cache entries.