Repository navigation
fix: cache custom /v1/models lookups by bare model id and memoize misses - #187
linhongyu510 wants to merge 1 commit into
Conversation
_fetch_custom_model_limit keyed its cache on the full model string
('custom/tools-pool') while the /v1/models response stores bare ids
('tools-pool'), so every lookup missed and neither caller remembered a
miss — GET /v1/models fired before each chat completion (184 GETs for
47 completions in one long session on 3.5.87, SAIL-Research-Lab#183). Endpoints that do
not report max_model_len/context_window were asked forever.
Look the cache up by bare_model(model) and remember a miss as 0
(returned as None), so an undescribed model is asked about once per
process instead of per call. Network failures are not memoized.
|
Merged — thanks for this. #182 had already been fixed on main by #188 while this was open, so I resolved the conflict on my side rather than asking you to: main's version is kept (it also accepts I'm closing #187 as superseded by #188. One note in case it's useful: calling Both were good catches. Sorry for the overlap. |
Problem
_fetch_custom_model_limit(used byresolve_max_tokensandget_model_context_window) receives the full model string (custom/tools-pool) but fills_custom_ctx_cache[base_url]with the bare ids the endpoint returns (tools-pool).if model in cache/cache.get(model)therefore miss on every call, and a miss is not remembered — so both callers issueGET /v1/modelsbefore each chat completion: 184 GETs for 47 completions in one long session on 3.5.87 (#183). Endpoints that do not reportmax_model_len/context_window(e.g. LiteLLM'smax_input_tokens) are asked forever.Fix
Two small changes in
cheetahclaws/providers.py:bare_model(model)so the prefixed caller key and the bare response id actually match.0(returned asNone), so a model the endpoint does not describe is asked about once per process instead of before every call. Transient network failures are not memoized — a later successful fetch still works.This matches the approach described by the issue reporter's attached patch; the implementation here is written independently, with test coverage added for the cache-hit, cross-form (bare vs prefixed), memoized-miss, and network-failure paths.
Tests
New
tests/test_custom_provider_cache.py, 4 tests (all fail onmain, pass here):Verification: the 4 new tests pass; regression run of
test_custom_provider_cache.py+test_litellm_provider.py+test_nim_provider.py+test_prompt_cache.py+test_skills.py= 92 passed.ruff checkonproviders.pyreports the same 16 pre-existing findings asmain— no new lint issues. No live endpoint was used; the HTTP layer is stubbed.