litellm - ✅(Solved) Fix [Bug]: Redis env vars ignored when cache_params has any keys - breaks spend tracking with multiple pods [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
BerriAI/litellm#26233Fetched 2026-04-23 07:24:31
View on GitHub
Comments
0
Participants
1
Timeline
7
Reactions
0
Participants
Timeline (top)
referenced ×4labeled ×2cross-referenced ×1

Root Cause

This is a problem because spend tracking between pods relies on Redis to sync counters. Without it, each pod tracks usage independently and the whole thing falls apart.

Fix Action

Fix / Workaround

Workaround: Adding type: redis explicitly fixes it:

cache_params:
  type: redis
  mode: default_off

PR fix notes

PR #26244: fix(proxy): load REDIS_* env vars when cache_params has non-connection keys

Description (problem / solution / changelog)

Relevant issues

Fixes #26233

Type

Bug Fix

Changes

Problem

In ProxyConfig.load_config, the Redis env-var fallback is gated on len(cache_params.keys()) == 0:

if (cache_type == "redis" or cache_type == "redis-semantic") \
    and len(cache_params.keys()) == 0:
    # load REDIS_HOST / REDIS_PORT / REDIS_PASSWORD

This means a config like:

litellm_settings:
  cache: true
  cache_params:
    mode: default_off

combined with REDIS_HOST + REDIS_PORT env vars silently falls back to InMemoryCache — because the presence of mode makes cache_params non-empty, skipping the env-var branch entirely.

Downstream, spend_counter_cache.redis_cache stays None and per-pod counters diverge. In multi-pod deployments this breaks spend tracking and budget enforcement without any error.

Fix

Replace the length check with "no connection details supplied":

if (cache_type == "redis" or cache_type == "redis-semantic") and (
    "host" not in cache_params and "url" not in cache_params
):

The env-var fallback now triggers whenever the user has not specified host or url. Other cache_params keys (mode, ttl, default_in_memory_ttl, etc.) no longer suppress it.

Tests

Two new tests in tests/test_litellm/proxy/test_proxy_server.py:

  1. test_redis_env_vars_loaded_when_cache_params_has_non_connection_keys — regression test for the reported bug.
  2. test_explicit_cache_params_host_not_overwritten_by_env_vars — ensures env-var fallback does not clobber user-supplied host.

Both patch ProxyConfig._init_cache and assert the computed cache_params dict contains the expected Redis connection values.

Pre-Submission checklist

  • Added testing in tests/test_litellm/
  • PR passes make test-unit on the touched file (110/110 in test_proxy_server.py)
  • Scope isolated to a single bug
  • Greptile review (will request after submission)

Changed files

  • litellm/proxy/proxy_server.py (modified, +7/-3)
  • tests/test_litellm/proxy/test_proxy_server.py (modified, +107/-0)

Code Example

if (
    cache_type == "redis" or cache_type == "redis-semantic"
) and len(cache_params.keys()) == 0:
    cache_host = get_secret("REDIS_HOST", None)
    # ...

---

cache_params:
  type: redis
  mode: default_off

---

if cache_type == "redis" and "host" not in cache_params:
    cache_host = get_secret("REDIS_HOST", None)
    if cache_host is not None:
        cache_params.update({
            "type": "redis",
            "host": cache_host,
            "port": get_secret("REDIS_PORT", None),
        })
        # etc

---

REDIS_HOST=our-redis-host
REDIS_PORT=6379
REDIS_PASSWORD=xxx

---

litellm_settings:
  cache: true
  cache_params:
    mode: default_off

---
RAW_BUFFERClick to expand / collapse

Check for existing issues

  • I have searched the existing issues and checked that my issue is not a duplicate.

What happened?

Found a bug that broke our spend tracking in production. When you set cache: true with cache_params that has literally any key in it (like mode: default_off), the proxy completely ignores Redis environment variables and falls back to in-memory cache instead.

This is a problem because spend tracking between pods relies on Redis to sync counters. Without it, each pod tracks usage independently and the whole thing falls apart.

Took me a while to debug but the issue is in proxy_server.py around line 3138:

if (
    cache_type == "redis" or cache_type == "redis-semantic"
) and len(cache_params.keys()) == 0:
    cache_host = get_secret("REDIS_HOST", None)
    # ...

The len(cache_params.keys()) == 0 check means it only loads Redis from env vars when cache_params is completely empty. If you add mode or any other key, this condition fails and Redis config from env never gets added.

Then later when the code checks isinstance(litellm.cache.cache, RedisCache) to set up spend_counter_cache, it returns False because we got InMemoryCache instead.

Workaround: Adding type: redis explicitly fixes it:

cache_params:
  type: redis
  mode: default_off

But this isn't obvious and people using mode: default_off probably won't realize they need to also specify the type.

Possible fix: Instead of checking if cache_params is empty, check if Redis env vars actually exist and use them when type/host aren't already specified in cache_params. Something like:

if cache_type == "redis" and "host" not in cache_params:
    cache_host = get_secret("REDIS_HOST", None)
    if cache_host is not None:
        cache_params.update({
            "type": "redis",
            "host": cache_host,
            "port": get_secret("REDIS_PORT", None),
        })
        # etc

This bit us pretty hard in production because usage tracking silently failed and we only noticed when budgets weren't enforced correctly. Worth fixing since multi-pod deployments are probably pretty common.

Steps to Reproduce

How to reproduce:

We have Redis configured via env vars:

REDIS_HOST=our-redis-host
REDIS_PORT=6379
REDIS_PASSWORD=xxx

And this config:

litellm_settings:
  cache: true
  cache_params:
    mode: default_off

Running multiple pods, making requests through different ones, and spend tracking is completely wrong.

What I expected: Redis should connect using the env vars and sync spend counters across pods.

What actually happens:

  • Cache gets initialized as InMemoryCache instead of RedisCache
  • spend_counter_cache.redis_cache stays None
  • Each pod has its own local counters
  • Usage tracking doesn't work properly

Relevant log output

What part of LiteLLM is this about?

Proxy

What LiteLLM version are you on ?

v1.28.0

Twitter / LinkedIn details

No response

extent analysis

TL;DR

The issue can be fixed by modifying the proxy_server.py to check for Redis environment variables and update cache_params accordingly, even when cache_params is not empty.

Guidance

  • Verify that the issue is caused by the len(cache_params.keys()) == 0 check in proxy_server.py by adding a debug log or print statement to confirm the condition is not met when cache_params contains keys.
  • Update the proxy_server.py code to check for Redis environment variables and update cache_params as suggested in the possible fix section.
  • Test the updated code with the provided reproduction steps to ensure the issue is resolved.
  • Consider adding a check to ensure that the type key in cache_params is set to "redis" when Redis environment variables are used.

Example

if cache_type == "redis" and "host" not in cache_params:
    cache_host = get_secret("REDIS_HOST", None)
    if cache_host is not None:
        cache_params.update({
            "type": "redis",
            "host": cache_host,
            "port": get_secret("REDIS_PORT", None),
        })
        # etc

Notes

The provided workaround of adding type: redis to cache_params may not be obvious to users, so updating the code to handle this case is a better solution.

Recommendation

Apply the suggested fix to update the proxy_server.py code to handle Redis environment variables correctly, as this will ensure that spend tracking works properly in multi-pod deployments.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

litellm - ✅(Solved) Fix [Bug]: Redis env vars ignored when cache_params has any keys - breaks spend tracking with multiple pods [1 pull requests, 1 participants]