Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(backend/copilot): LaunchDarkly per-user model routing (#12881)

Zamil Majdyebb0d3b95bfd
diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.py
index beb1af3f74..826a06ac0b 100644
--- a/autogpt_platform/backend/backend/copilot/baseline/service.py
+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py
@@ -45,6 +45,7 @@ from backend.copilot.model import (
maybe_append_user_message,
upsert_chat_session,
)
+from backend.copilot.model_router import resolve_model
from backend.copilot.pending_message_helpers import (
combine_pending_with_current,
drain_pending_safe,
@@ -318,20 +319,17 @@ def _filter_tools_by_permissions(
]
-def _resolve_baseline_model(tier: CopilotLlmModel | None) -> str:
+async def _resolve_baseline_model(
+ tier: CopilotLlmModel | None, user_id: str | None
+) -> str:
"""Pick the model for the baseline path based on the per-request tier.
- Baseline resolves independently of SDK via the ``fast_*_model`` cells
- of the (path, tier) matrix. ``'standard'`` / ``None`` picks Kimi
- K2.6 by default (cheap + OpenRouter ``reasoning`` support);
- ``'advanced'`` picks Opus by default so the advanced tier is a clean
- A/B against the SDK advanced tier — same model, different path —
- isolating reasoning-wire + cache differences from model capability.
- Both defaults are overridable per ``CHAT_FAST_*_MODEL`` env vars.
+ Delegates to :func:`copilot.model_router.resolve_model` so the
+ ``(fast, tier)`` cell is LD-overridable per user. ``None`` tier
+ maps to ``"standard"``.
"""
- if tier == "advanced":
- return config.fast_advanced_model
- return config.fast_standard_model
+ tier_name = "advanced" if tier == "advanced" else "standard"
+ return await resolve_model("fast", tier_name, user_id, config=config)
@dataclass
@@ -1358,7 +1356,7 @@ async def stream_chat_completion_baseline(
# Select model based on the per-request tier toggle (standard / advanced).
# The path (fast vs extended_thinking) is already decided — we're in the
# baseline (fast) path; ``mode`` is accepted for logging parity only.
- active_model = _resolve_baseline_model(model)
+ active_model = await _resolve_baseline_model(model, user_id)
# --- E2B sandbox setup (feature parity with SDK path) ---
e2b_sandbox = None
diff --git a/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py b/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py
index ad87114959..8a9e435743 100644
--- a/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py
+++ b/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py
@@ -67,34 +67,40 @@ class TestResolveBaselineModel:
Baseline reads the ``fast_*_model`` cells of the (path, tier) matrix
and never falls through to the SDK-side ``thinking_*_model`` cells.
- Default routing:
- - ``standard`` / ``None`` → ``config.fast_standard_model`` (Kimi K2.6)
- - ``advanced`` → ``config.fast_advanced_model`` (Opus — same as SDK's
- advanced tier, so the advanced A/B isolates path differences)
+ Without a user_id (so no LD context) the resolver returns the
+ ``ChatConfig`` static default; per-user overrides are exercised in
+ ``copilot/model_router_test.py``.
"""
- def test_advanced_tier_selects_fast_advanced_model(self):
- assert _resolve_baseline_model("advanced") == config.fast_advanced_model
-
- def test_standard_tier_selects_fast_standard_model(self):
- assert _resolve_baseline_model("standard") == config.fast_standard_model
-
- def test_none_tier_selects_fast_standard_model(self):
- """Baseline users without a tier get the cheap fast-standard default."""
- assert _resolve_baseline_model(None) == config.fast_standard_model
+ @pytest.mark.asyncio
+ async def test_advanced_tier_selects_fast_advanced_model(self):
+ assert (
+ await _resolve_baseline_model("advanced", None)
+ == config.fast_advanced_model
+ )
- def test_fast_standard_default_is_kimi(self):
- """Shipped default: Kimi K2.6 on the baseline standard cell.
+ @pytest.mark.asyncio
+ async def test_standard_tier_selects_fast_standard_model(self):
+ assert (
+ await _resolve_baseline_model("standard", None)
+ == config.fast_standard_model
+ )
- Asserts the declared ``Field`` default — env-independent — so a
- deploy-time ``CHAT_FAST_STANDARD_MODEL`` rollback override
- doesn't fail CI while still pinning the shipped default.
- """
+ @pytest.mark.asyncio
+ async def test_none_tier_selects_fast_standard_model(self):
+ """Baseline users without a tier get the fast-standard default."""
+ assert await _resolve_baseline_model(None, None) == config.fast_standard_model
+
+ def test_fast_standard_default_is_sonnet(self):
+ """Shipped default: Sonnet on the baseline standard cell — the
+ non-Anthropic routes ship via the LD flag instead of a config
+ change. Asserts the declared ``Field`` default so a deploy-time
+ ``CHAT_FAST_STANDARD_MODEL`` override doesn't flake CI."""
from backend.copilot.config import ChatConfig
assert (
ChatConfig.model_fields["fast_standard_model"].default
- == "moonshotai/kimi-k2.6"
+ == "anthropic/claude-sonnet-4-6"
)
def test_fast_advanced_default_is_opus(self):
@@ -108,17 +114,6 @@ class TestResolveBaselineModel:
== "anthropic/claude-opus-4.7"
)
- def test_standard_cells_share_kimi_default_across_paths(self):
- """After PR #12878 both paths default to the same cheap model
- (``moonshotai/kimi-k2.6``). The split exists for *override*
- flexibility, not for forcing a price gap — guard against an
- accidental regression that pins the SDK path back to Sonnet."""
- from backend.copilot.config import ChatConfig
-
- kimi = "moonshotai/kimi-k2.6"
- assert ChatConfig.model_fields["fast_standard_model"].default == kimi
- assert ChatConfig.model_fields["thinking_standard_model"].default == kimi
-
def test_standard_and_advanced_cells_differ_on_fast(self):
"""Advanced tier defaults to a different model than standard on
the baseline path. Checked against declared ``Field`` defaults
diff --git a/autogpt_platform/backend/backend/copilot/config.py b/autogpt_platform/backend/backend/copilot/config.py
index f9799ecb22..3d785c8ee9 100644
--- a/autogpt_platform/backend/backend/copilot/config.py
+++ b/autogpt_platform/backend/backend/copilot/config.py
@@ -43,42 +43,29 @@ class ChatConfig(BaseSettings):
# ``CHAT_FAST_MODEL``) are preserved via ``validation_alias`` so
# existing deployments continue to override the same effective cell.
fast_standard_model: str = Field(
- default="moonshotai/kimi-k2.6",
+ default="anthropic/claude-sonnet-4-6",
validation_alias=AliasChoices(
"CHAT_FAST_STANDARD_MODEL",
"CHAT_FAST_MODEL",
),
- description="Baseline path, 'standard' / ``None`` tier. Kimi K2.6 "
- "by default: ~5x cheaper input and ~5.4x cheaper output than Sonnet, "
- "SWE-Bench Verified parity with Opus, and OpenRouter advertises the "
- "``reasoning`` + ``include_reasoning`` extension params on the "
- "Moonshot endpoints — so the baseline reasoning plumbing lights up "
- "without provider-specific code. Roll back to the Anthropic route "
- "via ``CHAT_FAST_STANDARD_MODEL=anthropic/claude-sonnet-4.6`` (then "
- "``cache_control`` breakpoints reactivate via "
- "``_is_anthropic_model``).",
+ description="Baseline path, 'standard' / ``None`` tier. Per-user "
+ "overrides flow through the ``copilot-fast-standard-model`` LD flag "
+ "(see ``copilot/model_router.py``); this value is the fallback.",
)
fast_advanced_model: str = Field(
default="anthropic/claude-opus-4.7",
validation_alias=AliasChoices("CHAT_FAST_ADVANCED_MODEL"),
- description="Baseline path, 'advanced' tier. Opus by default. "
- "Override via ``CHAT_FAST_ADVANCED_MODEL``.",
+ description="Baseline path, 'advanced' tier. LD override: "
+ "``copilot-fast-advanced-model``.",
)
thinking_standard_model: str = Field(
- default="moonshotai/kimi-k2.6",
+ default="anthropic/claude-sonnet-4-6",
validation_alias=AliasChoices(
"CHAT_THINKING_STANDARD_MODEL",
"CHAT_MODEL",
),
description="SDK (extended-thinking) path, 'standard' / ``None`` "
- "tier. Kimi K2.6 by default: routed via OpenRouter's Anthropic-"
- "compatible ``/v1/messages`` endpoint, which the Claude Agent SDK "
- "CLI accepts as a drop-in ``ANTHROPIC_BASE_URL`` target. The same "
- "cost/capability rationale as the baseline path applies — ~5x "
- "cheaper than Sonnet at SWE-Bench parity. Roll back to Sonnet via "
- "``CHAT_THINKING_STANDARD_MODEL=anthropic/claude-sonnet-4.6`` (then "
- "the SDK ``cache_control`` markers reactivate). Direct-Anthropic "
- "deployments (no OpenRouter) must override to an Anthropic model.",
+ "tier. LD override: ``copilot-thinking-standard-model``.",
)
thinking_advanced_model: str = Field(
default="anthropic/claude-opus-4.7",
@@ -86,9 +73,8 @@ class ChatConfig(BaseSettings):
"CHAT_THINKING_ADVANCED_MODEL",
"CHAT_ADVANCED_MODEL",
),
- description="SDK (extended-thinking) path, 'advanced' tier. Opus "
- "by default. Override via ``CHAT_THINKING_ADVANCED_MODEL`` "
- "(legacy ``CHAT_ADVANCED_MODEL`` still honored).",
+ description="SDK (extended-thinking) path, 'advanced' tier. LD "
+ "override: ``copilot-thinking-advanced-model``.",
)
title_model: str = Field(
default="openai/gpt-4o-mini",
diff --git a/autogpt_platform/backend/backend/copilot/config_test.py b/autogpt_platform/backend/backend/copilot/config_test.py
index 7279061447..42e36bc1f4 100644
--- a/autogpt_platform/backend/backend/copilot/config_test.py
+++ b/autogpt_platform/backend/backend/copilot/config_test.py
@@ -203,35 +203,38 @@ class TestSdkModelVendorCompatibility:
guard in ``_normalize_model_name`` so misconfig surfaces at boot
instead of as a 500 on the first SDK turn."""
- def test_direct_anthropic_with_kimi_default_raises(self):
- """The ``moonshotai/kimi-k2.6`` default must fail at config load
- when the deployment has no OpenRouter credentials."""
+ def test_direct_anthropic_with_kimi_override_raises(self):
+ """A non-Anthropic SDK model must fail at config load when the
+ deployment has no OpenRouter credentials."""
with pytest.raises(Exception, match="requires an Anthropic model"):
ChatConfig(
use_openrouter=False,
api_key=None,
base_url=None,
use_claude_code_subscription=False,
+ thinking_standard_model="moonshotai/kimi-k2.6",
)
- def test_direct_anthropic_with_anthropic_override_succeeds(self):
- """Direct-Anthropic mode is fine when both SDK slugs are anthropic/*."""
+ def test_direct_anthropic_with_anthropic_default_succeeds(self):
+ """Direct-Anthropic mode is fine when both SDK slugs are anthropic/*
+ — which is the default after the LD-routed model rollout."""
cfg = ChatConfig(
use_openrouter=False,
api_key=None,
base_url=None,
use_claude_code_subscription=False,
- thinking_standard_model="anthropic/claude-sonnet-4-6",
)
assert cfg.thinking_standard_model == "anthropic/claude-sonnet-4-6"
- def test_openrouter_with_kimi_default_succeeds(self):
- """Default Kimi slug round-trips cleanly when OpenRouter is on."""
+ def test_openrouter_with_kimi_override_succeeds(self):
+ """Kimi slug round-trips cleanly when OpenRouter is on — exercised
+ via the LD-flag override path in production."""
cfg = ChatConfig(
use_openrouter=True,
api_key="or-key",
base_url="https://openrouter.ai/api/v1",
use_claude_code_subscription=False,
+ thinking_standard_model="moonshotai/kimi-k2.6",
)
assert cfg.thinking_standard_model == "moonshotai/kimi-k2.6"
diff --git a/autogpt_platform/backend/backend/copilot/model_router.py b/autogpt_platform/backend/backend/copilot/model_router.py
new file mode 100644
index 0000000000..35a881393e
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/model_router.py
@@ -0,0 +1,104 @@
+"""LaunchDarkly-aware model selection for the copilot.
+
+Each cell of the ``(mode, tier)`` matrix has a static default baked into
+``ChatConfig`` (see ``copilot/config.py``) and a matching LaunchDarkly
+string-valued feature flag that can override it per-user. This module
+centralises the lookup so both the baseline and SDK paths agree on the
+selection rule and so A/B experiments can target a single cell without
+shipping a config change.
+
+Matrix:
+
+ +----------+-------------------------------------+-------------------------------------+
+ | | standard | advanced |
+ +----------+-------------------------------------+-------------------------------------+
+ | fast | copilot-fast-standard-model | copilot-fast-advanced-model |
+ | thinking | copilot-thinking-standard-model | copilot-thinking-advanced-model |
+ +----------+-------------------------------------+-------------------------------------+
+
+LD flag values are arbitrary strings (model identifiers, e.g.
+``"anthropic/claude-sonnet-4-6"`` or ``"moonshotai/kimi-k2.6"``). Empty
+or non-string values fall back to the config default.
+"""
+
+from __future__ import annotations
+
+import logging
+from typing import Literal
+
+from backend.copilot.config import ChatConfig
+from backend.util.feature_flag import Flag, get_feature_flag_value
+
+logger = logging.getLogger(__name__)
+
+ModelMode = Literal["fast", "thinking"]
+ModelTier = Literal["standard", "advanced"]
+
+
+_FLAG_BY_CELL: dict[tuple[ModelMode, ModelTier], Flag] = {
+ ("fast", "standard"): Flag.COPILOT_FAST_STANDARD_MODEL,
+ ("fast", "advanced"): Flag.COPILOT_FAST_ADVANCED_MODEL,
+ ("thinking", "standard"): Flag.COPILOT_THINKING_STANDARD_MODEL,
+ ("thinking", "advanced"): Flag.COPILOT_THINKING_ADVANCED_MODEL,
+}
+
+
+def _config_default(config: ChatConfig, mode: ModelMode, tier: ModelTier) -> str:
+ if mode == "fast":
+ return (
+ config.fast_advanced_model
+ if tier == "advanced"
+ else config.fast_standard_model
+ )
+ return (
+ config.thinking_advanced_model
+ if tier == "advanced"
+ else config.thinking_standard_model
+ )
+
+
+async def resolve_model(
+ mode: ModelMode,
+ tier: ModelTier,
+ user_id: str | None,
+ *,
+ config: ChatConfig,
+) -> str:
+ """Return the model identifier for a ``(mode, tier)`` cell.
+
+ Consults the matching LaunchDarkly flag for *user_id* first and
+ falls back to the ``ChatConfig`` default on missing user, missing
+ flag, or non-string flag value. Passing *config* explicitly keeps
+ the resolver cheap to unit-test.
+ """
+ fallback = _config_default(config, mode, tier).strip()
+ if not user_id:
+ return fallback
+
+ flag = _FLAG_BY_CELL[(mode, tier)]
+ try:
+ value = await get_feature_flag_value(flag.value, user_id, default=fallback)
+ except Exception:
+ logger.warning(
+ "[model_router] LD lookup failed for %s — using config default %s",
+ flag.value,
+ fallback,
+ exc_info=True,
+ )
+ return fallback
+
+ if isinstance(value, str) and value.strip():
+ return value.strip()
+ if value != fallback:
+ reason = (
+ "empty string"
+ if isinstance(value, str)
+ else f"non-string ({type(value).__name__})"
+ )
+ logger.warning(
+ "[model_router] LD flag %s returned %s — using config default %s",
+ flag.value,
+ reason,
+ fallback,
+ )
+ return fallback
diff --git a/autogpt_platform/backend/backend/copilot/model_router_test.py b/autogpt_platform/backend/backend/copilot/model_router_test.py
new file mode 100644
index 0000000000..e388d5018b
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/model_router_test.py
@@ -0,0 +1,166 @@
+"""Tests for the LD-aware model resolver."""
+
+from unittest.mock import AsyncMock, patch
+
+import pytest
+
+from backend.copilot.config import ChatConfig
+from backend.copilot.model_router import _FLAG_BY_CELL, _config_default, resolve_model
+
+
+def _make_config() -> ChatConfig:
+ """Build a config with the canonical defaults so tests read naturally."""
+ return ChatConfig(
+ fast_standard_model="anthropic/claude-sonnet-4-6",
+ fast_advanced_model="anthropic/claude-opus-4.7",
+ thinking_standard_model="anthropic/claude-sonnet-4-6",
+ thinking_advanced_model="anthropic/claude-opus-4.7",
+ )
+
+
+class TestConfigDefault:
+ def test_fast_standard(self):
+ cfg = _make_config()
+ assert _config_default(cfg, "fast", "standard") == cfg.fast_standard_model
+
+ def test_fast_advanced(self):
+ cfg = _make_config()
+ assert _config_default(cfg, "fast", "advanced") == cfg.fast_advanced_model
+
+ def test_thinking_standard(self):
+ cfg = _make_config()
+ assert (
+ _config_default(cfg, "thinking", "standard") == cfg.thinking_standard_model
+ )
+
+ def test_thinking_advanced(self):
+ cfg = _make_config()
+ assert (
+ _config_default(cfg, "thinking", "advanced") == cfg.thinking_advanced_model
+ )
+
+
+class TestResolveModel:
+ @pytest.mark.asyncio
+ async def test_missing_user_returns_fallback(self):
+ """Without user_id there's no LD context — skip the lookup entirely."""
+ cfg = _make_config()
+ with patch("backend.copilot.model_router.get_feature_flag_value") as mock_flag:
+ result = await resolve_model("fast", "standard", None, config=cfg)
+ assert result == cfg.fast_standard_model
+ mock_flag.assert_not_called()
+
+ @pytest.mark.asyncio
+ async def test_missing_user_strips_whitespace_from_fallback(self):
+ """Sentry MEDIUM: the anonymous-user branch returned an unstripped
+ config value. If ``CHAT_*_MODEL`` env carries trailing whitespace
+ the downstream ``resolved == tier_default`` check in
+ ``_resolve_sdk_model_for_request`` would diverge from the
+ whitespace-stripped LD side, bypassing subscription mode for
+ every anonymous request. Strip at the source."""
+ cfg = ChatConfig(
+ fast_standard_model="anthropic/claude-sonnet-4-6 ", # trailing ws
+ fast_advanced_model="anthropic/claude-opus-4.7",
+ thinking_standard_model="anthropic/claude-sonnet-4-6",
+ thinking_advanced_model="anthropic/claude-opus-4.7",
+ )
+ result = await resolve_model("fast", "standard", None, config=cfg)
+ assert result == "anthropic/claude-sonnet-4-6"
+
+ @pytest.mark.asyncio
+ async def test_ld_string_override_wins(self):
+ """LD-returned model string replaces the config default."""
+ cfg = _make_config()
+ with patch(
+ "backend.copilot.model_router.get_feature_flag_value",
+ new=AsyncMock(return_value="moonshotai/kimi-k2.6"),
+ ):
+ result = await resolve_model("fast", "standard", "user-1", config=cfg)
+ assert result == "moonshotai/kimi-k2.6"
+
+ @pytest.mark.asyncio
+ async def test_whitespace_is_stripped(self):
+ cfg = _make_config()
+ with patch(
+ "backend.copilot.model_router.get_feature_flag_value",
+ new=AsyncMock(return_value=" xai/grok-4 "),
+ ):
+ result = await resolve_model("thinking", "advanced", "user-1", config=cfg)
+ assert result == "xai/grok-4"
+
+ @pytest.mark.asyncio
+ async def test_non_string_value_falls_back_with_type_in_warning(self, caplog):
+ """LD misconfigured as a boolean flag — don't try to use ``True`` as a
+ model name; return the config default. Warning must say
+ 'non-string' (not 'empty string') so the LD operator knows the
+ flag type is wrong, not just missing a value."""
+ import logging
+
+ cfg = _make_config()
+ with caplog.at_level(logging.WARNING, logger="backend.copilot.model_router"):
+ with patch(
+ "backend.copilot.model_router.get_feature_flag_value",
+ new=AsyncMock(return_value=True),
+ ):
+ result = await resolve_model("fast", "advanced", "user-1", config=cfg)
+ assert result == cfg.fast_advanced_model
+ assert any("non-string" in r.message for r in caplog.records)
+
+ @pytest.mark.asyncio
+ async def test_empty_string_falls_back_with_empty_in_warning(self, caplog):
+ """When LD returns ``""`` the warning must say 'empty string' —
+ not 'non-string' — so the operator doesn't chase a type bug
+ when the flag is simply unset to an empty value."""
+ import logging
+
+ cfg = _make_config()
+ with caplog.at_level(logging.WARNING, logger="backend.copilot.model_router"):
+ with patch(
+ "backend.copilot.model_router.get_feature_flag_value",
+ new=AsyncMock(return_value=""),
+ ):
+ result = await resolve_model("fast", "standard", "user-1", config=cfg)
+ assert result == cfg.fast_standard_model
+ messages = [r.message for r in caplog.records]
+ assert any("empty string" in m for m in messages)
+ assert not any("non-string" in m for m in messages)
+
+ @pytest.mark.asyncio
+ async def test_ld_exception_falls_back(self):
+ """LD client throws (network blip, SDK init race) — serve the default
+ instead of failing the whole request."""
+ cfg = _make_config()
+ with patch(
+ "backend.copilot.model_router.get_feature_flag_value",
+ new=AsyncMock(side_effect=RuntimeError("LD down")),
+ ):
+ result = await resolve_model("fast", "standard", "user-1", config=cfg)
+ assert result == cfg.fast_standard_model
+
+ @pytest.mark.asyncio
+ async def test_all_four_cells_hit_distinct_flags(self):
+ """Each (mode, tier) cell must route to its own flag — regression
+ guard against copy-paste bugs in the _FLAG_BY_CELL map."""
+ cfg = _make_config()
+ calls: list[str] = []
+
+ async def _capture(flag_key, user_id, default):
+ calls.append(flag_key)
+ return default
+
+ with patch(
+ "backend.copilot.model_router.get_feature_flag_value",
+ new=AsyncMock(side_effect=_capture),
+ ):
+ await resolve_model("fast", "standard", "u", config=cfg)
+ await resolve_model("fast", "advanced", "u", config=cfg)
+ await resolve_model("thinking", "standard", "u", config=cfg)
+ await resolve_model("thinking", "advanced", "u", config=cfg)
+
+ assert calls == [
+ _FLAG_BY_CELL[("fast", "standard")].value,
+ _FLAG_BY_CELL[("fast", "advanced")].value,
+ _FLAG_BY_CELL[("thinking", "standard")].value,
+ _FLAG_BY_CELL[("thinking", "advanced")].value,
+ ]
+ assert len(set(calls)) == 4
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index d62ba2afff..056c4538b7 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -57,6 +57,7 @@ from ..constants import (
from ..session_cleanup import prune_orphan_tool_calls
from ..context import encode_cwd_for_cli, get_workspace_manager
from ..graphiti.config import is_enabled_for_user
+from ..model_router import resolve_model
from ..model import (
ChatMessage,
ChatSession,
@@ -773,14 +774,16 @@ def _override_cost_for_non_anthropic(
def _resolve_sdk_model() -> str | None:
- """Resolve the model name for the Claude Agent SDK CLI.
+ """Resolve the SDK-CLI model name from static config (no LD lookup).
- Uses `config.claude_agent_model` if set, otherwise derives from
- `config.thinking_standard_model` via :func:`_normalize_model_name`.
+ ``config.claude_agent_model`` is an explicit override that wins
+ unconditionally. When the Claude Code subscription is enabled and no
+ override is set, returns ``None`` so the CLI picks the model for the
+ user's subscription plan. Otherwise derives from
+ ``config.thinking_standard_model``.
- When `use_claude_code_subscription` is enabled and no explicit
- `claude_agent_model` is set, returns `None` so the CLI uses the
- default model for the user's subscription plan.
+ For per-user routing (LaunchDarkly overrides), see
+ :func:`_resolve_sdk_model_for_request`.
"""
if config.claude_agent_model:
return config.claude_agent_model
@@ -789,6 +792,18 @@ def _resolve_sdk_model() -> str | None:
return _normalize_model_name(config.thinking_standard_model)
+async def _resolve_thinking_model_for_user(
+ tier: "CopilotLlmModel",
+ user_id: str | None,
+) -> str:
+ """LD-aware thinking-tier model pick for a specific user.
+
+ Consults ``copilot-thinking-{tier}-model`` and falls back to the
+ ``ChatConfig`` default on missing user / missing flag.
+ """
+ return await resolve_model("thinking", tier, user_id, config=config)
+
+
def _resolve_fallback_model() -> str | None:
"""Resolve the fallback model name via :func:`_normalize_model_name`.
@@ -803,37 +818,94 @@ def _resolve_fallback_model() -> str | None:
async def _resolve_sdk_model_for_request(
model: "CopilotLlmModel | None",
session_id: str,
+ user_id: str | None = None,
) -> str | None:
"""Resolve the SDK model string for a turn.
Priority (highest first):
- 1. Explicit per-request ``model`` tier from the frontend toggle.
- 2. Global config default (``_resolve_sdk_model()``).
-
- Returns ``None`` when the Claude Code subscription default applies.
- Rate-limit accounting no longer applies a multiplier — the real turn
- cost (reported by the SDK) already reflects model-pricing differences.
+ 1. ``config.claude_agent_model`` — unconditional override, bypasses LD.
+ 2. LaunchDarkly ``copilot-thinking-{tier}-model`` if it serves a value
+ different from the config default for *user_id*. An LD-served
+ override wins over subscription mode so admins can route specific
+ users to a specific model without flipping subscription on/off.
+ 3. ``config.use_claude_code_subscription`` on the standard tier —
+ returns ``None`` so the CLI picks the subscription default (this
+ branch fires when LD has no opinion, i.e. the value equals the
+ config default).
+ 4. ``ChatConfig`` static default for the tier.
"""
- if model == "advanced":
- sdk_model = _normalize_model_name(config.thinking_advanced_model)
+ if config.claude_agent_model:
+ return config.claude_agent_model
+
+ tier_name: "CopilotLlmModel" = "advanced" if model == "advanced" else "standard"
+ # Strip at read time so a stray trailing space in ``CHAT_*_MODEL`` (a
+ # common ``.env`` pitfall) doesn't make the ``resolved == tier_default``
+ # comparison below spuriously diverge — ``resolve_model`` already strips
+ # the LD side, so both halves must end up whitespace-normalised to stay
+ # equal when they're semantically equal. Downstream ``_normalize_model_name``
+ # also benefits from the strip.
+ tier_default = (
+ config.thinking_advanced_model
+ if tier_name == "advanced"
+ else config.thinking_standard_model
+ ).strip()
+
+ resolved = await _resolve_thinking_model_for_user(tier_name, user_id)
+
+ # Subscription mode on standard tier only wins when LD has no opinion
+ # (value == config default ⇒ admin hasn't explicitly pointed this
+ # user somewhere). Any LD override — even to the same value with
+ # stripped whitespace normalised — is an explicit admin choice that
+ # must be honoured. Without this, a subscription-mode deployment
+ # silently ignores the ``copilot-thinking-standard-model`` flag
+ # entirely, which defeats the point of cohort-based routing.
+ ld_overrides_default = resolved != tier_default
+ if (
+ not ld_overrides_default
+ and tier_name == "standard"
+ and config.use_claude_code_subscription
+ ):
logger.info(
- "[SDK] [%s] Per-request model override: advanced (%s)",
+ "[SDK] [%s] Subscription default (tier=standard, LD unset)",
session_id[:12] if session_id else "?",
- sdk_model,
)
- return sdk_model
-
- if model == "standard":
- # Reset to config default — respects subscription mode (None = CLI default).
- sdk_model = _resolve_sdk_model()
- logger.info(
- "[SDK] [%s] Per-request model override: standard (%s)",
+ return None
+ try:
+ sdk_model = _normalize_model_name(resolved)
+ except ValueError as exc:
+ # The per-user LD value didn't pass ``_normalize_model_name``'s
+ # vendor check (most commonly: a ``moonshotai/kimi-*`` slug on a
+ # direct-Anthropic deployment that has no OpenRouter route). Fail
+ # soft to the TIER-SPECIFIC config default — using the generic
+ # ``_resolve_sdk_model()`` here would pin advanced-tier requests to
+ # ``thinking_standard_model`` (Sonnet) whenever LD misconfigures
+ # the advanced cell, silently downgrading the user's chosen tier.
+ try:
+ sdk_model = _normalize_model_name(tier_default)
+ except ValueError:
+ # Config default is *also* invalid for the active routing
+ # mode — this is a deployment-level misconfig that the
+ # ``model_validator`` should catch at startup. Re-raise the
+ # original LD error so the issue surfaces loudly rather than
+ # returning something misleading.
+ raise exc
+ logger.warning(
+ "[SDK] [%s] LD model %r rejected for tier=%s (%s); falling "
+ "back to tier default %s",
session_id[:12] if session_id else "?",
- sdk_model or "subscription-default",
+ resolved,
+ tier_name,
+ exc,
+ sdk_model,
)
return sdk_model
-
- return _resolve_sdk_model()
+ logger.info(
+ "[SDK] [%s] Resolved model for tier=%s: %s",
+ session_id[:12] if session_id else "?",
+ tier_name,
+ sdk_model,
+ )
+ return sdk_model
_MAX_TRANSIENT_BACKOFF_SECONDS = 30
@@ -3193,8 +3265,8 @@ async def stream_chat_completion_sdk(
mcp_server = create_copilot_mcp_server(use_e2b=use_e2b)
- # Resolve model (request tier → config default).
- sdk_model = await _resolve_sdk_model_for_request(model, session_id)
+ # Resolve model (request tier → LD per-user override → config default).
+ sdk_model = await _resolve_sdk_model_for_request(model, session_id, user_id)
# Track SDK-internal compaction (PreCompact hook → start, next msg → end)
compaction = CompactionTracker()
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_test.py
index 7f53cb67b5..a2e1ac35bb 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service_test.py
@@ -18,6 +18,7 @@ from .service import (
_override_cost_for_non_anthropic,
_prepare_file_attachments,
_resolve_sdk_model,
+ _resolve_sdk_model_for_request,
_safe_close_sdk_client,
)
@@ -517,6 +518,213 @@ class TestResolveSdkModel:
assert _resolve_sdk_model() == "claude-opus-4-6"
+class TestResolveSdkModelForRequestLdFallback:
+ """``_resolve_sdk_model_for_request`` must fail soft when the LD value
+ can't be normalised for the active routing mode — flagged as MAJOR by
+ CodeRabbit + HIGH by Sentry when it was a hard ValueError."""
+
+ @pytest.mark.asyncio
+ async def test_direct_anthropic_mode_rejects_kimi_ld_value_and_falls_back(
+ self, monkeypatch, _clean_config_env
+ ):
+ """LD serves ``moonshotai/kimi-k2.6`` but we're on direct-Anthropic
+ (no OpenRouter key). ``_normalize_model_name`` raises; the
+ resolver must log + return the config-default path instead of
+ 500-ing the turn."""
+ cfg = cfg_mod.ChatConfig(
+ thinking_standard_model="anthropic/claude-sonnet-4-6",
+ claude_agent_model=None,
+ use_openrouter=False,
+ api_key=None,
+ base_url=None,
+ use_claude_code_subscription=False,
+ )
+ monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)
+
+ with patch(
+ "backend.copilot.sdk.service._resolve_thinking_model_for_user",
+ new=AsyncMock(return_value="moonshotai/kimi-k2.6"),
+ ):
+ resolved = await _resolve_sdk_model_for_request(
+ model="standard", session_id="sess-abc", user_id="user-1"
+ )
+
+ # Fallback == tier-specific config default (thinking_standard_model
+ # normalised to hyphen-form for direct-Anthropic mode).
+ assert resolved == "claude-sonnet-4-6"
+
+ @pytest.mark.asyncio
+ async def test_openrouter_mode_accepts_ld_kimi_value(
+ self, monkeypatch, _clean_config_env
+ ):
+ """On OpenRouter the Kimi slug is legitimate — no fallback,
+ value returned as-is."""
+ cfg = cfg_mod.ChatConfig(
+ thinking_standard_model="anthropic/claude-sonnet-4-6",
+ claude_agent_model=None,
+ use_openrouter=True,
+ api_key="or-key",
+ base_url="https://openrouter.ai/api/v1",
+ use_claude_code_subscription=False,
+ )
+ monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)
+
+ with patch(
+ "backend.copilot.sdk.service._resolve_thinking_model_for_user",
+ new=AsyncMock(return_value="moonshotai/kimi-k2.6"),
+ ):
+ resolved = await _resolve_sdk_model_for_request(
+ model="standard", session_id="sess-abc", user_id="user-1"
+ )
+ assert resolved == "moonshotai/kimi-k2.6"
+
+ @pytest.mark.asyncio
+ async def test_advanced_tier_fallback_uses_advanced_default_not_standard(
+ self, monkeypatch, _clean_config_env
+ ):
+ """An LD-rejected ADVANCED slug must fall back to the advanced
+ config default (Opus) — not the standard default (Sonnet).
+ Using ``_resolve_sdk_model()`` as the fallback silently
+ downgraded the user's chosen tier. Flagged MAJOR by CodeRabbit
+ + HIGH by Sentry on the first fail-soft commit."""
+ cfg = cfg_mod.ChatConfig(
+ thinking_standard_model="anthropic/claude-sonnet-4-6",
+ thinking_advanced_model="anthropic/claude-opus-4.7",
+ claude_agent_model=None,
+ use_openrouter=False,
+ api_key=None,
+ base_url=None,
+ use_claude_code_subscription=False,
+ )
+ monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)
+
+ with patch(
+ "backend.copilot.sdk.service._resolve_thinking_model_for_user",
+ new=AsyncMock(return_value="moonshotai/kimi-k2.6"),
+ ):
+ resolved = await _resolve_sdk_model_for_request(
+ model="advanced", session_id="sess-adv", user_id="user-1"
+ )
+
+ # Direct-Anthropic normalises anthropic/claude-opus-4.7 → claude-opus-4-7
+ assert resolved == "claude-opus-4-7"
+
+ @pytest.mark.asyncio
+ async def test_standard_ld_override_wins_over_subscription(
+ self, monkeypatch, _clean_config_env
+ ):
+ """Bug reported in local test: subscription mode + LD serving Kimi
+ on ``copilot-thinking-standard-model`` returned ``None`` (CLI
+ picked subscription default Opus), silently ignoring the LD
+ override. An LD value different from the config default is an
+ explicit admin decision and must win."""
+ cfg = cfg_mod.ChatConfig(
+ thinking_standard_model="anthropic/claude-sonnet-4-6",
+ claude_agent_model=None,
+ use_openrouter=True,
+ api_key="or-key",
+ base_url="https://openrouter.ai/api/v1",
+ use_claude_code_subscription=True,
+ )
+ monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)
+
+ with patch(
+ "backend.copilot.sdk.service._resolve_thinking_model_for_user",
+ new=AsyncMock(return_value="moonshotai/kimi-k2.6"),
+ ):
+ resolved = await _resolve_sdk_model_for_request(
+ model="standard", session_id="sess-std-sub", user_id="user-1"
+ )
+ # Expect LD-served Kimi, NOT None (the old subscription-default bypass)
+ assert resolved == "moonshotai/kimi-k2.6"
+
+ @pytest.mark.asyncio
+ async def test_standard_subscription_survives_trailing_whitespace_in_env(
+ self, monkeypatch, _clean_config_env
+ ):
+ """``_resolve_thinking_model_for_user`` strips whitespace from the LD
+ side; the config tier default must be stripped too, otherwise a
+ stray trailing space in ``CHAT_THINKING_STANDARD_MODEL`` makes
+ ``resolved == tier_default`` spuriously False and bypasses
+ subscription-default mode. Sentry HIGH on L856."""
+ cfg = cfg_mod.ChatConfig(
+ thinking_standard_model="anthropic/claude-sonnet-4-6 ", # trailing spaces
+ claude_agent_model=None,
+ use_openrouter=False,
+ api_key=None,
+ base_url=None,
+ use_claude_code_subscription=True,
+ )
+ monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)
+
+ with patch(
+ "backend.copilot.sdk.service._resolve_thinking_model_for_user",
+ new=AsyncMock(return_value="anthropic/claude-sonnet-4-6"),
+ ):
+ resolved = await _resolve_sdk_model_for_request(
+ model="standard", session_id="sess-ws", user_id="user-1"
+ )
+ assert resolved is None, (
+ "LD value semantically matches the whitespace-padded config "
+ "default — subscription mode must still win and return None"
+ )
+
+ @pytest.mark.asyncio
+ async def test_standard_subscription_default_honoured_when_ld_matches_config(
+ self, monkeypatch, _clean_config_env
+ ):
+ """When LD serves the SAME value as the config default (i.e. the
+ flag is effectively unset / no override), subscription mode still
+ wins and we return ``None`` so the CLI uses the subscription
+ default model."""
+ cfg = cfg_mod.ChatConfig(
+ thinking_standard_model="anthropic/claude-sonnet-4-6",
+ claude_agent_model=None,
+ use_openrouter=False,
+ api_key=None,
+ base_url=None,
+ use_claude_code_subscription=True,
+ )
+ monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)
+
+ with patch(
+ "backend.copilot.sdk.service._resolve_thinking_model_for_user",
+ new=AsyncMock(return_value="anthropic/claude-sonnet-4-6"),
+ ):
+ resolved = await _resolve_sdk_model_for_request(
+ model="standard", session_id="sess-std-nop", user_id="user-1"
+ )
+ assert resolved is None
+
+ @pytest.mark.asyncio
+ async def test_advanced_tier_consults_ld_under_subscription(
+ self, monkeypatch, _clean_config_env
+ ):
+ """Subscription mode bypasses LD only on the standard tier —
+ the advanced tier always consults LD because the user explicitly
+ asked for the premium path. A subscription + advanced request
+ with LD-served Opus must return Opus (not ``None``)."""
+ cfg = cfg_mod.ChatConfig(
+ thinking_standard_model="anthropic/claude-sonnet-4-6",
+ thinking_advanced_model="anthropic/claude-opus-4.7",
+ claude_agent_model=None,
+ use_openrouter=True,
+ api_key="or-key",
+ base_url="https://openrouter.ai/api/v1",
+ use_claude_code_subscription=True,
+ )
+ monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)
+
+ with patch(
+ "backend.copilot.sdk.service._resolve_thinking_model_for_user",
+ new=AsyncMock(return_value="anthropic/claude-opus-4.7"),
+ ):
+ resolved = await _resolve_sdk_model_for_request(
+ model="advanced", session_id="sess-adv-sub", user_id="user-1"
+ )
+ assert resolved == "anthropic/claude-opus-4.7"
+
+
# ---------------------------------------------------------------------------
# _is_sdk_disconnect_error — classify client disconnect cleanup errors
# ---------------------------------------------------------------------------
diff --git a/autogpt_platform/backend/backend/util/feature_flag.py b/autogpt_platform/backend/backend/util/feature_flag.py
index 1e29ff4102..8699fc2eeb 100644
--- a/autogpt_platform/backend/backend/util/feature_flag.py
+++ b/autogpt_platform/backend/backend/util/feature_flag.py
@@ -48,6 +48,16 @@ class Flag(str, Enum):
STRIPE_PRICE_BUSINESS = "stripe-price-id-business"
GRAPHITI_MEMORY = "graphiti-memory"
+ # Copilot model routing — string-valued, returns the model identifier
+ # (e.g. ``"anthropic/claude-sonnet-4-6"`` or ``"moonshotai/kimi-k2.6"``)
+ # to use for each cell of the (mode, tier) matrix. Falls back to the
+ # ``CHAT_*_MODEL`` env/config default when the flag is unset or LD is
+ # unavailable. Evaluated per user_id so cohorts can be targeted.
+ COPILOT_FAST_STANDARD_MODEL = "copilot-fast-standard-model"
+ COPILOT_FAST_ADVANCED_MODEL = "copilot-fast-advanced-model"
+ COPILOT_THINKING_STANDARD_MODEL = "copilot-thinking-standard-model"
+ COPILOT_THINKING_ADVANCED_MODEL = "copilot-thinking-advanced-model"
+
def is_configured() -> bool:
"""Check if LaunchDarkly is configured with an SDK key."""