Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion agent/agent_runtime_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,7 @@ def switch_model(agent, new_model, new_provider, api_key='', base_url='', api_mo

old_model = agent.model
old_provider = agent.provider
_saved_config_ctx = getattr(agent, "_config_context_length", None)

# Clear the per-config context_length override so the new model's
# actual context window is resolved via get_model_context_length()
Expand Down Expand Up @@ -1469,12 +1470,19 @@ def switch_model(agent, new_model, new_provider, api_key='', base_url='', api_mo
# length normally resolves via config or static catalogs and
# never hits a probe, but coerce to empty string defensively.
_ctx_api_key = agent.api_key if isinstance(agent.api_key, str) else ""
# If the switched model is the same as the previous model,
# restore the user's explicit config override so the resolution
# chain doesn't fall through to DEFAULT_FALLBACK_CONTEXT (256K).
# See #32423.
_effective_config_ctx = getattr(agent, "_config_context_length", None)
if _saved_config_ctx and new_model == old_model:
_effective_config_ctx = _saved_config_ctx
new_context_length = get_model_context_length(
agent.model,
base_url=agent.base_url,
api_key=_ctx_api_key,
provider=agent.provider,
config_context_length=getattr(agent, "_config_context_length", None),
config_context_length=_effective_config_ctx,
custom_providers=_sm_custom_providers,
)
agent.context_compressor.update_model(
Expand Down
10 changes: 9 additions & 1 deletion agent/chat_completion_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ def try_activate_fallback(agent, reason: "FailoverReason | None" = None) -> bool
fb_api_mode = "bedrock_converse"

old_model = agent.model
_saved_config_ctx = getattr(agent, "_config_context_length", None)

# Clear the per-config context_length override so the fallback
# model's actual context window is resolved instead of inheriting
Expand Down Expand Up @@ -1086,6 +1087,13 @@ def try_activate_fallback(agent, reason: "FailoverReason | None" = None) -> bool
# the fallback activation drops to 128K even when config says 204800.
if hasattr(agent, 'context_compressor') and agent.context_compressor:
from agent.model_metadata import get_model_context_length
# If the fallback model is the same as the primary, restore
# the user's explicit config override so the resolution chain
# doesn't fall through to DEFAULT_FALLBACK_CONTEXT (256K).
# See #32423.
_effective_config_ctx = getattr(agent, "_config_context_length", None)
if _saved_config_ctx and fb_model == old_model:
_effective_config_ctx = _saved_config_ctx
# ``agent.api_key`` may be callable (Entra ID); the
# context-length resolver expects a string for live
# probes. Foundry typically resolves via config/static
Expand All @@ -1094,7 +1102,7 @@ def try_activate_fallback(agent, reason: "FailoverReason | None" = None) -> bool
fb_context_length = get_model_context_length(
agent.model, base_url=agent.base_url,
api_key=_fb_ctx_api_key, provider=agent.provider,
config_context_length=getattr(agent, "_config_context_length", None),
config_context_length=_effective_config_ctx,
custom_providers=getattr(agent, "_custom_providers", None),
)
agent.context_compressor.update_model(
Expand Down