Semantic Caching
A semantic cache embeds the prompt and serves the closest cached response whose cosine similarity
clears similarity_threshold, so it can hit on prompts that are similar rather than identical.
LiteLLM supports three backends for it: Qdrant, Valkey and Redis. A response served from a semantic
cache carries an x-litellm-semantic-similarity header.
Semantic caching is designed for single-shot prompts. On multi-turn or agentic traffic it will replay stale responses. Read Semantic Caching and Multi-Turn Agentic Traffic before enabling it for those workloads.
Qdrant
Caching can be enabled by adding the cache key in the config.yaml
Step 1: Add cache to the config.yaml
model_list:
- model_name: fake-openai-endpoint
litellm_params:
model: openai/fake
api_key: fake-key
api_base: https://exampleopenaiendpoint-production.up.railway.app/
- model_name: openai-embedding
litellm_params:
model: openai/text-embedding-3-small
api_key: os.environ/OPENAI_API_KEY
litellm_settings:
set_verbose: True
cache: True # set cache responses to True, litellm defaults to using a redis cache
cache_params:
type: qdrant-semantic
qdrant_semantic_cache_embedding_model: openai-embedding # the model should be defined on the model_list
qdrant_collection_name: test_collection
qdrant_quantization_config: binary
qdrant_semantic_cache_vector_size: 1536 # vector size must match embedding model dimensionality
similarity_threshold: 0.8 # similarity threshold for semantic cache
Step 2: Add Qdrant Credentials to your .env
QDRANT_API_KEY = "16rJUMBRx*************"
QDRANT_API_BASE = "https://5392d382-45*********.cloud.qdrant.io"
Step 3: Run proxy with config
$ litellm --config /path/to/config.yaml
Step 4. Test it
curl -i http://localhost:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $LITELLM_API_KEY" \
-d '{
"model": "fake-openai-endpoint",
"messages": [
{"role": "user", "content": "Hello"}
]
}'
Expect to see x-litellm-semantic-similarity in the response headers when semantic caching is
on
Valkey
Semantic caching on a Valkey instance running the valkey-search module, such as AWS ElastiCache for Valkey. RediSearch and RedisVL are not required.
The valkey-search module must be loaded (check with MODULE LIST / FT._LIST). On AWS ElastiCache, vector search needs a node-based Valkey 8.2+ cluster; a cluster-mode-disabled node group is supported and recommended, and a primary with read replicas is fine since only horizontal sharding is unsupported. ElastiCache Serverless does not support vector search. Multi-shard (cluster-mode-enabled) endpoints are not supported here, so use a cluster-mode-disabled endpoint and scale vertically.
Step 1: Add cache to the config.yaml
model_list:
- model_name: fake-openai-endpoint
litellm_params:
model: openai/fake
api_key: fake-key
api_base: https://exampleopenaiendpoint-production.up.railway.app/
- model_name: openai-embedding
litellm_params:
model: openai/text-embedding-3-small
api_key: os.environ/OPENAI_API_KEY
litellm_settings:
set_verbose: True
cache: True
cache_params:
type: valkey-semantic
host: os.environ/VALKEY_HOST
port: os.environ/VALKEY_PORT
valkey_semantic_cache_embedding_model: openai-embedding # the model should be defined on the model_list
valkey_semantic_cache_index_name: litellm_semantic_cache_index # optional
similarity_threshold: 0.8 # similarity threshold for semantic cache