Skip to content

fix: clear stale _config_context_length when switching providers#24079

Closed
TorvaldUtne wants to merge 1 commit into
NousResearch:mainfrom
TorvaldUtne:fix/context-length-provider-switch
Closed

fix: clear stale _config_context_length when switching providers#24079
TorvaldUtne wants to merge 1 commit into
NousResearch:mainfrom
TorvaldUtne:fix/context-length-provider-switch

Conversation

@TorvaldUtne

Copy link
Copy Markdown

fix: clear stale _config_context_length when switching providers

Problem

When switching from a cloud provider (context_length: 1000000) to a local provider (context_length: 8192) via /model, the top-level model.context_length stored as _config_context_length at startup persists and shadows the new provider's context_length. This causes the context window to show 1M instead of the local provider's actual limit.

Root Cause

_config_context_length is resolved once during __init__ from model.context_length and stored as self._config_context_length. When switch_model() calls get_model_context_length(), it passes this stale value as config_context_length, which takes precedence over the new provider's per-model/per-provider context_length.

Fix

In switch_model(), re-resolve _config_context_length from the live config before updating the context compressor:

  • If the new provider matches the default provider's provider field → keep the top-level context_length
  • If the new provider differs → clear it so the new provider's per-model/per-provider context_length takes effect via get_custom_provider_context_length()

Testing

Added 3 regression tests to tests/run_agent/test_switch_model_context.py:

  • test_switch_to_local_provider_clears_stale_config_context — switching from cloud (1M) to local (8K) clears the stale value
  • test_switch_to_local_no_explicit_ctx — no explicit ctx falls back cleanly
  • test_same_provider_keeps_config_context — switching models within the same provider preserves the config value

Related

When switching from a cloud provider (context_length: 1000000) to a local
provider (context_length: 8192) via /model, the top-level model.context_length
stored as _config_context_length at startup persisted and shadowed the new
provider's context_length. This caused the context window to show 1M instead
of the local provider's actual limit.

The fix re-resolves _config_context_length from the live config when
switching providers: it checks whether the new provider matches the default
provider (and only then uses the top-level context_length), otherwise clears
it so the new provider's per-model/per-provider context_length takes effect.

Regression tests cover: provider switch clears stale value, no explicit ctx
falls back cleanly, same-provider switch preserves config value.
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent loop, run_agent.py, prompt builder labels May 12, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #22387 and #18868 which fix the same root cause (_config_context_length not re-resolved on provider switch). Also overlaps with #11438 (respects per-model context_length from custom_providers). Fixes #24072.

@teknium1

Copy link
Copy Markdown
Contributor

Automated hermes-sweeper review: this stale context-length switching bug is already fixed on current main.

Evidence:

  • agent/agent_runtime_helpers.py:1426 clears agent._config_context_length = None before the live model/provider swap, so a previous provider's configured context window no longer shadows the newly selected provider.
  • agent/agent_runtime_helpers.py:1551 passes the now-cleared config_context_length into get_model_context_length() when refreshing the context compressor.
  • agent/model_metadata.py:1561 shows why this matters: a positive explicit config context length wins before custom-provider/provider resolution, so clearing the stale value lets the new provider's context length resolve normally.
  • The fix was merged via fix(agent): clear stale config context_length on model switch #24724 as salvage of duplicate fix(agent): clear stale config context_length on model switch #22387, with implementation commit b6d48ebfde63f325143bc601cbc622d89d428f1a; it is contained in v2026.5.16 and later tags.

Thanks for the report and regression coverage. This PR's exact selective re-resolve approach was not the one merged, but the stale _config_context_length shadowing issue it fixes is implemented on main.

@teknium1 teknium1 closed this Jun 11, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent loop, run_agent.py, prompt builder P2 Medium — degraded but workaround exists sweeper:implemented-on-main Sweeper: behavior already present on current main type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants