Skip to content

model.context_length global config override is not re-read after /model switch #40979

Description

@bonaluo

Bug Description

model.context_length in config.yaml is read once at agent initialization in agent/agent_init.py (line 1314) and stored as agent._config_context_length. When the user switches models via /model, switch_model() in agent/agent_runtime_helpers.py (line 1426) sets agent._config_context_length = None to clear the old value, but never re-reads the config file to restore the global model.context_length override.

Resolution chain after switch:

get_model_context_length(
    config_context_length=None,       # step 0: global override LOST
    custom_providers=custom_providers_list,  # step 0b: per-model override works
)
  • Step 0 (config_context_length): Always None after switch, so the global model.context_length is lost.
  • Step 0b (custom_providers per-model): Works correctly — the config file is re-read and custom_providers[i].models.<model>.context_length is honored.
  • If the user only set the global model.context_length (not per-model in custom_providers) and the custom endpoint can't auto-detect via /models -> falls through to 256K default.

This creates an inconsistent experience: the global model.context_length works at startup but silently disappears after any /model command, even when switching back to the same model.

Steps to Reproduce

  1. Configure in ~/.hermes/config.yaml:
    model:
      provider: custom:my-api
      model: deepseek-v4-pro
      context_length: 128000
  2. Start a session — context length correctly shows 128K.
  3. Run /model mini-max-m2.7 then /model deepseek-v4-pro (back to original).
  4. Now context length shows auto-detected value (e.g. 256K), not 128K.

Expected Behavior

The global model.context_length should be re-read from config.yaml after every /model switch, so it stays effective for any model that doesn't have a per-model override.

Alternatively, if the per-model custom_providers override is the intended approach, the docs should clarify that the global model.context_length is a one-time init-only setting and per-model overrides in custom_providers.models.<name>.context_length are required for persistence across model switches.

Additional Context

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existsarea/configConfig system, migrations, profilescomp/agentCore agent runtime: loop, agent_init, prompt builder, context-compression, responses endpointtype/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions