{
  "schema_version": 2,
  "id": "integrate/litellm",
  "title": "Redis with LiteLLM",
  "url": "https://redis.io/docs/latest/integrate/litellm/",
  "summary": "Cache LLM responses by exact match or semantic similarity, and share rate-limit counters across LiteLLM instances, with Redis.",
  "tags": [
    "docs",
    "integrate",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-11-23T13:52:33Z",
  "children": [],
  "page_type": "content",
  "content_hash": "0f52d8735a5ff627764e6ae77df4fc77c8bdef40eee7a7e454bb28d0d21bd9ec",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "[LiteLLM](https://docs.litellm.ai/) gives you one interface to many LLM providers. You can use it as a Python library or run it as a proxy server. Redis supports it in three ways:\n\n| Use | What Redis stores |\n|-----|-------------------|\n| **Response cache** | Model responses, keyed by the request, so an identical request skips the provider call. |\n| **Semantic cache** | Responses and prompt embeddings, so a request with the same meaning as an earlier one skips the provider call. |\n| **Router usage counters** | Per-deployment request and token counts, so several LiteLLM processes count against the same limit. |\n\nSee the [semantic cache](https://redis.io/docs/latest/develop/use-cases/semantic-cache) and\n[rate limiter](https://redis.io/docs/latest/develop/use-cases/rate-limiter) use cases for more\ninformation about when to use these patterns."
    },
    {
      "id": "requirements",
      "title": "Requirements",
      "role": "content",
      "text": "- Python 3.10 to 3.15\n- Redis with [Redis Search](https://redis.io/docs/latest/develop/ai/search-and-query) for the semantic cache \n(Redis 8 and later versions include it but earlier versions need Redis Stack). The response cache and router \ncounters work on any Redis server.\n\nInstall LiteLLM with the `proxy` extra, plus the Redis clients:\n\n[code example]\n\nA plain `pip install litellm` is not enough. A cached `completion()` call fails with `ModuleNotFoundError: No module named 'fastapi'`, because the caching code imports `fastapi`, which only the `proxy` extra installs. LiteLLM doesn't install `redis` or `redisvl` itself, and the semantic cache needs both.\n\nThe examples on this page were run with LiteLLM 1.110.0 and Redis 8.10. They use `mock_response` so that they run without a provider key. Replace `mock_response` with the key for your provider to call a real model."
    },
    {
      "id": "cache-responses",
      "title": "Cache responses",
      "role": "content",
      "text": "Set `litellm.cache` to a `Cache` of type `redis`, then pass `caching=True` on each call that should use it:\n\n[code example]\n\nLiteLLM builds the cache key from the model, the messages, and the other request parameters\n(so two requests that only differ in the `temperature` value will still have separate\ncache entries, for example). Each entry is a Redis string with the key `<namespace>:<hash>`\nand a time to live (TTL) of `ttl` seconds. `port` is a string.\n\nTo pass a password or use TLS, set `password` and the other `Cache` arguments, or put the connection settings in environment variables such as `REDIS_HOST`, `REDIS_PORT`, and `REDIS_PASSWORD`."
    },
    {
      "id": "cache-by-meaning-with-the-semantic-cache",
      "title": "Cache by meaning with the semantic cache",
      "role": "content",
      "text": "The `redis-semantic` cache embeds each prompt, searches a Redis vector index for a stored prompt within a similarity threshold, and returns that prompt's response:\n\n[code example]\n\n- `similarity_threshold` is a value from 0 to 1. A higher value needs a closer match. Tune it on your own prompts.\n- `redis_semantic_cache_embedding_model` is any embedding model that LiteLLM can call. It runs on every request, so it adds a call and a cost even for a cache hit.\n- Pass the connection as `redis_url`. With `host` and `port` alone, the cache also needs a password and raises `Missing required Redis configuration: REDIS_PASSWORD` without one.\n- LiteLLM creates the index on the first request. It takes the vector size from the embedding model, so you don't define a schema.\n\nThe semantic cache returns an earlier response for any prompt that is close enough, but this can be wrong when a small change in wording changes the answer. Set the threshold high, or use the exact-match cache, for prompts where fine discrimination is important."
    },
    {
      "id": "share-rate-limits-across-instances",
      "title": "Share rate limits across instances",
      "role": "content",
      "text": "A LiteLLM `Router` spreads requests over model deployments and can enforce `rpm` (requests per minute) and `tpm` (tokens per minute) limits on each one. By default, each process counts its own usage. Pass `redis_host` and `redis_port` and the router counts in Redis instead:\n\n[code example]\n\nEvery process that runs this code adds to the same counters, so the `rpm` limit applies to all of them together. In a test with a limit of 4, a first process sent 5 requests and 3 succeeded. A second process then sent 5 requests and none succeeded. Without `redis_host`, each process allowed 3 or 4.\n\nThe router writes one key per deployment and minute for requests and for tokens, in the form `<deployment hash>:<model>:rpm:<hour>-<minute>`, with a TTL of about a minute. It also writes a second set of keys with a `global_router:` prefix. Treat the limit as approximate: in the test above, a limit of 4 let 3 requests through."
    },
    {
      "id": "run-the-proxy-with-redis",
      "title": "Run the proxy with Redis",
      "role": "content",
      "text": "In the proxy, set the same options in `config.yaml`:\n\n[code example]\n\nStart the proxy with a master key:\n\n[code example]\n\nThe proxy refuses to start without a strong master key. A repeated request returns a `x-litellm-cache-key` response header with the Redis key of the cached entry. The first request, which isn't cached, has no such header.\n\nIn testing, the proxy wrote its `rpm` and `tpm` counters to Redis when `router_settings` named a Redis server. It didn't reject requests above an `rpm` set on a single deployment, so check how your own deployment behaves before you rely on a proxy limit."
    },
    {
      "id": "more-information",
      "title": "More information",
      "role": "content",
      "text": "- [LiteLLM caching](https://docs.litellm.ai/docs/caching/all_caches)\n- [LiteLLM routing](https://docs.litellm.ai/docs/routing)\n- [RedisVL](https://redis.io/docs/latest/develop/ai/redisvl), which LiteLLM uses for the semantic cache\n- [Redis LangCache](https://redis.io/docs/latest/develop/ai/langcache), a managed semantic cache"
    }
  ],
  "examples": [
    {
      "id": "requirements-ex0",
      "language": "bash",
      "code": "pip install 'litellm[proxy]' redis redisvl",
      "section_id": "requirements"
    },
    {
      "id": "cache-responses-ex0",
      "language": "python",
      "code": "import litellm\nfrom litellm import completion\nfrom litellm.caching.caching import Cache\n\nlitellm.cache = Cache(type=\"redis\", host=\"localhost\", port=\"6379\", ttl=120, namespace=\"llm\")\n\nmessages = [{\"role\": \"user\", \"content\": \"What is Redis?\"}]\n\nfirst = completion(model=\"gpt-4o-mini\", messages=messages, mock_response=\"An in-memory data store.\", caching=True)\nsecond = completion(model=\"gpt-4o-mini\", messages=messages, mock_response=\"An in-memory data store.\", caching=True)\n\nprint(second._hidden_params.get(\"cache_hit\"))  # True",
      "section_id": "cache-responses"
    },
    {
      "id": "cache-by-meaning-with-the-semantic-cache-ex0",
      "language": "python",
      "code": "litellm.cache = Cache(\n    type=\"redis-semantic\",\n    redis_url=\"redis://localhost:6379\",\n    similarity_threshold=0.8,\n    redis_semantic_cache_embedding_model=\"text-embedding-3-small\",\n    redis_semantic_cache_index_name=\"llm_semantic_cache\",\n)\n\ndef ask(question):\n    response = completion(\n        model=\"gpt-4o-mini\",\n        messages=[{\"role\": \"user\", \"content\": question}],\n        caching=True,\n    )\n    return response._hidden_params.get(\"cache_hit\"), response.choices[0].message.content\n\nprint(ask(\"How do I reset my password on the website?\"))   # miss: calls the model\nprint(ask(\"How can I reset the password on this website?\"))  # hit: same meaning\nprint(ask(\"What is the capital of France?\"))                # miss: different meaning",
      "section_id": "cache-by-meaning-with-the-semantic-cache"
    },
    {
      "id": "share-rate-limits-across-instances-ex0",
      "language": "python",
      "code": "from litellm import Router\n\nrouter = Router(\n    model_list=[\n        {\n            \"model_name\": \"chat\",\n            \"litellm_params\": {\"model\": \"gpt-4o-mini\", \"rpm\": 100},\n        }\n    ],\n    routing_strategy=\"usage-based-routing-v2\",\n    enable_pre_call_checks=True,\n    redis_host=\"localhost\",\n    redis_port=6379,\n)",
      "section_id": "share-rate-limits-across-instances"
    },
    {
      "id": "run-the-proxy-with-redis-ex0",
      "language": "yaml",
      "code": "model_list:\n  - model_name: gpt-4o-mini\n    litellm_params:\n      model: openai/gpt-4o-mini\n      api_key: os.environ/OPENAI_API_KEY\n\nlitellm_settings:\n  cache: true\n  cache_params:\n    type: redis\n    host: localhost\n    port: 6379\n    ttl: 600\n    namespace: \"litellm.caching\"\n\nrouter_settings:\n  routing_strategy: usage-based-routing-v2\n  redis_host: localhost\n  redis_port: 6379",
      "section_id": "run-the-proxy-with-redis"
    },
    {
      "id": "run-the-proxy-with-redis-ex1",
      "language": "bash",
      "code": "export LITELLM_MASTER_KEY=<your-master-key>\nlitellm --config config.yaml --port 4000",
      "section_id": "run-the-proxy-with-redis"
    }
  ]
}
