litellm/litellm/caching
yucheng-berri 432954a2ab
Some checks failed
CodSpeed Benchmarks / benchmarks (push) Waiting to run
GitHub Actions Security Analysis / zizmor (push) Waiting to run
LiteLLM Rust / rustfmt, clippy, test (push) Has been cancelled
fix(cache): make in-memory and disk cache increments atomic (#34013)
* fix(cache): make in-memory and disk increments atomic

* refactor(cache): narrow in-memory increment lock scope

* fix(cache): address follow-up review on increment tests/types

* fix(cache): refresh atomic increment coverage

* test(cache): widen increment race window with non-zero _SlowInt seed

The zero seed was falsy, so InMemoryCache.increment_cache's `get_cache(...) or 0`
and DiskCache.get_cache's truthiness guard both discarded the _SlowInt before
__add__ could run, leaving the sleep-based window-widening inert. Seed a non-zero
value and return _SlowInt from __add__ so the sleep fires on every read-modify-write
in both backends, making the concurrency regression deterministic.

* test(cache): cover InMemoryCache.async_increment delegation

Add a focused async test asserting async_increment accumulates through the
locked sync path, exercising the previously uncovered delegation line.

---------

Co-authored-by: Emerson Gomes <emerson.gomes@thalesgroup.com>
2026-07-20 15:51:01 -07:00
..
__init__.py Add GCS bucket caching support (#13122) 2025-08-04 16:09:33 -07:00
_embedding_router.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
_internal_lru_cache.py (litellm SDK perf improvements) - handle cases when unable to lookup model in model cost map (#7750) 2025-01-13 19:58:46 -08:00
azure_blob_cache.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
base_cache.py style: run black formatter on entire codebase 2026-03-11 17:07:57 -03:00
caching.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
caching_handler.py fix(logging): stop pinning large request payloads past request end (#33455) 2026-07-15 15:28:37 -07:00
disk_cache.py fix(cache): make in-memory and disk cache increments atomic (#34013) 2026-07-20 15:51:01 -07:00
dual_cache.py fix(proxy): stop stale auth cache re-publish so key updates and deletes propagate across replicas (#33565) 2026-07-16 15:00:33 -07:00
gcs_cache.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
in_memory_cache.py fix(cache): make in-memory and disk cache increments atomic (#34013) 2026-07-20 15:51:01 -07:00
llm_caching_handler.py fix: don't close HTTP/SDK clients on LLMClientCache eviction (#22925) 2026-03-05 12:00:38 -08:00
qdrant_semantic_cache.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
Readme.md add azure blob cache support (#12587) 2025-07-15 11:47:38 -07:00
redis_cache.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
redis_cluster_cache.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
redis_semantic_cache.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
s3_cache.py style: unify ruff format width on 120 (#31518) 2026-06-27 12:39:29 -07:00
valkey_semantic_cache.py fix(caching): pass only metadata to valkey semantic async embedding (#32295) 2026-07-07 08:52:03 +03: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. DiskCache
  6. S3Cache
  7. AzureBlobCache
  8. DualCache (updates both Redis and an in-memory cache simultaneously)

Folder Structure

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

Documentation