Repository navigation
fix: cache custom /v1/models lookups by bare model id and memoize misses - #187
Open
linhongyu510 wants to merge 1 commit into
Open
linhongyu510 wants to merge 1 commit into
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.