litellm/litellm/caching
ryan-crabbe-berri cae28c5515
perf(caching): hash response cache keys piece by piece and store under the looked-up key (#52267)
* test(caching): pin response cache keys and cover large image and clamped max_tokens requests

Pins the cache key to the sha256 of the same formatted request text it has always
used, caps memory while hashing a request full of large images, and checks that a
repeat of a request whose max_tokens LiteLLM lowered is served from the cache on
async, sync and no-cache paths and through the proxy.

* perf(caching): hash cache keys piece by piece and reuse the lookup key on every write

Requests carrying large base64 images no longer build the whole request as one
string to hash it. Only the parts holding a large string are walked, so small
requests stay at C speed, and the hashed bytes are unchanged so existing cache
entries and the Rust cache reader keep hitting.

The key is computed once before max_tokens is clamped (async, sync, and when the
read is skipped via no-cache), and every cache write reuses it, so a clamped
request is stored under the key its identical repeat looks up.

* fix(caching): drop a comment that restated the cache check and measure only the key's own memory in the test

* test(code-quality): allow the depth-capped cache key walkers in the recursion detector
2026-11-17 15:58:07 -08: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 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
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 chore(caching)!: remove the disk cache backend (#48114) 2026-10-29 18:02:31 -07:00
redis_batch.py refactor(redis): delete the Python RedisBatch now that every request op is native (#51383) 2026-11-13 03:22:28 +00:00
redis_cache.py refactor(redis): delete the Python RedisBatch now that every request op is native (#51383) 2026-11-13 03:22:28 +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

Documentation