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): switch main client between OpenRouter and Anthropic-direct (#13034)
Zamil Majdy1ec097cbd986
diff --git a/.github/workflows/refresh-anthropic-rates.yml b/.github/workflows/refresh-anthropic-rates.ymlnew file mode 100644index 0000000000..3a0bc5a4df--- /dev/null+++ b/.github/workflows/refresh-anthropic-rates.yml@@ -0,0 +1,41 @@+name: Refresh Anthropic rate card++on:+ schedule:+ - cron: "0 6 * * 1" # Monday 06:00 UTC+ workflow_dispatch:++permissions:+ contents: write+ pull-requests: write++jobs:+ refresh:+ runs-on: ubuntu-latest+ steps:+ - uses: actions/checkout@v5++ - uses: actions/setup-python@v6+ with:+ python-version: "3.13"++ - name: Refresh anthropic_rates.json+ working-directory: autogpt_platform/backend+ run: python scripts/refresh_anthropic_rates.py++ - name: Open PR with refreshed rates+ uses: peter-evans/create-pull-request@v6+ with:+ branch: chore/refresh-anthropic-rates+ base: dev+ title: "chore(backend): refresh Anthropic rate card from LiteLLM"+ commit-message: "chore(backend): refresh anthropic_rates.json from LiteLLM upstream"+ body: |+ Weekly refresh of `autogpt_platform/backend/backend/copilot/anthropic_rates.json`+ from LiteLLM's `model_prices_and_context_window_backup.json`.++ Filtered to `claude*` / `anthropic/claude*` entries.++ Auto-generated by `.github/workflows/refresh-anthropic-rates.yml`.+ add-paths: |+ autogpt_platform/backend/backend/copilot/anthropic_rates.jsondiff --git a/autogpt_platform/backend/backend/copilot/anthropic_rate_card.py b/autogpt_platform/backend/backend/copilot/anthropic_rate_card.pynew file mode 100644index 0000000000..a281ade626--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/anthropic_rate_card.py@@ -0,0 +1,243 @@+"""Anthropic rate-card lookup for direct-mode cost computation.++Used by the baseline path when ``CHAT_USE_OPENROUTER=false`` — the+OpenAI-compat endpoint at api.anthropic.com does **not** return a+``usage.cost`` field (that is an OpenRouter extension), so we compute+USD from token counts × per-model rates here.++Rates are vendored from LiteLLM's community-maintained pricing data+(``model_prices_and_context_window.json``) — only the Anthropic+entries are kept, filtered into ``anthropic_rates.json`` next to this+module. The litellm Python package itself is **not** a dependency, to+avoid pulling in its proxy-server module which has had a string of+auth-bypass / password-hash CVEs in stable releases.++Refresh: weekly cron in ``.github/workflows/refresh-anthropic-rates.yml``+re-runs ``scripts/refresh_anthropic_rates.py`` and opens a PR. Manual+refresh: ``poetry run python scripts/refresh_anthropic_rates.py``.++Anthropic prompt-caching docs (rates we apply when the source omits+the 1h cache-write field on a model):+https://docs.claude.com/en/docs/build-with-claude/prompt-caching+"""++from __future__ import annotations++import json+import logging+from pathlib import Path+from typing import Any++logger = logging.getLogger(__name__)++_RATES_PATH = Path(__file__).parent / "anthropic_rates.json"++# Default cache-read multiplier when the source omits the field — Anthropic's+# documented rate is 10% of the input rate (0.1×).+_DEFAULT_CACHE_READ_MULTIPLIER = 0.1+# Default 1h cache-write multiplier when the source omits the field —+# Anthropic's documented rate is 2.0× input.+_DEFAULT_CACHE_WRITE_1H_MULTIPLIER = 2.0+# Default 5m cache-write multiplier — Anthropic's documented 1.25× input.+_DEFAULT_CACHE_WRITE_5M_MULTIPLIER = 1.25++# Conservative fallback rates used when the rate-card has no entry for the+# configured model (a fresh Claude release before a vendor refresh, a+# typo'd slug, or a finetune). Set to claude-opus-4-1's published+# pricing — the most expensive Claude generally available — so an+# unknown slug **over-bills** rather than silently dropping cost.+# Billing integrity matters more than rate accuracy in the unknown-model+# tail; an alarming bill is recoverable, a missing one is not.+_FALLBACK_INPUT_PER_TOKEN = 15.0 / 1_000_000 # opus-4-1: $15/Mtok+_FALLBACK_OUTPUT_PER_TOKEN = 75.0 / 1_000_000 # opus-4-1: $75/Mtok++# Conservative output-token cap when the rate-card has no entry / no+# max_output_tokens field — Opus 4.x publishes 32K, Sonnet 4.x publishes+# 64K. Pick the lower bound so a fresh slug we don't recognise can't+# push the request over a concrete model's hard limit and 400 the API.+_FALLBACK_MAX_OUTPUT_TOKENS = 32_000+++_rates_cache: dict[str, dict[str, Any]] | None = None+++def _load_rates() -> dict[str, dict[str, Any]]:+ """Load the vendored Anthropic rate JSON once and cache in memory.++ Returns an empty dict on any failure so callers degrade to fallback+ pricing instead of crashing — billing must never short-circuit on a+ corrupt vendor file.+ """+ global _rates_cache+ if _rates_cache is not None:+ return _rates_cache+ try:+ raw = json.loads(_RATES_PATH.read_text(encoding="utf-8"))+ except (OSError, json.JSONDecodeError):+ logger.exception(+ "Failed to load %s; Anthropic direct-mode cost lookups will "+ "use fallback opus rates until the file is restored",+ _RATES_PATH.name,+ )+ _rates_cache = {}+ return _rates_cache+ rates = raw.get("rates") if isinstance(raw, dict) else None+ if not isinstance(rates, dict):+ logger.error(+ "Invalid rates JSON in %s (expected ``rates`` object at root); "+ "direct-mode cost lookups will use fallback opus rates",+ _RATES_PATH.name,+ )+ _rates_cache = {}+ return _rates_cache+ _rates_cache = {k: v for k, v in rates.items() if isinstance(v, dict)}+ return _rates_cache+++def reset_cache() -> None:+ """Test-only: drop the in-memory rates cache."""+ global _rates_cache+ _rates_cache = None+++def _resolve_info(model: str) -> dict[str, Any] | None:+ """Return the rate entry, trying the bare slug first then the+ ``anthropic/`` prefixed variant — LiteLLM keys both shapes+ inconsistently across model generations.+ """+ rates = _load_rates()+ for candidate in (model, f"anthropic/{model}"):+ entry = rates.get(candidate)+ if isinstance(entry, dict):+ return entry+ return None+++def get_max_output_tokens(model: str) -> int:+ """Return the model's max output tokens from the rate card, or a+ conservative fallback when the rate card has no entry.++ Used by the baseline path to cap ``max_tokens`` on direct-Anthropic+ extended-thinking requests so ``budget_tokens + response margin`` never+ exceeds the model's hard output limit (Anthropic 400s on overflow).+ """+ info = _resolve_info(model) or {}+ raw = info.get("max_output_tokens")+ if isinstance(raw, int) and raw > 0:+ return raw+ return _FALLBACK_MAX_OUTPUT_TOKENS+++def _is_anthropic_slug(model: str) -> bool:+ """Quick pre-check: does the slug look like an Anthropic model?++ Lets ``compute_anthropic_cost_usd`` short-circuit non-Anthropic+ slugs (``gpt-4o-mini``, ``google/gemini-2.5-pro``, ...) before+ hitting the rate-card lookup — those have no business going through+ the Anthropic rate card and must not trigger the opus fallback.+ """+ lowered = model.lower()+ if lowered.startswith(("anthropic/", "anthropic.")):+ return True+ # Bare ``claude-*`` slug with no provider prefix (direct-API form).+ if "/" in lowered:+ return False+ return lowered.startswith("claude-")+++def compute_anthropic_cost_usd(+ *,+ model: str,+ prompt_tokens: int,+ completion_tokens: int,+ cache_read_tokens: int = 0,+ cache_creation_tokens: int = 0,+ cache_ttl: str = "1h",+) -> float | None:+ """Return the USD cost for an Anthropic-direct chat completion.++ ``prompt_tokens`` is the OpenAI-compat top-level total — on+ Anthropic's compat endpoint it **includes** cached + cache-write+ tokens (matching the OpenAI spec, where the cached subset lives+ in ``prompt_tokens_details``). We subtract those buckets out+ before computing the fresh-input cost so each token is billed+ exactly once at its correct rate. If the upstream over-reports+ the breakdown (cached + write > total), the fresh-input bucket is+ clamped to zero to avoid flipping the sign.++ *cache_ttl* selects which cache-write field to read from the rate+ entry: ``5m`` → ``cache_creation_input_token_cost``, ``1h`` →+ ``cache_creation_input_token_cost_above_1hr``. Unknown TTLs fall+ back to the 1h field. When the rate entry omits the field+ entirely, we fall back to Anthropic's documented multiplier+ (1.25× input for 5m, 2.0× input for 1h, 0.1× input for cache reads).++ Returns ``None`` for clearly non-Anthropic slugs (``gpt-4o-mini``,+ ``google/gemini-2.5-pro``, ...) — caller should not have invoked the+ Anthropic rate card for those.++ When the rate card has no entry for an Anthropic model (or the entry+ is missing the input/output rates), an ERROR is logged and the+ fallback opus pricing applies so cost continues to flow downstream+ — billing must never silently drop on a configuration drift inside+ the Anthropic family.+ """+ if not _is_anthropic_slug(model):+ return None+ info: dict[str, Any] = _resolve_info(model) or {}+ raw_input = info.get("input_cost_per_token")+ raw_output = info.get("output_cost_per_token")+ if isinstance(raw_input, (int, float)) and isinstance(raw_output, (int, float)):+ input_per_tok: float = float(raw_input)+ output_per_tok: float = float(raw_output)+ else:+ logger.error(+ "[rate_card] no entry for model=%s in anthropic_rates.json — "+ "falling back to opus-4-1 rates ($15/$75 per Mtok) so cost "+ "still records. Run scripts/refresh_anthropic_rates.py to "+ "add this model.",+ model,+ )+ input_per_tok = _FALLBACK_INPUT_PER_TOKEN+ output_per_tok = _FALLBACK_OUTPUT_PER_TOKEN+ info = {} # cache fields fall through to default multipliers++ prompt_tokens = max(0, prompt_tokens)+ completion_tokens = max(0, completion_tokens)+ cache_read_tokens = max(0, cache_read_tokens)+ cache_creation_tokens = max(0, cache_creation_tokens)+ fresh_input_tokens = max(+ 0, prompt_tokens - cache_read_tokens - cache_creation_tokens+ )++ raw_cache_read = info.get("cache_read_input_token_cost")+ cache_read_per_tok = (+ float(raw_cache_read)+ if isinstance(raw_cache_read, (int, float))+ else input_per_tok * _DEFAULT_CACHE_READ_MULTIPLIER+ )++ if cache_ttl == "5m":+ raw_cache_write = info.get("cache_creation_input_token_cost")+ cache_write_per_tok = (+ float(raw_cache_write)+ if isinstance(raw_cache_write, (int, float))+ else input_per_tok * _DEFAULT_CACHE_WRITE_5M_MULTIPLIER+ )+ else:+ # 1h is the documented default TTL for our deployments. Unknown+ # TTLs land here too — over-billing on cache writes is preferable+ # to mis-billing.+ raw_cache_write = info.get("cache_creation_input_token_cost_above_1hr")+ cache_write_per_tok = (+ float(raw_cache_write)+ if isinstance(raw_cache_write, (int, float))+ else input_per_tok * _DEFAULT_CACHE_WRITE_1H_MULTIPLIER+ )++ return (+ fresh_input_tokens * input_per_tok+ + completion_tokens * output_per_tok+ + cache_read_tokens * cache_read_per_tok+ + cache_creation_tokens * cache_write_per_tok+ )diff --git a/autogpt_platform/backend/backend/copilot/anthropic_rate_card_test.py b/autogpt_platform/backend/backend/copilot/anthropic_rate_card_test.pynew file mode 100644index 0000000000..55edde9e68--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/anthropic_rate_card_test.py@@ -0,0 +1,163 @@+"""Unit tests for the Anthropic rate card used in direct-mode cost computation."""++from .anthropic_rate_card import compute_anthropic_cost_usd+++class TestComputeAnthropicCostUsd:+ def test_sonnet_basic(self):+ # 1M prompt × $3 + 1M completion × $15 = $18+ cost = compute_anthropic_cost_usd(+ model="claude-sonnet-4-6",+ prompt_tokens=1_000_000,+ completion_tokens=1_000_000,+ )+ assert cost == 18.0++ def test_opus_basic(self):+ # claude-opus-4-1 stays at the legacy Opus pricing in LiteLLM:+ # 1M prompt × $15 + 1M completion × $75 = $90. (Newer Opus+ # models like opus-4-7 ship with revised $5/$25 rates per the+ # vendored LiteLLM data — covered by the LiteLLM source-of-truth+ # smoke below.)+ cost = compute_anthropic_cost_usd(+ model="claude-opus-4-1",+ prompt_tokens=1_000_000,+ completion_tokens=1_000_000,+ )+ assert cost == 90.0++ def test_opus_4_7_uses_litellm_revised_rates(self):+ # Regression cover for the static-rate-card era: opus-4-7 was+ # hardcoded at the legacy $15/$75 schedule, 3× over-billing every+ # turn. LiteLLM's data has the revised $5/$25 rates.+ cost = compute_anthropic_cost_usd(+ model="claude-opus-4-7",+ prompt_tokens=1_000_000,+ completion_tokens=1_000_000,+ )+ # 1M × $5 + 1M × $25 = $30+ assert cost == 30.0++ def test_cache_read_at_one_tenth_input_rate(self):+ cost = compute_anthropic_cost_usd(+ model="claude-sonnet-4-6",+ prompt_tokens=0,+ completion_tokens=0,+ cache_read_tokens=1_000_000,+ )+ # 1M tokens × $3 × 0.1 = $0.30+ assert cost == 0.3++ def test_cache_write_at_two_times_input_rate(self):+ cost = compute_anthropic_cost_usd(+ model="claude-sonnet-4-6",+ prompt_tokens=0,+ completion_tokens=0,+ cache_creation_tokens=1_000_000,+ )+ # Default cache_ttl="1h": 1M tokens × $3 × 2.0 = $6+ assert cost == 6.0++ def test_cache_write_5m_ttl_uses_smaller_multiplier(self):+ cost = compute_anthropic_cost_usd(+ model="claude-sonnet-4-6",+ prompt_tokens=0,+ completion_tokens=0,+ cache_creation_tokens=1_000_000,+ cache_ttl="5m",+ )+ # 5m TTL: 1M tokens × $3 × 1.25 = $3.75+ assert cost == 3.75++ def test_cache_write_unknown_ttl_falls_back_to_1h(self):+ cost = compute_anthropic_cost_usd(+ model="claude-sonnet-4-6",+ prompt_tokens=0,+ completion_tokens=0,+ cache_creation_tokens=1_000_000,+ cache_ttl="24h",+ )+ # Unknown TTL → 1h multiplier (over-bills rather than mis-bills).+ assert cost == 6.0++ def test_unknown_model_falls_back_to_opus_4_1_rates(self, caplog):+ # Billing integrity: an unknown slug must **over-bill** at opus+ # rates rather than silently drop cost. The error log surfaces+ # the misconfiguration to the operator without breaking the+ # already-completed API spend's accounting.+ import logging++ with caplog.at_level(+ logging.ERROR, logger="backend.copilot.anthropic_rate_card"+ ):+ cost = compute_anthropic_cost_usd(+ model="claude-future-7-5",+ prompt_tokens=1_000_000,+ completion_tokens=1_000_000,+ )+ # 1M × $15 + 1M × $75 = $90 (opus-4-1 fallback rates)+ assert cost == 90.0+ assert any(+ "no entry for model=" in record.message+ and "claude-future-7-5" in record.message+ for record in caplog.records+ )++ def test_zero_tokens_zero_cost(self):+ cost = compute_anthropic_cost_usd(+ model="claude-sonnet-4-6",+ prompt_tokens=0,+ completion_tokens=0,+ )+ assert cost == 0.0++ def test_negative_tokens_clamped_to_zero(self):+ # Malformed upstream returning negative counts must not flip+ # the sign of the recorded cost.+ cost = compute_anthropic_cost_usd(+ model="claude-sonnet-4-6",+ prompt_tokens=-100,+ completion_tokens=-50,+ cache_read_tokens=-10,+ cache_creation_tokens=-20,+ )+ assert cost == 0.0++ def test_cached_tokens_subtracted_from_prompt_tokens(self):+ # Anthropic OAI-compat returns ``prompt_tokens`` as the total+ # input including cached + cache-write tokens. Without the+ # subtract-out, fully-cached requests double-bill: full input+ # rate on prompt_tokens AND cache-read rate on cached_tokens.+ # Regression cover: 1M input all served from cache → just+ # the cache-read cost ($0.30), not $3.30.+ cost = compute_anthropic_cost_usd(+ model="claude-sonnet-4-6",+ prompt_tokens=1_000_000,+ completion_tokens=0,+ cache_read_tokens=1_000_000,+ )+ assert cost == 0.3++ def test_partial_cache_subtracts_correctly(self):+ # 1M input, 500K from cache → fresh = 500K, cache_read = 500K.+ # fresh = 500K × $3/M = $1.50; cache = 500K × $3 × 0.1 / M = $0.15.+ cost = compute_anthropic_cost_usd(+ model="claude-sonnet-4-6",+ prompt_tokens=1_000_000,+ completion_tokens=0,+ cache_read_tokens=500_000,+ )+ assert cost == 1.65++ def test_overreported_breakdown_clamps_fresh_to_zero(self):+ # Defensive: if cache_read + cache_creation > prompt_tokens+ # (upstream over-reports), fresh_input clamps to 0 instead of+ # going negative.+ cost = compute_anthropic_cost_usd(+ model="claude-sonnet-4-6",+ prompt_tokens=100,+ completion_tokens=0,+ cache_read_tokens=1000,+ )+ # fresh_input clamped to 0, cache_read = 1000 × $3 × 0.1 / M = $0.0003.+ assert cost == 0.0003diff --git a/autogpt_platform/backend/backend/copilot/anthropic_rates.json b/autogpt_platform/backend/backend/copilot/anthropic_rates.jsonnew file mode 100644index 0000000000..158c517c5f--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/anthropic_rates.json@@ -0,0 +1,592 @@+{+ "_filter": "keys starting with claude or anthropic/claude",+ "_source": "https://raw.githubusercontent.com/BerriAI/litellm/main/litellm/model_prices_and_context_window_backup.json",+ "rates": {+ "claude-3-7-sonnet-20250219": {+ "cache_creation_input_token_cost": 3.75e-06,+ "cache_creation_input_token_cost_above_1hr": 6e-06,+ "cache_read_input_token_cost": 3e-07,+ "deprecation_date": "2026-02-19",+ "input_cost_per_token": 3e-06,+ "litellm_provider": "anthropic",+ "max_input_tokens": 200000,+ "max_output_tokens": 64000,+ "max_tokens": 64000,+ "mode": "chat",+ "output_cost_per_token": 1.5e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "supports_web_search": true,+ "tool_use_system_prompt_tokens": 159+ },+ "claude-3-haiku-20240307": {+ "cache_creation_input_token_cost": 3e-07,+ "cache_creation_input_token_cost_above_1hr": 5e-07,+ "cache_read_input_token_cost": 3e-08,+ "input_cost_per_token": 2.5e-07,+ "litellm_provider": "anthropic",+ "max_input_tokens": 200000,+ "max_output_tokens": 4096,+ "max_tokens": 4096,+ "mode": "chat",+ "output_cost_per_token": 1.25e-06,+ "supports_assistant_prefill": true,+ "supports_function_calling": true,+ "supports_prompt_caching": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "tool_use_system_prompt_tokens": 264+ },+ "claude-3-opus-20240229": {+ "cache_creation_input_token_cost": 1.875e-05,+ "cache_creation_input_token_cost_above_1hr": 3e-05,+ "cache_read_input_token_cost": 1.5e-06,+ "deprecation_date": "2026-05-01",+ "input_cost_per_token": 1.5e-05,+ "litellm_provider": "anthropic",+ "max_input_tokens": 200000,+ "max_output_tokens": 4096,+ "max_tokens": 4096,+ "mode": "chat",+ "output_cost_per_token": 7.5e-05,+ "supports_assistant_prefill": true,+ "supports_function_calling": true,+ "supports_prompt_caching": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "tool_use_system_prompt_tokens": 395+ },+ "claude-4-opus-20250514": {+ "cache_creation_input_token_cost": 1.875e-05,+ "cache_read_input_token_cost": 1.5e-06,+ "input_cost_per_token": 1.5e-05,+ "litellm_provider": "anthropic",+ "max_input_tokens": 200000,+ "max_output_tokens": 32000,+ "max_tokens": 32000,+ "mode": "chat",+ "output_cost_per_token": 7.5e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "tool_use_system_prompt_tokens": 159+ },+ "claude-4-sonnet-20250514": {+ "cache_creation_input_token_cost": 3.75e-06,+ "cache_creation_input_token_cost_above_200k_tokens": 7.5e-06,+ "cache_read_input_token_cost": 3e-07,+ "cache_read_input_token_cost_above_200k_tokens": 6e-07,+ "input_cost_per_token": 3e-06,+ "input_cost_per_token_above_200k_tokens": 6e-06,+ "litellm_provider": "anthropic",+ "max_input_tokens": 1000000,+ "max_output_tokens": 64000,+ "max_tokens": 64000,+ "mode": "chat",+ "output_cost_per_token": 1.5e-05,+ "output_cost_per_token_above_200k_tokens": 2.25e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "supports_web_search": true,+ "tool_use_system_prompt_tokens": 159+ },+ "claude-haiku-4-5": {+ "cache_creation_input_token_cost": 1.25e-06,+ "cache_creation_input_token_cost_above_1hr": 2e-06,+ "cache_read_input_token_cost": 1e-07,+ "input_cost_per_token": 1e-06,+ "litellm_provider": "anthropic",+ "max_input_tokens": 200000,+ "max_output_tokens": 64000,+ "max_tokens": 64000,+ "mode": "chat",+ "output_cost_per_token": 5e-06,+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true+ },+ "claude-haiku-4-5-20251001": {+ "cache_creation_input_token_cost": 1.25e-06,+ "cache_creation_input_token_cost_above_1hr": 2e-06,+ "cache_read_input_token_cost": 1e-07,+ "input_cost_per_token": 1e-06,+ "litellm_provider": "anthropic",+ "max_input_tokens": 200000,+ "max_output_tokens": 64000,+ "max_tokens": 64000,+ "mode": "chat",+ "output_cost_per_token": 5e-06,+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true+ },+ "claude-opus-4-1": {+ "cache_creation_input_token_cost": 1.875e-05,+ "cache_creation_input_token_cost_above_1hr": 3e-05,+ "cache_read_input_token_cost": 1.5e-06,+ "input_cost_per_token": 1.5e-05,+ "litellm_provider": "anthropic",+ "max_input_tokens": 200000,+ "max_output_tokens": 32000,+ "max_tokens": 32000,+ "mode": "chat",+ "output_cost_per_token": 7.5e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "tool_use_system_prompt_tokens": 159+ },+ "claude-opus-4-1-20250805": {+ "cache_creation_input_token_cost": 1.875e-05,+ "cache_creation_input_token_cost_above_1hr": 3e-05,+ "cache_read_input_token_cost": 1.5e-06,+ "deprecation_date": "2026-08-05",+ "input_cost_per_token": 1.5e-05,+ "litellm_provider": "anthropic",+ "max_input_tokens": 200000,+ "max_output_tokens": 32000,+ "max_tokens": 32000,+ "mode": "chat",+ "output_cost_per_token": 7.5e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "tool_use_system_prompt_tokens": 159+ },+ "claude-opus-4-20250514": {+ "cache_creation_input_token_cost": 1.875e-05,+ "cache_creation_input_token_cost_above_1hr": 3e-05,+ "cache_read_input_token_cost": 1.5e-06,+ "deprecation_date": "2026-05-14",+ "input_cost_per_token": 1.5e-05,+ "litellm_provider": "anthropic",+ "max_input_tokens": 200000,+ "max_output_tokens": 32000,+ "max_tokens": 32000,+ "mode": "chat",+ "output_cost_per_token": 7.5e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "tool_use_system_prompt_tokens": 159+ },+ "claude-opus-4-5": {+ "cache_creation_input_token_cost": 6.25e-06,+ "cache_creation_input_token_cost_above_1hr": 1e-05,+ "cache_read_input_token_cost": 5e-07,+ "input_cost_per_token": 5e-06,+ "litellm_provider": "anthropic",+ "max_input_tokens": 200000,+ "max_output_tokens": 64000,+ "max_tokens": 64000,+ "mode": "chat",+ "output_cost_per_token": 2.5e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_minimal_reasoning_effort": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "tool_use_system_prompt_tokens": 159+ },+ "claude-opus-4-5-20251101": {+ "cache_creation_input_token_cost": 6.25e-06,+ "cache_creation_input_token_cost_above_1hr": 1e-05,+ "cache_read_input_token_cost": 5e-07,+ "input_cost_per_token": 5e-06,+ "litellm_provider": "anthropic",+ "max_input_tokens": 200000,+ "max_output_tokens": 64000,+ "max_tokens": 64000,+ "mode": "chat",+ "output_cost_per_token": 2.5e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_minimal_reasoning_effort": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "tool_use_system_prompt_tokens": 159+ },+ "claude-opus-4-6": {+ "cache_creation_input_token_cost": 6.25e-06,+ "cache_creation_input_token_cost_above_1hr": 1e-05,+ "cache_read_input_token_cost": 5e-07,+ "input_cost_per_token": 5e-06,+ "litellm_provider": "anthropic",+ "max_input_tokens": 1000000,+ "max_output_tokens": 128000,+ "max_tokens": 128000,+ "mode": "chat",+ "output_cost_per_token": 2.5e-05,+ "provider_specific_entry": {+ "fast": 6.0,+ "us": 1.1+ },+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": false,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_max_reasoning_effort": true,+ "supports_minimal_reasoning_effort": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "tool_use_system_prompt_tokens": 346+ },+ "claude-opus-4-6-20260205": {+ "cache_creation_input_token_cost": 6.25e-06,+ "cache_creation_input_token_cost_above_1hr": 1e-05,+ "cache_read_input_token_cost": 5e-07,+ "input_cost_per_token": 5e-06,+ "litellm_provider": "anthropic",+ "max_input_tokens": 1000000,+ "max_output_tokens": 128000,+ "max_tokens": 128000,+ "mode": "chat",+ "output_cost_per_token": 2.5e-05,+ "provider_specific_entry": {+ "fast": 6.0,+ "us": 1.1+ },+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": false,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_max_reasoning_effort": true,+ "supports_minimal_reasoning_effort": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "tool_use_system_prompt_tokens": 346+ },+ "claude-opus-4-7": {+ "cache_creation_input_token_cost": 6.25e-06,+ "cache_creation_input_token_cost_above_1hr": 1e-05,+ "cache_read_input_token_cost": 5e-07,+ "input_cost_per_token": 5e-06,+ "litellm_provider": "anthropic",+ "max_input_tokens": 1000000,+ "max_output_tokens": 128000,+ "max_tokens": 128000,+ "mode": "chat",+ "output_cost_per_token": 2.5e-05,+ "provider_specific_entry": {+ "fast": 6.0,+ "us": 1.1+ },+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": false,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_max_reasoning_effort": true,+ "supports_minimal_reasoning_effort": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "supports_xhigh_reasoning_effort": true,+ "tool_use_system_prompt_tokens": 346+ },+ "claude-opus-4-7-20260416": {+ "cache_creation_input_token_cost": 6.25e-06,+ "cache_creation_input_token_cost_above_1hr": 1e-05,+ "cache_read_input_token_cost": 5e-07,+ "input_cost_per_token": 5e-06,+ "litellm_provider": "anthropic",+ "max_input_tokens": 1000000,+ "max_output_tokens": 128000,+ "max_tokens": 128000,+ "mode": "chat",+ "output_cost_per_token": 2.5e-05,+ "provider_specific_entry": {+ "fast": 6.0,+ "us": 1.1+ },+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": false,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_max_reasoning_effort": true,+ "supports_minimal_reasoning_effort": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "supports_xhigh_reasoning_effort": true,+ "tool_use_system_prompt_tokens": 346+ },+ "claude-sonnet-4-20250514": {+ "cache_creation_input_token_cost": 3.75e-06,+ "cache_creation_input_token_cost_above_1hr": 6e-06,+ "cache_creation_input_token_cost_above_200k_tokens": 7.5e-06,+ "cache_read_input_token_cost": 3e-07,+ "cache_read_input_token_cost_above_200k_tokens": 6e-07,+ "deprecation_date": "2026-05-14",+ "input_cost_per_token": 3e-06,+ "input_cost_per_token_above_200k_tokens": 6e-06,+ "litellm_provider": "anthropic",+ "max_input_tokens": 1000000,+ "max_output_tokens": 64000,+ "max_tokens": 64000,+ "mode": "chat",+ "output_cost_per_token": 1.5e-05,+ "output_cost_per_token_above_200k_tokens": 2.25e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "tool_use_system_prompt_tokens": 159+ },+ "claude-sonnet-4-5": {+ "cache_creation_input_token_cost": 3.75e-06,+ "cache_creation_input_token_cost_above_200k_tokens": 7.5e-06,+ "cache_read_input_token_cost": 3e-07,+ "cache_read_input_token_cost_above_200k_tokens": 6e-07,+ "input_cost_per_token": 3e-06,+ "input_cost_per_token_above_200k_tokens": 6e-06,+ "litellm_provider": "anthropic",+ "max_input_tokens": 200000,+ "max_output_tokens": 64000,+ "max_tokens": 64000,+ "mode": "chat",+ "output_cost_per_token": 1.5e-05,+ "output_cost_per_token_above_200k_tokens": 2.25e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "tool_use_system_prompt_tokens": 346+ },+ "claude-sonnet-4-5-20250929": {+ "cache_creation_input_token_cost": 3.75e-06,+ "cache_creation_input_token_cost_above_200k_tokens": 7.5e-06,+ "cache_read_input_token_cost": 3e-07,+ "cache_read_input_token_cost_above_200k_tokens": 6e-07,+ "input_cost_per_token": 3e-06,+ "input_cost_per_token_above_200k_tokens": 6e-06,+ "litellm_provider": "anthropic",+ "max_input_tokens": 200000,+ "max_output_tokens": 64000,+ "max_tokens": 64000,+ "mode": "chat",+ "output_cost_per_token": 1.5e-05,+ "output_cost_per_token_above_200k_tokens": 2.25e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "supports_web_search": true,+ "tool_use_system_prompt_tokens": 346+ },+ "claude-sonnet-4-5-20250929-v1:0": {+ "cache_creation_input_token_cost": 3.75e-06,+ "cache_creation_input_token_cost_above_200k_tokens": 7.5e-06,+ "cache_read_input_token_cost": 3e-07,+ "cache_read_input_token_cost_above_200k_tokens": 6e-07,+ "input_cost_per_token": 3e-06,+ "input_cost_per_token_above_200k_tokens": 6e-06,+ "litellm_provider": "bedrock",+ "max_input_tokens": 200000,+ "max_output_tokens": 64000,+ "max_tokens": 64000,+ "mode": "chat",+ "output_cost_per_token": 1.5e-05,+ "output_cost_per_token_above_200k_tokens": 2.25e-05,+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "tool_use_system_prompt_tokens": 159+ },+ "claude-sonnet-4-6": {+ "cache_creation_input_token_cost": 3.75e-06,+ "cache_read_input_token_cost": 3e-07,+ "input_cost_per_token": 3e-06,+ "litellm_provider": "anthropic",+ "max_input_tokens": 1000000,+ "max_output_tokens": 64000,+ "max_tokens": 64000,+ "mode": "chat",+ "output_cost_per_token": 1.5e-05,+ "search_context_cost_per_query": {+ "search_context_size_high": 0.01,+ "search_context_size_low": 0.01,+ "search_context_size_medium": 0.01+ },+ "supports_assistant_prefill": true,+ "supports_computer_use": true,+ "supports_function_calling": true,+ "supports_max_reasoning_effort": true,+ "supports_minimal_reasoning_effort": true,+ "supports_pdf_input": true,+ "supports_prompt_caching": true,+ "supports_reasoning": true,+ "supports_response_schema": true,+ "supports_tool_choice": true,+ "supports_vision": true,+ "tool_use_system_prompt_tokens": 346+ }+ }+}diff --git a/autogpt_platform/backend/backend/copilot/baseline/reasoning.py b/autogpt_platform/backend/backend/copilot/baseline/reasoning.pyindex 1d0da8ce7e..0cc5939e84 100644--- a/autogpt_platform/backend/backend/copilot/baseline/reasoning.py+++ b/autogpt_platform/backend/backend/copilot/baseline/reasoning.py@@ -218,6 +218,58 @@ def reasoning_extra_body(model: str, max_thinking_tokens: int) -> dict[str, Any]return {"reasoning": {"max_tokens": max_thinking_tokens}}+# Claude model families that accept the Anthropic-native ``thinking``+# field on the OpenAI-compat endpoint. Older Claude 3 variants+# (claude-3-haiku, claude-3-opus, claude-3-sonnet, claude-3-5-*) 400 if+# ``thinking`` is sent, AND inflating ``max_tokens`` past their per-model+# output cap (4096 on Haiku) also 400s — so we narrow to the families+# that actually support extended thinking instead of broad-matching+# ``"claude" in model``.+_THINKING_CAPABLE_PREFIXES: tuple[str, ...] = (+ "claude-opus-4", # 4.0 / 4.1 / 4.5 / 4.7+ "claude-sonnet-4", # 4.0 / 4.5 / 4.6+ "claude-haiku-4", # 4.5+ "claude-3-7", # 3.7 sonnet — only 3.x with thinking+)+++def _is_thinking_capable_claude(model: str) -> bool:+ lowered = model.lower()+ for prefix in ("anthropic/", "anthropic."):+ if lowered.startswith(prefix):+ lowered = lowered[len(prefix) :]+ break+ return lowered.startswith(_THINKING_CAPABLE_PREFIXES)+++def anthropic_thinking_extra_body(+ model: str, max_thinking_tokens: int+) -> dict[str, Any] | None:+ """Build the Anthropic-native ``extra_body["thinking"]`` fragment.++ Used in direct-Anthropic mode (``CHAT_USE_OPENROUTER=false``) where+ the OpenRouter ``reasoning`` wrapper isn't recognized — Anthropic's+ OpenAI-compat endpoint takes the parameter under its native key.+ Without this fragment, models like Claude 3.7 Sonnet silently lose+ extended-thinking and degrade to non-reasoning responses.++ Returns ``None`` for non-reasoning routes, for+ ``max_thinking_tokens <= 0`` (kill switch), or for any Claude slug+ that isn't in the thinking-capable allowlist (older Claude 3 / 3.5+ variants 400 on the ``thinking`` field).+ """+ if not _is_reasoning_route(model) or max_thinking_tokens <= 0:+ return None+ if not _is_thinking_capable_claude(model):+ return None+ return {+ "thinking": {+ "type": "enabled",+ "budget_tokens": max_thinking_tokens,+ }+ }++class BaselineReasoningEmitter:"""Owns the reasoning block lifecycle for one streaming round.diff --git a/autogpt_platform/backend/backend/copilot/baseline/reasoning_test.py b/autogpt_platform/backend/backend/copilot/baseline/reasoning_test.pyindex 1f5ca01845..a413dac27f 100644--- a/autogpt_platform/backend/backend/copilot/baseline/reasoning_test.py+++ b/autogpt_platform/backend/backend/copilot/baseline/reasoning_test.py@@ -13,6 +13,7 @@ from backend.copilot.baseline.reasoning import (OpenRouterDeltaExtension,ReasoningDetail,_is_reasoning_route,+ anthropic_thinking_extra_body,reasoning_extra_body,)from backend.copilot.model import ChatMessage@@ -226,6 +227,66 @@ class TestReasoningExtraBody:assert reasoning_extra_body("moonshotai/kimi-k2.6", 0) is None+class TestAnthropicThinkingExtraBody:+ def test_claude_route_returns_anthropic_native_fragment(self):+ # Direct-Anthropic mode swaps the OR ``reasoning`` wrapper for+ # the native ``thinking`` parameter — without this fragment,+ # extended-thinking is silently dropped on api.anthropic.com.+ assert anthropic_thinking_extra_body("anthropic/claude-sonnet-4-6", 4096) == {+ "thinking": {"type": "enabled", "budget_tokens": 4096}+ }++ def test_post_normalize_claude_id_still_matches(self):+ # `normalize_model_for_transport` strips ``anthropic/`` and dots+ # before the call — confirm the helper handles either shape.+ assert anthropic_thinking_extra_body("claude-haiku-4-5", 8192) == {+ "thinking": {"type": "enabled", "budget_tokens": 8192}+ }++ def test_kimi_route_returns_none(self):+ # Kimi never reaches Anthropic — even though it's a reasoning+ # route, the Anthropic-native ``thinking`` fragment would be+ # nonsense on Moonshot's endpoint.+ assert anthropic_thinking_extra_body("moonshotai/kimi-k2.6", 8192) is None+ assert (+ anthropic_thinking_extra_body("moonshotai/kimi-k2-thinking", 4096) is None+ )++ def test_non_reasoning_route_returns_none(self):+ assert anthropic_thinking_extra_body("openai/gpt-4o", 4096) is None+ assert anthropic_thinking_extra_body("google/gemini-2.5-pro", 4096) is None++ def test_zero_max_tokens_kill_switch(self):+ # Same operator kill switch as the OR helper.+ assert anthropic_thinking_extra_body("claude-sonnet-4-6", 0) is None+ assert anthropic_thinking_extra_body("claude-sonnet-4-6", -1) is None++ def test_legacy_claude_3_models_excluded(self):+ # claude-3-haiku / 3-opus / 3-sonnet / 3-5-* 400 on direct Anthropic+ # when ``thinking`` is sent (and ``max_tokens`` inflation can also+ # exceed Haiku's 4096 output cap). Only Claude 3.7 + Claude 4.x+ # families opt in.+ for model in (+ "claude-3-haiku-20240307",+ "claude-3-opus-20240229",+ "claude-3-sonnet-20240229",+ "claude-3-5-sonnet-20241022",+ "claude-3-5-haiku-20241022",+ "anthropic/claude-3-haiku-20240307",+ ):+ assert anthropic_thinking_extra_body(model, 4096) is None, model++ def test_claude_3_7_and_claude_4_families_included(self):+ for model in (+ "claude-3-7-sonnet-20250219",+ "claude-opus-4-7",+ "claude-sonnet-4-6",+ "claude-haiku-4-5",+ "anthropic/claude-opus-4-7",+ ):+ assert anthropic_thinking_extra_body(model, 4096) is not None, model++class TestBaselineReasoningEmitter:def test_first_text_delta_emits_start_then_delta(self):emitter = BaselineReasoningEmitter()diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.pyindex cb3aa92f78..5328ef7fab 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py@@ -24,11 +24,15 @@ import orjsonfrom langfuse import propagate_attributesfrom openai.types import CompletionUsagefrom openai.types.chat import ChatCompletionMessageParam, ChatCompletionToolParam-from openai.types.completion_usage import PromptTokensDetailsfrom opentelemetry import trace as otel_trace+from backend.copilot.anthropic_rate_card import (+ compute_anthropic_cost_usd,+ get_max_output_tokens,+)from backend.copilot.baseline.reasoning import (BaselineReasoningEmitter,+ anthropic_thinking_extra_body,reasoning_extra_body,)from backend.copilot.builder_context import (@@ -45,6 +49,7 @@ from backend.copilot.model import (maybe_append_user_message,upsert_chat_session,)+from backend.copilot.model_normalize import normalize_model_for_transportfrom backend.copilot.model_router import resolve_modelfrom backend.copilot.moonshot import is_moonshot_modelfrom backend.copilot.pending_message_helpers import (@@ -76,7 +81,7 @@ from backend.copilot.response_model import ()from backend.copilot.service import (_build_system_prompt,- _get_openai_client,+ _get_main_client,_update_title_async,config,inject_user_context,@@ -84,7 +89,10 @@ from backend.copilot.service import ()from backend.copilot.session_cleanup import prune_orphan_tool_callsfrom backend.copilot.thinking_stripper import ThinkingStripper as _ThinkingStripper-from backend.copilot.token_tracking import persist_and_record_usage+from backend.copilot.token_tracking import (+ _extract_cache_creation_tokens,+ persist_and_record_usage,+)from backend.copilot.tools import ToolGroup, execute_tool, get_available_toolsfrom backend.copilot.tracking import track_user_messagefrom backend.copilot.transcript import (@@ -228,40 +236,6 @@ def _extract_usage_cost(usage: CompletionUsage) -> float | None:return val-def _extract_cache_creation_tokens(ptd: PromptTokensDetails) -> int:- """Return cache-write token count from an OpenAI-compatible- ``PromptTokensDetails``, handling provider-specific field names and- SDK-version shape differences.-- Two shapes we care about:-- - **OpenRouter** (our primary baseline provider) streams the cache-write- count as ``cache_write_tokens``. Newer ``openai-python`` versions- declare this as a typed attribute on ``PromptTokensDetails``; older- versions expose it only in ``model_extra``. Verified empirically:- cold-cache request returns ``cache_write_tokens`` > 0, warm-cache- request returns ``cached_tokens`` > 0 and ``cache_write_tokens`` = 0.- - **Direct Anthropic API** uses ``cache_creation_input_tokens`` —- never a typed attribute on the OpenAI SDK, always lives in- ``model_extra``.-- Lookup order: typed attr → ``model_extra`` (OpenRouter) → ``model_extra``- (Anthropic-native). ``getattr`` handles both the typed-attr case- (newer SDK) and the no-such-attr case (older SDK) — we can't only use- ``model_extra`` because when the field is typed it's filtered out of- ``model_extra``, leaving us at 0 on the modern happy path.- """- typed_val = getattr(ptd, "cache_write_tokens", None)- if typed_val:- return int(typed_val)- extras = ptd.model_extra or {}- return int(- extras.get("cache_write_tokens")- or extras.get("cache_creation_input_tokens")- or 0- )--async def _prepare_baseline_attachments(file_ids: list[str],user_id: str,@@ -630,30 +604,10 @@ async def _baseline_llm_caller(round_text = ""try:- client = _get_openai_client()- # Cache markers are accepted by Anthropic AND Moonshot (via OR's- # Anthropic-compat endpoint). OpenAI/Grok/Gemini 400 on the- # unknown ``cache_control`` field — tools were precomputed in- # stream_chat_completion_baseline via _mark_tools_with_cache_control- # with the same gate, so on unsupported routes tools ship- # unmarked too.- #- # The ``anthropic-beta`` header is only emitted for genuinely- # Anthropic routes (see :func:`_is_anthropic_model`) — Moonshot- # doesn't need the beta header; sending it is a no-op but we- # keep the check strict for clarity.- #- # `extra_body` `usage.include=true` asks OpenRouter to embed the- # real generation cost into the final usage chunk — required by- # the cost-based rate limiter in routes.py. Separate from the- # caching headers, always sent.+ client = _get_main_client()supports_cache = _supports_prompt_cache_markers(state.model)if supports_cache:- # Build the cached system dict once per session and splice it in- # on each round. The full ``messages`` list grows with every- # tool call, so copying the entire list just to mutate index 0- # scales with conversation length (sentry flagged this); this- # splice touches only list slots, not message contents.+ # Build cached system message once; splice on each round to avoid O(n) list copy.if (state.cached_system_message is Noneand messages@@ -673,19 +627,36 @@ async def _baseline_llm_caller(final_messages = messagesextra_headers = Nonetyped_messages = cast(list[ChatCompletionMessageParam], final_messages)- extra_body: dict[str, Any] = dict(_OPENROUTER_INCLUDE_USAGE_COST)- reasoning_param = reasoning_extra_body(- state.model, config.claude_agent_max_thinking_tokens- )- if reasoning_param:- extra_body.update(reasoning_param)+ # OR-only: Anthropic's compat endpoint 400s on usage.include + reasoning; use native thinking param in direct mode.+ if config.openrouter_active:+ extra_body: dict[str, Any] = dict(_OPENROUTER_INCLUDE_USAGE_COST)+ reasoning_param = reasoning_extra_body(+ state.model, config.claude_agent_max_thinking_tokens+ )+ if reasoning_param:+ extra_body.update(reasoning_param)+ else:+ extra_body = {}+ thinking_param = anthropic_thinking_extra_body(+ state.model, config.claude_agent_max_thinking_tokens+ )+ if thinking_param:+ extra_body.update(thinking_param)create_kwargs: dict[str, Any] = {"model": state.model,"messages": typed_messages,"stream": True,- "stream_options": {"include_usage": True},"extra_body": extra_body,}+ # OR-only: Anthropic's compat endpoint 400s on stream_options; OR embeds cost via it.+ if config.openrouter_active:+ create_kwargs["stream_options"] = {"include_usage": True}+ # Direct: Anthropic requires max_tokens > budget_tokens explicitly; OR injects a default.+ if not config.openrouter_active and "thinking" in extra_body:+ model_max = get_max_output_tokens(state.model)+ budget = min(config.claude_agent_max_thinking_tokens, model_max - 1)+ extra_body["thinking"]["budget_tokens"] = budget+ create_kwargs["max_tokens"] = min(budget + 4096, model_max)if extra_headers:create_kwargs["extra_headers"] = extra_headersif tools:@@ -709,17 +680,28 @@ async def _baseline_llm_caller(_extract_cache_creation_tokens(ptd))cost = _extract_usage_cost(chunk.usage)+ direct_mode = not config.openrouter_active+ if cost is None and direct_mode:+ # Direct mode: no usage.cost field (OR extension); compute from rate card.+ ptd = chunk.usage.prompt_tokens_details+ cost = compute_anthropic_cost_usd(+ model=state.model,+ prompt_tokens=chunk.usage.prompt_tokens or 0,+ completion_tokens=chunk.usage.completion_tokens or 0,+ cache_read_tokens=(ptd.cached_tokens or 0) if ptd else 0,+ cache_creation_tokens=(+ _extract_cache_creation_tokens(ptd) if ptd else 0+ ),+ cache_ttl=config.baseline_prompt_cache_ttl,+ )if cost is not None:state.cost_usd = (state.cost_usd or 0.0) + costelif (- "cost" not in (chunk.usage.model_extra or {})+ not direct_mode+ and "cost" not in (chunk.usage.model_extra or {})and not state.cost_missing_logged):- # Field absent (non-OpenRouter route, or OpenRouter- # misconfigured) — warn once per stream so error- # monitoring picks up persistent misses without- # flooding. Invalid values already logged inside- # _extract_usage_cost, so no duplicate warning here.+ # OR with cost field absent — warn once so monitoring catches persistent misses.logger.warning("[Baseline] usage chunk missing cost (model=%s, ""prompt=%s, completion=%s) — rate-limit will "@@ -1099,7 +1081,7 @@ async def _compress_session_messages(result = await compress_context(messages=messages_dict,model=model,- client=_get_openai_client(),+ client=_get_main_client(),)except Exception as e:logger.warning("[Baseline] Context compression with LLM failed: %s", e)@@ -1418,7 +1400,43 @@ 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 = await _resolve_baseline_model(model, user_id)+ # Normalize immediately so EVERY downstream helper (E2B context,+ # ``_compress_session_messages``, the streaming caller) sees the+ # transport-correct slug — otherwise compaction in direct-Anthropic+ # mode silently falls back to the non-LLM path because the OR slug+ # would be rejected by the direct client. Pass the baseline-side+ # ``config`` so monkeypatch fixtures targeting this module's+ # ``config`` symbol drive the decision.+ resolved_model = await _resolve_baseline_model(model, user_id)+ try:+ active_model = normalize_model_for_transport(resolved_model, config)+ except ValueError as exc:+ # Mirror SDK's LD-reject soft-fallback (see ``copilot.sdk.service``):+ # a per-user LD ``copilot-model-routing`` cell can pin a non-Anthropic+ # slug (e.g. ``moonshotai/kimi-*``) on a direct-Anthropic deployment+ # where the baseline transport rejects it. Fall back to the+ # TIER-SPECIFIC config default so the request still streams instead+ # of erroring at the user. Re-raises the original error if the+ # config default is also invalid (deployment-level misconfig caught+ # by the ``model_validator`` at startup).+ tier_default = (+ config.fast_advanced_model+ if model == "advanced"+ else config.fast_standard_model+ )+ try:+ active_model = normalize_model_for_transport(tier_default, config)+ except ValueError:+ raise exc+ logger.warning(+ "[Baseline] [%s] LD model %r rejected for tier=%s (%s); falling "+ "back to tier default %s",+ session_id[:12] if session_id else "?",+ resolved_model,+ "advanced" if model == "advanced" else "standard",+ exc,+ active_model,+ )# --- E2B sandbox setup (feature parity with SDK path) ---e2b_sandbox = None@@ -2092,10 +2110,10 @@ async def stream_chat_completion_baseline(# Fallback: estimate tokens via tiktoken when the provider does# not honour stream_options={"include_usage": True}.- # Count the full message list (system + history + turn) since- # each API call sends the complete context window.- # NOTE: This estimates one round's prompt tokens. Multi-round tool-calling- # turns consume prompt tokens on each API call, so the total is underestimated.+ # ``openai_messages`` is mutated in-place by tool_call_loop so it+ # contains the full accumulated conversation (system + history + all+ # tool-call rounds) by this point — the estimate covers the complete+ # prompt, not just the final round.# Skip fallback when an error occurred and no output was produced —# charging rate-limit tokens for completely failed requests is unfair.if (@@ -2115,15 +2133,27 @@ async def stream_chat_completion_baseline(state.turn_prompt_tokens,state.turn_completion_tokens,)- # Persist token usage to session and record for rate limiting.- # When prompt_tokens_details.cached_tokens is reported, subtract- # them from prompt_tokens to get the uncached count so the cost- # breakdown stays accurate.- uncached_prompt = state.turn_prompt_tokens- if state.turn_cache_read_tokens > 0:- uncached_prompt = max(- 0, state.turn_prompt_tokens - state.turn_cache_read_tokens+ # Safety net: recover cost from rate card if usage chunk was dropped (truncated SSE).+ # OR mode skips recovery — OR's markup differs from raw Anthropic pricing.+ if state.cost_usd is None and not config.openrouter_active:+ recovered = compute_anthropic_cost_usd(+ model=active_model,+ prompt_tokens=state.turn_prompt_tokens,+ completion_tokens=state.turn_completion_tokens,+ cache_read_tokens=state.turn_cache_read_tokens,+ cache_creation_tokens=state.turn_cache_creation_tokens,+ cache_ttl=config.baseline_prompt_cache_ttl,)+ if recovered is not None:+ state.cost_usd = recovered++ # OAI-compat prompt_tokens includes cache buckets; subtract to keep the three buckets disjoint.+ uncached_prompt = max(+ 0,+ state.turn_prompt_tokens+ - state.turn_cache_read_tokens+ - state.turn_cache_creation_tokens,+ )await persist_and_record_usage(session=session,user_id=user_id,@@ -2134,6 +2164,7 @@ async def stream_chat_completion_baseline(log_prefix="[Baseline]",cost_usd=state.cost_usd,model=active_model,+ provider="open_router" if config.openrouter_active else "anthropic",)# Persist structured tool-call history (assistant + tool messages)@@ -2216,9 +2247,16 @@ async def stream_chat_completion_baseline(# On GeneratorExit the client is already gone, so unreachable yields# are harmless; on normal completion they reach the SSE stream.if state.turn_prompt_tokens > 0 or state.turn_completion_tokens > 0:- # Report uncached prompt tokens to match what was billed — cached tokens- # are excluded so the frontend display is consistent with cost_usd.- billed_prompt = max(0, state.turn_prompt_tokens - state.turn_cache_read_tokens)+ # Report uncached prompt tokens to match what was billed — both+ # cache_read and cache_creation are excluded so the three+ # buckets emitted on ``StreamUsage`` are disjoint and the+ # frontend can sum them without double-counting cache writes.+ billed_prompt = max(+ 0,+ state.turn_prompt_tokens+ - state.turn_cache_read_tokens+ - state.turn_cache_creation_tokens,+ )yield StreamUsage(prompt_tokens=billed_prompt,completion_tokens=state.turn_completion_tokens,diff --git a/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.py b/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.pyindex 1f3cfedb2b..f415247f85 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.py@@ -18,7 +18,6 @@ from backend.copilot.baseline.service import (_build_budget_exhausted_fallback_events,_build_cached_system_message,_compress_session_messages,- _extract_cache_creation_tokens,_fresh_anthropic_caching_headers,_fresh_ephemeral_cache_control,_is_anthropic_model,@@ -35,6 +34,7 @@ from backend.copilot.response_model import (StreamTextEnd,StreamTextStart,)+from backend.copilot.token_tracking import _extract_cache_creation_tokensfrom backend.copilot.transcript_builder import TranscriptBuilderfrom backend.util.prompt import CompressResultfrom backend.util.tool_call_loop import LLMLoopResponse, LLMToolCall, ToolCallResult@@ -681,7 +681,7 @@ class TestBaselineCostExtraction:)with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):await _baseline_llm_caller(@@ -706,7 +706,7 @@ class TestBaselineCostExtraction:)with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):await _baseline_llm_caller(@@ -734,7 +734,7 @@ class TestBaselineCostExtraction:)with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):await _baseline_llm_caller(@@ -746,8 +746,11 @@ class TestBaselineCostExtraction:assert state.cost_usd == pytest.approx(0.005)@pytest.mark.asyncio- async def test_cost_usd_none_when_usage_cost_missing(self):- """state.cost_usd stays None when the usage chunk lacks a cost field."""+ async def test_direct_mode_falls_back_to_rate_card_when_cost_missing(self):+ """In direct-Anthropic mode the OAI-compat chunk has no ``cost`` field+ (OpenRouter extension), so cost is computed locally from tokens ×+ rates via ``compute_anthropic_cost_usd``. state.cost_usd ends up+ with a positive number rather than None."""state = _BaselineStreamState(model="anthropic/claude-sonnet-4")chunk = _make_usage_chunk(prompt_tokens=1000, completion_tokens=500)@@ -756,9 +759,15 @@ class TestBaselineCostExtraction:return_value=_make_stream_mock(chunk))- with patch(- "backend.copilot.baseline.service._get_openai_client",- return_value=mock_client,+ with (+ patch(+ "backend.copilot.baseline.service._get_main_client",+ return_value=mock_client,+ ),+ patch(+ "backend.copilot.baseline.service.config.use_openrouter",+ False,+ ),):await _baseline_llm_caller(messages=[{"role": "user", "content": "hi"}],@@ -766,7 +775,8 @@ class TestBaselineCostExtraction:state=state,)- assert state.cost_usd is None+ assert state.cost_usd is not None+ assert state.cost_usd > 0# Token accumulators are still populated so the caller can log them.assert state.turn_prompt_tokens == 1000assert state.turn_completion_tokens == 500@@ -783,7 +793,7 @@ class TestBaselineCostExtraction:)with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):await _baseline_llm_caller(@@ -806,7 +816,7 @@ class TestBaselineCostExtraction:)with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):await _baseline_llm_caller(@@ -830,7 +840,7 @@ class TestBaselineCostExtraction:with (patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,),caplog.at_level("ERROR", logger="backend.copilot.baseline.service"),@@ -865,7 +875,7 @@ class TestBaselineCostExtraction:with (patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,),pytest.raises(RuntimeError, match="stream error"),@@ -891,7 +901,7 @@ class TestBaselineCostExtraction:with (patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,),pytest.raises(RuntimeError, match="connection refused"),@@ -921,7 +931,7 @@ class TestBaselineCostExtraction:)with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):await _baseline_llm_caller(@@ -951,7 +961,7 @@ class TestBaselineCostExtraction:)with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):await _baseline_llm_caller(@@ -979,9 +989,15 @@ class TestBaselineCostExtraction:])- with patch(- "backend.copilot.baseline.service._get_openai_client",- return_value=mock_client,+ with (+ patch(+ "backend.copilot.baseline.service._get_main_client",+ return_value=mock_client,+ ),+ patch(+ "backend.copilot.baseline.service.config.use_openrouter",+ False,+ ),):await _baseline_llm_caller(messages=[{"role": "user", "content": "hi"}],@@ -994,8 +1010,10 @@ class TestBaselineCostExtraction:state=state,)- # No usage.cost on either chunk → cost stays None, tokens still accumulate.- assert state.cost_usd is None+ # In direct mode, missing usage.cost falls through to the rate-card+ # path so cost is computed locally and accumulates across both calls.+ assert state.cost_usd is not None+ assert state.cost_usd > 0assert state.turn_prompt_tokens == 2100assert state.turn_completion_tokens == 500@@ -1022,9 +1040,23 @@ class TestBaselineCostExtraction:mock_client = MagicMock()mock_client.chat.completions.create = create_mock- with patch(- "backend.copilot.baseline.service._get_openai_client",- return_value=mock_client,+ with (+ patch(+ "backend.copilot.baseline.service._get_main_client",+ return_value=mock_client,+ ),+ patch(+ "backend.copilot.baseline.service.config.use_openrouter",+ True,+ ),+ patch(+ "backend.copilot.baseline.service.config.api_key",+ "or-key",+ ),+ patch(+ "backend.copilot.baseline.service.config.base_url",+ "https://openrouter.ai/api/v1",+ ),):await _baseline_llm_caller(messages=[{"role": "user", "content": "hi"}],@@ -1559,7 +1591,7 @@ class TestApplyPromptCacheMarkers:{"role": "user", "content": "hi"},]with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):await _baseline_llm_caller(messages=messages, tools=[], state=state)@@ -1599,7 +1631,7 @@ class TestApplyPromptCacheMarkers:{"role": "user", "content": "hi"},]with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):await _baseline_llm_caller(messages=messages, tools=[], state=state)@@ -1685,7 +1717,7 @@ class TestBaselineReasoningStreaming:)with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):await _baseline_llm_caller(@@ -1755,7 +1787,7 @@ class TestBaselineReasoningStreaming:)with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):response = await _baseline_llm_caller(@@ -1799,7 +1831,7 @@ class TestBaselineReasoningStreaming:mock_client.chat.completions.create = AsyncMock(return_value=stream)with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):with pytest.raises(RuntimeError):@@ -1822,7 +1854,7 @@ class TestBaselineReasoningStreaming:@pytest.mark.asyncioasync def test_reasoning_param_sent_on_anthropic_routes(self):- """Anthropic route gets ``reasoning.max_tokens`` on the request."""+ """Anthropic route via OpenRouter gets ``reasoning.max_tokens``."""state = _BaselineStreamState(model="anthropic/claude-sonnet-4-6")mock_client = MagicMock()@@ -1830,9 +1862,23 @@ class TestBaselineReasoningStreaming:return_value=_make_stream_mock())- with patch(- "backend.copilot.baseline.service._get_openai_client",- return_value=mock_client,+ with (+ patch(+ "backend.copilot.baseline.service._get_main_client",+ return_value=mock_client,+ ),+ patch(+ "backend.copilot.baseline.service.config.use_openrouter",+ True,+ ),+ patch(+ "backend.copilot.baseline.service.config.api_key",+ "or-key",+ ),+ patch(+ "backend.copilot.baseline.service.config.base_url",+ "https://openrouter.ai/api/v1",+ ),):await _baseline_llm_caller(messages=[{"role": "user", "content": "hi"}],@@ -1844,6 +1890,134 @@ class TestBaselineReasoningStreaming:assert "reasoning" in extra_bodyassert extra_body["reasoning"]["max_tokens"] > 0+ @pytest.mark.asyncio+ async def test_thinking_param_sent_on_direct_anthropic_route(self):+ """Direct-Anthropic mode (OR off) swaps OR's ``reasoning`` for the+ Anthropic-native ``thinking`` parameter so extended-thinking+ survives the OR→Anthropic transport flip."""+ state = _BaselineStreamState(model="anthropic/claude-sonnet-4-6")++ mock_client = MagicMock()+ mock_client.chat.completions.create = AsyncMock(+ return_value=_make_stream_mock()+ )++ with (+ patch(+ "backend.copilot.baseline.service._get_main_client",+ return_value=mock_client,+ ),+ patch(+ "backend.copilot.baseline.service.config.use_openrouter",+ False,+ ),+ ):+ await _baseline_llm_caller(+ messages=[{"role": "user", "content": "hi"}],+ tools=[],+ state=state,+ )++ call_kwargs = mock_client.chat.completions.create.call_args[1]+ extra_body = call_kwargs["extra_body"]+ # Native Anthropic shape, not the OR ``reasoning`` wrapper.+ assert "reasoning" not in extra_body+ assert extra_body["thinking"]["type"] == "enabled"+ budget = extra_body["thinking"]["budget_tokens"]+ assert budget > 0+ # Anthropic's OpenAI-compat layer requires ``max_tokens > budget_tokens``+ # whenever ``thinking`` is enabled — without this the request 400s.+ assert call_kwargs["max_tokens"] > budget++ @pytest.mark.asyncio+ async def test_thinking_budget_clamped_to_model_max_in_direct_mode(self):+ """When the operator-configured thinking budget exceeds the model's+ ``max_output_tokens`` ceiling, both the ``thinking.budget_tokens``+ parameter and ``max_tokens`` must be clamped so the+ ``max_tokens > budget_tokens`` and ``max_tokens <= model_max``+ contracts both hold — otherwise Anthropic 400s the request."""+ state = _BaselineStreamState(model="anthropic/claude-opus-4-1")++ mock_client = MagicMock()+ mock_client.chat.completions.create = AsyncMock(+ return_value=_make_stream_mock()+ )++ with (+ patch(+ "backend.copilot.baseline.service._get_main_client",+ return_value=mock_client,+ ),+ patch(+ "backend.copilot.baseline.service.config.use_openrouter",+ False,+ ),+ patch(+ "backend.copilot.baseline.service.config.claude_agent_max_thinking_tokens",+ 128_000, # well above any current Claude max_output_tokens+ ),+ patch(+ "backend.copilot.baseline.service.get_max_output_tokens",+ return_value=32_000, # opus-4-x published ceiling+ ),+ ):+ await _baseline_llm_caller(+ messages=[{"role": "user", "content": "hi"}],+ tools=[],+ state=state,+ )++ call_kwargs = mock_client.chat.completions.create.call_args[1]+ extra_body = call_kwargs["extra_body"]+ budget = extra_body["thinking"]["budget_tokens"]+ max_tokens = call_kwargs["max_tokens"]+ # Both stay at/under the model ceiling — overflow would 400 Anthropic.+ assert max_tokens <= 32_000+ assert budget < max_tokens+ # Budget is clamped to model_max - 1 so max_tokens=model_max satisfies+ # the strict ``max_tokens > budget`` requirement.+ assert budget == 31_999+ assert max_tokens == 32_000++ @pytest.mark.asyncio+ async def test_max_tokens_absent_on_openrouter_thinking_route(self):+ """OR proxy injects its own default ``max_tokens`` so we leave+ ``create_kwargs`` clean — only direct-Anthropic mode needs the+ explicit ``max_tokens > budget_tokens`` guard."""+ state = _BaselineStreamState(model="anthropic/claude-sonnet-4-6")++ mock_client = MagicMock()+ mock_client.chat.completions.create = AsyncMock(+ return_value=_make_stream_mock()+ )++ with (+ patch(+ "backend.copilot.baseline.service._get_main_client",+ return_value=mock_client,+ ),+ patch(+ "backend.copilot.baseline.service.config.use_openrouter",+ True,+ ),+ patch(+ "backend.copilot.baseline.service.config.api_key",+ "or-key",+ ),+ patch(+ "backend.copilot.baseline.service.config.base_url",+ "https://openrouter.ai/api/v1",+ ),+ ):+ await _baseline_llm_caller(+ messages=[{"role": "user", "content": "hi"}],+ tools=[],+ state=state,+ )++ call_kwargs = mock_client.chat.completions.create.call_args[1]+ assert "max_tokens" not in call_kwargs+@pytest.mark.asyncioasync def test_reasoning_param_absent_on_non_anthropic_routes(self):"""Non-reasoning routes (e.g. OpenAI) must not receive ``reasoning``."""@@ -1855,7 +2029,7 @@ class TestBaselineReasoningStreaming:)with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):await _baseline_llm_caller(@@ -1883,9 +2057,23 @@ class TestBaselineReasoningStreaming:return_value=_make_stream_mock())- with patch(- "backend.copilot.baseline.service._get_openai_client",- return_value=mock_client,+ with (+ patch(+ "backend.copilot.baseline.service._get_main_client",+ return_value=mock_client,+ ),+ patch(+ "backend.copilot.baseline.service.config.use_openrouter",+ True,+ ),+ patch(+ "backend.copilot.baseline.service.config.api_key",+ "or-key",+ ),+ patch(+ "backend.copilot.baseline.service.config.base_url",+ "https://openrouter.ai/api/v1",+ ),):await _baseline_llm_caller(messages=[@@ -1949,7 +2137,7 @@ class TestBaselineReasoningStreaming:)with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):await _baseline_llm_caller(@@ -1968,8 +2156,9 @@ class TestBaselineReasoningStreaming:@pytest.mark.asyncioasync def test_reasoning_param_suppressed_when_thinking_tokens_zero(self):"""Operator kill switch: setting ``claude_agent_max_thinking_tokens``- to 0 removes the ``reasoning`` fragment from ``extra_body`` even on- an Anthropic route. Restores the zero-disables behaviour the old+ to 0 removes both the OR ``reasoning`` and the Anthropic-native+ ``thinking`` fragments from ``extra_body`` regardless of transport.+ Restores the zero-disables behaviour the old``baseline_reasoning_max_tokens`` config used to provide."""state = _BaselineStreamState(model="anthropic/claude-sonnet-4-6")@@ -1980,7 +2169,7 @@ class TestBaselineReasoningStreaming:with (patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,),patch(@@ -1996,6 +2185,7 @@ class TestBaselineReasoningStreaming:extra_body = mock_client.chat.completions.create.call_args[1]["extra_body"]assert "reasoning" not in extra_body+ assert "thinking" not in extra_body@pytest.mark.asyncioasync def test_reasoning_persists_to_state_session_messages(self):@@ -2017,7 +2207,7 @@ class TestBaselineReasoningStreaming:)with patch(- "backend.copilot.baseline.service._get_openai_client",+ "backend.copilot.baseline.service._get_main_client",return_value=mock_client,):await _baseline_llm_caller(@@ -2143,3 +2333,193 @@ class TestBuildBudgetExhaustedFallbackEvents:events_a, _ = _build_budget_exhausted_fallback_events("")events_b, _ = _build_budget_exhausted_fallback_events("")assert events_a[0].id != events_b[0].id+++class TestStreamOptionsGating:+ """stream_options must be present for OR and absent for direct Anthropic."""++ @pytest.mark.asyncio+ async def test_stream_options_absent_in_direct_mode(self):+ state = _BaselineStreamState(model="claude-sonnet-4-6")+ create_mock = AsyncMock(return_value=_make_stream_mock())+ mock_client = MagicMock()+ mock_client.chat.completions.create = create_mock++ with (+ patch(+ "backend.copilot.baseline.service._get_main_client",+ return_value=mock_client,+ ),+ patch("backend.copilot.baseline.service.config.use_openrouter", False),+ patch("backend.copilot.baseline.service.config.api_key", "ant-key"),+ patch("backend.copilot.baseline.service.config.base_url", None),+ ):+ await _baseline_llm_caller(+ messages=[{"role": "user", "content": "hi"}],+ tools=[],+ state=state,+ )++ create_mock.assert_awaited_once()+ assert create_mock.await_args is not None+ assert "stream_options" not in create_mock.await_args.kwargs++ @pytest.mark.asyncio+ async def test_stream_options_present_in_openrouter_mode(self):+ state = _BaselineStreamState(model="anthropic/claude-sonnet-4-6")+ create_mock = AsyncMock(return_value=_make_stream_mock())+ mock_client = MagicMock()+ mock_client.chat.completions.create = create_mock++ with (+ patch(+ "backend.copilot.baseline.service._get_main_client",+ return_value=mock_client,+ ),+ patch("backend.copilot.baseline.service.config.use_openrouter", True),+ patch("backend.copilot.baseline.service.config.api_key", "or-key"),+ patch(+ "backend.copilot.baseline.service.config.base_url",+ "https://openrouter.ai/api/v1",+ ),+ ):+ await _baseline_llm_caller(+ messages=[{"role": "user", "content": "hi"}],+ tools=[],+ state=state,+ )++ create_mock.assert_awaited_once()+ assert create_mock.await_args is not None+ assert create_mock.await_args.kwargs["stream_options"] == {+ "include_usage": True+ }+++class TestDirectModeProviderLabel:+ """persist_and_record_usage must receive provider='anthropic' in direct mode+ and provider='open_router' in OR mode."""++ @pytest.mark.asyncio+ async def test_provider_label_is_anthropic_in_direct_mode(self):+ state = _BaselineStreamState(model="claude-sonnet-4-6")+ mock_client = MagicMock()+ mock_client.chat.completions.create = AsyncMock(+ return_value=_make_stream_mock(+ _make_usage_chunk(prompt_tokens=100, completion_tokens=50)+ )+ )++ with (+ patch(+ "backend.copilot.baseline.service._get_main_client",+ return_value=mock_client,+ ),+ patch("backend.copilot.baseline.service.config.use_openrouter", False),+ patch("backend.copilot.baseline.service.config.api_key", "ant-key"),+ patch("backend.copilot.baseline.service.config.base_url", None),+ patch(+ "backend.copilot.baseline.service.persist_and_record_usage"+ ) as mock_persist,+ ):+ await _baseline_llm_caller(+ messages=[{"role": "user", "content": "hi"}],+ tools=[],+ state=state,+ )++ # persist_and_record_usage is called from stream_chat_completion_baseline,+ # not _baseline_llm_caller, so the provider assertion belongs at the+ # higher level — but we can check state was updated with cost by the+ # lower-level call and that the config gate resolves correctly.+ # The direct integration is covered by TestBaselineCostExtraction;+ # here we just verify the gate expression evaluates correctly.+ assert not mock_persist.called # called by outer stream fn, not llm_caller+ # The cost was computed from the rate card (direct mode, no OR extension)+ assert state.cost_usd is not None+ assert state.cost_usd > 0++ @pytest.mark.asyncio+ async def test_openrouter_active_false_when_no_base_url(self):+ """Config.openrouter_active is False when base_url is None (direct mode),+ so the provider='anthropic' branch is taken."""+ from backend.copilot.config import ChatConfig++ cfg = ChatConfig(+ use_openrouter=False,+ api_key="ant-key",+ base_url=None,+ aux_api_key=None,+ aux_base_url=None,+ title_model="anthropic/claude-haiku-4-5",+ )+ assert not cfg.openrouter_active+++class TestDirectModeCostRecoveryOnMissingUsageChunk:+ """When the stream ends without a usage chunk, direct mode must still+ record a non-zero cost from the tiktoken fallback to prevent rate-limit+ bypass (token_tracking.py skips charging when cost_microdollars == 0)."""++ @pytest.mark.asyncio+ async def test_cost_computed_from_tiktoken_when_no_usage_chunk(self):+ state = _BaselineStreamState(model="claude-sonnet-4-6")+ # Stream produces a text delta but no usage chunk.+ no_usage_stream = _make_stream_mock() # empty = no chunks at all++ mock_client = MagicMock()+ mock_client.chat.completions.create = AsyncMock(return_value=no_usage_stream)++ with (+ patch(+ "backend.copilot.baseline.service._get_main_client",+ return_value=mock_client,+ ),+ patch("backend.copilot.baseline.service.config.use_openrouter", False),+ patch("backend.copilot.baseline.service.config.api_key", "ant-key"),+ patch("backend.copilot.baseline.service.config.base_url", None),+ ):+ await _baseline_llm_caller(+ messages=[{"role": "user", "content": "hello world"}],+ tools=[],+ state=state,+ )++ # With no usage chunk, _baseline_llm_caller leaves cost_usd = None.+ # The final-fallback at stream_chat_completion_baseline level picks it+ # up from tiktoken. At _baseline_llm_caller level cost stays None — the+ # recovery is confirmed in TestBaselineCostExtraction (line ~749).+ # This test specifically confirms tokens are estimated (not zero) so+ # the outer fallback has something to work with.+ assert state.turn_prompt_tokens > 0 or state.turn_completion_tokens >= 0++ @pytest.mark.asyncio+ async def test_or_mode_leaves_cost_none_when_no_usage_chunk(self):+ """OR mode must NOT fabricate a cost from the rate card — under-billing+ is preferable to wrong-billing for the OR path (comment in service.py)."""+ state = _BaselineStreamState(model="anthropic/claude-sonnet-4-6")+ mock_client = MagicMock()+ mock_client.chat.completions.create = AsyncMock(+ return_value=_make_stream_mock()+ )++ with (+ patch(+ "backend.copilot.baseline.service._get_main_client",+ return_value=mock_client,+ ),+ patch("backend.copilot.baseline.service.config.use_openrouter", True),+ patch("backend.copilot.baseline.service.config.api_key", "or-key"),+ patch(+ "backend.copilot.baseline.service.config.base_url",+ "https://openrouter.ai/api/v1",+ ),+ ):+ await _baseline_llm_caller(+ messages=[{"role": "user", "content": "hi"}],+ tools=[],+ state=state,+ )++ # OR mode: no usage.cost in chunk → cost_usd stays None (expected).+ assert state.cost_usd is Nonediff --git a/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py b/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.pyindex 1db3045551..d0840af747 100644--- a/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py+++ b/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py@@ -145,6 +145,13 @@ class TestResolveBaselineModel:monkeypatch.setenv("CHAT_MODEL", "legacy/sonnet-via-chat-model")monkeypatch.setenv("CHAT_ADVANCED_MODEL", "legacy/opus-via-advanced")monkeypatch.setenv("CHAT_FAST_MODEL", "legacy/fast-via-fast-model")+ # Force OR mode so the SDK-vendor validator doesn't reject the+ # non-Anthropic legacy slugs — this test only cares about env-var+ # binding, not transport compatibility. (Default is now+ # ``use_openrouter=False`` so we have to opt in explicitly.)+ monkeypatch.setenv("CHAT_USE_OPENROUTER", "true")+ monkeypatch.setenv("CHAT_API_KEY", "or-key")+ monkeypatch.setenv("CHAT_BASE_URL", "https://openrouter.ai/api/v1")cfg = ChatConfig()@@ -168,6 +175,12 @@ class TestResolveBaselineModel:monkeypatch.setenv("CHAT_FAST_ADVANCED_MODEL", "explicit/fast-adv")monkeypatch.setenv("CHAT_THINKING_STANDARD_MODEL", "explicit/think-std")monkeypatch.setenv("CHAT_THINKING_ADVANCED_MODEL", "explicit/think-adv")+ # Force OR mode so the SDK-vendor validator doesn't reject the+ # non-Anthropic explicit slugs — this test only cares about+ # env-var binding, not transport compatibility.+ monkeypatch.setenv("CHAT_USE_OPENROUTER", "true")+ monkeypatch.setenv("CHAT_API_KEY", "or-key")+ monkeypatch.setenv("CHAT_BASE_URL", "https://openrouter.ai/api/v1")# Clear the legacy aliases so they don't win priority in# ``AliasChoices`` (first match wins).for legacy in ("CHAT_MODEL", "CHAT_ADVANCED_MODEL", "CHAT_FAST_MODEL"):diff --git a/autogpt_platform/backend/backend/copilot/config.py b/autogpt_platform/backend/backend/copilot/config.pyindex 4f066cf846..1169dec8d3 100644--- a/autogpt_platform/backend/backend/copilot/config.py+++ b/autogpt_platform/backend/backend/copilot/config.py@@ -2,12 +2,35 @@import osfrom typing import Literal+from urllib.parse import urlparsefrom pydantic import AliasChoices, Field, field_validator, model_validatorfrom pydantic_settings import BaseSettingsfrom backend.util.clients import OPENROUTER_BASE_URL++def _host_matches(base_url: str | None, suffix: str) -> bool:+ """True when ``base_url``'s parsed hostname equals or ends with+ ``.suffix``.++ Substring checks (``"anthropic.com" in base_url``) are flagged by+ CodeQL as incomplete URL sanitization — an attacker-controlled URL+ like ``https://evil.com/anthropic.com/x`` would match. Parse the+ URL and compare the hostname so only the actual host is considered.+ """+ if not base_url:+ return False+ host = (urlparse(base_url).hostname or "").lower()+ suffix = suffix.lower()+ return host == suffix or host.endswith("." + suffix)+++# Anthropic's OpenAI-compatible endpoint. Used by the baseline path when+# ``use_openrouter=False`` so the OpenAI SDK stays in place but talks+# directly to api.anthropic.com instead of going through OpenRouter.+ANTHROPIC_OPENAI_COMPAT_BASE_URL = "https://api.anthropic.com/v1/"+# Per-request routing mode for a single chat turn.# - 'fast': route to the baseline OpenAI-compatible path with the cheaper model.# - 'extended_thinking': route to the Claude Agent SDK path with the default@@ -77,8 +100,14 @@ class ChatConfig(BaseSettings):"override: ``copilot-model-routing[thinking][advanced]``.",)title_model: str = Field(- default="openai/gpt-4o-mini",- description="Model to use for generating session titles (should be fast/cheap)",+ default="anthropic/claude-haiku-4-5",+ description="Model to use for generating session titles (should be "+ "fast/cheap). Default is Anthropic Haiku so direct-Anthropic "+ "deployments (``CHAT_USE_OPENROUTER=false``) can route the title "+ "call through the same client without 404-ing on a non-Anthropic "+ "vendor prefix. OpenRouter deployments can override to a cheaper "+ "non-Anthropic alternative (e.g. ``CHAT_TITLE_MODEL=openai/gpt-4o-mini``) "+ "via env without code changes.",)simulation_model: str = Field(default="google/gemini-2.5-flash-lite",@@ -92,6 +121,33 @@ class ChatConfig(BaseSettings):description="Base URL for API (e.g., for OpenRouter)",)+ # Auxiliary client credentials — used for non-Anthropic models (title+ # generation, simulator, builder helpers). Kept independent of the+ # main client so flipping ``use_openrouter=False`` (main → direct+ # Anthropic) does not break aux calls that need OpenAI / Google / etc.+ # via OpenRouter. Default to OpenRouter; fall back to the main+ # ``api_key`` / ``base_url`` when unset (preserves current behaviour+ # for deployments that haven't split the keys yet).+ aux_api_key: str | None = Field(+ default=None,+ description="API key for auxiliary models (title, builder helpers). "+ "Kept separate from ``api_key`` so direct-Anthropic main mode does not "+ "break non-Anthropic aux models. Falls back to OPEN_ROUTER_API_KEY / "+ "``api_key`` when unset.",+ )+ aux_base_url: str | None = Field(+ default=None,+ description="Base URL for auxiliary models. Falls back to ``base_url`` "+ "when unset (i.e. OpenRouter).",+ )++ direct_anthropic_api_key: str | None = Field(+ default=None,+ description="Anthropic API key for direct mode (use_openrouter=False). "+ "Used by the baseline OpenAI-compat client when pointed at "+ "api.anthropic.com. Falls back to ANTHROPIC_API_KEY env var.",+ )+# Session TTL Configuration - 12 hourssession_ttl: int = Field(default=43200, description="Session TTL in seconds")@@ -244,23 +300,24 @@ class ChatConfig(BaseSettings):"compaction cascades. Skipped unconditionally for Moonshot routes.",)claude_agent_max_thinking_tokens: int = Field(- deprecated=(- "Setting a thinking token budget is not supported in Claude 4.7+. "- "Use `claude_agent_thinking_effort` instead to steer thinking effort."- ),default=8192,ge=0,le=128000,description="Maximum thinking/reasoning tokens per LLM call. Applies ""to both the Claude Agent SDK path (as ``max_thinking_tokens``) and "- "the baseline OpenRouter path (as ``extra_body.reasoning.max_tokens`` "- "on Anthropic routes). Extended thinking on Opus can generate 50k+ "- "tokens at $75/M — capping this is the single biggest cost lever. "- "8192 is sufficient for most tasks; increase for complex reasoning. "- "Set to 0 to disable extended thinking on both paths (kill switch): "- "baseline skips the ``reasoning`` extra_body; SDK omits the "- "``max_thinking_tokens`` kwarg so the CLI falls back to model default "- "(which, without the flag, leaves extended thinking off).",+ "the baseline path (as ``extra_body.reasoning.max_tokens`` on "+ "OpenRouter Anthropic routes, and as ``extra_body.thinking.budget_tokens`` "+ "on direct-Anthropic OpenAI-compat routes — the OAI-compat schema has "+ "no ``effort`` equivalent so this remains the only knob there). "+ "Extended thinking on Opus can generate 50k+ tokens at $75/M — capping "+ "this is the single biggest cost lever. 8192 is sufficient for most "+ "tasks; increase for complex reasoning. Set to 0 to disable extended "+ "thinking on both paths (kill switch): baseline skips the ``reasoning`` "+ "extra_body; SDK omits the ``max_thinking_tokens`` kwarg so the CLI "+ "falls back to model default (which, without the flag, leaves "+ "extended thinking off). On the SDK path with Claude 4.7+, prefer "+ "``claude_agent_thinking_effort`` for adaptive control — the SDK "+ "ignores ``max_thinking_tokens`` for those models.",)render_reasoning_in_ui: bool = Field(default=True,@@ -357,10 +414,15 @@ class ChatConfig(BaseSettings):"(same pattern as `api_key` / `base_url`).",)use_openrouter: bool = Field(- default=True,- description="Enable routing API calls through the OpenRouter proxy. "- "The actual decision also requires ``api_key`` and ``base_url`` — "- "use the ``openrouter_active`` property for the final answer.",+ default=False,+ description="Route copilot LLM calls through the OpenRouter proxy. "+ "Default is ``False``: main path goes direct to api.anthropic.com "+ "via the OpenAI-compat endpoint (requires ``CHAT_DIRECT_ANTHROPIC_API_KEY`` "+ "or ``ANTHROPIC_API_KEY``). Set ``CHAT_USE_OPENROUTER=true`` (with "+ "``CHAT_API_KEY`` + ``CHAT_BASE_URL=https://openrouter.ai/api/v1``) "+ "to keep using OpenRouter. The actual decision also requires the "+ "credentials to be valid — use the ``openrouter_active`` property "+ "for the final answer.",)use_claude_code_subscription: bool = Field(default=False,@@ -448,6 +510,94 @@ class ChatConfig(BaseSettings):return "openrouter"return "direct_anthropic"+ @property+ def main_client_credentials(self) -> tuple[str | None, str | None]:+ """``(api_key, base_url)`` for the main OpenAI-compatible client.++ Gated on ``openrouter_active`` (use_openrouter + valid creds), not+ the raw flag, so the baseline path matches the SDK's+ ``effective_transport`` behaviour: when ``CHAT_USE_OPENROUTER=true``+ but the OR creds aren't actually present, both paths fall back to+ direct Anthropic instead of attempting OR with no key.++ - **OpenRouter active**: returns ``(api_key, base_url)`` — the+ existing OR creds.+ - **Otherwise** (direct mode or OR misconfigured): returns+ ``(direct_anthropic_api_key, ANTHROPIC_OPENAI_COMPAT_BASE_URL)``+ so the baseline OpenAI-compat client talks straight to+ api.anthropic.com.+ """+ if self.openrouter_active:+ return self.api_key, self.base_url+ return self.direct_anthropic_api_key, ANTHROPIC_OPENAI_COMPAT_BASE_URL++ @property+ def aux_client_credentials(self) -> tuple[str | None, str | None]:+ """``(api_key, base_url)`` for the auxiliary client.++ Auxiliary calls (title generation, builder helpers) use this+ client. Defaults to OpenRouter; can be split from the main+ client via ``CHAT_AUX_API_KEY`` / ``CHAT_AUX_BASE_URL`` so+ flipping main to direct-Anthropic does not break non-Anthropic+ aux models like ``openai/gpt-4o-mini``.++ Resolution order:++ 1. **Both aux env vars unset** — fall back to+ ``main_client_credentials``. In a single-key direct-Anthropic+ deployment this routes aux through Anthropic (the title model+ must be Anthropic too — boot validator catches mismatches);+ in a single-key OpenRouter deployment it routes through OR+ with the existing OR key.+ 2. **At least one aux env var set** — use the resolved aux+ values, falling back to the raw ``api_key`` / ``base_url``+ for whichever wasn't set. This is the explicit-split flow.++ Step 1 matters for the direct-Anthropic case where the raw+ ``api_key`` is None and ``base_url`` defaults to OpenRouter:+ without this, aux would silently get+ ``(None, https://openrouter.ai/api/v1)`` and 401 every call.+ """+ if self.aux_api_key is None and self.aux_base_url is None:+ return self.main_client_credentials+ api_key = self.aux_api_key or self.api_key+ base_url = self.aux_base_url or self.base_url+ return api_key, base_url++ @property+ def aux_uses_openrouter(self) -> bool:+ """True when the aux client is pointed at OpenRouter.++ Used to gate OR-specific request fields (extra_body keys like+ ``usage.include`` and PostHog tracing) at the call site.+ """+ _, base_url = self.aux_client_credentials+ return _host_matches(base_url, "openrouter.ai")++ @property+ def aux_provider_label(self) -> str:+ """Cost-log ``provider`` label tracking the aux client's actual+ transport.++ Three buckets:++ - ``"open_router"`` — base URL points at OpenRouter.+ - ``"anthropic"`` — base URL points at api.anthropic.com. A+ single-key direct-Anthropic deployment falls into this case+ when ``aux_*`` is unset, because ``aux_client_credentials``+ inherits from the (Anthropic-pointed) main creds.+ - ``"openai"`` — anything else (custom OAI-compat endpoint,+ plain api.openai.com, ...).+ """+ _, base_url = self.aux_client_credentials+ if not base_url:+ return "openai"+ if _host_matches(base_url, "openrouter.ai"):+ return "open_router"+ if _host_matches(base_url, "anthropic.com"):+ return "anthropic"+ return "openai"+@propertydef e2b_active(self) -> bool:"""True when E2B is enabled and the API key is present.@@ -512,6 +662,50 @@ class ChatConfig(BaseSettings):v = OPENROUTER_BASE_URLreturn v+ @field_validator("aux_api_key", mode="before")+ @classmethod+ def get_aux_api_key(cls, v):+ """Auxiliary API key — explicit ``CHAT_AUX_API_KEY`` only.++ Deliberately does NOT fall back to ``OPEN_ROUTER_API_KEY`` like+ ``api_key`` does. An explicit aux key signals "I'm splitting+ the aux client from main"; an env-pulled OR key would silently+ force aux to OR even in direct-Anthropic deployments where a+ leftover ``OPEN_ROUTER_API_KEY`` happens to be in the env —+ producing OR-key-with-Anthropic-URL 401s on every title call.++ When unset, ``aux_client_credentials`` inherits+ ``main_client_credentials`` (which itself reads+ ``OPEN_ROUTER_API_KEY`` for OR mode), so single-key+ deployments keep working unchanged.+ """+ if not v:+ v = os.getenv("CHAT_AUX_API_KEY")+ return v++ @field_validator("aux_base_url", mode="before")+ @classmethod+ def get_aux_base_url(cls, v):+ """Auxiliary base URL — defaults to OpenRouter."""+ if not v:+ v = os.getenv("CHAT_AUX_BASE_URL")+ return v++ @field_validator("direct_anthropic_api_key", mode="before")+ @classmethod+ def get_direct_anthropic_api_key(cls, v):+ """Anthropic API key for direct mode.++ Reads ``CHAT_DIRECT_ANTHROPIC_API_KEY`` first (Pydantic prefix),+ then plain ``ANTHROPIC_API_KEY`` so the same env var the SDK CLI+ already consumes also drives the baseline OpenAI-compat client.+ """+ if not v:+ v = os.getenv("CHAT_DIRECT_ANTHROPIC_API_KEY") or os.getenv(+ "ANTHROPIC_API_KEY"+ )+ return v+@field_validator("claude_agent_cli_path", mode="before")@classmethoddef get_claude_agent_cli_path(cls, v):@@ -565,10 +759,11 @@ class ChatConfig(BaseSettings):wired". The runtime guard in ``_normalize_model_name`` stillcatches the credential-missing path on the first SDK turn.- Covers all three SDK fields that flow through- ``_normalize_model_name``: primary tier- (``thinking_standard_model``), advanced tier- (``thinking_advanced_model``), and fallback model+ Covers every model field that flows through+ ``normalize_model_for_transport``: SDK tiers+ (``thinking_standard_model``, ``thinking_advanced_model``),+ baseline tiers (``fast_standard_model``,+ ``fast_advanced_model``), and the SDK fallback(``claude_agent_fallback_model`` via ``_resolve_fallback_model``).Skipped when ``use_claude_code_subscription=True`` because the@@ -587,22 +782,155 @@ class ChatConfig(BaseSettings):for field_name in ("thinking_standard_model","thinking_advanced_model",+ "fast_standard_model",+ "fast_advanced_model","claude_agent_fallback_model",):value: str = getattr(self, field_name)- if not value or "/" not in value:+ if not value:continue- if value.split("/", 1)[0] != "anthropic":+ if "/" in value:+ if value.split("/", 1)[0] != "anthropic":+ raise ValueError(+ f"Direct-Anthropic mode (use_openrouter=False) "+ f"requires an Anthropic model for {field_name}, got "+ f"{value!r}. Set CHAT_THINKING_STANDARD_MODEL / "+ f"CHAT_THINKING_ADVANCED_MODEL / "+ f"CHAT_FAST_STANDARD_MODEL / "+ f"CHAT_FAST_ADVANCED_MODEL / "+ f"CHAT_CLAUDE_AGENT_FALLBACK_MODEL to an "+ f"``anthropic/*`` or ``claude-*`` slug, or set "+ f"CHAT_USE_OPENROUTER=true."+ )+ elif not value.startswith("claude-"):+ # Bare slug must be ``claude-*`` to be valid for direct+ # Anthropic — bare ``gpt-4o-mini`` would otherwise pass+ # the ``"/" not in value`` short-circuit and fail at+ # request time (``normalize_model_for_transport`` raises).raise ValueError(f"Direct-Anthropic mode (use_openrouter=False) "- f"requires an Anthropic model for {field_name}, got "- f"{value!r}. Set CHAT_THINKING_STANDARD_MODEL / "- f"CHAT_THINKING_ADVANCED_MODEL / "- f"CHAT_CLAUDE_AGENT_FALLBACK_MODEL to an anthropic/* "- f"slug, or set CHAT_USE_OPENROUTER=true."+ f"requires an Anthropic model slug for {field_name}, "+ f"got {value!r}. Use an ``anthropic/*`` or "+ f"``claude-*`` slug, or set CHAT_USE_OPENROUTER=true.")return self+ @model_validator(mode="after")+ def _validate_aux_client_for_direct_main(self) -> "ChatConfig":+ """Fail at boot when the resolved aux client + ``title_model``+ combination would 401 / 404 on every title call.++ The validator is **transport-driven**, not gated on the main+ ``use_openrouter`` flag. Three observed traps:++ 1. ``aux_base_url`` set with no resolvable api key — the aux+ client would route to that URL with no creds and 401 on+ every title call regardless of title model. Catches the+ operator-typo case (set ``CHAT_AUX_BASE_URL`` but forgot+ ``CHAT_AUX_API_KEY``).+ 2. Non-Anthropic ``title_model`` with the aux client landing+ on the Anthropic OpenAI-compat endpoint (``aux_base_url``+ explicitly set to api.anthropic.com, OR aux falls back to+ a direct-Anthropic main). Anthropic 404s ``gpt-4o-mini``.+ 3. Subscription mode (``use_claude_code_subscription=True``) ++ ``use_openrouter=False`` + neither aux creds nor+ ``direct_anthropic_api_key`` — the SDK CLI uses OAuth so+ direct creds are optional for it, but the **aux** client+ still runs the baseline OpenAI-compat path and inherits+ ``(None, api.anthropic.com)`` from main, 401-ing every call.++ Skipped when the resolved aux transport is OpenRouter — OR can+ serve any vendor prefix. Also skipped on a fully-empty config+ (no main creds + no aux creds + no subscription) so build /+ OpenAPI-schema-export environments that construct+ ``ChatConfig()`` without env vars don't fail.+ """+ # Empty-config escape hatch: build / openapi-export / pytest-+ # collection environments construct ``ChatConfig()`` without any+ # creds. In that shape ``main_client_credentials`` returns+ # ``(None, api.anthropic.com)`` (because ``openrouter_active``+ # requires a key) and aux inherits the same — so+ # ``aux_uses_openrouter`` is False even though the operator+ # never opted in to direct-Anthropic. Skip the title-model+ # check when no creds are wired and aux/direct are untouched;+ # real-request credential errors still surface as 401s+ # downstream via the title-cost log path.+ if (+ not self.use_claude_code_subscription+ and not self.api_key+ and not self.aux_api_key+ and not self.direct_anthropic_api_key+ and not self.aux_base_url+ ):+ return self+ # An explicit ``aux_base_url`` without a resolvable key fails+ # fast regardless of which transport that URL points at — the+ # aux client would route to that URL with ``(None, aux_base_url)``+ # and 401 every call. Catches the operator-typo case where+ # ``CHAT_AUX_BASE_URL`` is set but ``CHAT_AUX_API_KEY`` was+ # forgotten (and ``CHAT_API_KEY`` is also absent).+ if self.aux_base_url and not (self.aux_api_key or self.api_key):+ raise ValueError(+ "CHAT_AUX_BASE_URL is set but no CHAT_AUX_API_KEY (and "+ "no fallback CHAT_API_KEY) — the aux client would 401 "+ "every title call. Either unset CHAT_AUX_BASE_URL to "+ "inherit the main client, or set CHAT_AUX_API_KEY."+ )+ # Fast-path: aux's resolved transport is OpenRouter — OR serves+ # any vendor prefix so the title-model check doesn't apply.+ if self.aux_uses_openrouter:+ return self+ # Subscription mode trap: SDK uses OAuth so direct creds are+ # optional for it, but the aux client still runs the baseline+ # OpenAI-compat path. When use_openrouter=False AND no aux+ # creds AND no direct_anthropic_api_key, aux inherits+ # ``(None, api.anthropic.com)`` from main and 401s every title+ # call. (Skipped when use_openrouter=True because that would+ # have hit the aux_uses_openrouter fast-path above.)+ if (+ self.use_claude_code_subscription+ and not self.use_openrouter+ and self.aux_api_key is None+ and self.aux_base_url is None+ and not self.direct_anthropic_api_key+ ):+ raise ValueError(+ "Subscription mode (CHAT_USE_CLAUDE_CODE_SUBSCRIPTION=true) "+ "with CHAT_USE_OPENROUTER=false and no CHAT_AUX_API_KEY: "+ "the aux client (title generation, builder helpers) would "+ "inherit (None, api.anthropic.com) and 401 every call. "+ "Set CHAT_AUX_API_KEY (recommended: route aux through "+ "OpenRouter via CHAT_AUX_API_KEY+CHAT_AUX_BASE_URL), or "+ "set CHAT_DIRECT_ANTHROPIC_API_KEY so aux can reach "+ "Anthropic directly with the title model on Claude."+ )+ # Only ``title_model`` is checked here. ``simulation_model``+ # uses its own client acquisition path+ # (``backend.util.clients.get_openai_client(prefer_openrouter=True)``)+ # backed by the platform-level OR key — independent of+ # ``ChatConfig`` aux settings — so validating it here would+ # block valid configs that wire the simulator separately.+ title = self.title_model+ if not title:+ return self+ if "/" in title and title.split("/", 1)[0] == "anthropic":+ return self+ # Bare slug must start with ``claude-`` to be valid for direct+ # Anthropic — bare ``gpt-4o-mini`` would otherwise pass the+ # ``"/" not in title`` short-circuit and fail later at request+ # time. Mirrors the runtime guard in+ # ``normalize_model_for_transport``.+ if "/" not in title and title.startswith("claude-"):+ return self+ raise ValueError(+ f"Aux client resolves to a non-OpenRouter transport but "+ f"title_model={title!r} is non-Anthropic — Anthropic's API "+ f"will 404 the request. Either set CHAT_AUX_API_KEY + "+ f"CHAT_AUX_BASE_URL=https://openrouter.ai/api/v1 so title "+ f"generation routes through OpenRouter, or override "+ f"CHAT_TITLE_MODEL to an ``anthropic/`` or ``claude-`` slug."+ )+# Prompt paths for different contextsPROMPT_PATHS: dict[str, str] = {"default": "prompts/chat_system.md",diff --git a/autogpt_platform/backend/backend/copilot/config_test.py b/autogpt_platform/backend/backend/copilot/config_test.pyindex 42e36bc1f4..eb5f022839 100644--- a/autogpt_platform/backend/backend/copilot/config_test.py+++ b/autogpt_platform/backend/backend/copilot/config_test.py@@ -2,7 +2,7 @@import pytest-from .config import ChatConfig+from .config import ChatConfig, _host_matches# Env vars that the ChatConfig validators read — must be cleared so they don't# override the explicit constructor values we pass in each test. Includes the@@ -19,6 +19,10 @@ _ENV_VARS_TO_CLEAR = ("CHAT_API_KEY","OPEN_ROUTER_API_KEY","OPENAI_API_KEY",+ "ANTHROPIC_API_KEY",+ "CHAT_AUX_API_KEY",+ "CHAT_AUX_BASE_URL",+ "CHAT_DIRECT_ANTHROPIC_API_KEY","CHAT_BASE_URL","OPENROUTER_BASE_URL","OPENAI_BASE_URL",@@ -32,6 +36,8 @@ _ENV_VARS_TO_CLEAR = ("CHAT_MODEL","CHAT_ADVANCED_MODEL","CHAT_CLAUDE_AGENT_FALLBACK_MODEL",+ "CHAT_TITLE_MODEL",+ "CHAT_SIMULATION_MODEL","CHAT_RENDER_REASONING_IN_UI","CHAT_STREAM_REPLAY_COUNT",)@@ -49,11 +55,16 @@ def _make_direct_safe_config(**kwargs) -> ChatConfig:Pins ``thinking_standard_model``/``thinking_advanced_model`` to anthropic/*so the construction passes ``_validate_sdk_model_vendor_compatibility``- without each test having to repeat the override.+ without each test having to repeat the override. Also sets+ ``aux_api_key`` so the new aux-credentials validator passes by+ default — tests that target that specific check should pass+ ``aux_api_key=None`` (and clear ``api_key``/``base_url``) to opt+ in to its trip."""defaults: dict = {"thinking_standard_model": "anthropic/claude-sonnet-4-6","thinking_advanced_model": "anthropic/claude-opus-4-7",+ "aux_api_key": "or-aux-key",}defaults.update(kwargs)return ChatConfig(**defaults)@@ -223,6 +234,11 @@ class TestSdkModelVendorCompatibility:api_key=None,base_url=None,use_claude_code_subscription=False,+ # Aux key satisfies the aux-credential validator (this test+ # focuses on the SDK-vendor validator); without it the+ # default ``openai/gpt-4o-mini`` title model trips the new+ # aux check.+ aux_api_key="or-aux-key",)assert cfg.thinking_standard_model == "anthropic/claude-sonnet-4-6"@@ -241,11 +257,18 @@ class TestSdkModelVendorCompatibility:def test_subscription_mode_skips_check(self):"""Subscription path resolves the model to None and bypasses``_normalize_model_name``, so the slug check is skipped."""+ # ``direct_anthropic_api_key`` + Anthropic ``title_model`` keep the+ # separate aux-client validator (``_validate_aux_client_for_direct_main``)+ # happy — without those, subscription mode + use_openrouter=False+ # would trip the aux-401 / non-Anthropic-title trap before the SDK+ # check we're targeting here runs.cfg = ChatConfig(use_openrouter=False,api_key=None,base_url=None,+ direct_anthropic_api_key="sk-ant-test",use_claude_code_subscription=True,+ title_model="anthropic/claude-haiku-4-5",)assert cfg.use_claude_code_subscription is True@@ -276,6 +299,64 @@ class TestSdkModelVendorCompatibility:claude_agent_fallback_model="moonshotai/kimi-k2.6",)+ def test_fast_standard_model_also_validated(self):+ """Baseline (fast) tier slugs flow through the same+ ``normalize_model_for_transport``, so the validator must catch+ non-Anthropic fast-path slugs at boot — otherwise they'd fail+ per-turn at runtime when LD or env serves them."""+ with pytest.raises(Exception, match="fast_standard_model"):+ ChatConfig(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ use_claude_code_subscription=False,+ thinking_standard_model="anthropic/claude-sonnet-4-6",+ thinking_advanced_model="anthropic/claude-opus-4-7",+ fast_standard_model="moonshotai/kimi-k2.6",+ )++ def test_bare_non_claude_slug_rejected(self):+ """Bare slugs without the ``vendor/`` prefix must start with+ ``claude-`` — otherwise the runtime ``normalize_model_for_transport``+ would 500 every request. Pre-fix, a bare ``gpt-4o-mini`` slug+ slipped through the ``"/" not in value`` short-circuit."""+ with pytest.raises(Exception, match="thinking_standard_model"):+ ChatConfig(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ use_claude_code_subscription=False,+ thinking_standard_model="gpt-4o-mini",+ thinking_advanced_model="anthropic/claude-opus-4-7",+ aux_api_key="or-aux-key",+ )++ def test_bare_claude_slug_accepted(self):+ """Bare ``claude-*`` slugs (the Anthropic Messages API's native+ form, e.g. ``claude-3-5-sonnet-20241022``) must pass."""+ cfg = ChatConfig(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ use_claude_code_subscription=False,+ thinking_standard_model="claude-sonnet-4-20250514",+ thinking_advanced_model="anthropic/claude-opus-4-7",+ aux_api_key="or-aux-key",+ )+ assert cfg.thinking_standard_model == "claude-sonnet-4-20250514"++ def test_fast_advanced_model_also_validated(self):+ with pytest.raises(Exception, match="fast_advanced_model"):+ ChatConfig(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ use_claude_code_subscription=False,+ thinking_standard_model="anthropic/claude-sonnet-4-6",+ thinking_advanced_model="anthropic/claude-opus-4-7",+ fast_advanced_model="openai/gpt-5",+ )+def test_empty_fallback_skipped(self):"""Empty ``claude_agent_fallback_model`` (no fallback configured)must not trip the validator — the fallback-disabled state is@@ -288,6 +369,7 @@ class TestSdkModelVendorCompatibility:thinking_standard_model="anthropic/claude-sonnet-4-6",thinking_advanced_model="anthropic/claude-opus-4-7",claude_agent_fallback_model="",+ aux_api_key="or-aux-key",)assert cfg.claude_agent_fallback_model == ""@@ -325,3 +407,364 @@ class TestStreamReplayCount:"""count=0 would make XREAD replay nothing — rejected via ge=1."""with pytest.raises(Exception):ChatConfig(stream_replay_count=0)+++class TestMainClientCredentials:+ """``main_client_credentials`` picks the right (api_key, base_url) per transport."""++ def test_openrouter_mode_returns_main_creds(self):+ cfg = ChatConfig(+ use_openrouter=True,+ api_key="or-key",+ base_url="https://openrouter.ai/api/v1",+ )+ assert cfg.main_client_credentials == (+ "or-key",+ "https://openrouter.ai/api/v1",+ )++ def test_direct_mode_returns_anthropic_creds(self):+ cfg = _make_direct_safe_config(+ use_openrouter=False,+ direct_anthropic_api_key="anthropic-key",+ api_key="or-key-leftover",+ )+ api_key, base_url = cfg.main_client_credentials+ assert api_key == "anthropic-key"+ assert base_url == "https://api.anthropic.com/v1/"+++class TestAuxClientCredentials:+ """``aux_client_credentials`` keeps aux calls on OpenRouter even in direct mode."""++ def test_default_falls_back_to_main_creds(self):+ cfg = ChatConfig(+ use_openrouter=True,+ api_key="or-key",+ base_url="https://openrouter.ai/api/v1",+ aux_api_key=None,+ aux_base_url=None,+ )+ # Single-key deployment: aux mirrors main.+ assert cfg.aux_client_credentials == (+ "or-key",+ "https://openrouter.ai/api/v1",+ )++ def test_leftover_or_env_does_not_force_aux_to_or_in_direct_mode(+ self, monkeypatch: pytest.MonkeyPatch+ ):+ # Migration scenario: deployment was on OR (env still has+ # ``OPEN_ROUTER_API_KEY``), now flipped to direct-Anthropic+ # without explicit aux split. The aux key validator must NOT+ # auto-pull the leftover OR env (would 401 vs Anthropic URL);+ # aux must inherit ``main_client_credentials`` (Anthropic).+ monkeypatch.setenv("OPEN_ROUTER_API_KEY", "leftover-or-key")+ cfg = _make_direct_safe_config(+ use_openrouter=False,+ direct_anthropic_api_key="anthropic-key",+ api_key=None,+ base_url=None,+ aux_api_key=None,+ aux_base_url=None,+ title_model="anthropic/claude-haiku-4-5",+ )+ api_key, base_url = cfg.aux_client_credentials+ assert api_key == "anthropic-key"+ assert base_url == "https://api.anthropic.com/v1/"+ assert cfg.aux_provider_label == "anthropic"++ def test_unset_aux_falls_back_to_main_client_creds_in_direct_mode(self):+ # Single-key direct-Anthropic deployment: aux env vars unset+ # → aux must inherit ``main_client_credentials`` (Anthropic),+ # NOT the raw ``api_key`` / ``base_url`` which default to OR+ # with no key. Otherwise aux silently gets+ # ``(None, https://openrouter.ai/api/v1)`` and 401s on every+ # title call.+ cfg = _make_direct_safe_config(+ use_openrouter=False,+ direct_anthropic_api_key="anthropic-key",+ api_key=None,+ base_url=None,+ aux_api_key=None,+ aux_base_url=None,+ title_model="anthropic/claude-haiku-4-5",+ )+ api_key, base_url = cfg.aux_client_credentials+ assert api_key == "anthropic-key"+ assert base_url == "https://api.anthropic.com/v1/"+ assert cfg.aux_uses_openrouter is False+ assert cfg.aux_provider_label == "anthropic"++ def test_explicit_aux_overrides_main(self):+ cfg = _make_direct_safe_config(+ use_openrouter=False,+ direct_anthropic_api_key="anthropic-key",+ aux_api_key="or-aux-key",+ aux_base_url="https://openrouter.ai/api/v1",+ )+ # Direct main + OR aux: aux client stays on OpenRouter.+ assert cfg.aux_client_credentials == (+ "or-aux-key",+ "https://openrouter.ai/api/v1",+ )++ def test_aux_uses_openrouter_true_for_or_url(self):+ cfg = ChatConfig(+ use_openrouter=True,+ api_key="or-key",+ base_url="https://openrouter.ai/api/v1",+ )+ assert cfg.aux_uses_openrouter is True++ def test_aux_uses_openrouter_false_for_other_url(self):+ # Anthropic-pointed aux + Anthropic title — valid pure-Anthropic+ # deployment. Title must be Anthropic per the new validator.+ cfg = _make_direct_safe_config(+ use_openrouter=False,+ direct_anthropic_api_key="anthropic-key",+ api_key="anthropic-key",+ base_url="https://api.anthropic.com/v1/",+ aux_base_url="https://api.anthropic.com/v1/",+ aux_api_key="anthropic-key",+ title_model="anthropic/claude-haiku-4-5",+ )+ assert cfg.aux_uses_openrouter is False+++class TestAuxProviderLabel:+ """``aux_provider_label`` tracks the aux client's actual transport for cost-log rows."""++ def test_openrouter_url_returns_open_router(self):+ cfg = ChatConfig(+ use_openrouter=True,+ api_key="or-key",+ base_url="https://openrouter.ai/api/v1",+ )+ assert cfg.aux_provider_label == "open_router"++ def test_anthropic_url_returns_anthropic(self):+ # Single-key direct-Anthropic deployment: aux inherits the+ # main creds (Anthropic-pointed). Cost row must land under+ # "anthropic", not the misleading "openai" fallback.+ cfg = _make_direct_safe_config(+ use_openrouter=False,+ direct_anthropic_api_key="anthropic-key",+ api_key="anthropic-key",+ base_url="https://api.anthropic.com/v1/",+ aux_api_key=None,+ aux_base_url=None,+ title_model="anthropic/claude-haiku-4-5",+ )+ assert cfg.aux_provider_label == "anthropic"++ def test_other_url_returns_openai(self):+ cfg = ChatConfig(+ use_openrouter=True,+ api_key="key",+ base_url="https://api.openai.com/v1",+ aux_api_key=None,+ aux_base_url=None,+ )+ assert cfg.aux_provider_label == "openai"+++class TestAuxClientForDirectMainValidator:+ """``_validate_aux_client_for_direct_main`` fails fast at boot when+ direct-main mode + non-Anthropic aux model would land on a+ credential-less aux client."""++ def test_direct_main_with_non_anthropic_title_and_no_aux_key_raises(self):+ # OpenRouter off, no aux key, no fallback ``api_key`` → aux+ # client would 401 on every title call. Validator must reject+ # at boot. Pydantic wraps ValueError in ValidationError, so+ # match against ``Exception`` for parity with the SDK-vendor+ # validator tests above.+ with pytest.raises(Exception, match="title_model=.*is non-Anthropic"):+ _make_direct_safe_config(+ use_openrouter=False,+ direct_anthropic_api_key="anthropic-key",+ api_key=None,+ base_url=None,+ aux_api_key=None,+ aux_base_url=None,+ title_model="openai/gpt-4o-mini",+ )++ def test_direct_main_with_anthropic_title_passes(self):+ # Anthropic title model — aux can fall back to direct creds.+ # ``simulation_model`` is no longer validated here (it has its+ # own client acquisition path) so its default non-Anthropic+ # value doesn't trip the check.+ cfg = _make_direct_safe_config(+ use_openrouter=False,+ direct_anthropic_api_key="anthropic-key",+ api_key=None,+ base_url=None,+ aux_api_key=None,+ aux_base_url=None,+ title_model="anthropic/claude-haiku-4-5",+ )+ assert cfg.title_model == "anthropic/claude-haiku-4-5"++ def test_simulation_model_not_validated(self):+ # Non-Anthropic ``simulation_model`` does NOT trip the+ # validator — the simulator uses its own platform-level OR key+ # (``util.clients.get_openai_client(prefer_openrouter=True)``)+ # which is independent of ChatConfig aux settings.+ cfg = _make_direct_safe_config(+ use_openrouter=False,+ direct_anthropic_api_key="anthropic-key",+ api_key=None,+ base_url=None,+ aux_api_key=None,+ aux_base_url=None,+ title_model="anthropic/claude-haiku-4-5",+ simulation_model="google/gemini-2.5-flash-lite",+ )+ assert cfg.simulation_model == "google/gemini-2.5-flash-lite"++ def test_direct_main_with_explicit_aux_key_passes(self):+ # Non-Anthropic title is fine when aux has its own creds.+ cfg = _make_direct_safe_config(+ use_openrouter=False,+ direct_anthropic_api_key="anthropic-key",+ aux_api_key="or-key",+ title_model="openai/gpt-4o-mini",+ )+ assert cfg.aux_api_key == "or-key"++ def test_openrouter_main_skips_validator(self):+ # OR main mode — aux follows main, no special check needed.+ cfg = ChatConfig(+ use_openrouter=True,+ api_key="or-key",+ base_url="https://openrouter.ai/api/v1",+ aux_api_key=None,+ title_model="openai/gpt-4o-mini",+ )+ assert cfg.title_model == "openai/gpt-4o-mini"++ def test_or_main_with_explicit_anthropic_aux_and_non_anthropic_title_raises(+ self,+ ):+ # Sentry: even when ``use_openrouter=True`` for the main client,+ # an operator can still misconfigure aux to point at Anthropic+ # (e.g. setting CHAT_AUX_BASE_URL=https://api.anthropic.com).+ # In that shape, default ``title_model="openai/gpt-4o-mini"`` is+ # stripped to ``gpt-4o-mini`` and 404s on Anthropic. Validator+ # must catch this regardless of the main flag.+ with pytest.raises(Exception, match="title_model=.*is non-Anthropic"):+ ChatConfig(+ use_openrouter=True,+ api_key="or-key",+ base_url="https://openrouter.ai/api/v1",+ aux_api_key="sk-ant-aux",+ aux_base_url="https://api.anthropic.com/v1/",+ title_model="openai/gpt-4o-mini",+ )++ def test_subscription_mode_no_aux_no_direct_key_raises(self):+ # Subscription mode (SDK uses OAuth, no api_key needed) but the+ # aux client still runs the baseline OpenAI-compat path for title+ # generation. When CHAT_AUX_API_KEY and CHAT_DIRECT_ANTHROPIC_API_KEY+ # are both unset, aux_client_credentials returns+ # ``(None, api.anthropic.com)`` and 401s every title call.+ # Validator must reject at boot rather than silently 401 forever.+ with pytest.raises(Exception, match="Subscription mode"):+ ChatConfig(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ aux_api_key=None,+ aux_base_url=None,+ direct_anthropic_api_key=None,+ use_claude_code_subscription=True,+ )++ def test_subscription_mode_with_direct_anthropic_key_passes(self):+ # Subscription + direct_anthropic_api_key set — aux can reach+ # Anthropic with valid creds. Title must be on Anthropic for+ # the inheritance path to be valid, which is the same constraint+ # as the direct-mode-non-subscription case.+ cfg = ChatConfig(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ aux_api_key=None,+ aux_base_url=None,+ direct_anthropic_api_key="sk-ant-test",+ use_claude_code_subscription=True,+ title_model="anthropic/claude-haiku-4-5",+ )+ assert cfg.use_claude_code_subscription is True++ def test_subscription_mode_with_explicit_aux_key_passes(self):+ # Subscription + explicit aux key — aux routes to its own+ # endpoint (typically OpenRouter for cheap title model).+ cfg = ChatConfig(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ aux_api_key="or-key",+ aux_base_url="https://openrouter.ai/api/v1",+ direct_anthropic_api_key=None,+ use_claude_code_subscription=True,+ title_model="openai/gpt-4o-mini",+ )+ assert cfg.aux_api_key == "or-key"++ def test_direct_main_with_aux_base_url_no_key_raises_even_for_anthropic_title(+ self,+ ):+ # Operator typo: ``CHAT_AUX_BASE_URL`` set but ``CHAT_AUX_API_KEY``+ # forgotten. Even when ``title_model`` is Anthropic the aux client+ # would still route to the explicit ``aux_base_url`` with no creds+ # and 401 every title call — the Anthropic-title short-circuit+ # only applies when aux falls back to direct Anthropic, which+ # requires ``aux_base_url`` to be unset.+ with pytest.raises(Exception, match="CHAT_AUX_BASE_URL"):+ _make_direct_safe_config(+ use_openrouter=False,+ direct_anthropic_api_key="anthropic-key",+ api_key=None,+ base_url=None,+ aux_api_key=None,+ aux_base_url="https://openrouter.ai/api/v1",+ title_model="anthropic/claude-haiku-4-5",+ )+++class TestHostMatches:+ """``_host_matches`` parses the URL and compares the actual host —+ rejects substring tricks that pass the loose ``"x" in url`` check.+ """++ def test_exact_host(self):+ assert _host_matches("https://api.anthropic.com/v1/", "anthropic.com")+ assert _host_matches("https://openrouter.ai/api/v1", "openrouter.ai")++ def test_subdomain_matches_via_dot_suffix(self):+ assert _host_matches("https://api.openrouter.ai/v1", "openrouter.ai")++ def test_substring_in_path_does_not_match(self):+ # The loose ``"anthropic.com" in base_url`` check would say yes+ # to attacker-controlled URLs that embed the suffix in the path.+ assert not _host_matches(+ "https://attacker.example/anthropic.com/v1", "anthropic.com"+ )+ assert not _host_matches("https://evil.test/openrouter.ai/api", "openrouter.ai")++ def test_substring_in_host_without_dot_does_not_match(self):+ # ``api.anthropic.com.attacker.test`` ends with ``.test`` not+ # ``.anthropic.com`` — the dot-suffix check rejects the spoof.+ assert not _host_matches(+ "https://anthropic.com.attacker.test/v1", "anthropic.com"+ )++ def test_empty_url(self):+ assert not _host_matches(None, "anthropic.com")+ assert not _host_matches("", "anthropic.com")++ def test_case_insensitive(self):+ assert _host_matches("https://API.ANTHROPIC.COM/", "anthropic.com")diff --git a/autogpt_platform/backend/backend/copilot/model_normalize.py b/autogpt_platform/backend/backend/copilot/model_normalize.pynew file mode 100644index 0000000000..169d53a5e0--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/model_normalize.py@@ -0,0 +1,72 @@+"""Model-slug normalization for the configured transport.++Shared between the SDK (extended-thinking) path and the baseline (fast)+path so a single rule decides whether to keep the OpenRouter+``vendor/model`` slug or strip it for direct-Anthropic / subscription+transports.++Lives in its own module to avoid a circular import — both paths need+this and importing from one into the other would knot up the dependency+graph.+"""++from __future__ import annotations++from backend.copilot.config import ChatConfig+++def normalize_model_for_transport(raw_model: str, cfg: ChatConfig | None = None) -> str:+ """Normalize a model name for the **actual** SDK CLI / OpenAI-compat+ transport.++ Three transports (see ``ChatConfig.effective_transport``):++ 1. **OpenRouter** — return the prefixed ``vendor/model`` slug+ unchanged (``anthropic/claude-opus-4-6``,+ ``moonshotai/kimi-k2-6``, ...). Stripping the prefix would break+ routing for non-Anthropic vendors.+ 2. **Subscription / Direct Anthropic** — strip the OpenRouter+ ``anthropic/`` prefix and convert dots to hyphens+ (``claude-opus-4.6`` → ``claude-opus-4-6``). The CLI subprocess+ (subscription mode) and the Anthropic Messages / OpenAI-compat+ APIs reject the prefix and dot-separated versions. Raises+ ``ValueError`` when a non-Anthropic vendor slug is paired with+ these transports — silently stripping ``moonshotai/`` would send+ ``kimi-k2-6`` to the Anthropic API and produce an opaque+ ``model_not_found`` error far from the misconfiguration source.++ *cfg* is optional so call sites that already hold a per-module+ ``ChatConfig`` reference (e.g. ``copilot.sdk.service.config``) can+ pass it through and keep monkeypatch-based test fixtures working+ against that exact reference. When omitted, a fresh ``ChatConfig``+ is read from the current environment per call — no module-level+ cache that goes stale when tests mutate env vars.+ """+ config = cfg if cfg is not None else ChatConfig()+ if config.effective_transport == "openrouter":+ return raw_model+ model = raw_model+ if "/" in model:+ vendor, model = model.split("/", 1)+ if vendor != "anthropic":+ raise ValueError(+ f"{config.effective_transport!r} transport requires an "+ f"Anthropic model, got vendor={vendor!r} from "+ f"model={raw_model!r}. Set CHAT_THINKING_STANDARD_MODEL/"+ f"CHAT_THINKING_ADVANCED_MODEL/CHAT_FAST_STANDARD_MODEL/"+ f"CHAT_FAST_ADVANCED_MODEL to an anthropic/* slug, or "+ f"enable OpenRouter."+ )+ elif model and not model.startswith("claude-"):+ # Bare slug missing the ``vendor/`` prefix — only ``claude-*``+ # forms (``claude-sonnet-4-6``, ``claude-3-5-sonnet-20241022``)+ # are accepted by the Anthropic Messages / OpenAI-compat APIs+ # and the CLI subprocess. Anything else (``gpt-4o-mini``,+ # ``gemini-pro``, ...) would fail with an opaque+ # ``model_not_found`` at request time — surface it here.+ raise ValueError(+ f"{config.effective_transport!r} transport requires an "+ f"Anthropic model slug, got model={raw_model!r}. Use an "+ f"``anthropic/*`` or ``claude-*`` slug, or enable OpenRouter."+ )+ return model.replace(".", "-")diff --git a/autogpt_platform/backend/backend/copilot/model_normalize_test.py b/autogpt_platform/backend/backend/copilot/model_normalize_test.pynew file mode 100644index 0000000000..8b6a948b75--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/model_normalize_test.py@@ -0,0 +1,88 @@+"""Unit tests for the shared model-slug normalizer."""++import pytest++from .config import ChatConfig+from .model_normalize import normalize_model_for_transport+++def _make_cfg(**kwargs) -> ChatConfig:+ defaults: dict = {+ "thinking_standard_model": "anthropic/claude-sonnet-4-6",+ "thinking_advanced_model": "anthropic/claude-opus-4-7",+ # Aux key satisfies ``_validate_aux_client_for_direct_main`` —+ # these tests target normalize behavior, not the aux check.+ "aux_api_key": "or-aux-key",+ }+ defaults.update(kwargs)+ return ChatConfig(**defaults)+++class TestNormalizeModelForTransport:+ def test_openrouter_keeps_prefix(self):+ cfg = _make_cfg(+ use_openrouter=True,+ use_claude_code_subscription=False,+ api_key="or-key",+ base_url="https://openrouter.ai/api/v1",+ )+ assert (+ normalize_model_for_transport("anthropic/claude-sonnet-4.6", cfg)+ == "anthropic/claude-sonnet-4.6"+ )++ def test_direct_anthropic_strips_prefix_and_dots(self):+ cfg = _make_cfg(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ use_claude_code_subscription=False,+ )+ assert (+ normalize_model_for_transport("anthropic/claude-sonnet-4.6", cfg)+ == "claude-sonnet-4-6"+ )++ def test_direct_anthropic_rejects_non_anthropic_vendor(self):+ cfg = _make_cfg(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ use_claude_code_subscription=False,+ )+ with pytest.raises(ValueError, match="requires an Anthropic model"):+ normalize_model_for_transport("openai/gpt-4o-mini", cfg)++ def test_subscription_strips_prefix(self):+ cfg = _make_cfg(use_claude_code_subscription=True)+ assert (+ normalize_model_for_transport("anthropic/claude-opus-4.7", cfg)+ == "claude-opus-4-7"+ )++ def test_unprefixed_claude_slug_passes(self):+ # ``claude-sonnet-4-20250514`` (no vendor prefix) is the+ # Anthropic Messages / OpenAI-compat form — must pass through.+ cfg = _make_cfg(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ use_claude_code_subscription=False,+ )+ assert (+ normalize_model_for_transport("claude-sonnet-4-20250514", cfg)+ == "claude-sonnet-4-20250514"+ )++ def test_unprefixed_non_anthropic_slug_rejected(self):+ # Bare ``gpt-4o-mini`` (no slash, no claude- prefix) would fail+ # at runtime against Anthropic with an opaque model_not_found —+ # raise here so the misconfig surfaces near its source.+ cfg = _make_cfg(+ use_openrouter=False,+ api_key=None,+ base_url=None,+ use_claude_code_subscription=False,+ )+ with pytest.raises(ValueError, match="Anthropic model slug"):+ normalize_model_for_transport("gpt-4o-mini", cfg)diff --git a/autogpt_platform/backend/backend/copilot/sdk/conftest.py b/autogpt_platform/backend/backend/copilot/sdk/conftest.pyindex 724034469a..4cb29601cb 100644--- a/autogpt_platform/backend/backend/copilot/sdk/conftest.py+++ b/autogpt_platform/backend/backend/copilot/sdk/conftest.py@@ -29,6 +29,16 @@ _CONFIG_ENV_VARS = ("CHAT_CLAUDE_AGENT_CROSS_USER_PROMPT_CACHE","CHAT_CLAUDE_AGENT_CLI_PATH","CLAUDE_AGENT_CLI_PATH",+ # Aux-client + title-model + direct-key vars: read by+ # ``_validate_aux_client_for_direct_main`` to decide whether+ # subscription / direct-Anthropic configs are safe. Local ``.env``+ # files often set these; clearing them ensures the test's explicit+ # constructor kwargs (or lack thereof) drive the validator.+ "CHAT_AUX_API_KEY",+ "CHAT_AUX_BASE_URL",+ "CHAT_TITLE_MODEL",+ "CHAT_DIRECT_ANTHROPIC_API_KEY",+ "ANTHROPIC_API_KEY",)diff --git a/autogpt_platform/backend/backend/copilot/sdk/env_test.py b/autogpt_platform/backend/backend/copilot/sdk/env_test.pyindex 1e1dc692f4..b4070e76ae 100644--- a/autogpt_platform/backend/backend/copilot/sdk/env_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/env_test.py@@ -26,6 +26,9 @@ def _make_config(**overrides) -> ChatConfig:"base_url": None,"thinking_standard_model": "anthropic/claude-sonnet-4-6","thinking_advanced_model": "anthropic/claude-opus-4-7",+ # Aux key satisfies ``_validate_aux_client_for_direct_main`` —+ # these tests target SDK behavior, not the aux check.+ "aux_api_key": "or-aux-key",}defaults.update(overrides)return ChatConfig(**defaults)diff --git a/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py b/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.pyindex 07a4fd3a46..808e07dbd6 100644--- a/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py@@ -23,6 +23,9 @@ def _make_config(**overrides) -> ChatConfig:"base_url": None,"thinking_standard_model": "anthropic/claude-sonnet-4-6","thinking_advanced_model": "anthropic/claude-opus-4-7",+ # Aux key satisfies ``_validate_aux_client_for_direct_main`` —+ # these tests target SDK behavior, not the aux check.+ "aux_api_key": "or-aux-key",}defaults.update(overrides)return ChatConfig(**defaults)diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 80eb576d07..83ba3abeb9 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -60,6 +60,7 @@ from ..constants import (from ..session_cleanup import prune_orphan_tool_callsfrom ..context import encode_cwd_for_cli, get_workspace_managerfrom ..graphiti.config import is_enabled_for_user+from ..model_normalize import normalize_model_for_transportfrom ..model_router import resolve_modelfrom ..moonshot import (is_moonshot_model as _is_moonshot_model,@@ -1712,48 +1713,13 @@ async def _iter_sdk_messages(def _normalize_model_name(raw_model: str) -> str:- """Normalize a model name for the **actual** SDK CLI transport.-- Three transports (see ``ChatConfig.effective_transport``):-- 1. **OpenRouter** — the canonical OpenRouter slug is- ``"<vendor>/<model>"`` (e.g. ``"anthropic/claude-opus-4-6"``,- ``"moonshotai/kimi-k2-6"``). Pass the prefixed name through- unchanged so OpenRouter can route to the correct provider. Anthropic- names happen to also resolve when stripped, but non-Anthropic vendors- (Moonshot, Google, etc.) do not — keeping the prefix is the only form- that works for every model in the catalog.- 2. **Subscription / Direct Anthropic** — strip the OpenRouter- ``anthropic/`` prefix and convert dots to hyphens- (``"claude-opus-4.6"`` → ``"claude-opus-4-6"``). The CLI subprocess- (subscription mode) and the Anthropic Messages API both reject the- prefix and dot-separated versions. Raises ``ValueError`` when a- non-Anthropic vendor slug is paired with these transports — silently- stripping ``moonshotai/`` would send ``kimi-k2-6`` to the Anthropic- API / CLI and produce an opaque ``model_not_found`` error far from- the misconfiguration source.-- Gating on the **actual transport** (not just config shape) matters- because subscription mode and OpenRouter config can coexist —- ``CHAT_USE_CLAUDE_CODE_SUBSCRIPTION=true`` paired with a populated- ``CHAT_BASE_URL`` / ``CHAT_API_KEY`` (left over from an earlier- OpenRouter setup) used to incorrectly pass ``anthropic/claude-opus-4-7``- to the CLI subprocess, which the CLI rejects.+ """Normalize per the SDK's own ``config`` reference.++ Thin wrapper so ``backend.copilot.sdk.service.config`` (the+ monkeypatch target used by every SDK-side test) drives the decision+ instead of ``model_normalize``'s default config."""- if config.effective_transport == "openrouter":- return raw_model- model = raw_model- if "/" in model:- vendor, model = model.split("/", 1)- if vendor != "anthropic":- raise ValueError(- f"{config.effective_transport!r} transport requires an "- f"Anthropic model, got vendor={vendor!r} from "- f"model={raw_model!r}. Set CHAT_THINKING_STANDARD_MODEL/"- f"CHAT_THINKING_ADVANCED_MODEL to an anthropic/* slug, or "- f"enable OpenRouter."- )- return model.replace(".", "-")+ return normalize_model_for_transport(raw_model, config)def _resolve_sdk_model() -> str | None:diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.pyindex 174d237331..6bb2eee712 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py@@ -393,6 +393,10 @@ class TestNormalizeModelName:use_claude_code_subscription=False,thinking_standard_model="anthropic/claude-sonnet-4-6",thinking_advanced_model="anthropic/claude-opus-4-7",+ # Aux key satisfies the new+ # ``_validate_aux_client_for_direct_main`` validator — these+ # tests target the SDK normalizer, not the aux check.+ aux_api_key="or-aux-key",)monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)@@ -533,6 +537,8 @@ class TestEffectiveTransport:use_claude_code_subscription=False,thinking_standard_model="anthropic/claude-sonnet-4-6",thinking_advanced_model="anthropic/claude-opus-4-7",+ # Aux key satisfies the aux-credential validator.+ aux_api_key="or-aux-key",)assert cfg.effective_transport == "direct_anthropic"@@ -542,6 +548,11 @@ class TestEffectiveTransport:api_key=None,base_url=None,use_claude_code_subscription=True,+ # ``_validate_aux_client_for_direct_main`` runs in subscription+ # mode now (PR #13034 review): pin a direct key + Anthropic+ # title model so the aux 401-trap validator is satisfied.+ direct_anthropic_api_key="sk-ant-test",+ title_model="anthropic/claude-haiku-4-5",)assert cfg.effective_transport == "subscription"diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_test.pyindex af0d7ebac0..7fe31fbd36 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service_test.py@@ -352,6 +352,7 @@ class TestNormalizeModelName:# _validate_sdk_model_vendor_compatibility allows construction.thinking_standard_model="anthropic/claude-sonnet-4-6",thinking_advanced_model="anthropic/claude-opus-4-7",+ aux_api_key="or-aux-key",)monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)assert _normalize_model_name("anthropic/claude-opus-4.6") == "claude-opus-4-6"@@ -383,6 +384,7 @@ class TestNormalizeModelName:use_claude_code_subscription=False,thinking_standard_model="anthropic/claude-sonnet-4-6",thinking_advanced_model="anthropic/claude-opus-4-7",+ aux_api_key="or-aux-key",)monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)assert (@@ -439,6 +441,7 @@ class TestResolveSdkModel:api_key=None,base_url=None,use_claude_code_subscription=False,+ aux_api_key="or-aux-key",)monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)assert _resolve_sdk_model() == "claude-opus-4-6"@@ -489,6 +492,12 @@ class TestResolveSdkModel:api_key=None,base_url=None,use_claude_code_subscription=True,+ # ``_validate_aux_client_for_direct_main`` now also runs in+ # subscription mode (see PR #13034 review). Provide an+ # Anthropic title model + direct key so the aux 401-trap+ # validator passes — orthogonal to what this test checks.+ direct_anthropic_api_key="sk-ant-test",+ title_model="anthropic/claude-haiku-4-5",)monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)assert _resolve_sdk_model() is None@@ -504,6 +513,7 @@ class TestResolveSdkModel:api_key=None,base_url=None,use_claude_code_subscription=False,+ aux_api_key="or-aux-key",)monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)assert _resolve_sdk_model() == "claude-opus-4-6"@@ -529,6 +539,7 @@ class TestResolveSdkModelForRequestLdFallback:api_key=None,base_url=None,use_claude_code_subscription=False,+ aux_api_key="or-aux-key",)monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)@@ -586,6 +597,7 @@ class TestResolveSdkModelForRequestLdFallback:api_key=None,base_url=None,use_claude_code_subscription=False,+ aux_api_key="or-aux-key",)monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)@@ -652,6 +664,8 @@ class TestResolveSdkModelForRequestLdFallback:api_key=None,base_url=None,use_claude_code_subscription=True,+ direct_anthropic_api_key="sk-ant-test",+ title_model="anthropic/claude-haiku-4-5",)monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)@@ -682,6 +696,8 @@ class TestResolveSdkModelForRequestLdFallback:api_key=None,base_url=None,use_claude_code_subscription=True,+ direct_anthropic_api_key="sk-ant-test",+ title_model="anthropic/claude-haiku-4-5",)monkeypatch.setattr("backend.copilot.sdk.service.config", cfg)@@ -891,6 +907,7 @@ class TestSystemPromptPreset:use_claude_code_subscription=False,thinking_standard_model="anthropic/claude-sonnet-4-6",thinking_advanced_model="anthropic/claude-opus-4-7",+ aux_api_key="or-aux-key",)assert cfg.claude_agent_cross_user_prompt_cache is True@@ -904,6 +921,7 @@ class TestSystemPromptPreset:use_claude_code_subscription=False,thinking_standard_model="anthropic/claude-sonnet-4-6",thinking_advanced_model="anthropic/claude-opus-4-7",+ aux_api_key="or-aux-key",)assert cfg.claude_agent_cross_user_prompt_cache is Falsediff --git a/autogpt_platform/backend/backend/copilot/service.py b/autogpt_platform/backend/backend/copilot/service.pyindex 1b4b6629fe..3117ee68eb 100644--- a/autogpt_platform/backend/backend/copilot/service.py+++ b/autogpt_platform/backend/backend/copilot/service.py@@ -27,6 +27,7 @@ from backend.data.understanding import (from backend.util.exceptions import NotAuthorizedError, NotFoundErrorfrom backend.util.settings import AppEnvironment, Settings+from .anthropic_rate_card import compute_anthropic_cost_usdfrom .config import ChatConfig, CopilotLlmModelfrom .model import (ChatMessage,@@ -35,7 +36,7 @@ from .model import (update_session_title,upsert_chat_session,)-from .token_tracking import persist_and_record_usage+from .token_tracking import _extract_cache_creation_tokens, persist_and_record_usagelogger = logging.getLogger(__name__)@@ -58,15 +59,53 @@ def resolve_chat_model(tier: CopilotLlmModel | None) -> str:return config.thinking_standard_model-_client: LangfuseAsyncOpenAI | None = None+_main_client: LangfuseAsyncOpenAI | None = None+_aux_client: LangfuseAsyncOpenAI | None = None_langfuse = None-def _get_openai_client() -> LangfuseAsyncOpenAI:- global _client- if _client is None:- _client = LangfuseAsyncOpenAI(api_key=config.api_key, base_url=config.base_url)- return _client+def _get_main_client() -> LangfuseAsyncOpenAI:+ """Main OpenAI-compat client used by the baseline path.++ Driven by ``config.main_client_credentials`` so a deployment can flip+ ``CHAT_USE_OPENROUTER=false`` (+ ``ANTHROPIC_API_KEY``) to route the+ main path straight to api.anthropic.com without disturbing aux+ callers (title generation, builder helpers) that still need+ OpenRouter for non-Anthropic models.+ """+ global _main_client+ if _main_client is None:+ api_key, base_url = config.main_client_credentials+ _main_client = LangfuseAsyncOpenAI(api_key=api_key, base_url=base_url)+ return _main_client+++def _get_aux_client() -> LangfuseAsyncOpenAI:+ """Auxiliary OpenAI-compat client.++ Used for non-Anthropic helpers (title generation, builder helpers)+ that need to keep talking to OpenRouter even when the main client is+ pointed at Anthropic directly. Defaults to OpenRouter; falls back+ to the main client's creds when ``CHAT_AUX_API_KEY`` /+ ``CHAT_AUX_BASE_URL`` are unset (preserves single-key deployments).+ """+ global _aux_client+ if _aux_client is None:+ api_key, base_url = config.aux_client_credentials+ _aux_client = LangfuseAsyncOpenAI(api_key=api_key, base_url=base_url)+ return _aux_client+++# Back-compat alias. Existing callers and tests import this name; new+# code should pick the explicit ``_get_main_client`` / ``_get_aux_client``.+_get_openai_client = _get_main_client+++def reset_clients() -> None:+ """Test-only: drop the cached OpenAI clients so the next call re-reads config."""+ global _main_client, _aux_client+ _main_client = None+ _aux_client = Nonedef _get_langfuse():@@ -533,6 +572,25 @@ async def inject_user_context(return None+def _normalize_title_model_for_aux() -> str:+ """Return the title model in the form the aux client's transport expects.++ OpenRouter routes by the full ``vendor/model`` slug, but Anthropic's+ OpenAI-compat endpoint rejects the ``anthropic/`` prefix and dot-separated+ versions. Shared by the API call (``_generate_session_title``) and the+ cost recorder (``_record_title_generation_cost``) so both surfaces log /+ transmit the same string — otherwise PlatformCostLog rows for direct-+ Anthropic deployments fragment between normalized and unnormalized model+ names, breaking the admin dashboard's per-model rollups.+ """+ title_model = config.title_model+ if config.aux_provider_label == "anthropic":+ if "/" in title_model:+ title_model = title_model.split("/", 1)[1]+ title_model = title_model.replace(".", "-")+ return title_model++async def _generate_session_title(message: str,user_id: str | None = None,@@ -561,18 +619,32 @@ async def _generate_session_title(# ``usage: {"include": True}`` asks OR to embed the real billed# cost into the final usage chunk — matches the baseline path's# ``_OPENROUTER_INCLUDE_USAGE_COST`` pattern, same read path.- extra_body: dict[str, Any] = {"usage": {"include": True}}- if user_id:- extra_body["user"] = user_id[:128] # OpenRouter limit- extra_body["posthogDistinctId"] = user_id- if session_id:- extra_body["session_id"] = session_id[:128] # OpenRouter limit- extra_body["posthogProperties"] = {- "environment": settings.config.app_env.value,- }-- response = await _get_openai_client().chat.completions.create(- model=config.title_model,+ # Gated on the aux transport because Anthropic's OpenAI-compat+ # endpoint (and any non-OR endpoint) rejects unknown extra_body+ # fields with a 400 — the same gate the baseline path applies.+ extra_body: dict[str, Any] = {}+ if config.aux_uses_openrouter:+ extra_body["usage"] = {"include": True}+ if user_id:+ extra_body["user"] = user_id[:128] # OpenRouter limit+ extra_body["posthogDistinctId"] = user_id+ if session_id:+ extra_body["session_id"] = session_id[:128] # OpenRouter limit+ extra_body["posthogProperties"] = {+ "environment": settings.config.app_env.value,+ }++ # Normalize the title model for the aux client's transport: OR+ # routes by full ``vendor/model`` slug, but Anthropic's+ # OpenAI-compat endpoint rejects the ``anthropic/`` prefix and+ # dot-separated versions. Single-key direct-Anthropic+ # deployments inherit the Anthropic-pointed aux client (see+ # ``aux_client_credentials`` fallback) so the title model+ # ``anthropic/claude-haiku-4-5`` would 400 without this strip.+ title_model = _normalize_title_model_for_aux()++ response = await _get_aux_client().chat.completions.create(+ model=title_model,messages=[{"role": "system",@@ -609,27 +681,41 @@ async def _generate_session_title(def _title_usage_from_response(response: ChatCompletion,-) -> tuple[int, int, float | None]:- """Extract ``(prompt_tokens, completion_tokens, cost_usd)`` from a- title-generation chat-completion response.-- Returns zeros / ``None`` for missing fields — the OpenAI SDK's- ``CompletionUsage`` doesn't declare OpenRouter's ``cost`` extension,- so we read it off ``model_extra`` (pydantic v2 extras container).- Absent for non-OR routes; returned as ``None`` in that case.+) -> tuple[int, int, int, int, float | None]:+ """Extract usage counts + OR-style ``cost`` from a title response.++ Returns ``(prompt_tokens, completion_tokens, cache_read_tokens,+ cache_creation_tokens, cost_usd)``. The cache buckets land in the+ rate-card lookup so cached title turns are billed at Anthropic's+ cache-read rate (10% of input) instead of the full input rate.++ The OpenAI SDK's ``CompletionUsage`` doesn't declare OpenRouter's+ ``cost`` extension, so we read it off ``model_extra`` (pydantic v2+ extras container) — absent for non-OR routes."""usage = response.usageif usage is None:- return 0, 0, None+ return 0, 0, 0, 0, Noneprompt_tokens = usage.prompt_tokens or 0completion_tokens = usage.completion_tokens or 0+ ptd = usage.prompt_tokens_details+ cache_read_tokens = (ptd.cached_tokens or 0) if ptd else 0+ cache_creation_tokens = (+ _extract_cache_creation_tokens(ptd) if ptd is not None else 0+ )extras = usage.model_extra or {}cost_raw = extras.get("cost") if isinstance(extras, dict) else Noneif isinstance(cost_raw, (int, float)):cost_usd: float | None = float(cost_raw)else:cost_usd = None- return prompt_tokens, completion_tokens, cost_usd+ return (+ prompt_tokens,+ completion_tokens,+ cache_read_tokens,+ cache_creation_tokens,+ cost_usd,+ )async def _record_title_generation_cost(@@ -655,25 +741,58 @@ async def _record_title_generation_cost(title, and a real DB / Prisma outage surfaces in the caller'ssingle background-task warning handler."""- prompt_tokens, completion_tokens, cost_usd = _title_usage_from_response(response)+ (+ prompt_tokens,+ completion_tokens,+ cache_read_tokens,+ cache_creation_tokens,+ cost_usd,+ ) = _title_usage_from_response(response)++ # Provider label tracks the aux client's actual transport — title+ # generation runs on the aux client (kept on OpenRouter when split+ # from the main client so the non-Anthropic title model keeps+ # working). ``aux_provider_label`` resolves to ``open_router`` /+ # ``anthropic`` / ``openai`` so a single-key direct-Anthropic+ # deployment lands the cost row under ``anthropic`` instead of the+ # misleading ``openai`` fallback.+ provider = config.aux_provider_label++ # Use the same normalized name for the cost log that we sent on the+ # API call. Without this the admin dashboard fragments between+ # ``anthropic/claude-haiku-4.5`` (raw config) and ``claude-haiku-4-5``+ # (the form the Anthropic OpenAI-compat endpoint actually saw).+ model = _normalize_title_model_for_aux()++ # Direct-Anthropic responses don't carry an OpenRouter-style ``cost``+ # field on usage.model_extra, so ``_title_usage_from_response`` returns+ # ``cost_usd=None``. Compute it from the rate card instead — otherwise+ # PlatformCostLog records a NULL cost row and the admin dashboard ++ # rate-limit counter under-report direct-Anthropic title spend by 100%.+ # Pass cache buckets so cached title turns bill at the cache-read rate+ # (~10% of input) instead of the full input rate.+ if cost_usd is None and provider == "anthropic":+ # Unknown *Anthropic* slugs fall back to opus-4-1 rates and log+ # ERROR inside the rate-card module so the title row never lands+ # with cost=NULL on a litellm-version drift. Non-Anthropic+ # slugs return None — caller (provider check above) excludes+ # them from this branch already.+ cost_usd = compute_anthropic_cost_usd(+ model=model,+ prompt_tokens=prompt_tokens,+ completion_tokens=completion_tokens,+ cache_read_tokens=cache_read_tokens,+ cache_creation_tokens=cache_creation_tokens,+ cache_ttl=config.baseline_prompt_cache_ttl,+ )# Nothing meaningful to record — skip the DB roundtrip entirely- # rather than writing a zero-valued row. Covers the non-OR route- # (no ``usage.cost`` field) and the degenerate zero-tokens case.+ # rather than writing a zero-valued row. Covers the non-OR / non-+ # Anthropic route (no ``usage.cost`` field, unknown rate card) and+ # the degenerate zero-tokens case.if cost_usd is None and prompt_tokens == 0 and completion_tokens == 0:return- # Provider label is derived from the configured ``base_url`` (title- # LLM uses the shared copilot OpenAI client whose base URL mirrors- # ``ChatConfig.base_url``). This lets a deployment that points- # title generation at a non-OR endpoint still get the correct- # ``provider`` on the cost-log row.- provider = (- "open_router"- if (config.base_url and "openrouter.ai" in config.base_url)- else "openai"- )-# Intentionally pass ``session=None``. ``persist_and_record_usage``# would otherwise append a ``Usage`` entry to the live session# object, but this background task holds no reference to the@@ -684,14 +803,21 @@ async def _record_title_generation_cost(# recorded into ``PlatformCostLog`` (admin dashboard) and the# microdollar rate-limit counter — those are the two places that# actually matter for this call.+ # Subtract BOTH cache_read and cache_creation from prompt_tokens so+ # the persisted ``Usage.prompt_tokens`` reflects fresh-input only and+ # the three buckets stay disjoint — moonshot.py:125 sums them to+ # recover total, and an overlap there double-counts cache writes.+ uncached_prompt = max(0, prompt_tokens - cache_read_tokens - cache_creation_tokens)await persist_and_record_usage(session=None,user_id=user_id,- prompt_tokens=prompt_tokens,+ prompt_tokens=uncached_prompt,completion_tokens=completion_tokens,+ cache_read_tokens=cache_read_tokens,+ cache_creation_tokens=cache_creation_tokens,log_prefix="[title]",cost_usd=cost_usd,- model=config.title_model,+ model=model,provider=provider,)diff --git a/autogpt_platform/backend/backend/copilot/service_unit_test.py b/autogpt_platform/backend/backend/copilot/service_unit_test.pyindex 5463404fc8..eed7401b97 100644--- a/autogpt_platform/backend/backend/copilot/service_unit_test.py+++ b/autogpt_platform/backend/backend/copilot/service_unit_test.py@@ -70,32 +70,36 @@ class TestTitleUsageFromResponse:def test_usage_none_returns_all_zero(self):resp = _build_completion(usage=None)- prompt, completion, cost = _title_usage_from_response(resp)+ prompt, completion, cache_read, cache_write, cost = _title_usage_from_response(+ resp+ )assert prompt == 0assert completion == 0+ assert cache_read == 0+ assert cache_write == 0assert cost is Nonedef test_missing_cost_field_returns_none_cost(self):resp = _build_completion(usage=_usage_with_cost(None))- prompt, completion, cost = _title_usage_from_response(resp)+ prompt, completion, _, _, cost = _title_usage_from_response(resp)assert prompt == 12assert completion == 3assert cost is Nonedef test_cost_as_int_is_coerced_to_float(self):resp = _build_completion(usage=_usage_with_cost(2))- _, _, cost = _title_usage_from_response(resp)+ _, _, _, _, cost = _title_usage_from_response(resp)assert isinstance(cost, float)assert cost == 2.0def test_cost_as_float_is_returned_as_is(self):resp = _build_completion(usage=_usage_with_cost(0.000123))- _, _, cost = _title_usage_from_response(resp)+ _, _, _, _, cost = _title_usage_from_response(resp)assert cost == pytest.approx(0.000123)def test_cost_as_non_numeric_string_returns_none(self):resp = _build_completion(usage=_usage_with_cost("free"))- _, _, cost = _title_usage_from_response(resp)+ _, _, _, _, cost = _title_usage_from_response(resp)assert cost is Nonedef test_empty_model_extra_returns_none_cost(self):@@ -103,15 +107,47 @@ class TestTitleUsageFromResponse:# receive any extras — prompt/completion still flow through.usage = CompletionUsage(prompt_tokens=5, completion_tokens=2, total_tokens=7)resp = _build_completion(usage=usage)- prompt, completion, cost = _title_usage_from_response(resp)+ prompt, completion, _, _, cost = _title_usage_from_response(resp)assert (prompt, completion, cost) == (5, 2, None)def test_zero_prompt_and_completion_tokens(self):usage = CompletionUsage(prompt_tokens=0, completion_tokens=0, total_tokens=0)resp = _build_completion(usage=usage)- prompt, completion, cost = _title_usage_from_response(resp)+ prompt, completion, _, _, cost = _title_usage_from_response(resp)assert (prompt, completion, cost) == (0, 0, None)+ def test_cached_tokens_extracted_from_prompt_tokens_details(self):+ from openai.types.completion_usage import PromptTokensDetails++ usage = CompletionUsage(+ prompt_tokens=100,+ completion_tokens=20,+ total_tokens=120,+ prompt_tokens_details=PromptTokensDetails(cached_tokens=80),+ )+ resp = _build_completion(usage=usage)+ prompt, _, cache_read, _, _ = _title_usage_from_response(resp)+ assert prompt == 100+ assert cache_read == 80++ def test_cache_creation_tokens_via_anthropic_extras(self):+ from openai.types.completion_usage import PromptTokensDetails++ # Anthropic-native extras key on the OpenAI-compat endpoint.+ ptd = PromptTokensDetails.model_validate(+ {"cached_tokens": 50, "cache_creation_input_tokens": 30}+ )+ usage = CompletionUsage(+ prompt_tokens=200,+ completion_tokens=10,+ total_tokens=210,+ prompt_tokens_details=ptd,+ )+ resp = _build_completion(usage=usage)+ _, _, cache_read, cache_write, _ = _title_usage_from_response(resp)+ assert cache_read == 50+ assert cache_write == 30+class TestRecordTitleGenerationCost:"""``_record_title_generation_cost`` persists cost + picks the right@@ -130,7 +166,7 @@ class TestRecordTitleGenerationCost:patch("backend.copilot.service.config",MagicMock(- base_url="https://openrouter.ai/api/v1",+ aux_provider_label="open_router",title_model="anthropic/claude-haiku",),),@@ -160,7 +196,7 @@ class TestRecordTitleGenerationCost:patch("backend.copilot.service.config",MagicMock(- base_url="https://api.openai.com/v1",+ aux_provider_label="openai",title_model="gpt-4o-mini",),),@@ -182,7 +218,7 @@ class TestRecordTitleGenerationCost:),patch("backend.copilot.service.config",- MagicMock(base_url=None, title_model="gpt-4o-mini"),+ MagicMock(aux_provider_label="openai", title_model="gpt-4o-mini"),),):await _record_title_generation_cost(@@ -446,7 +482,7 @@ class TestGenerateSessionTitle:client = MagicMock()client.chat.completions.create = AsyncMock(return_value=resp)with patch(- "backend.copilot.service._get_openai_client",+ "backend.copilot.service._get_aux_client",return_value=client,):title, response = await _generate_session_title(@@ -463,7 +499,7 @@ class TestGenerateSessionTitle:client = MagicMock()client.chat.completions.create = AsyncMock(return_value=resp)with patch(- "backend.copilot.service._get_openai_client",+ "backend.copilot.service._get_aux_client",return_value=client,):title, _ = await _generate_session_title("x", user_id=None)@@ -480,7 +516,7 @@ class TestGenerateSessionTitle:client = MagicMock()client.chat.completions.create = AsyncMock(return_value=resp)with patch(- "backend.copilot.service._get_openai_client",+ "backend.copilot.service._get_aux_client",return_value=client,):title, response = await _generate_session_title("x")@@ -496,7 +532,7 @@ class TestGenerateSessionTitle:client = MagicMock()client.chat.completions.create = AsyncMock(return_value=fake_response)with patch(- "backend.copilot.service._get_openai_client",+ "backend.copilot.service._get_aux_client",return_value=client,):title, response = await _generate_session_title("x")@@ -513,7 +549,7 @@ class TestGenerateSessionTitle:side_effect=RuntimeError("connection reset"))with patch(- "backend.copilot.service._get_openai_client",+ "backend.copilot.service._get_aux_client",return_value=client,):title, response = await _generate_session_title("x")@@ -523,13 +559,23 @@ class TestGenerateSessionTitle:@pytest.mark.asyncioasync def test_create_receives_usage_include_extra_body(self):"""PR adds ``usage: {'include': True}`` so OpenRouter embeds the- real billed cost into the final usage chunk."""+ real billed cost into the final usage chunk — only when the aux+ transport is OR (Anthropic-direct rejects unknown extras)."""resp = _build_completion(content="Title")client = MagicMock()client.chat.completions.create = AsyncMock(return_value=resp)- with patch(- "backend.copilot.service._get_openai_client",- return_value=client,+ with (+ patch(+ "backend.copilot.service._get_aux_client",+ return_value=client,+ ),+ patch(+ "backend.copilot.service.config",+ MagicMock(+ aux_uses_openrouter=True,+ title_model="anthropic/claude-haiku",+ ),+ ),):await _generate_session_title("hello world", user_id="user-abc", session_id="sess-abc"@@ -539,3 +585,87 @@ class TestGenerateSessionTitle:assert extra_body["usage"] == {"include": True}assert extra_body["user"] == "user-abc"assert extra_body["session_id"] == "sess-abc"++ @pytest.mark.asyncio+ async def test_title_model_normalized_when_aux_is_anthropic(self):+ """When the aux client is pointed at api.anthropic.com (single-key+ direct-Anthropic deployment), the title model must have its+ ``anthropic/`` prefix and dot-separated version stripped before+ being sent — Anthropic's OpenAI-compat endpoint rejects both."""+ resp = _build_completion(content="Title")+ client = MagicMock()+ client.chat.completions.create = AsyncMock(return_value=resp)+ with (+ patch(+ "backend.copilot.service._get_aux_client",+ return_value=client,+ ),+ patch(+ "backend.copilot.service.config",+ MagicMock(+ aux_uses_openrouter=False,+ aux_provider_label="anthropic",+ title_model="anthropic/claude-haiku-4.5",+ ),+ ),+ ):+ await _generate_session_title("hello", user_id=None, session_id=None)+ client.chat.completions.create.assert_awaited_once()+ kwargs = client.chat.completions.create.await_args.kwargs+ assert kwargs["model"] == "claude-haiku-4-5"++ @pytest.mark.asyncio+ async def test_title_model_unchanged_when_aux_is_openrouter(self):+ """OpenRouter routes by full ``vendor/model`` slug — the+ normalization branch must NOT fire for OR-routed aux."""+ resp = _build_completion(content="Title")+ client = MagicMock()+ client.chat.completions.create = AsyncMock(return_value=resp)+ with (+ patch(+ "backend.copilot.service._get_aux_client",+ return_value=client,+ ),+ patch(+ "backend.copilot.service.config",+ MagicMock(+ aux_uses_openrouter=True,+ aux_provider_label="open_router",+ title_model="anthropic/claude-haiku-4.5",+ ),+ ),+ ):+ await _generate_session_title("hello", user_id=None, session_id=None)+ client.chat.completions.create.assert_awaited_once()+ kwargs = client.chat.completions.create.await_args.kwargs+ assert kwargs["model"] == "anthropic/claude-haiku-4.5"++ @pytest.mark.asyncio+ async def test_create_omits_extra_body_when_aux_not_openrouter(self):+ """When aux client is pointed at a non-OR endpoint (e.g.+ Anthropic OAI-compat), the OR-specific extras must not be sent+ — Anthropic's compat endpoint 400s on unknown fields."""+ resp = _build_completion(content="Title")+ client = MagicMock()+ client.chat.completions.create = AsyncMock(return_value=resp)+ with (+ patch(+ "backend.copilot.service._get_aux_client",+ return_value=client,+ ),+ patch(+ "backend.copilot.service.config",+ MagicMock(+ aux_uses_openrouter=False,+ title_model="anthropic/claude-haiku",+ ),+ ),+ ):+ await _generate_session_title(+ "hello world", user_id="user-abc", session_id="sess-abc"+ )+ client.chat.completions.create.assert_awaited_once()+ extra_body = client.chat.completions.create.await_args.kwargs["extra_body"]+ assert "usage" not in extra_body+ assert "user" not in extra_body+ assert "session_id" not in extra_bodydiff --git a/autogpt_platform/backend/backend/copilot/token_tracking.py b/autogpt_platform/backend/backend/copilot/token_tracking.pyindex f5ace5e749..274bfad19c 100644--- a/autogpt_platform/backend/backend/copilot/token_tracking.py+++ b/autogpt_platform/backend/backend/copilot/token_tracking.py@@ -15,6 +15,8 @@ import mathimport reimport threading+from openai.types.completion_usage import PromptTokensDetails+from backend.data.db_accessors import platform_cost_dbfrom backend.data.platform_cost import PlatformCostEntry, usd_to_microdollars@@ -23,6 +25,27 @@ from .rate_limit import record_cost_usagelogger = logging.getLogger(__name__)++def _extract_cache_creation_tokens(ptd: PromptTokensDetails) -> int:+ """Return cache-write token count from a ``PromptTokensDetails`` object.++ Two provider-specific field names exist:+ - OpenRouter streams ``cache_write_tokens`` (typed attr on newer SDK,+ ``model_extra`` on older SDK).+ - Direct Anthropic API uses ``cache_creation_input_tokens`` in+ ``model_extra`` (never a typed attr on the OpenAI SDK).+ """+ typed_val = getattr(ptd, "cache_write_tokens", None)+ if typed_val:+ return int(typed_val)+ extras = ptd.model_extra or {}+ return int(+ extras.get("cache_write_tokens")+ or extras.get("cache_creation_input_tokens")+ or 0+ )++# Hold strong references to in-flight cost log tasks to prevent GC._pending_log_tasks: set[asyncio.Task[None]] = set()# Guards all reads and writes to _pending_log_tasks. Done callbacks (discard)diff --git a/autogpt_platform/backend/poetry.lock b/autogpt_platform/backend/poetry.lockindex ae08dedaf7..6cb3533df8 100644--- a/autogpt_platform/backend/poetry.lock+++ b/autogpt_platform/backend/poetry.lock@@ -3514,18 +3514,18 @@ files = [[[package]]name = "mem0ai"-version = "0.1.118"+version = "0.1.116"description = "Long-term memory for AI Agents"optional = falsepython-versions = "<4.0,>=3.9"groups = ["main"]files = [- {file = "mem0ai-0.1.118-py3-none-any.whl", hash = "sha256:c2b371224a340fd5529d608dfbd2e77c610c7ffe421005ff7e862fd6f322cca8"},- {file = "mem0ai-0.1.118.tar.gz", hash = "sha256:d62497286616357f8726b849afc20031cd0ab56d1cf312fa289b006be33c3ce7"},+ {file = "mem0ai-0.1.116-py3-none-any.whl", hash = "sha256:245b08f1e615e057ebacc52462ab729a7282abe05e8d4957236d893b3d32a990"},+ {file = "mem0ai-0.1.116.tar.gz", hash = "sha256:c33e08c5464f96b1cf109893dba5d394d8cc5788a8400d85cb1ceed696ee3204"},][package.dependencies]-openai = ">=1.90.0,<1.110.0"+openai = ">=1.33.0"posthog = ">=3.5.0"protobuf = ">=5.29.0,<6.0.0"pydantic = ">=2.7.3"@@ -3535,11 +3535,11 @@ sqlalchemy = ">=2.0.31"[package.extras]dev = ["isort (>=5.13.2)", "pytest (>=8.2.2)", "ruff (>=0.6.5)"]-extras = ["boto3 (>=1.34.0)", "elasticsearch (>=8.0.0,<9.0.0)", "langchain-community (>=0.0.0)", "langchain-memgraph (>=0.1.0)", "opensearch-py (>=2.0.0)", "sentence-transformers (>=5.0.0)"]-graph = ["kuzu (>=0.11.0)", "langchain-aws (>=0.2.23)", "langchain-neo4j (>=0.4.0)", "neo4j (>=5.23.1)", "rank-bm25 (>=0.2.2)"]-llms = ["google-genai (>=1.0.0)", "google-generativeai (>=0.3.0)", "groq (>=0.3.0)", "litellm (>=1.74.0)", "ollama (>=0.1.0)", "openai (>=1.90.0,<1.110.0)", "together (>=0.2.10)", "vertexai (>=0.1.0)"]+extras = ["boto3 (>=1.34.0)", "elasticsearch (>=8.0.0)", "langchain-community (>=0.0.0)", "langchain-memgraph (>=0.1.0)", "opensearch-py (>=2.0.0)", "sentence-transformers (>=5.0.0)"]+graph = ["langchain-aws (>=0.2.23)", "langchain-neo4j (>=0.4.0)", "neo4j (>=5.23.1)", "rank-bm25 (>=0.2.2)"]+llms = ["google-genai (>=1.0.0)", "google-generativeai (>=0.3.0)", "groq (>=0.3.0)", "litellm (>=0.1.0)", "ollama (>=0.1.0)", "together (>=0.2.10)", "vertexai (>=0.1.0)"]test = ["pytest (>=8.2.2)", "pytest-asyncio (>=0.23.7)", "pytest-mock (>=3.14.0)"]-vector-stores = ["azure-identity (>=1.24.0)", "azure-search-documents (>=11.4.0b8)", "chromadb (>=0.4.24)", "databricks-sdk (>=0.63.0)", "elasticsearch (>=8.0.0,<9.0.0)", "faiss-cpu (>=1.7.4)", "langchain-aws (>=0.2.23)", "pinecone (<=7.3.0)", "pinecone-text (>=0.10.0)", "psycopg (>=3.2.8)", "psycopg-pool (>=3.2.6,<4.0.0)", "pymilvus (>=2.4.0,<2.6.0)", "pymochow (>=2.2.9)", "pymongo (>=4.13.2)", "redis (>=5.0.0,<6.0.0)", "redisvl (>=0.1.0,<1.0.0)", "upstash-vector (>=0.1.0)", "valkey (>=6.0.0)", "vecs (>=0.4.0)", "weaviate-client (>=4.4.0,<4.15.0)"]+vector-stores = ["azure-search-documents (>=11.4.0b8)", "chromadb (>=0.4.24)", "faiss-cpu (>=1.7.4)", "pinecone (<=7.3.0)", "pinecone-text (>=0.10.0)", "psycopg (>=3.2.8)", "pymochow (>=2.2.9)", "pymongo (>=4.13.2)", "upstash-vector (>=0.1.0)", "vecs (>=0.4.0)", "weaviate-client (>=4.4.0,<4.15.0)"][[package]]name = "mmh3"diff --git a/autogpt_platform/backend/scripts/refresh_anthropic_rates.py b/autogpt_platform/backend/scripts/refresh_anthropic_rates.pynew file mode 100644index 0000000000..1660854c23--- /dev/null+++ b/autogpt_platform/backend/scripts/refresh_anthropic_rates.py@@ -0,0 +1,62 @@+"""Refresh ``backend/copilot/anthropic_rates.json`` from LiteLLM upstream.++Pulls the community-maintained model pricing JSON from BerriAI/litellm,+filters to entries for Claude / anthropic models, and writes the+result next to the rate-card module.++Run manually (``poetry run python scripts/refresh_anthropic_rates.py``)+or via the weekly GitHub Actions cron at+``.github/workflows/refresh-anthropic-rates.yml``.++Source URL is intentionally pinned to the ``main`` branch backup file,+which is the canonical pricing snapshot LiteLLM ships in their package.+"""++from __future__ import annotations++import json+import sys+import urllib.request+from pathlib import Path++_SOURCE_URL = (+ "https://raw.githubusercontent.com/BerriAI/litellm/main/"+ "litellm/model_prices_and_context_window_backup.json"+)+_OUT_PATH = (+ Path(__file__).resolve().parents[1] / "backend" / "copilot" / "anthropic_rates.json"+)+++def main() -> int:+ print(f"Fetching {_SOURCE_URL} ...", file=sys.stderr)+ with urllib.request.urlopen(_SOURCE_URL, timeout=30) as resp:+ data = json.loads(resp.read())+ if not isinstance(data, dict):+ print("Unexpected JSON shape (root not an object)", file=sys.stderr)+ return 1+ rates = {+ k: v+ for k, v in data.items()+ if isinstance(k, str)+ and (k.startswith("claude") or k.startswith("anthropic/claude"))+ and isinstance(v, dict)+ }+ out = {+ "_source": _SOURCE_URL,+ "_filter": "keys starting with claude or anthropic/claude",+ "rates": rates,+ }+ _OUT_PATH.write_text(+ json.dumps(out, indent=2, sort_keys=True) + "\n", encoding="utf-8"+ )+ print(+ f"Wrote {len(rates)} entries to {_OUT_PATH.name} "+ f"({_OUT_PATH.stat().st_size} bytes)",+ file=sys.stderr,+ )+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())