fix: resolve context_length from custom_providers on model switch#13052
fix: resolve context_length from custom_providers on model switch#13052TroyMitchell911 wants to merge 1 commit into
Conversation
Three related bugs caused custom_providers per-model context_length overrides to be ignored, particularly when switching models at runtime: 1. __init__: self._config_context_length was assigned *before* the custom_providers lookup, so per-model overrides were never stored on the agent instance. Moved the assignment after the lookup. 2. switch_model: re-resolve config_context_length from config.yaml for the new (model, base_url) pair instead of reusing the stale value from __init__. Checks both top-level model.context_length and custom_providers per-model overrides. 3. get_model_context_length: when a custom endpoint's /models response omits context_length, fall through to models.dev / OpenRouter / hardcoded defaults instead of returning 128K immediately. Proxy endpoints (LiteLLM, NewAPI, etc.) often omit context_length but serve well-known models whose metadata is available downstream. Signed-off-by: Troy Mitchell <i@troy-y.org>
|
Good work on this fix. Key improvements:
The changes address:
This ensures models served through proxy endpoints correctly report their context lengths. Ready to merge. |
ca849be to
bc0ce12
Compare
The PR is ready to be merged. :) |
|
Thanks for the well-structured fix and the thorough test coverage, @TroyMitchell911! This is an automated hermes-sweeper review. After inspecting current Evidence:
The fix on main is broader in scope than this PR (five call sites vs three), so closing this as superseded. The tests added here may still be worth cherry-picking if they cover paths not already covered by the new |
Summary
Three related bugs caused
custom_providersper-modelcontext_lengthoverrides to be ignored, particularly when switching models at runtime via/model. This resulted in models likegemini-3.1-pro-preview(1M context) being incorrectly reported as 128K when served through proxy endpoints (LiteLLM, NewAPI, etc.).Root Cause
__init__assignment order (run_agent.py):self._config_context_lengthwas assigned before thecustom_providerslookup, so per-model overrides fromcustom_providerswere never stored on the agent instance.switch_modelstale value (run_agent.py): When switching models, the old model's_config_context_lengthwas reused instead of re-resolving fromcustom_providersfor the new(model, base_url)pair.Early return on unknown endpoints (
model_metadata.py): When a custom endpoint's/modelsresponse omittedcontext_length,get_model_context_length()returned 128K immediately instead of falling through to models.dev / OpenRouter / hardcoded defaults.Changes
run_agent.py: Moveself._config_context_lengthassignment after thecustom_providerslookup in__init__.run_agent.py: Inswitch_model(), re-resolveconfig_context_lengthfrom config.yaml (both top-levelmodel.context_lengthandcustom_providersper-model overrides) for the new model.agent/model_metadata.py: Remove the earlyreturn DEFAULT_FALLBACK_CONTEXTfor unknown custom endpoints when/modelslackscontext_length— fall through to downstream registries instead.test_switch_model_context.pyand added new test files for both fixes.Test Plan
gemini-3.1-pro-previewvia a proxy endpoint now correctly reports 1M context.