perf: increase default LRU cache size to reduce multi-model thrash - #21139
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile OverviewGreptile SummaryIncreases the default LRU cache size (
Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| litellm/constants.py | Changed DEFAULT_MAX_LRU_CACHE_SIZE default from 16 to 64. Added helpful inline comment explaining the rationale. Env var override is preserved. |
| tests/test_litellm/test_constants.py | Added test_default_max_lru_cache_size_is_64 guard test — properly uses monkeypatch to clear the env var before reloading the module. No network calls. |
Sequence Diagram
sequenceDiagram
participant Client
participant Router as litellm.router
participant Utils as litellm.utils
participant CostCalc as litellm.cost_calculator
participant Constants as litellm.constants
participant LRU as functools.lru_cache
Note over Constants: DEFAULT_MAX_LRU_CACHE_SIZE = 64<br/>(was 16, env-overridable)
Client->>Router: completion request
Router->>LRU: _cached_get_model_group_info(group)
LRU-->>Router: cached ModelGroupInfo (if hit)
Router->>Utils: get_model_info(model, provider)
Utils->>LRU: lru_cache lookup (maxsize=64)
LRU-->>Utils: cached ModelInfo (if hit)
Utils->>Utils: _cached_get_model_info_helper(model, provider)
Utils->>LRU: lru_cache lookup (maxsize=64)
LRU-->>Utils: cached ModelInfoBase (if hit)
Utils->>Utils: _select_tokenizer_helper(model)
Utils->>LRU: lru_cache lookup (maxsize=64)
LRU-->>Utils: cached tokenizer (if hit)
Router->>CostCalc: completion_cost()
CostCalc->>LRU: _model_contains_known_llm_provider(model)
LRU-->>CostCalc: cached bool (if hit)
CostCalc-->>Router: cost result
Router-->>Client: response
Last reviewed commit: 73e589f
Additional Comments (1)
|
There was a problem hiding this comment.
Pull request overview
Increases LiteLLM’s shared functools.lru_cache default maxsize to reduce cache eviction thrash in multi-model workloads, and adds a test to lock in the new production default.
Changes:
- Increase
DEFAULT_MAX_LRU_CACHE_SIZEdefault from16to64(still env-overridable). - Add a unit test asserting the default is
64when the env var is unset.
Reviewed changes
Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
litellm/constants.py |
Updates the default LRU cache size constant and documents rationale inline. |
tests/test_litellm/test_constants.py |
Adds a regression test to guard the new default value. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
93b8484
into
BerriAI:litellm_oss_staging_02_17_2026
…erriAI#21139) * perf: increase default LRU cache size to 64 * chore: remove default LRU constant test * docs: update DEFAULT_MAX_LRU_CACHE_SIZE default to 64
Summary
Increase
DEFAULT_MAX_LRU_CACHE_SIZEfrom16to64and sync docs.litellm/constants.py: default changed to64(still fully env-overridable).docs/my-website/docs/proxy/config_settings.md: updatedDEFAULT_MAX_LRU_CACHE_SIZEdoc default from16to64.Why
DEFAULT_MAX_LRU_CACHE_SIZEis shared by several@lru_cachehot paths:litellm.utils.get_model_infolitellm.utils._cached_get_model_info_helperlitellm.router.Router._cached_get_model_group_infolitellm.cost_calculator._model_contains_known_llm_providerlitellm.utils._select_tokenizer_helperWith the default of
16, workloads with >16 active model keys (or model groups) can force near-constant eviction and very low cache hit rates.Benchmark results (local, controlled)
All runs were executed in fresh Python processes with only
DEFAULT_MAX_LRU_CACHE_SIZEchanged.1)
get_model_info(model, provider)- 50 models, round-robin16:0.086815 ms/op, hit rate0%64:0.000414 ms/op, hit rate99.75%2)
_cached_get_model_info_helper(model, provider)- 50 models, round-robin16:0.014779 ms/op, hit rate0%64:0.000286 ms/op, hit rate99.75%3) Router
_cached_get_model_group_info- 50 groups, round-robin16:0.255323 ms/op, hit rate0%64:0.000169 ms/op, hit rate99.75%4) Realistic random traffic shape (uniform over 50 models)
get_model_info(model, provider):16: hit rate32.17%,0.068214 ms/op64: hit rate99.9%,0.000193 ms/opThis reproduces the low-hit-rate pattern expected when active model cardinality is much larger than cache size.
5)
completion_costend-to-end check (openai-only, 50 models)Median over 5 runs:
16:0.216659 ms/op64:0.120326 ms/opWhy
64and not25664removes thrash for common 50-model working sets while keeping memory growth modest.In a local
tracemallocrun populating bothget_model_infoand helper caches:16: current delta ~766 KB64: current delta ~937 KB256: current delta ~1.67 MBSo
64gives the major performance win with significantly lower memory growth than256.Backward compatibility
DEFAULT_MAX_LRU_CACHE_SIZEcan still be set explicitly.