litellm/litellm/caching
yujonglee 4616174c45
refactor(redis): move per-request miss, settlement and shutdown-drain tracking from Python into Rust (#51384)
Rust already sent each request's Redis batches, but Python kept the rules about them. Three pieces of per-request
state now live on the Rust RequestRedis: the keys the request's MGET found absent, the flag saying its committed
spend increment has its reply, and the registry of requests whose post-call writes are still unsent at shutdown.
Python only passes the request to the store call, and the Python test doubles no longer re-implement those rules

Auth write-backs and prefetch reads always ride the request's pre-call batch, so a write inside a request cannot
leave its key marked absent. One post-call flush runs at a time, and the shutdown drain waits for it
2026-11-20 17:57:37 +09:00
..
__init__.py chore(caching)!: remove the disk cache backend (#48114) 2026-10-29 18:02:31 -07:00
_embedding_router.py refactor(router): move router callers onto the entry points and invert the proxy imports (#48847) 2026-10-29 17:26:33 +00:00
_internal_lru_cache.py feat(lint): enforce Final on locals and freeze function parameters (LIT010, LIT011) 2026-08-04 12:54:39 -07:00
affinity_cache.py feat(redis): serve the router's session pins from a native store (#50870) 2026-11-13 03:22:28 +00:00
azure_blob_cache.py refactor(typing): clear 1181 basedpyright Any errors across 431 files (#47789) 2026-10-24 11:31:09 -07:00
base_cache.py refactor(typing): widen delete_nested_value to Mapping and drop two unused Span aliases (#48001) 2026-10-24 19:39:27 +00:00
caching.py fix(caching): allow excluding embeddings from response caching (#52902) 2026-11-19 11:48:35 -08:00
caching_handler.py perf(caching): hash response cache keys piece by piece and store under the looked-up key (#52267) 2026-11-17 15:58:07 -08:00
dual_cache.py perf(caching): format dual cache debug logs lazily (#51902) 2026-11-15 06:50:04 +00:00
evicted_client_closer.py fix(caching): key cached async clients by a per-loop token instead of id(loop) (#48877) 2026-10-29 13:14:48 -07:00
gcs_cache.py refactor(typing): clear 1143 basedpyright Any errors across 273 files (#46574) 2026-10-15 21:08:47 +00:00
in_memory_cache.py feat(router): add deployment rpd and tpd daily limits to enforce_model_rate_limits (#48875) 2026-10-28 15:52:44 -07:00
llm_caching_handler.py fix(caching): key cached async clients by a per-loop token instead of id(loop) (#48877) 2026-10-29 13:14:48 -07:00
qdrant_semantic_cache.py refactor(typing): clear 1141 basedpyright Any errors across 182 files (#50711) 2026-11-11 22:11:47 +00:00
Readme.md fix(caching): allow excluding embeddings from response caching (#52902) 2026-11-19 11:48:35 -08:00
redis_batch.py refactor(redis): move per-request miss, settlement and shutdown-drain tracking from Python into Rust (#51384) 2026-11-20 17:57:37 +09:00
redis_cache.py fix(responses): stop the background stream when a polling response is cancelled (#49384) 2026-11-18 14:33:39 +00:00
redis_cluster_cache.py refactor(typing): clear 1143 basedpyright Any errors across 273 files (#46574) 2026-10-15 21:08:47 +00:00
redis_cluster_node_isolation.py fix(caching): keep a node timeout from forcing a cluster-wide topology reinit on redis-py 8.x (#39349) 2026-09-03 17:31:58 -07:00
redis_semantic_cache.py refactor(typing): clear about 1,100 basedpyright Any errors across 303 files (#49986) 2026-11-06 13:31:15 -08:00
s3_cache.py chore(lint): strip inert type: ignore comments and zero LIT009, LIT010, LIT011 headroom 2026-08-05 02:37:24 -07:00
valkey_semantic_cache.py refactor(typing): clear 1143 basedpyright Any errors across 273 files (#46574) 2026-10-15 21:08:47 +00:00

Caching on LiteLLM

LiteLLM supports multiple caching mechanisms. This allows users to choose the most suitable caching solution for their use case.

The following caching mechanisms are supported:

  1. RedisCache
  2. RedisSemanticCache
  3. QdrantSemanticCache
  4. InMemoryCache
  5. S3Cache
  6. AzureBlobCache
  7. DualCache (updates both Redis and an in-memory cache simultaneously)

Folder Structure

litellm/caching/
├── base_cache.py
├── caching.py
├── caching_handler.py
├── dual_cache.py
├── in_memory_cache.py
├── qdrant_semantic_cache.py
├── redis_cache.py
├── redis_semantic_cache.py
├── s3_cache.py

Embeddings and shared Redis memory

The response cache stores embeddings per input item. A batched embeddings request with N inputs writes N keys, each holding a full vector. A batch job over unique texts gets almost no cache hits, yet it can fill a Redis instance. Once Redis reaches maxmemory, a volatile-lru policy evicts any key with a TTL, and that includes the proxy's rate limit counters, spend and budget state, and auth cache entries when they share the instance. A shorter cache TTL does not prevent this, since the cache can still fill memory before keys expire.

To stop caching embeddings while keeping every other default call type, exclude them:

litellm_settings:
  cache: true
  cache_params:
    type: redis
    excluded_call_types: ["embedding", "aembedding"]

excluded_call_types removes names from supported_call_types, so list both the sync and async name. Listing every call type you do want in supported_call_types without the embedding names has the same effect.

To keep caching embeddings, give the response cache its own Redis instance. A different namespace or database number on the same instance does not help, because maxmemory and eviction apply to the whole instance. Point general_settings.coordination_redis, which holds rate limits, spend tracking and locks, and router_settings (redis_host, redis_port) at a different instance from cache_params:

litellm_settings:
  cache: true
  cache_params:
    type: redis
    host: cache-redis
    port: 6379

general_settings:
  coordination_redis:
    host: coordination-redis
    port: 6379

router_settings:
  redis_host: coordination-redis
  redis_port: 6379

Documentation