Skip to content

# Bug Report: model.context_length persists across /model provider switches #24072

@TorvaldUtne

Description

@TorvaldUtne

context-length-fix.patch

Summary

When switching providers mid-session via /model, the top-level model.context_length from config.yaml is stored as _config_context_length at startup and never re-resolved. This causes the context window of the new provider to be silently shadowed by the old provider's value.

Steps to Reproduce

  1. Configure config.yaml with a cloud provider as default:
model:
  default: mimo-v2.5-pro
  provider: custom
  base_url: http://localhost:4000/v1
  api_key: sk-xxx
  context_length: 1000000

providers:
  local:
    name: local
    base_url: http://localhost:8080/v1
    api_key: "not-needed"
    model: gemma-4-26b
    context_length: 8192
  1. Start Hermes — context window shows 1M (correct for cloud).
  2. Switch to local provider: /model gemma-4-26b --provider custom:local
  3. Check /status — context window still shows 1M instead of 8192.

Expected Behavior

After switching to a provider with context_length: 8192, the context window should reflect 8192.

Actual Behavior

The context window remains at 1,000,000 — the stale _config_context_length from startup shadows the new provider's value.

Root Cause

In run_agent.py, _config_context_length is resolved once during __init__ from model.context_length (line ~2194) and stored as self._config_context_length. When switch_model() calls get_model_context_length() (line ~2679), it passes this stale value as config_context_length, which takes precedence over the new provider's context_length.

Fix

In switch_model(), re-resolve _config_context_length from the live config before updating the context compressor. If the new provider doesn't match the default provider, clear the stale value so the new provider's per-model/per-provider context_length takes effect.

Environment

  • Hermes Agent v2026.4.x (Docker)
  • WSL2 / Linux
  • Multiple providers: cloud (LiteLLM proxy, 1M ctx) + local (llamacpp, 8K ctx)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existsarea/configConfig system, migrations, profilescomp/agentCore agent loop, run_agent.py, prompt buildersweeper:implemented-on-mainSweeper: behavior already present on current maintype/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions