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(copilot): standard/advanced model toggle with Opus rate-limit multiplier (#12786)
Zamil Majdyf835674498fc
diff --git a/autogpt_platform/backend/backend/api/features/chat/routes.py b/autogpt_platform/backend/backend/api/features/chat/routes.pyindex aa2dc85e15..f8c3e3b804 100644--- a/autogpt_platform/backend/backend/api/features/chat/routes.py+++ b/autogpt_platform/backend/backend/api/features/chat/routes.py@@ -15,7 +15,7 @@ from pydantic import BaseModel, ConfigDict, Field, field_validatorfrom backend.copilot import service as chat_servicefrom backend.copilot import stream_registry-from backend.copilot.config import ChatConfig, CopilotMode+from backend.copilot.config import ChatConfig, CopilotLlmModel, CopilotModefrom backend.copilot.db import get_chat_messages_paginatedfrom backend.copilot.executor.utils import enqueue_cancel_task, enqueue_copilot_turnfrom backend.copilot.model import (@@ -139,6 +139,11 @@ class StreamChatRequest(BaseModel):description="Autopilot mode: 'fast' for baseline LLM, 'extended_thinking' for Claude Agent SDK. ""If None, uses the server default (extended_thinking).",)+ model: CopilotLlmModel | None = Field(+ default=None,+ description="Model tier: 'standard' for the default model, 'advanced' for the highest-capability model. "+ "If None, the server applies per-user LD targeting then falls back to config.",+ )class CreateSessionRequest(BaseModel):@@ -891,6 +896,7 @@ async def stream_chat_post(context=request.context,file_ids=sanitized_file_ids,mode=request.mode,+ model=request.model,)setup_time = (time.perf_counter() - stream_start_time) * 1000diff --git a/autogpt_platform/backend/backend/copilot/config.py b/autogpt_platform/backend/backend/copilot/config.pyindex cfbc6feef4..d5418bf872 100644--- a/autogpt_platform/backend/backend/copilot/config.py+++ b/autogpt_platform/backend/backend/copilot/config.py@@ -16,6 +16,13 @@ from backend.util.clients import OPENROUTER_BASE_URL# subscription flag → LaunchDarkly COPILOT_SDK → config.use_claude_agent_sdk.CopilotMode = Literal["fast", "extended_thinking"]+# Per-request model tier set by the frontend model toggle.+# 'standard' uses the global config default (currently Sonnet).+# 'advanced' forces the highest-capability model (currently Opus).+# None means no preference — falls through to LD per-user targeting, then config.+# Using tier names instead of model names keeps the contract model-agnostic.+CopilotLlmModel = Literal["standard", "advanced"]+class ChatConfig(BaseSettings):"""Configuration for the chat system."""@@ -163,12 +170,12 @@ class ChatConfig(BaseSettings):"CHAT_CLAUDE_AGENT_MAX_TURNS env var if your workflows need more.",)claude_agent_max_budget_usd: float = Field(- default=15.0,+ default=10.0,ge=0.01,le=1000.0,description="Maximum spend in USD per SDK query. The CLI attempts ""to wrap up gracefully when this budget is reached. "- "Set to $15 to allow most tasks to complete (p50=$5.37, p75=$13.07). "+ "Set to $10 to allow most tasks to complete (p50=$5.37, p75=$13.07). ""Override via CHAT_CLAUDE_AGENT_MAX_BUDGET_USD env var.",)claude_agent_max_thinking_tokens: int = Field(diff --git a/autogpt_platform/backend/backend/copilot/executor/processor.py b/autogpt_platform/backend/backend/copilot/executor/processor.pyindex cc83b2dd99..0266e57806 100644--- a/autogpt_platform/backend/backend/copilot/executor/processor.py+++ b/autogpt_platform/backend/backend/copilot/executor/processor.py@@ -351,6 +351,7 @@ class CoPilotProcessor:context=entry.context,file_ids=entry.file_ids,mode=effective_mode,+ model=entry.model,)async for chunk in stream_registry.stream_and_publish(session_id=entry.session_id,diff --git a/autogpt_platform/backend/backend/copilot/executor/utils.py b/autogpt_platform/backend/backend/copilot/executor/utils.pyindex 0f7d23d9ba..3256f94869 100644--- a/autogpt_platform/backend/backend/copilot/executor/utils.py+++ b/autogpt_platform/backend/backend/copilot/executor/utils.py@@ -9,7 +9,7 @@ import loggingfrom pydantic import BaseModel-from backend.copilot.config import CopilotMode+from backend.copilot.config import CopilotLlmModel, CopilotModefrom backend.data.rabbitmq import Exchange, ExchangeType, Queue, RabbitMQConfigfrom backend.util.logging import TruncatedLogger, is_structured_logging_enabled@@ -160,6 +160,9 @@ class CoPilotExecutionEntry(BaseModel):mode: CopilotMode | None = None"""Autopilot mode override: 'fast' or 'extended_thinking'. None = server default."""+ model: CopilotLlmModel | None = None+ """Per-request model tier: 'standard' or 'advanced'. None = server default."""+class CancelCoPilotEvent(BaseModel):"""Event to cancel a CoPilot operation."""@@ -180,6 +183,7 @@ async def enqueue_copilot_turn(context: dict[str, str] | None = None,file_ids: list[str] | None = None,mode: CopilotMode | None = None,+ model: CopilotLlmModel | None = None,) -> None:"""Enqueue a CoPilot task for processing by the executor service.@@ -192,6 +196,7 @@ async def enqueue_copilot_turn(context: Optional context for the message (e.g., {url: str, content: str})file_ids: Optional workspace file IDs attached to the user's messagemode: Autopilot mode override ('fast' or 'extended_thinking'). None = server default.+ model: Per-request model tier ('standard' or 'advanced'). None = server default."""from backend.util.clients import get_async_copilot_queue@@ -204,6 +209,7 @@ async def enqueue_copilot_turn(context=context,file_ids=file_ids,mode=mode,+ model=model,)queue_client = await get_async_copilot_queue()diff --git a/autogpt_platform/backend/backend/copilot/rate_limit.py b/autogpt_platform/backend/backend/copilot/rate_limit.pyindex f72d36de23..3124c28992 100644--- a/autogpt_platform/backend/backend/copilot/rate_limit.py+++ b/autogpt_platform/backend/backend/copilot/rate_limit.py@@ -302,6 +302,7 @@ async def record_token_usage(*,cache_read_tokens: int = 0,cache_creation_tokens: int = 0,+ model_cost_multiplier: float = 1.0,) -> None:"""Record token usage for a user across all windows.@@ -315,12 +316,17 @@ async def record_token_usage(``prompt_tokens`` should be the *uncached* input count (``input_tokens``from the API response). Cache counts are passed separately.+ ``model_cost_multiplier`` scales the final weighted total to reflect+ relative model cost. Use 5.0 for Opus (5× more expensive than Sonnet)+ so that Opus turns deplete the rate limit faster, proportional to cost.+Args:user_id: The user's ID.prompt_tokens: Uncached input tokens.completion_tokens: Output tokens.cache_read_tokens: Tokens served from prompt cache (10% cost).cache_creation_tokens: Tokens written to prompt cache (25% cost).+ model_cost_multiplier: Relative model cost factor (1.0 = Sonnet, 5.0 = Opus)."""prompt_tokens = max(0, prompt_tokens)completion_tokens = max(0, completion_tokens)@@ -332,7 +338,9 @@ async def record_token_usage(+ round(cache_creation_tokens * 0.25)+ round(cache_read_tokens * 0.1))- total = weighted_input + completion_tokens+ total = round(+ (weighted_input + completion_tokens) * max(1.0, model_cost_multiplier)+ )if total <= 0:return@@ -340,11 +348,12 @@ async def record_token_usage(prompt_tokens + cache_read_tokens + cache_creation_tokens + completion_tokens)logger.info(- "Recording token usage for %s: raw=%d, weighted=%d "+ "Recording token usage for %s: raw=%d, weighted=%d, multiplier=%.1fx ""(uncached=%d, cache_read=%d@10%%, cache_create=%d@25%%, output=%d)",user_id[:8],raw_total,total,+ model_cost_multiplier,prompt_tokens,cache_read_tokens,cache_creation_tokens,diff --git a/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py b/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.pyindex 7077337a79..9305320fea 100644--- a/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py@@ -207,7 +207,7 @@ class TestConfigDefaults:def test_max_budget_usd_default(self):cfg = _make_config()- assert cfg.claude_agent_max_budget_usd == 15.0+ assert cfg.claude_agent_max_budget_usd == 10.0def test_max_thinking_tokens_default(self):cfg = _make_config()diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex c7d166adba..3b655ffd1b 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -56,7 +56,7 @@ from backend.executor.cluster_lock import AsyncClusterLockfrom backend.util.exceptions import NotFoundErrorfrom backend.util.settings import Settings-from ..config import ChatConfig, CopilotMode+from ..config import ChatConfig, CopilotLlmModel, CopilotModefrom ..constants import (COPILOT_ERROR_PREFIX,COPILOT_RETRYABLE_ERROR_PREFIX,@@ -132,6 +132,11 @@ _MAX_STREAM_ATTEMPTS = 3# self-correct. The limit is generous to allow recovery attempts._EMPTY_TOOL_CALL_LIMIT = 5+# Cost multiplier for Opus model turns — Opus is ~5× more expensive than Sonnet+# ($15/$75 vs $3/$15 per M tokens). Applied to rate-limit counters so Opus+# turns deplete quota proportionally faster.+_OPUS_COST_MULTIPLIER = 5.0+# User-facing error shown when the empty-tool-call circuit breaker trips._CIRCUIT_BREAKER_ERROR_MSG = ("AutoPilot was unable to complete the tool call "@@ -674,6 +679,48 @@ def _resolve_fallback_model() -> str | None:return _normalize_model_name(raw)+async def _resolve_model_and_multiplier(+ model: "CopilotLlmModel | None",+ session_id: str,+) -> tuple[str | None, float]:+ """Resolve the SDK model string and rate-limit cost multiplier for a turn.++ Priority (highest first):+ 1. Explicit per-request ``model`` tier from the frontend toggle.+ 2. Global config default (``_resolve_sdk_model()``).++ Returns a ``(sdk_model, cost_multiplier)`` pair.+ ``sdk_model`` is ``None`` when the Claude Code subscription default applies.+ ``cost_multiplier`` is 5.0 for Opus, 1.0 otherwise.+ """+ sdk_model = _resolve_sdk_model()++ if model == "advanced":+ sdk_model = _normalize_model_name("anthropic/claude-opus-4-6")+ logger.info(+ "[SDK] [%s] Per-request model override: advanced (%s)",+ session_id[:12] if session_id else "?",+ sdk_model,+ )+ return sdk_model, _OPUS_COST_MULTIPLIER++ 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)",+ session_id[:12] if session_id else "?",+ sdk_model or "subscription-default",+ )+ return sdk_model, 1.0++ # No per-request override; derive multiplier from final resolved model.+ cost_multiplier = (+ _OPUS_COST_MULTIPLIER if sdk_model and "opus" in sdk_model else 1.0+ )+ return sdk_model, cost_multiplier++_MAX_TRANSIENT_BACKOFF_SECONDS = 30@@ -2155,6 +2202,7 @@ async def stream_chat_completion_sdk(file_ids: list[str] | None = None,permissions: "CopilotPermissions | None" = None,mode: CopilotMode | None = None,+ model: CopilotLlmModel | None = None,**_kwargs: Any,) -> AsyncIterator[StreamBaseResponse]:"""Stream chat completion using Claude Agent SDK.@@ -2165,6 +2213,9 @@ async def stream_chat_completion_sdk(saved to the SDK working directory for the Read tool.mode: Accepted for signature compatibility with the baseline path.The SDK path does not currently branch on this value.+ model: Per-request model preference from the frontend toggle.+ 'advanced' → Claude Opus; 'standard' → global config default.+ Takes priority over per-user LaunchDarkly targeting."""_ = mode # SDK path ignores the requested mode.@@ -2279,6 +2330,10 @@ async def stream_chat_completion_sdk(turn_cache_creation_tokens = 0turn_cost_usd: float | None = Nonegraphiti_enabled = False+ # Defaults ensure the finally block can always reference these safely even when+ # an early return (e.g. sdk_cwd error) skips their normal assignment below.+ sdk_model: str | None = None+ model_cost_multiplier: float = 1.0# Make sure there is no more code between the lock acquisition and try-block.try:@@ -2490,7 +2545,10 @@ async def stream_chat_completion_sdk(mcp_server = create_copilot_mcp_server(use_e2b=use_e2b)- sdk_model = _resolve_sdk_model()+ # Resolve model and cost multiplier (request tier → config default).+ sdk_model, model_cost_multiplier = await _resolve_model_and_multiplier(+ model, session_id+ )# Track SDK-internal compaction (PreCompact hook → start, next msg → end)compaction = CompactionTracker()@@ -3175,8 +3233,9 @@ async def stream_chat_completion_sdk(cache_creation_tokens=turn_cache_creation_tokens,log_prefix=log_prefix,cost_usd=turn_cost_usd,- model=config.model,+ model=sdk_model or config.model,provider="anthropic",+ model_cost_multiplier=model_cost_multiplier,)# --- Persist session messages ---diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.pyindex 5f1487c43b..9d8b4bb135 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py@@ -20,6 +20,7 @@ from .service import (_is_prompt_too_long,_is_tool_only_message,_iter_sdk_messages,+ _normalize_model_name,_reduce_context,_TokenUsage,)@@ -353,6 +354,47 @@ class TestIsParallelContinuation:assert _is_tool_only_message(msg) is True+# ---------------------------------------------------------------------------+# _normalize_model_name — used by per-request model override+# ---------------------------------------------------------------------------+++class TestNormalizeModelName:+ """Unit tests for the model-name normalisation helper.++ The per-request model toggle calls _normalize_model_name with either+ ``"anthropic/claude-opus-4-6"`` (for 'advanced') or ``config.model`` (for+ 'standard'). These tests verify the OpenRouter/provider-prefix stripping+ that keeps the value compatible with the Claude CLI.+ """++ def test_strips_anthropic_prefix(self):+ assert _normalize_model_name("anthropic/claude-opus-4-6") == "claude-opus-4-6"++ def test_strips_openai_prefix(self):+ assert _normalize_model_name("openai/gpt-4o") == "gpt-4o"++ def test_strips_google_prefix(self):+ assert _normalize_model_name("google/gemini-2.5-flash") == "gemini-2.5-flash"++ def test_already_normalized_unchanged(self):+ assert (+ _normalize_model_name("claude-sonnet-4-20250514")+ == "claude-sonnet-4-20250514"+ )++ def test_empty_string_unchanged(self):+ assert _normalize_model_name("") == ""++ def test_opus_model_roundtrip(self):+ """The exact string used for the 'opus' toggle strips correctly."""+ assert _normalize_model_name("anthropic/claude-opus-4-6") == "claude-opus-4-6"++ def test_sonnet_openrouter_model(self):+ """Sonnet model as stored in config (OpenRouter-prefixed) strips cleanly."""+ assert _normalize_model_name("anthropic/claude-sonnet-4") == "claude-sonnet-4"++# ---------------------------------------------------------------------------# _TokenUsage — null-safe accumulation (OpenRouter initial-stream-event bug)# ---------------------------------------------------------------------------diff --git a/autogpt_platform/backend/backend/copilot/token_tracking.py b/autogpt_platform/backend/backend/copilot/token_tracking.pyindex e84b64d449..19406ced93 100644--- a/autogpt_platform/backend/backend/copilot/token_tracking.py+++ b/autogpt_platform/backend/backend/copilot/token_tracking.py@@ -96,6 +96,7 @@ async def persist_and_record_usage(cost_usd: float | str | None = None,model: str | None = None,provider: str = "open_router",+ model_cost_multiplier: float = 1.0,) -> int:"""Persist token usage to session and record for rate limiting.@@ -109,6 +110,9 @@ async def persist_and_record_usage(log_prefix: Prefix for log messages (e.g. "[SDK]", "[Baseline]").cost_usd: Optional cost for logging (float from SDK, str otherwise).provider: Cost provider name (e.g. "anthropic", "open_router").+ model_cost_multiplier: Relative model cost factor for rate limiting+ (1.0 = Sonnet/default, 5.0 = Opus). Scales the token counter so+ more expensive models deplete the rate limit proportionally faster.Returns:The computed total_tokens (prompt + completion; cache excluded).@@ -163,6 +167,7 @@ async def persist_and_record_usage(completion_tokens=completion_tokens,cache_read_tokens=cache_read_tokens,cache_creation_tokens=cache_creation_tokens,+ model_cost_multiplier=model_cost_multiplier,)except Exception as usage_err:logger.warning("%s Failed to record token usage: %s", log_prefix, usage_err)diff --git a/autogpt_platform/backend/backend/copilot/token_tracking_test.py b/autogpt_platform/backend/backend/copilot/token_tracking_test.pyindex 04c7667368..11757ce541 100644--- a/autogpt_platform/backend/backend/copilot/token_tracking_test.py+++ b/autogpt_platform/backend/backend/copilot/token_tracking_test.py@@ -230,6 +230,7 @@ class TestRateLimitRecording:completion_tokens=50,cache_read_tokens=1000,cache_creation_tokens=200,+ model_cost_multiplier=1.0,)@pytest.mark.asynciodiff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/store.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/store.test.tsindex f993daf58d..fd95bbdb2c 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/store.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/store.test.ts@@ -1,4 +1,4 @@-import { describe, expect, it, beforeEach, vi } from "vitest";+import { describe, expect, it, beforeEach, afterEach, vi } from "vitest";import { useCopilotUIStore } from "../store";vi.mock("@sentry/nextjs", () => ({@@ -22,7 +22,8 @@ describe("useCopilotUIStore", () => {isNotificationsEnabled: false,isSoundEnabled: true,showNotificationDialog: false,- copilotMode: "extended_thinking",+ copilotChatMode: "extended_thinking",+ copilotLlmModel: "standard",});});@@ -154,35 +155,52 @@ describe("useCopilotUIStore", () => {});});- describe("copilotMode", () => {+ describe("copilotChatMode", () => {it("defaults to extended_thinking", () => {- expect(useCopilotUIStore.getState().copilotMode).toBe(+ expect(useCopilotUIStore.getState().copilotChatMode).toBe("extended_thinking",);});it("sets mode to fast", () => {- useCopilotUIStore.getState().setCopilotMode("fast");- expect(useCopilotUIStore.getState().copilotMode).toBe("fast");+ useCopilotUIStore.getState().setCopilotChatMode("fast");+ expect(useCopilotUIStore.getState().copilotChatMode).toBe("fast");});it("sets mode back to extended_thinking", () => {- useCopilotUIStore.getState().setCopilotMode("fast");- useCopilotUIStore.getState().setCopilotMode("extended_thinking");- expect(useCopilotUIStore.getState().copilotMode).toBe(+ useCopilotUIStore.getState().setCopilotChatMode("fast");+ useCopilotUIStore.getState().setCopilotChatMode("extended_thinking");+ expect(useCopilotUIStore.getState().copilotChatMode).toBe("extended_thinking",);});- it("does not persist mode to localStorage", () => {- useCopilotUIStore.getState().setCopilotMode("fast");- expect(window.localStorage.getItem("copilot-mode")).toBeNull();+ it("persists mode to localStorage", () => {+ useCopilotUIStore.getState().setCopilotChatMode("fast");+ expect(window.localStorage.getItem("copilot-mode")).toBe("fast");+ });+ });++ describe("copilotLlmModel", () => {+ it("defaults to standard", () => {+ expect(useCopilotUIStore.getState().copilotLlmModel).toBe("standard");+ });++ it("sets model to advanced", () => {+ useCopilotUIStore.getState().setCopilotLlmModel("advanced");+ expect(useCopilotUIStore.getState().copilotLlmModel).toBe("advanced");+ });++ it("persists model to localStorage", () => {+ useCopilotUIStore.getState().setCopilotLlmModel("advanced");+ expect(window.localStorage.getItem("copilot-model")).toBe("advanced");});});describe("clearCopilotLocalData", () => {it("resets state and clears localStorage keys", () => {- useCopilotUIStore.getState().setCopilotMode("fast");+ useCopilotUIStore.getState().setCopilotChatMode("fast");+ useCopilotUIStore.getState().setCopilotLlmModel("advanced");useCopilotUIStore.getState().setNotificationsEnabled(true);useCopilotUIStore.getState().toggleSound();useCopilotUIStore.getState().addCompletedSession("s1");@@ -190,7 +208,8 @@ describe("useCopilotUIStore", () => {useCopilotUIStore.getState().clearCopilotLocalData();const state = useCopilotUIStore.getState();- expect(state.copilotMode).toBe("extended_thinking");+ expect(state.copilotChatMode).toBe("extended_thinking");+ expect(state.copilotLlmModel).toBe("standard");expect(state.isNotificationsEnabled).toBe(false);expect(state.isSoundEnabled).toBe(true);expect(state.completedSessionIDs.size).toBe(0);@@ -198,6 +217,8 @@ describe("useCopilotUIStore", () => {window.localStorage.getItem("copilot-notifications-enabled"),).toBeNull();expect(window.localStorage.getItem("copilot-sound-enabled")).toBeNull();+ expect(window.localStorage.getItem("copilot-mode")).toBeNull();+ expect(window.localStorage.getItem("copilot-model")).toBeNull();expect(window.localStorage.getItem("copilot-completed-sessions"),).toBeNull();@@ -222,3 +243,24 @@ describe("useCopilotUIStore", () => {});});});++describe("useCopilotUIStore localStorage initialisation", () => {+ afterEach(() => {+ vi.resetModules();+ window.localStorage.clear();+ });++ it("reads fast chat mode from localStorage on store creation", async () => {+ window.localStorage.setItem("copilot-mode", "fast");+ vi.resetModules();+ const { useCopilotUIStore: fresh } = await import("../store");+ expect(fresh.getState().copilotChatMode).toBe("fast");+ });++ it("reads advanced model from localStorage on store creation", async () => {+ window.localStorage.setItem("copilot-model", "advanced");+ vi.resetModules();+ const { useCopilotUIStore: fresh } = await import("../store");+ expect(fresh.getState().copilotLlmModel).toBe("advanced");+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsxindex d1e1ca4f9d..b6fedb722e 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx@@ -13,6 +13,7 @@ import { ChangeEvent, useEffect, useState } from "react";import { AttachmentMenu } from "./components/AttachmentMenu";import { DryRunToggleButton } from "./components/DryRunToggleButton";import { FileChips } from "./components/FileChips";+import { ModelToggleButton } from "./components/ModelToggleButton";import { ModeToggleButton } from "./components/ModeToggleButton";import { RecordingButton } from "./components/RecordingButton";import { RecordingIndicator } from "./components/RecordingIndicator";@@ -50,16 +51,22 @@ export function ChatInput({onDroppedFilesConsumed,hasSession = false,}: Props) {- const { copilotMode, setCopilotMode, isDryRun, setIsDryRun } =- useCopilotUIStore();+ const {+ copilotChatMode,+ setCopilotChatMode,+ copilotLlmModel,+ setCopilotLlmModel,+ isDryRun,+ setIsDryRun,+ } = useCopilotUIStore();const showModeToggle = useGetFlag(Flag.CHAT_MODE_OPTION);const showDryRunToggle = showModeToggle;const [files, setFiles] = useState<File[]>([]);function handleToggleMode() {const next =- copilotMode === "extended_thinking" ? "fast" : "extended_thinking";- setCopilotMode(next);+ copilotChatMode === "extended_thinking" ? "fast" : "extended_thinking";+ setCopilotChatMode(next);toast({title:next === "fast"@@ -72,6 +79,21 @@ export function ChatInput({});}+ function handleToggleModel() {+ const next = copilotLlmModel === "advanced" ? "standard" : "advanced";+ setCopilotLlmModel(next);+ toast({+ title:+ next === "advanced"+ ? "Switched to Advanced model"+ : "Switched to Standard model",+ description:+ next === "advanced"+ ? "Using the highest-capability model."+ : "Using the balanced standard model.",+ });+ }+function handleToggleDryRun() {const next = !isDryRun;setIsDryRun(next);@@ -198,10 +220,16 @@ export function ChatInput({/>{showModeToggle && !isStreaming && (<ModeToggleButton- mode={copilotMode}+ mode={copilotChatMode}onToggle={handleToggleMode}/>)}+ {showModeToggle && !isStreaming && (+ <ModelToggleButton+ model={copilotLlmModel}+ onToggle={handleToggleModel}+ />+ )}{showDryRunToggle && (!hasSession || isDryRun) && (<DryRunToggleButtonisDryRun={isDryRun}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsxindex ee92b7cc94..ddcf8489ce 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx@@ -8,14 +8,21 @@ import { afterEach, describe, expect, it, vi } from "vitest";import { ChatInput } from "../ChatInput";let mockCopilotMode = "extended_thinking";-const mockSetCopilotMode = vi.fn((mode: string) => {+const mockSetCopilotChatMode = vi.fn((mode: string) => {mockCopilotMode = mode;});+let mockCopilotLlmModel = "standard";+const mockSetCopilotLlmModel = vi.fn((model: string) => {+ mockCopilotLlmModel = model;+});+vi.mock("@/app/(platform)/copilot/store", () => ({useCopilotUIStore: () => ({- copilotMode: mockCopilotMode,- setCopilotMode: mockSetCopilotMode,+ copilotChatMode: mockCopilotMode,+ setCopilotChatMode: mockSetCopilotChatMode,+ copilotLlmModel: mockCopilotLlmModel,+ setCopilotLlmModel: mockSetCopilotLlmModel,initialPrompt: null,setInitialPrompt: vi.fn(),}),@@ -107,6 +114,7 @@ afterEach(() => {cleanup();vi.clearAllMocks();mockCopilotMode = "extended_thinking";+ mockCopilotLlmModel = "standard";});describe("ChatInput mode toggle", () => {@@ -141,7 +149,7 @@ describe("ChatInput mode toggle", () => {mockCopilotMode = "extended_thinking";render(<ChatInput onSend={mockOnSend} />);fireEvent.click(screen.getByLabelText(/switch to fast mode/i));- expect(mockSetCopilotMode).toHaveBeenCalledWith("fast");+ expect(mockSetCopilotChatMode).toHaveBeenCalledWith("fast");});it("toggles from fast to extended_thinking on click", () => {@@ -149,7 +157,7 @@ describe("ChatInput mode toggle", () => {mockCopilotMode = "fast";render(<ChatInput onSend={mockOnSend} />);fireEvent.click(screen.getByLabelText(/switch to extended thinking/i));- expect(mockSetCopilotMode).toHaveBeenCalledWith("extended_thinking");+ expect(mockSetCopilotChatMode).toHaveBeenCalledWith("extended_thinking");});it("hides toggle button when streaming", () => {@@ -187,3 +195,69 @@ describe("ChatInput mode toggle", () => {);});});++describe("ChatInput model toggle", () => {+ it("renders model toggle button when flag is enabled", () => {+ mockFlagValue = true;+ render(<ChatInput onSend={mockOnSend} />);+ expect(screen.getByLabelText(/switch to advanced model/i)).toBeDefined();+ });++ it("does not render model toggle when flag is disabled", () => {+ mockFlagValue = false;+ render(<ChatInput onSend={mockOnSend} />);+ expect(+ screen.queryByLabelText(/switch to (advanced|standard) model/i),+ ).toBeNull();+ });++ it("toggles from standard to advanced on click", () => {+ mockFlagValue = true;+ mockCopilotLlmModel = "standard";+ render(<ChatInput onSend={mockOnSend} />);+ fireEvent.click(screen.getByLabelText(/switch to advanced model/i));+ expect(mockSetCopilotLlmModel).toHaveBeenCalledWith("advanced");+ });++ it("toggles from advanced to standard on click", () => {+ mockFlagValue = true;+ mockCopilotLlmModel = "advanced";+ render(<ChatInput onSend={mockOnSend} />);+ fireEvent.click(screen.getByLabelText(/switch to standard model/i));+ expect(mockSetCopilotLlmModel).toHaveBeenCalledWith("standard");+ });++ it("hides model toggle when streaming", () => {+ mockFlagValue = true;+ render(<ChatInput onSend={mockOnSend} isStreaming />);+ expect(+ screen.queryByLabelText(/switch to (advanced|standard) model/i),+ ).toBeNull();+ });++ it("shows a toast when switching to advanced", async () => {+ const { toast } = await import("@/components/molecules/Toast/use-toast");+ mockFlagValue = true;+ mockCopilotLlmModel = "standard";+ render(<ChatInput onSend={mockOnSend} />);+ fireEvent.click(screen.getByLabelText(/switch to advanced model/i));+ expect(toast).toHaveBeenCalledWith(+ expect.objectContaining({+ title: expect.stringMatching(/switched to advanced model/i),+ }),+ );+ });++ it("shows a toast when switching to standard", async () => {+ const { toast } = await import("@/components/molecules/Toast/use-toast");+ mockFlagValue = true;+ mockCopilotLlmModel = "advanced";+ render(<ChatInput onSend={mockOnSend} />);+ fireEvent.click(screen.getByLabelText(/switch to standard model/i));+ expect(toast).toHaveBeenCalledWith(+ expect.objectContaining({+ title: expect.stringMatching(/switched to standard model/i),+ }),+ );+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ModelToggleButton.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ModelToggleButton.tsxnew file mode 100644index 0000000000..cb3bc25f4f--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/ModelToggleButton.tsx@@ -0,0 +1,38 @@+"use client";++import { cn } from "@/lib/utils";+import { Cpu } from "@phosphor-icons/react";+import type { CopilotLlmModel } from "../../../store";++interface Props {+ model: CopilotLlmModel;+ onToggle: () => void;+}++export function ModelToggleButton({ model, onToggle }: Props) {+ const isAdvanced = model === "advanced";+ return (+ <button+ type="button"+ aria-pressed={isAdvanced}+ onClick={onToggle}+ className={cn(+ "inline-flex min-h-11 min-w-11 items-center justify-center gap-1 rounded-md px-2 py-1 text-xs font-medium transition-colors",+ isAdvanced+ ? "bg-sky-100 text-sky-900 hover:bg-sky-200"+ : "text-neutral-500 hover:bg-neutral-100 hover:text-neutral-700",+ )}+ aria-label={+ isAdvanced ? "Switch to Standard model" : "Switch to Advanced model"+ }+ title={+ isAdvanced+ ? "Advanced model — highest capability (click to switch to Standard)"+ : "Standard model — click to switch to Advanced"+ }+ >+ <Cpu size={14} />+ {isAdvanced && "Advanced"}+ </button>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/ModelToggleButton.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/ModelToggleButton.test.tsxnew file mode 100644index 0000000000..a77cb5b6f4--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/__tests__/ModelToggleButton.test.tsx@@ -0,0 +1,36 @@+import { render, screen, fireEvent, cleanup } from "@testing-library/react";+import { afterEach, describe, expect, it, vi } from "vitest";+import { ModelToggleButton } from "../ModelToggleButton";++afterEach(cleanup);++describe("ModelToggleButton", () => {+ it("shows no label when model is standard", () => {+ render(<ModelToggleButton model="standard" onToggle={vi.fn()} />);+ expect(screen.queryByText("Advanced")).toBeNull();+ });++ it("shows Advanced label when model is advanced", () => {+ render(<ModelToggleButton model="advanced" onToggle={vi.fn()} />);+ expect(screen.getByText("Advanced")).toBeTruthy();+ });++ it("calls onToggle when clicked", () => {+ const onToggle = vi.fn();+ render(<ModelToggleButton model="standard" onToggle={onToggle} />);+ fireEvent.click(screen.getByRole("button"));+ expect(onToggle).toHaveBeenCalledTimes(1);+ });++ it("sets aria-pressed=false for standard", () => {+ render(<ModelToggleButton model="standard" onToggle={vi.fn()} />);+ const btn = screen.getByLabelText("Switch to Advanced model");+ expect(btn.getAttribute("aria-pressed")).toBe("false");+ });++ it("sets aria-pressed=true for advanced", () => {+ render(<ModelToggleButton model="advanced" onToggle={vi.fn()} />);+ const btn = screen.getByLabelText("Switch to Standard model");+ expect(btn.getAttribute("aria-pressed")).toBe("true");+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/store.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/store.tsindex d63c0bd76a..d8dcbd132c 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/store.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/store.ts@@ -53,6 +53,9 @@ export const DEFAULT_PANEL_WIDTH = 600;/** Autopilot response mode. */export type CopilotMode = "extended_thinking" | "fast";+/** Per-request model tier. 'standard' = current default; 'advanced' = highest-capability. */+export type CopilotLlmModel = "standard" | "advanced";+const isClient = typeof window !== "undefined";function getPersistedWidth(): number {@@ -134,8 +137,12 @@ interface CopilotUIState {goBackArtifact: () => void;/** Autopilot mode: 'extended_thinking' (default) or 'fast'. */- copilotMode: CopilotMode;- setCopilotMode: (mode: CopilotMode) => void;+ copilotChatMode: CopilotMode;+ setCopilotChatMode: (mode: CopilotMode) => void;++ /** Model tier: 'standard' (default) or 'advanced' (highest-capability). */+ copilotLlmModel: CopilotLlmModel;+ setCopilotLlmModel: (model: CopilotLlmModel) => void;/** Developer dry-run mode: sessions created with dry_run=true. */isDryRun: boolean;@@ -298,9 +305,22 @@ export const useCopilotUIStore = create<CopilotUIState>((set) => ({};}),- copilotMode: "extended_thinking",- setCopilotMode: (mode) => {- set({ copilotMode: mode });+ copilotChatMode: (() => {+ const saved = isClient ? storage.get(Key.COPILOT_MODE) : null;+ return saved === "fast" ? "fast" : "extended_thinking";+ })(),+ setCopilotChatMode: (mode) => {+ storage.set(Key.COPILOT_MODE, mode);+ set({ copilotChatMode: mode });+ },++ copilotLlmModel: (() => {+ const saved = isClient ? storage.get(Key.COPILOT_MODEL) : null;+ return saved === "advanced" ? "advanced" : "standard";+ })(),+ setCopilotLlmModel: (model) => {+ storage.set(Key.COPILOT_MODEL, model);+ set({ copilotLlmModel: model });},isDryRun: isClient && storage.get(Key.COPILOT_DRY_RUN) === "true",@@ -322,6 +342,8 @@ export const useCopilotUIStore = create<CopilotUIState>((set) => ({storage.clean(Key.COPILOT_ARTIFACT_PANEL_WIDTH);storage.clean(Key.COPILOT_COMPLETED_SESSIONS);storage.clean(Key.COPILOT_DRY_RUN);+ storage.clean(Key.COPILOT_MODE);+ storage.clean(Key.COPILOT_MODEL);set({completedSessionIDs: new Set<string>(),isNotificationsEnabled: false,@@ -334,7 +356,8 @@ export const useCopilotUIStore = create<CopilotUIState>((set) => ({activeArtifact: null,history: [],},- copilotMode: "extended_thinking",+ copilotChatMode: "extended_thinking",+ copilotLlmModel: "standard",isDryRun: false,});if (isClient) {diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.tsindex f8b0387c6b..01302c9f81 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts@@ -42,7 +42,8 @@ export function useCopilotPage() {setSessionToDelete,isDrawerOpen,setDrawerOpen,- copilotMode,+ copilotChatMode,+ copilotLlmModel,isDryRun,} = useCopilotUIStore();@@ -78,7 +79,8 @@ export function useCopilotPage() {hydratedMessages,hasActiveStream,refetchSession,- copilotMode: isModeToggleEnabled ? copilotMode : undefined,+ copilotMode: isModeToggleEnabled ? copilotChatMode : undefined,+ copilotModel: isModeToggleEnabled ? copilotLlmModel : undefined,});const { olderMessages, hasMore, isLoadingMore, loadMore } =diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.tsindex 918047d3d8..14ea672bfb 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts@@ -18,7 +18,7 @@ import {resolveInProgressTools,getSendSuppressionReason,} from "./helpers";-import type { CopilotMode } from "./store";+import type { CopilotLlmModel, CopilotMode } from "./store";const RECONNECT_BASE_DELAY_MS = 1_000;const RECONNECT_MAX_ATTEMPTS = 3;@@ -33,6 +33,8 @@ interface UseCopilotStreamArgs {refetchSession: () => Promise<{ data?: unknown }>;/** Autopilot mode to use for requests. `undefined` = let backend decide via feature flags. */copilotMode: CopilotMode | undefined;+ /** Model tier override. `undefined` = let backend decide. */+ copilotModel: CopilotLlmModel | undefined;}export function useCopilotStream({@@ -41,17 +43,20 @@ export function useCopilotStream({hasActiveStream,refetchSession,copilotMode,+ copilotModel,}: UseCopilotStreamArgs) {const queryClient = useQueryClient();const [rateLimitMessage, setRateLimitMessage] = useState<string | null>(null);function dismissRateLimit() {setRateLimitMessage(null);}- // Use a ref for copilotMode so the transport closure always reads the- // latest value without recreating the DefaultChatTransport (which would+ // Use refs for copilotMode and copilotModel so the transport closure always reads+ // the latest value without recreating the DefaultChatTransport (which would// reset useChat's internal Chat instance and break mid-session streaming).const copilotModeRef = useRef(copilotMode);copilotModeRef.current = copilotMode;+ const copilotModelRef = useRef(copilotModel);+ copilotModelRef.current = copilotModel;// Connect directly to the Python backend for SSE, bypassing the Next.js// serverless proxy. This eliminates the Vercel 800s function timeout that@@ -83,6 +88,7 @@ export function useCopilotStream({context: null,file_ids: fileIds && fileIds.length > 0 ? fileIds : null,mode: copilotModeRef.current ?? null,+ model: copilotModelRef.current ?? null,},headers: await getCopilotAuthHeaders(),};diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 732ef569d9..32e91bfd51 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -13931,6 +13931,14 @@],"title": "Mode","description": "Autopilot mode: 'fast' for baseline LLM, 'extended_thinking' for Claude Agent SDK. If None, uses the server default (extended_thinking)."+ },+ "model": {+ "anyOf": [+ { "type": "string", "enum": ["standard", "advanced"] },+ { "type": "null" }+ ],+ "title": "Model",+ "description": "Model tier: 'standard' for the default model, 'advanced' for the highest-capability model. If None, the server applies per-user LD targeting then falls back to config."}},"type": "object",diff --git a/autogpt_platform/frontend/src/services/storage/local-storage.ts b/autogpt_platform/frontend/src/services/storage/local-storage.tsindex de31967d53..b5c0392ecd 100644--- a/autogpt_platform/frontend/src/services/storage/local-storage.ts+++ b/autogpt_platform/frontend/src/services/storage/local-storage.ts@@ -17,6 +17,7 @@ export enum Key {COPILOT_NOTIFICATION_DIALOG_DISMISSED = "copilot-notification-dialog-dismissed",COPILOT_ARTIFACT_PANEL_WIDTH = "copilot-artifact-panel-width",COPILOT_MODE = "copilot-mode",+ COPILOT_MODEL = "copilot-model",COPILOT_COMPLETED_SESSIONS = "copilot-completed-sessions",COPILOT_DRY_RUN = "copilot-dry-run",}