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.
fix(backend/copilot): charge each SDK turn its own cost, not the session's running total (#15305)
Reinier van der Leerfab04d6ac93d
diff --git a/autogpt_platform/backend/backend/copilot/sdk/cli_cost_state_test.py b/autogpt_platform/backend/backend/copilot/sdk/cli_cost_state_test.pynew file mode 100644index 0000000000..766fd1f915--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/cli_cost_state_test.py@@ -0,0 +1,169 @@+"""The bundled CLI counts a resumed session's cost on from what our reader finds.++A turn is charged ``total_cost_usd`` minus ``cli_session_cost_usd`` of the file+the CLI resumed, so a CLI bump that changes where the resumed total comes from+must fail here rather than mis-bill. Runs against a localhost fake provider.+"""++import asyncio+import json+import uuid+from pathlib import Path++import pytest+from aiohttp import web++from backend.copilot.sdk.cost_tracking import read_cli_session_usage+from backend.copilot.transcript import cli_session_cost_usd, cli_session_path++from .cli_openrouter_compat_test import _resolve_cli_path++_MODEL = "claude-sonnet-5-5"+_CALL_USAGE = {+ "input_tokens": 1000,+ "output_tokens": 100,+ "cache_read_input_tokens": 100_000,+ "cache_creation_input_tokens": 2000,+}+++async def test_resumed_cli_total_is_restored_cost_plus_this_call(tmp_path, monkeypatch):+ cli = _resolve_cli_path()+ if cli is None or not cli.is_file():+ pytest.skip("No Claude Code CLI binary available")+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path / "config"))+ cwd = tmp_path / "run"+ cwd.mkdir()+ session_id = str(uuid.uuid4())+ runner, port = await _start_fake_provider()+ try:+ first = await _run_cli(cli, cwd, port, "--session-id", session_id)+ session_file = Path(cli_session_path(str(cwd), session_id))+ resumed = []+ for foreign_row in (False, True):+ if foreign_row:+ # A row from another session must move neither the CLI nor us.+ with session_file.open("a") as f:+ row = {"type": "cost-state", "sessionId": "x", "totalCostUSD": 5}+ f.write(json.dumps(row) + "\n")+ restored = cli_session_cost_usd(session_file.read_text(), session_id)+ result = await _run_cli(cli, cwd, port, "--resume", session_id)+ resumed.append((restored, result["total_cost_usd"]))+ for key, value in _CALL_USAGE.items():+ assert result["usage"][key] == value+ finally:+ await runner.cleanup()++ call_cost = first["total_cost_usd"]+ assert call_cost > 0+ assert first["usage"]["input_tokens"] == _CALL_USAGE["input_tokens"]+ for restored, total in resumed:+ assert restored > 0+ assert total - restored == pytest.approx(call_cost)+ snapshot = read_cli_session_usage(str(cwd), session_id, "")+ assert snapshot.cost_usd == pytest.approx(resumed[-1][1])+ assert snapshot.tokens == {key: 3 * value for key, value in _CALL_USAGE.items()}+++async def _start_fake_provider() -> tuple[web.AppRunner, int]:+ async def messages(request: web.Request) -> web.StreamResponse:+ response = web.StreamResponse(headers={"Content-Type": "text/event-stream"})+ await response.prepare(request)+ await response.write(_streamed_reply().encode())+ await response.write_eof()+ return response++ app = web.Application()+ app.router.add_post("/v1/messages", messages)+ app.router.add_route("*", "/{tail:.*}", lambda _r: web.Response(status=404))+ runner = web.AppRunner(app)+ await runner.setup()+ site = web.TCPSite(runner, "127.0.0.1", 0)+ await site.start()+ assert site._server is not None+ return runner, site._server.sockets[0].getsockname()[1]+++async def _run_cli(cli: Path, cwd: Path, port: int, *session_args: str) -> dict:+ # A clean environment: a Claude Code session's own CLAUDE_* flags change+ # what the CLI does.+ env = {+ "PATH": "/usr/bin:/bin",+ "HOME": str(cwd.parent),+ "CLAUDE_CONFIG_DIR": str(cwd.parent / "config"),+ "ANTHROPIC_BASE_URL": f"http://127.0.0.1:{port}",+ "ANTHROPIC_API_KEY": "sk-test-fake-key-not-real",+ "DISABLE_TELEMETRY": "1",+ "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",+ "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1",+ }+ query = {"type": "user", "message": {"role": "user", "content": "Say ok."}}+ proc = await asyncio.create_subprocess_exec(+ str(cli),+ "--print",+ "--verbose",+ "--output-format",+ "stream-json",+ "--input-format",+ "stream-json",+ "--model",+ _MODEL,+ "--tools",+ "",+ "--setting-sources",+ "",+ "--system-prompt",+ "Reply ok.",+ *session_args,+ cwd=cwd,+ env=env,+ stdin=asyncio.subprocess.PIPE,+ stdout=asyncio.subprocess.PIPE,+ stderr=asyncio.subprocess.PIPE,+ )+ try:+ stdout, stderr = await asyncio.wait_for(+ proc.communicate((json.dumps(query) + "\n").encode()), timeout=60+ )+ except TimeoutError:+ proc.kill()+ await proc.wait()+ raise+ assert proc.returncode == 0, stderr.decode()[-2000:]+ lines = [json.loads(line) for line in stdout.splitlines() if line.startswith(b"{")]+ return next(line for line in lines if line.get("type") == "result")+++def _streamed_reply() -> str:+ message = {+ "id": f"msg_{uuid.uuid4().hex}",+ "type": "message",+ "role": "assistant",+ "model": _MODEL,+ "content": [],+ "stop_reason": None,+ "stop_sequence": None,+ "usage": {**_CALL_USAGE, "output_tokens": 0},+ }+ events = [+ {"type": "message_start", "message": message},+ {+ "type": "content_block_start",+ "index": 0,+ "content_block": {"type": "text", "text": ""},+ },+ {+ "type": "content_block_delta",+ "index": 0,+ "delta": {"type": "text_delta", "text": "ok"},+ },+ {"type": "content_block_stop", "index": 0},+ {+ "type": "message_delta",+ "delta": {"stop_reason": "end_turn", "stop_sequence": None},+ "usage": {"output_tokens": _CALL_USAGE["output_tokens"]},+ },+ {"type": "message_stop"},+ ]+ return "".join(f"event: {e['type']}\ndata: {json.dumps(e)}\n\n" for e in events)diff --git a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_cli_test.py b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_cli_test.pyindex 4cb3bf7980..2b997f3c33 100644--- a/autogpt_platform/backend/backend/copilot/sdk/codex_compat_cli_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/codex_compat_cli_test.py@@ -84,7 +84,9 @@ class _ClaudeHarnessTransport:@pytest.mark.asyncioasync def test_bundled_claude_cli_executes_mcp_tool_through_codex_gateway(tmp_path,+ monkeypatch: pytest.MonkeyPatch,) -> None:+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path / "claude-config"))tool_called = asyncio.Event()stderr_lines: list[str] = []diff --git a/autogpt_platform/backend/backend/copilot/sdk/conftest.py b/autogpt_platform/backend/backend/copilot/sdk/conftest.pyindex 3621fc12af..0337044742 100644--- a/autogpt_platform/backend/backend/copilot/sdk/conftest.py+++ b/autogpt_platform/backend/backend/copilot/sdk/conftest.py@@ -11,6 +11,17 @@ import pytest_asynciofrom backend.util import json++def build_cli_cost_row(session_id: str, total: float) -> str:+ """Build a native CLI cost snapshot shared by retry and restart tests."""+ return (+ json.dumps(+ {"type": "cost-state", "sessionId": session_id, "totalCostUSD": total}+ )+ + "\n"+ )++# ---------------------------------------------------------------------------# Env vars that ``ChatConfig`` validators read — must be cleared so explicit# constructor values are used. Centralised here so adding a new env-backeddiff --git a/autogpt_platform/backend/backend/copilot/sdk/cost_tracking.py b/autogpt_platform/backend/backend/copilot/sdk/cost_tracking.pynew file mode 100644index 0000000000..8359bf20ef--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/cost_tracking.py@@ -0,0 +1,158 @@+"""Per-turn accounting for the CLI's cumulative session cost and token snapshots."""++import logging+from pathlib import Path+from typing import Any++from pydantic import BaseModel, Field++from backend.copilot.moonshot import is_moonshot_model, override_cost_usd+from backend.copilot.transcript import cli_session_cost_state, cli_session_path++logger = logging.getLogger(__name__)++_TOKEN_FIELDS = {+ "input_tokens": "inputTokens",+ "output_tokens": "outputTokens",+ "cache_read_input_tokens": "cacheReadInputTokens",+ "cache_creation_input_tokens": "cacheCreationInputTokens",+}+++class CLISessionUsage(BaseModel):+ """The running totals restored from a native CLI session file."""++ cost_usd: float = 0.0+ tokens: dict[str, int] = Field(default_factory=dict)+++def read_cli_session_usage(+ sdk_cwd: str, session_id: str | None, log_prefix: str+) -> CLISessionUsage:+ """Read the CLI's own baseline; alert when an unreadable file forces zero."""+ if not session_id or not sdk_cwd:+ return CLISessionUsage()+ try:+ content = Path(cli_session_path(sdk_cwd, session_id)).read_text(+ encoding="utf-8"+ )+ except (OSError, UnicodeDecodeError) as e:+ logger.error(+ "%s Over-charge fallback: could not read the cost of CLI session %s "+ "(%s); counting from $0 may charge the full session total on resume",+ log_prefix,+ session_id,+ type(e).__name__,+ )+ return CLISessionUsage()+ entry = cli_session_cost_state(content, session_id)+ if entry is None:+ return CLISessionUsage()+ return CLISessionUsage(+ cost_usd=float(entry["totalCostUSD"]), tokens=_snapshot_tokens(entry)+ )+++def _snapshot_tokens(entry: dict[str, Any]) -> dict[str, int]:+ """Normalize cumulative per-model CLI counters to ResultMessage usage keys."""+ models = entry.get("modelUsage")+ if not isinstance(models, dict):+ return {}+ tokens: dict[str, int] = {}+ for usage in models.values():+ if not isinstance(usage, dict):+ continue+ for key, native_key in _TOKEN_FIELDS.items():+ value = usage.get(native_key)+ if isinstance(value, int) and value >= 0:+ tokens[key] = tokens.get(key, 0) + value+ return tokens+++class TokenUsage(BaseModel):+ """Usage accumulated across all queries, retries, and CLI sessions in a turn."""++ prompt_tokens: int = 0+ completion_tokens: int = 0+ cache_read_tokens: int = 0+ cache_creation_tokens: int = 0+ cost_usd: float | None = None+ cli_cost_usd: float = 0.0+ cli_session_total_usd: float | None = None+ cli_accounted_tokens: dict[str, int] = Field(default_factory=dict)++ def start_cli_session(self, snapshot: CLISessionUsage) -> None:+ """Set the new session's baselines without discarding earlier turn usage."""+ self.cli_session_total_usd = snapshot.cost_usd+ self.cli_accounted_tokens = dict(snapshot.tokens)++ def record_result(+ self,+ usage: dict[str, Any] | None,+ total_cost_usd: float | None,+ model: str | None,+ log_prefix: str,+ ) -> None:+ """Price this query using its serving model, then add it to the turn."""+ tokens = {key: (usage or {}).get(key) or 0 for key in _TOKEN_FIELDS}+ spend = (+ self._spend_since_last_result(total_cost_usd, log_prefix)+ if total_cost_usd is not None+ else None+ )+ self.prompt_tokens += tokens["input_tokens"]+ self.completion_tokens += tokens["output_tokens"]+ self.cache_read_tokens += tokens["cache_read_input_tokens"]+ self.cache_creation_tokens += tokens["cache_creation_input_tokens"]+ for key, value in tokens.items():+ self.cli_accounted_tokens[key] = (+ self.cli_accounted_tokens.get(key, 0) + value+ )+ if spend is None:+ return+ self.cli_cost_usd += spend+ priced_spend = override_cost_usd(+ model=model,+ sdk_reported_usd=spend,+ prompt_tokens=tokens["input_tokens"],+ completion_tokens=tokens["output_tokens"],+ cache_read_tokens=tokens["cache_read_input_tokens"],+ cache_creation_tokens=tokens["cache_creation_input_tokens"],+ )+ self.cost_usd = (self.cost_usd or 0.0) + priced_spend++ def record_unreported(+ self, snapshot: CLISessionUsage, model: str | None, log_prefix: str+ ) -> None:+ """Account for a missing result without charging earlier results again."""+ if snapshot.cost_usd <= (self.cli_session_total_usd or 0.0):+ return+ tokens = {+ key: max(0, value - self.cli_accounted_tokens.get(key, 0))+ for key, value in snapshot.tokens.items()+ }+ if is_moonshot_model(model) and not any(tokens.values()):+ logger.error(+ "%s Over-charge fallback: incomplete CLI response has spend but no "+ "recoverable Moonshot tokens; charging the CLI-priced increment",+ log_prefix,+ )+ model = None+ self.record_result(tokens, snapshot.cost_usd, model, log_prefix)++ def _spend_since_last_result(self, total_cost_usd: float, log_prefix: str) -> float:+ """Take an increment, retaining the alerted fallback for a reset CLI total."""+ previous_total = self.cli_session_total_usd or 0.0+ spend = total_cost_usd - previous_total+ if spend < 0:+ logger.error(+ "%s Over-charge fallback: CLI total_cost_usd $%.6f is below the "+ "previous session total ($%.6f); charging the full CLI total",+ log_prefix,+ total_cost_usd,+ previous_total,+ )+ spend = total_cost_usd+ self.cli_accounted_tokens.clear()+ self.cli_session_total_usd = total_cost_usd+ return spenddiff --git a/autogpt_platform/backend/backend/copilot/sdk/cost_tracking_test.py b/autogpt_platform/backend/backend/copilot/sdk/cost_tracking_test.pynew file mode 100644index 0000000000..c55e8f3c73--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/cost_tracking_test.py@@ -0,0 +1,78 @@+"""Recover only the native CLI spend and tokens not reported in earlier results."""++import logging++import pytest++from backend.copilot.sdk.cost_tracking import CLISessionUsage, TokenUsage++from .turn_cost_test import _CALL_COST, _CALL_USAGE, _KIMI, _kimi_rate_card_cost++[email protected]("model", ["claude-sonnet-4-6", _KIMI])+def test_missing_result_recovery_excludes_reported_usage(model):+ usage = TokenUsage()+ usage.start_cli_session(+ CLISessionUsage(cost_usd=_CALL_COST, tokens=dict(_CALL_USAGE))+ )+ usage.record_result(_CALL_USAGE, 2 * _CALL_COST, model, "")+ snapshot = CLISessionUsage(+ cost_usd=3 * _CALL_COST,+ tokens={key: 3 * value for key, value in _CALL_USAGE.items()},+ )+ usage.record_unreported(snapshot, model, "")+ usage.record_unreported(snapshot, model, "")++ per_call = _kimi_rate_card_cost() if model == _KIMI else _CALL_COST+ assert usage.cost_usd == pytest.approx(2 * per_call)+ assert usage.prompt_tokens == 2 * _CALL_USAGE["input_tokens"]+ assert usage.completion_tokens == 2 * _CALL_USAGE["output_tokens"]+ assert usage.cache_read_tokens == 2 * _CALL_USAGE["cache_read_input_tokens"]+ assert usage.cache_creation_tokens == 2 * _CALL_USAGE["cache_creation_input_tokens"]++[email protected]("model", ["claude-sonnet-4-6", _KIMI])+def test_incomplete_query_does_not_reprice_previous_model(model):+ first_model = _KIMI if model != _KIMI else "claude-sonnet-4-6"+ usage = TokenUsage()+ usage.start_cli_session(CLISessionUsage())+ usage.record_result(_CALL_USAGE, _CALL_COST, first_model, "")+ usage.record_unreported(+ CLISessionUsage(+ cost_usd=2 * _CALL_COST,+ tokens={key: 2 * value for key, value in _CALL_USAGE.items()},+ ),+ model,+ "",+ )+ assert usage.cost_usd == pytest.approx(_CALL_COST + _kimi_rate_card_cost())+++def test_missing_moonshot_tokens_alerts_and_uses_cli_spend(caplog):+ usage = TokenUsage()+ usage.start_cli_session(CLISessionUsage(cost_usd=_CALL_COST))+ with caplog.at_level(logging.ERROR):+ usage.record_unreported(CLISessionUsage(cost_usd=2 * _CALL_COST), _KIMI, "")++ assert usage.cost_usd == pytest.approx(_CALL_COST)+ assert [record.levelno for record in caplog.records] == [logging.ERROR]+++def test_recovery_after_cli_counter_reset_uses_new_session_tokens():+ usage = TokenUsage()+ usage.start_cli_session(+ CLISessionUsage(+ cost_usd=2 * _CALL_COST,+ tokens={key: 2 * value for key, value in _CALL_USAGE.items()},+ )+ )+ usage.record_result(_CALL_USAGE, _CALL_COST, _KIMI, "")+ usage.record_unreported(+ CLISessionUsage(+ cost_usd=2 * _CALL_COST,+ tokens={key: 2 * value for key, value in _CALL_USAGE.items()},+ ),+ _KIMI,+ "",+ )+ assert usage.cost_usd == pytest.approx(2 * _kimi_rate_card_cost())diff --git a/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py b/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.pyindex cd8de1c1da..fce5cf920c 100644--- a/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py@@ -10,7 +10,7 @@ from backend.copilot.constants import is_transient_api_errorfrom backend.copilot.model import ChatSessionfrom backend.copilot.response_model import StreamCompactionProgressfrom backend.copilot.sdk.compaction import CompactionTracker-from backend.copilot.sdk.service import _EPHEMERAL_EVENT_TYPES+from backend.copilot.sdk.service import _EPHEMERAL_EVENT_TYPES, _TokenUsagedef _make_config(**overrides) -> ChatConfig:@@ -752,20 +752,36 @@ class TestDoTransientBackoff:mock_cls.assert_called_once_with(message_id="msg-1", session_id="sess-1")assert state.adapter is new_adapter- async def test_resets_usage_after_yield(self):- """state.usage.reset() is called so the next attempt starts with clean counters."""+ async def test_preserves_usage_after_yield(self):+ """A resumed retry must retain both reported spend and its CLI baseline."""from unittest.mock import AsyncMock, MagicMock, patchfrom backend.copilot.sdk.service import _do_transient_backoffstate = MagicMock()- state.usage = MagicMock()+ state.usage = _TokenUsage(+ prompt_tokens=100,+ completion_tokens=10,+ cache_read_tokens=20,+ cache_creation_tokens=30,+ cost_usd=0.02,+ cli_cost_usd=0.1,+ cli_session_total_usd=1.1,+ )with patch("asyncio.sleep", new=AsyncMock()):async for _ in _do_transient_backoff(2, state, "msg-id", "sess-id"):pass- state.usage.reset.assert_called_once()+ assert state.usage == _TokenUsage(+ prompt_tokens=100,+ completion_tokens=10,+ cache_read_tokens=20,+ cache_creation_tokens=30,+ cost_usd=0.02,+ cli_cost_usd=0.1,+ cli_session_total_usd=1.1,+ )# ---------------------------------------------------------------------------diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex ee0b840be0..7af777e5b1 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -61,6 +61,10 @@ from backend.copilot.segments import Segment, stamp_segmentfrom backend.copilot.graphiti.ingest import enqueue_conversation_turnfrom backend.copilot.sdk.codex_compat_gateway import CodexAnthropicGatewayfrom backend.copilot.sdk.trial_budget import resolve_trial_sdk_budget+from backend.copilot.sdk.cost_tracking import (+ TokenUsage as _TokenUsage,+ read_cli_session_usage,+)from backend.copilot.tool_display import tool_calls_for_providerfrom backend.data.db_accessors import chat_dbfrom backend.data.redis_client import get_redis_async@@ -104,7 +108,6 @@ from backend.data.llm_registry.llm_models import MODEL_DATE_SUFFIX_REfrom ..moonshot import (is_moonshot_model as _is_moonshot_model,- override_cost_usd as _override_cost_for_moonshot,)from ..model import (ChatMessage,@@ -693,6 +696,10 @@ async def _consume_sdk_until_done(# complete normally, the synthetic error text is stored# in the transcript, and the session grows without bound.if _is_prompt_too_long(RuntimeError(sdk_msg.result or "")):+ # A later API call can fail after earlier calls incurred spend.+ # Preflight rejections report zero even on a resumed session.+ if sdk_msg.total_cost_usd:+ _record_result_usage(sdk_msg, state, ctx.log_prefix)raise RuntimeError("Prompt is too long")_record_result_usage(sdk_msg, state, ctx.log_prefix)@@ -1241,26 +1248,14 @@ def _friendly_error_text(raw: str) -> str:def _record_result_usage(sdk_msg: ResultMessage, state: "_RetryState", log_prefix: str) -> None:- """Add the turn's token usage and cost from the CLI's ``ResultMessage``."""- # Capture token usage from ResultMessage.- # Anthropic reports cached tokens separately:- # input_tokens = uncached only- # cache_read_input_tokens = served from cache- # cache_creation_input_tokens = written to cache+ """Record this query using the model it actually ran on."""+ state.usage.record_result(+ sdk_msg.usage,+ sdk_msg.total_cost_usd,+ state.observed_model or getattr(state.options, "model", None),+ log_prefix,+ )if sdk_msg.usage:- # Use `or 0` instead of a default in .get() because- # OpenRouter may include the key with a null value (e.g.- # {"cache_read_input_tokens": null}) for models that don't- # yet report cache tokens, making .get("key", 0) return- # None rather than the fallback 0.- state.usage.prompt_tokens += sdk_msg.usage.get("input_tokens") or 0- state.usage.cache_read_tokens += (- sdk_msg.usage.get("cache_read_input_tokens") or 0- )- state.usage.cache_creation_tokens += (- sdk_msg.usage.get("cache_creation_input_tokens") or 0- )- state.usage.completion_tokens += sdk_msg.usage.get("output_tokens") or 0logger.info("%s Token usage: uncached=%d, cache_read=%d, cache_create=%d, output=%d",log_prefix,@@ -1269,36 +1264,6 @@ def _record_result_usage(state.usage.cache_creation_tokens,state.usage.completion_tokens,)- if sdk_msg.total_cost_usd is not None:- # Default: trust the CLI-reported value. Accurate for- # Anthropic models (the CLI's bundled pricing table is- # Anthropic-authored), and becomes the sync-path cost- # when the reconcile is disabled or fails.- # Prefer the ACTUALLY executed model- # (``state.observed_model`` from ``AssistantMessage.model``)- # over the requested primary (``state.options.model``)- # so a fallback activation doesn't mis-route pricing.- active_model = state.observed_model or getattr(state.options, "model", None)- if _is_moonshot_model(active_model):- # Moonshot slug — the CLI doesn't know Moonshot's- # rate card and silently bills at Sonnet rates- # (~5x over-charge). Replace with the rate-card- # estimate so the in-stream ``cost_usd`` and the- # reconcile's lookup-fail fallback reflect- # reality. Reconcile- # (``record_turn_cost_from_openrouter``) still- # overrides this value when every gen-ID lookup- # succeeds.- state.usage.cost_usd = _override_cost_for_moonshot(- model=active_model,- sdk_reported_usd=sdk_msg.total_cost_usd,- prompt_tokens=state.usage.prompt_tokens,- completion_tokens=state.usage.completion_tokens,- cache_read_tokens=state.usage.cache_read_tokens,- cache_creation_tokens=state.usage.cache_creation_tokens,- )- else:- state.usage.cost_usd = sdk_msg.total_cost_usddef _platform_out_of_credits_refusal(@@ -1387,31 +1352,6 @@ class ReducedContext(NamedTuple):target_tokens: int | None = None-@dataclass-class _TokenUsage:- """Token usage accumulators for a single turn.-- Separated from `_RetryState` because usage is reset between retry- attempts independently of the retry-control fields, and is read by- the outer `stream_chat_completion_sdk` scope after the retry loop- completes.- """-- prompt_tokens: int = 0- completion_tokens: int = 0- cache_read_tokens: int = 0- cache_creation_tokens: int = 0- cost_usd: float | None = None-- def reset(self) -> None:- """Reset all accumulators for a new attempt."""- self.prompt_tokens = 0- self.completion_tokens = 0- self.cache_read_tokens = 0- self.cache_creation_tokens = 0- self.cost_usd = None--@dataclassclass _RetryState:"""Mutable state passed to `_run_stream_attempt` instead of closures.@@ -2498,8 +2438,8 @@ async def _do_transient_backoff("""Emit a retry notification, sleep, and reset the SDK adapter.Yields a single :class:`StreamStatus` so the caller can forward it to- the client, then sleeps for *backoff* seconds and resets ``state.adapter``- and ``state.usage`` so the next attempt starts clean.+ the client, then sleeps for *backoff* seconds and resets ``state.adapter``.+ Usage and the CLI cost baseline survive because the retry resumes the session.Extracted from both exception handlers in the retry loop to removenear-identical code duplication.@@ -2510,7 +2450,6 @@ async def _do_transient_backoff(message_id=message_id,session_id=session_id,)- state.usage.reset()def _is_fallback_stderr(line: str) -> bool:@@ -4227,6 +4166,13 @@ async def _run_stream_attempt(# CLI subprocess spawn + MCP init can take seconds on cold starts —# narrate it so the status doesn't sit on the context-prep message.yield StreamStatus(message="Starting the assistant…")+ # An interruption can precede ResultMessage. Keep the baseline across+ # resumed retries so the next result includes that unreported spend.+ if state.usage.cli_session_total_usd is None:+ baseline = await asyncio.to_thread(+ read_cli_session_usage, ctx.sdk_cwd, state.options.resume, ctx.log_prefix+ )+ state.usage.start_cli_session(baseline)sdk_client = ClaudeSDKClient(options=state.options)client = await sdk_client.__aenter__()try:@@ -4373,6 +4319,17 @@ async def _run_stream_attempt(yield responseif not acc.stream_completed and not loop_state.ended_with_stream_error:+ snapshot = await asyncio.to_thread(+ read_cli_session_usage,+ ctx.sdk_cwd,+ state.options.resume or state.options.session_id or ctx.session_id,+ ctx.log_prefix,+ )+ state.usage.record_unreported(+ snapshot,+ state.observed_model or getattr(state.options, "model", None),+ ctx.log_prefix,+ )# User cancels raise ``asyncio.CancelledError`` upstream; reaching this# branch means the CLI hung up — per-query budget exhausted, max_turns,# OOM, or crash — without ever emitting a ResultMessage.@@ -5819,6 +5776,8 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesdelete_stale_cli_session_file(sdk_cwd, session_id, log_prefix)sdk_options_retry.resume = Nonesdk_options_retry.session_id = session_id+ # The new CLI total starts at zero; retain the turn's spend.+ state.usage.cli_session_total_usd = None# Recompute system_prompt for retry. When enabled, the preset# is safe on every turn (requires CLI ≥ 2.1.98, bundled in# claude-agent-sdk >= 0.1.64).@@ -5888,9 +5847,6 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues# silent failure on the retry.if prior_adapter.emitted_real_content_to_wire:state.adapter.prior_attempt_emitted_visible_content = True- # Reset token accumulators so a failed attempt's partial- # usage is not double-counted in the successful attempt.- state.usage.reset()pre_attempt_msg_count = len(session.messages)# Snapshot transcript builder state — it maintains andiff --git a/autogpt_platform/backend/backend/copilot/sdk/service_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_test.pyindex 127424d68c..cf10a1ba85 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service_test.py@@ -9,6 +9,7 @@ from typing import castfrom unittest.mock import AsyncMock, MagicMock, patchimport pytest+from claude_agent_sdk import ResultMessagefrom backend.copilot import config as cfg_modfrom backend.copilot.builder_context import BUILDER_BLOCKED_TOOLS@@ -17,6 +18,7 @@ from backend.copilot.permissions import CopilotPermissions, all_known_tool_namesfrom backend.data.sharing.workspace_refs import extract_workspace_file_idsfrom .codex_compat_gateway import CodexAnthropicGateway+from .cost_tracking import TokenUsagefrom .service import (_HUNG_TOOL_CAP_SECONDS,_IDLE_TIMEOUT_SECONDS,@@ -31,6 +33,7 @@ from .service import (_normalize_model_name,_prepare_file_attachments,_raise_deferred_codex_cleanup_error,+ _record_result_usage,_redact_cli_stderr,_resolve_dynamic_max_budget_usd,_resolve_sdk_model,@@ -1159,18 +1162,7 @@ class TestRetryStateObservedModel:class TestMoonshotCostOverrideGate:- """Regression guards for the decision logic in- ``_run_stream_attempt`` that picks between the CLI-reported cost- and the Moonshot rate-card override. The code:-- active_model = state.observed_model or getattr(state.options, "model", None)- if _is_moonshot_model(active_model):- state.usage.cost_usd = _override_cost_for_moonshot(...)- else:- state.usage.cost_usd = sdk_msg.total_cost_usd-- is critical-path billing logic — make sure observed_model wins over- the requested primary, and Anthropic turns pass through untouched."""+ """Observed model wins over requested primary when pricing a billed result."""def _decide_cost(self,@@ -1181,21 +1173,25 @@ class TestMoonshotCostOverrideGate:prompt_tokens: int = 0,completion_tokens: int = 0,) -> float:- """Mirror of the real decision block — lets us assert the gate- without constructing the whole 1000-line generator."""- from .service import _is_moonshot_model, _override_cost_for_moonshot-- active_model = observed_model or options_model- if _is_moonshot_model(active_model):- return _override_cost_for_moonshot(- model=active_model,- sdk_reported_usd=sdk_reported_usd,- prompt_tokens=prompt_tokens,- completion_tokens=completion_tokens,- cache_read_tokens=0,- cache_creation_tokens=0,- )- return sdk_reported_usd+ """Exercise the production result handler with a minimal SDK result."""+ state = MagicMock(+ observed_model=observed_model,+ options=SimpleNamespace(model=options_model),+ usage=TokenUsage(),+ )+ result = ResultMessage(+ subtype="success",+ duration_ms=0,+ duration_api_ms=0,+ is_error=False,+ num_turns=1,+ session_id="test",+ total_cost_usd=sdk_reported_usd,+ usage={"input_tokens": prompt_tokens, "output_tokens": completion_tokens},+ )+ _record_result_usage(result, state, "")+ assert state.usage.cost_usd is not None+ return state.usage.cost_usddef test_anthropic_turn_passes_sdk_cost_through(self):"""Anthropic — the CLI's pricing table is authoritative, so@@ -1283,10 +1279,7 @@ class TestMoonshotCostOverrideGate:class TestMoonshotHelperReexports:- """``sdk/service.py`` imports the Moonshot helpers under local- aliases (``_is_moonshot_model``, ``_override_cost_for_moonshot``).- Regression guard so a refactor doesn't silently break the import- path the hot-loop code relies on."""+ """The service's model-dependent environment gates use the shared predicate."""def test_is_moonshot_model_aliased(self):from backend.copilot.moonshot import is_moonshot_model as canonical@@ -1295,13 +1288,6 @@ class TestMoonshotHelperReexports:assert _is_moonshot_model is canonical- def test_override_cost_for_moonshot_aliased(self):- from backend.copilot.moonshot import override_cost_usd as canonical-- from .service import _override_cost_for_moonshot-- assert _override_cost_for_moonshot is canonical-class TestIdleTimeoutThreshold:"""SECRT-2247: stream uses two idle thresholds. The shorter 30-min threshold@@ -2098,7 +2084,7 @@ class TestStreamEndedWithoutResultMessage:transcript_builder.append_assistant = MagicMock()transcript_builder.append_tool_result = MagicMock()return _RetryState(- options=MagicMock(),+ options=MagicMock(resume=None, session_id=session_id),query_message="hello",compaction_stats=None,use_resume=False,diff --git a/autogpt_platform/backend/backend/copilot/sdk/turn_cost_e2e_test.py b/autogpt_platform/backend/backend/copilot/sdk/turn_cost_e2e_test.pynew file mode 100644index 0000000000..177d2da175--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/turn_cost_e2e_test.py@@ -0,0 +1,241 @@+"""Two chat turns through ``stream_chat_completion_sdk`` with the bundled CLI.++The second turn restores the first turn's uploaded CLI session on a fresh disk+and resumes it, as a turn on another pod does. Only the database, Redis and+transcript storage are stubbed; the CLI talks to a localhost fake provider.+"""++import contextlib+import json+import shutil+import uuid+from datetime import UTC, datetime+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from aiohttp import web+from claude_agent_sdk import ClaudeSDKClient++from backend.copilot.model import ChatMessage, ChatSession+from backend.copilot.sdk.service import stream_chat_completion_sdk+from backend.copilot.transcript import TranscriptDownload++from .cli_openrouter_compat_test import _resolve_cli_path+from .retry_scenarios_test import _make_sdk_patches++_SVC = "backend.copilot.sdk.service"+_MODEL = "claude-sonnet-5-5"+_CALL_USAGE = {+ "input_tokens": 1000,+ "output_tokens": 100,+ "cache_read_input_tokens": 100_000,+ "cache_creation_input_tokens": 2000,+}+_STUBBED_HERE = {+ f"{_SVC}.ClaudeSDKClient",+ f"{_SVC}._make_sdk_cwd",+ "os.makedirs",+ f"{_SVC}.download_transcript",+ f"{_SVC}.upload_transcript",+ f"{_SVC}.strip_for_upload",+ f"{_SVC}.validate_transcript",+ f"{_SVC}.build_sdk_env",+}+++async def test_second_turn_on_a_new_pod_is_charged_only_its_own_call(+ tmp_path, monkeypatch+):+ cli = _resolve_cli_path()+ if cli is None or not cli.is_file():+ pytest.skip("No Claude Code CLI binary available")+ config_dir = tmp_path / "config"+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(config_dir))+ runner, port = await _start_fake_provider()+ env = {+ "PATH": "/usr/bin:/bin",+ "HOME": str(tmp_path),+ "CLAUDE_CONFIG_DIR": str(config_dir),+ "ANTHROPIC_BASE_URL": f"http://127.0.0.1:{port}",+ "ANTHROPIC_API_KEY": "sk-test-fake-key-not-real",+ "DISABLE_TELEMETRY": "1",+ "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",+ "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1",+ }+ session_id = str(uuid.uuid4())+ stored: dict[str, TranscriptDownload] = {}+ launched: list[tuple[str | None, str | None]] = []+ results: list[float] = []++ def _bundled_cli_client(*args, options, **kwargs):+ launched.append((options.resume, options.session_id))+ options.cli_path = str(cli)+ options.env = env+ options.model = _MODEL+ options.fallback_model = None+ options.mcp_servers = {}+ options.hooks = None+ options.allowed_tools = []+ options.disallowed_tools = []+ options.system_prompt = "Reply ok."+ client = ClaudeSDKClient(*args, options=options, **kwargs)+ receive = client.receive_response++ async def _receive_and_note_totals():+ async for message in receive():+ total = getattr(message, "total_cost_usd", None)+ if total is not None:+ results.append(total)+ yield message++ client.receive_response = _receive_and_note_totals+ return client++ async def _upload(*, session_id, content, message_count, mode, **_):+ stored[session_id] = TranscriptDownload(+ content=content, message_count=message_count, mode=mode+ )++ async def _download(user_id, session_id, log_prefix="", **_):+ return stored.get(session_id)++ try:+ charged = []+ history: list[ChatMessage] = []+ for prompt in ("hello", "again"):+ history.append(ChatMessage(role="user", content=prompt))+ session = ChatSession(+ session_id=session_id,+ user_id="test-user",+ usage=[],+ started_at=datetime.now(UTC),+ updated_at=datetime.now(UTC),+ messages=list(history),+ title="Cost check",+ )+ persist = await _run_turn(+ session, prompt, tmp_path, env, _bundled_cli_client, _upload, _download+ )+ persist.assert_awaited_once()+ charged.append(persist.await_args.kwargs["cost_usd"])+ history = list(session.messages)+ # The next turn runs on another pod: only uploaded state survives.+ shutil.rmtree(config_dir / "projects", ignore_errors=True)+ finally:+ await runner.cleanup()++ assert launched == [(None, session_id), (session_id, None)]+ call_cost, resumed_total = results+ assert call_cost > 0+ # The CLI restored turn 1's total from the uploaded session file.+ assert resumed_total == pytest.approx(2 * call_cost)+ assert charged == [pytest.approx(call_cost), pytest.approx(call_cost)]+++async def _run_turn(session, prompt, tmp_path, env, client, upload, download):+ patches = [+ (target, kwargs)+ for target, kwargs in _make_sdk_patches(+ session,+ original_transcript="",+ compacted_transcript=None,+ client_side_effect=client,+ )+ if target not in _STUBBED_HERE+ ]+ sdk_cwd = tmp_path / "copilot-cwd"+ sdk_cwd.mkdir(exist_ok=True)+ with contextlib.ExitStack() as stack:+ for target, kwargs in patches:+ stack.enter_context(patch(target, **kwargs))+ stack.enter_context(patch(f"{_SVC}.ClaudeSDKClient", side_effect=client))+ stack.enter_context(patch(f"{_SVC}._make_sdk_cwd", return_value=str(sdk_cwd)))+ stack.enter_context(patch(f"{_SVC}.build_sdk_env", return_value=env))+ stack.enter_context(patch(f"{_SVC}.upload_transcript", side_effect=upload))+ stack.enter_context(patch(f"{_SVC}.download_transcript", side_effect=download))+ stack.enter_context(+ patch(+ f"{_SVC}.build_skills_update_notice",+ new_callable=AsyncMock,+ return_value=None,+ )+ )+ stack.enter_context(+ patch(f"{_SVC}.create_security_hooks", return_value=MagicMock())+ )+ for name, value in (+ ("inject_user_context", None),+ ("build_session_context", ""),+ ):+ stack.enter_context(+ patch(f"{_SVC}.{name}", new_callable=AsyncMock, return_value=value)+ )+ persist = stack.enter_context(+ patch(f"{_SVC}.persist_and_record_usage", new_callable=AsyncMock)+ )+ stack.enter_context(+ patch(f"{_SVC}.record_turn_cost_from_openrouter", new_callable=AsyncMock)+ )+ async for _ in stream_chat_completion_sdk(+ session_id=session.session_id,+ message=prompt,+ is_user_message=True,+ user_id="test-user",+ session=session,+ ):+ pass+ return persist+++async def _start_fake_provider() -> tuple[web.AppRunner, int]:+ async def messages(request: web.Request) -> web.StreamResponse:+ response = web.StreamResponse(headers={"Content-Type": "text/event-stream"})+ await response.prepare(request)+ await response.write(_streamed_reply().encode())+ await response.write_eof()+ return response++ app = web.Application()+ app.router.add_post("/v1/messages", messages)+ app.router.add_route("*", "/{tail:.*}", lambda _r: web.Response(status=404))+ runner = web.AppRunner(app)+ await runner.setup()+ site = web.TCPSite(runner, "127.0.0.1", 0)+ await site.start()+ assert site._server is not None+ return runner, site._server.sockets[0].getsockname()[1]+++def _streamed_reply() -> str:+ message = {+ "id": f"msg_{uuid.uuid4().hex}",+ "type": "message",+ "role": "assistant",+ "model": _MODEL,+ "content": [],+ "stop_reason": None,+ "stop_sequence": None,+ "usage": {**_CALL_USAGE, "output_tokens": 0},+ }+ events = [+ {"type": "message_start", "message": message},+ {+ "type": "content_block_start",+ "index": 0,+ "content_block": {"type": "text", "text": ""},+ },+ {+ "type": "content_block_delta",+ "index": 0,+ "delta": {"type": "text_delta", "text": "ok"},+ },+ {"type": "content_block_stop", "index": 0},+ {+ "type": "message_delta",+ "delta": {"stop_reason": "end_turn", "stop_sequence": None},+ "usage": {"output_tokens": _CALL_USAGE["output_tokens"]},+ },+ {"type": "message_stop"},+ ]+ return "".join(f"event: {e['type']}\ndata: {json.dumps(e)}\n\n" for e in events)diff --git a/autogpt_platform/backend/backend/copilot/sdk/turn_cost_incomplete_test.py b/autogpt_platform/backend/backend/copilot/sdk/turn_cost_incomplete_test.pynew file mode 100644index 0000000000..51298a0ccf--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/turn_cost_incomplete_test.py@@ -0,0 +1,120 @@+"""An incomplete SDK response must account for spend saved in its CLI session."""++import contextlib+import json+from datetime import UTC, datetime+from pathlib import Path+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from claude_agent_sdk import AssistantMessage, TextBlock++from backend.copilot.model import ChatMessage, ChatSession+from backend.copilot.sdk.service import stream_chat_completion_sdk+from backend.copilot.transcript import cli_session_path++from .conftest import build_test_transcript+from .retry_scenarios_test import _make_sdk_patches+from .turn_cost_test import (+ _CALL_COST,+ _CALL_USAGE,+ _KIMI,+ _SDK_CWD,+ _SESSION_ID,+ _SVC,+ _kimi_rate_card_cost,+)+++def _native_cost_row(model: str, calls: int) -> str:+ return (+ json.dumps(+ {+ "type": "cost-state",+ "sessionId": _SESSION_ID,+ "totalCostUSD": calls * _CALL_COST,+ "modelUsage": {+ model: {+ "inputTokens": calls * _CALL_USAGE["input_tokens"],+ "outputTokens": calls * _CALL_USAGE["output_tokens"],+ "cacheReadInputTokens": calls+ * _CALL_USAGE["cache_read_input_tokens"],+ "cacheCreationInputTokens": calls+ * _CALL_USAGE["cache_creation_input_tokens"],+ "costUSD": calls * _CALL_COST,+ }+ },+ }+ )+ + "\n"+ )++[email protected]("model", ["claude-sonnet-4-6", _KIMI])+async def test_cli_exit_without_result_records_its_spend(model, tmp_path, monkeypatch):+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path))+ session_file = Path(cli_session_path(_SDK_CWD, _SESSION_ID))+ session_file.parent.mkdir(parents=True)+ transcript = build_test_transcript(+ [("user", "prior question"), ("assistant", "prior answer")]+ ) + _native_cost_row(model, 1)+ session = ChatSession(+ session_id=_SESSION_ID,+ user_id="test-user",+ usage=[],+ started_at=datetime.now(UTC),+ updated_at=datetime.now(UTC),+ messages=[+ ChatMessage(role="user", content="prior question"),+ ChatMessage(role="assistant", content="prior answer"),+ ChatMessage(role="user", content="hello"),+ ],+ )++ def client_factory(*args, **kwargs):+ async def receive():+ with session_file.open("a") as handle:+ handle.write(_native_cost_row(model, 2))+ yield AssistantMessage(content=[TextBlock(text="partial")], model=model)++ client = MagicMock()+ client.query = AsyncMock()+ client.receive_response = receive+ context = AsyncMock()+ context.__aenter__.return_value = client+ context.__aexit__.return_value = None+ return context++ patches = _make_sdk_patches(+ session,+ original_transcript=transcript,+ compacted_transcript=None,+ client_side_effect=client_factory,+ )+ with contextlib.ExitStack() as stack:+ for target, kwargs in patches:+ stack.enter_context(patch(target, **kwargs))+ stack.enter_context(+ patch(+ f"{_SVC}.build_skills_update_notice",+ new_callable=AsyncMock,+ return_value=None,+ )+ )+ persist = stack.enter_context(+ patch(f"{_SVC}.persist_and_record_usage", new_callable=AsyncMock)+ )+ async for _ in stream_chat_completion_sdk(+ session_id=_SESSION_ID,+ message="hello",+ is_user_message=True,+ user_id="test-user",+ session=session,+ ):+ pass++ persist.assert_awaited_once()+ expected = _kimi_rate_card_cost() if model == _KIMI else _CALL_COST+ assert persist.await_args.kwargs["cost_usd"] == pytest.approx(expected)+ assert persist.await_args.kwargs["prompt_tokens"] == _CALL_USAGE["input_tokens"]diff --git a/autogpt_platform/backend/backend/copilot/sdk/turn_cost_restart_test.py b/autogpt_platform/backend/backend/copilot/sdk/turn_cost_restart_test.pynew file mode 100644index 0000000000..1e89bcb30d--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/turn_cost_restart_test.py@@ -0,0 +1,112 @@+"""Cost accounting across the building-mode CLI interruption and relaunch."""++import contextlib+from datetime import UTC, datetime+from pathlib import Path+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from claude_agent_sdk import AssistantMessage, TextBlock++from backend.copilot.model import ChatMessage, ChatSession+from backend.copilot.response_model import StreamError+from backend.copilot.sdk.service import stream_chat_completion_sdk+from backend.copilot.transcript import cli_session_path++from .conftest import build_cli_cost_row, build_test_transcript+from .retry_scenarios_test import _make_sdk_patches+from .turn_cost_test import _CALL_COST, _SDK_CWD, _SESSION_ID, _SVC, _result++[email protected]("result_before_restart", [False, True])+async def test_building_restart_counts_both_processes_once(+ result_before_restart, tmp_path, monkeypatch+):+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path))+ session_file = Path(cli_session_path(_SDK_CWD, _SESSION_ID))+ session_file.parent.mkdir(parents=True)+ transcript = build_test_transcript(+ [("user", "prior question"), ("assistant", "prior answer")]+ ) + build_cli_cost_row(_SESSION_ID, _CALL_COST)+ session = ChatSession(+ session_id=_SESSION_ID,+ user_id="test-user",+ usage=[],+ started_at=datetime.now(UTC),+ updated_at=datetime.now(UTC),+ messages=[+ ChatMessage(role="user", content="prior question"),+ ChatMessage(role="assistant", content="prior answer"),+ ChatMessage(role="user", content="build an agent"),+ ],+ )+ launched = []++ def client_factory(*args, **kwargs):+ launched.append(kwargs["options"])+ first_process = len(launched) == 1++ async def receive():+ if first_process:+ with session_file.open("a") as handle:+ handle.write(build_cli_cost_row(_SESSION_ID, 2 * _CALL_COST))+ session.building_mode_requested = not result_before_restart+ yield AssistantMessage(+ content=[TextBlock(text="ok")], model="claude-sonnet-4-6"+ )+ if first_process:+ session.building_mode_requested = True+ yield _result((2 if first_process else 3) * _CALL_COST)++ client = MagicMock()+ client.query = AsyncMock()+ client.interrupt = AsyncMock()+ client.receive_response = receive+ context = AsyncMock()+ context.__aenter__.return_value = client+ context.__aexit__.return_value = None+ return context++ patches = _make_sdk_patches(+ session,+ original_transcript=transcript,+ compacted_transcript=None,+ client_side_effect=client_factory,+ )+ with contextlib.ExitStack() as stack:+ for target, kwargs in patches:+ stack.enter_context(patch(target, **kwargs))+ stack.enter_context(+ patch(+ f"{_SVC}.build_skills_update_notice",+ new_callable=AsyncMock,+ return_value=None,+ )+ )+ stack.enter_context(+ patch(+ f"{_SVC}.build_builder_system_prompt_suffix",+ new_callable=AsyncMock,+ side_effect=["", "<building_guide>guide</building_guide>"],+ )+ )+ persist = stack.enter_context(+ patch(f"{_SVC}.persist_and_record_usage", new_callable=AsyncMock)+ )+ events = [+ event+ async for event in stream_chat_completion_sdk(+ session_id=_SESSION_ID,+ message="build an agent",+ is_user_message=True,+ user_id="test-user",+ session=session,+ )+ ]++ assert [options.resume for options in launched] == [_SESSION_ID, _SESSION_ID]+ assert session.guide_in_system_prompt+ assert not any(isinstance(event, StreamError) for event in events)+ persist.assert_awaited_once()+ assert persist.await_args.kwargs["cost_usd"] == pytest.approx(2 * _CALL_COST)diff --git a/autogpt_platform/backend/backend/copilot/sdk/turn_cost_retry_test.py b/autogpt_platform/backend/backend/copilot/sdk/turn_cost_retry_test.pynew file mode 100644index 0000000000..12e64dddfb--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/turn_cost_retry_test.py@@ -0,0 +1,131 @@+"""Retrying a platform turn must retain spend from the failed attempt."""++import contextlib+from datetime import UTC, datetime+from pathlib import Path+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from claude_agent_sdk import AssistantMessage, TextBlock++from backend.copilot.model import ChatMessage, ChatSession+from backend.copilot.response_model import StreamError+from backend.copilot.sdk.service import stream_chat_completion_sdk+from backend.copilot.transcript import cli_session_path++from .conftest import build_cli_cost_row, build_test_transcript+from .retry_scenarios_test import _make_sdk_patches+from .turn_cost_test import _CALL_COST, _SDK_CWD, _SESSION_ID, _SVC, _result+++ "failure", ["transient", "transient-assistant", "context-result", "context-zero"]+)+async def test_retry_retains_failed_attempt_spend(+ failure, tmp_path, monkeypatch, caplog+):+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path))+ session_file = Path(cli_session_path(_SDK_CWD, _SESSION_ID))+ session_file.parent.mkdir(parents=True)+ transcript = build_test_transcript(+ [("user", "prior question"), ("assistant", "prior answer")]+ ) + build_cli_cost_row(_SESSION_ID, _CALL_COST)+ compacted = build_test_transcript(+ [("user", "summary"), ("assistant", "summarized answer")]+ )+ session = ChatSession(+ session_id=_SESSION_ID,+ user_id="test-user",+ usage=[],+ started_at=datetime.now(UTC),+ updated_at=datetime.now(UTC),+ messages=[+ ChatMessage(role="user", content="prior question"),+ ChatMessage(role="assistant", content="prior answer"),+ ChatMessage(role="user", content="hello"),+ ],+ )+ launched = []++ def client_factory(*args, **kwargs):+ launched.append(kwargs["options"])+ first_process = len(launched) == 1++ async def receive():+ if first_process:+ if failure != "context-zero":+ with session_file.open("a") as handle:+ handle.write(build_cli_cost_row(_SESSION_ID, 2 * _CALL_COST))+ if failure == "transient":+ raise ConnectionError("ECONNRESET: connection reset by peer")+ if failure == "transient-assistant":+ yield AssistantMessage(+ content=[], model="claude-sonnet-4-6", error="rate_limit"+ )+ return+ result = _result(0 if failure == "context-zero" else 2 * _CALL_COST)+ result.subtype = "error_during_execution"+ result.is_error = True+ result.result = "Prompt is too long"+ if failure == "context-zero":+ result.usage = {}+ yield result+ return+ yield AssistantMessage(+ content=[TextBlock(text="ok")], model="claude-sonnet-4-6"+ )+ yield _result((2 if failure.startswith("context") else 3) * _CALL_COST)++ client = MagicMock()+ client.query = AsyncMock()+ client.receive_response = receive+ context = AsyncMock()+ context.__aenter__.return_value = client+ context.__aexit__.return_value = None+ return context++ patches = _make_sdk_patches(+ session,+ original_transcript=transcript,+ compacted_transcript=compacted,+ client_side_effect=client_factory,+ )+ with contextlib.ExitStack() as stack:+ for target, kwargs in patches:+ stack.enter_context(patch(target, **kwargs))+ stack.enter_context(+ patch(+ f"{_SVC}.build_skills_update_notice",+ new_callable=AsyncMock,+ return_value=None,+ )+ )+ stack.enter_context(patch(f"{_SVC}._compute_transient_backoff", return_value=0))+ persist = stack.enter_context(+ patch(f"{_SVC}.persist_and_record_usage", new_callable=AsyncMock)+ )+ events = [+ event+ async for event in stream_chat_completion_sdk(+ session_id=_SESSION_ID,+ message="hello",+ is_user_message=True,+ user_id="test-user",+ session=session,+ )+ ]++ assert [options.resume for options in launched] == [+ _SESSION_ID,+ None if failure.startswith("context") else _SESSION_ID,+ ]+ assert not any(isinstance(event, StreamError) for event in events)+ persist.assert_awaited_once()+ expected = (3 if failure == "context-result" else 2) * _CALL_COST+ assert persist.await_args.kwargs["cost_usd"] == pytest.approx(expected)+ if failure.startswith("context"):+ assert persist.await_args.kwargs["prompt_tokens"] == (+ 2000 if failure == "context-result" else 1000+ )+ assert "Over-charge fallback" not in caplog.textdiff --git a/autogpt_platform/backend/backend/copilot/sdk/turn_cost_test.py b/autogpt_platform/backend/backend/copilot/sdk/turn_cost_test.pynew file mode 100644index 0000000000..d81606464f--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/turn_cost_test.py@@ -0,0 +1,262 @@+"""A resumed SDK turn is charged its own cost, not the CLI session's running total.++On ``--resume`` the CLI restores its session total from the session file's+``cost-state`` row, so a resumed turn's ``ResultMessage.total_cost_usd`` also+counts every earlier turn of the chat.+"""++import contextlib+import json+import logging+import os+from datetime import UTC, datetime+from unittest.mock import AsyncMock, MagicMock, patch++import pytest+from claude_agent_sdk import AssistantMessage, ResultMessage, TextBlock++from backend.copilot.model import ChatMessage, ChatSession+from backend.copilot.moonshot import rate_card_usd+from backend.copilot.sdk.cost_tracking import read_cli_session_usage+from backend.copilot.sdk.service import (+ _record_result_usage,+ _RetryState,+ _TokenUsage,+ stream_chat_completion_sdk,+)+from backend.copilot.transcript import cli_session_path++from .conftest import build_test_transcript+from .retry_scenarios_test import _make_sdk_patches++_SVC = "backend.copilot.sdk.service"+_SESSION_ID = "5f0c2a4e-1b7d-4c9a-9e3f-2d8b6a1c4e70"+# What ``_make_sdk_patches`` pins ``_make_sdk_cwd`` to.+_SDK_CWD = "/tmp/test-sdk-cwd"+_CALL_USAGE = {+ "input_tokens": 1000,+ "output_tokens": 100,+ "cache_read_input_tokens": 100_000,+ "cache_creation_input_tokens": 2000,+}+# _CALL_USAGE at the CLI's Sonnet rates, as the locked CLI reports it.+_CALL_COST = 0.028+_KIMI = "moonshotai/kimi-k2.6"+++def _kimi_rate_card_cost() -> float:+ rates = rate_card_usd(_KIMI)+ assert rates is not None+ prompt = (+ _CALL_USAGE["input_tokens"]+ + _CALL_USAGE["cache_read_input_tokens"]+ + _CALL_USAGE["cache_creation_input_tokens"]+ )+ return (prompt * rates[0] + _CALL_USAGE["output_tokens"] * rates[1]) / 1e6+++ "model,message_id,sink,expected",+ [+ pytest.param(+ "claude-sonnet-4-6", None, "persist", _CALL_COST, id="cli-cost-recorded"+ ),+ pytest.param(+ "claude-sonnet-4-6",+ "gen-1",+ "reconcile",+ _CALL_COST,+ id="cli-cost-as-openrouter-fallback",+ ),+ pytest.param(+ _KIMI, None, "persist", _kimi_rate_card_cost(), id="moonshot-rate-card"+ ),+ ],+)+async def test_resumed_turn_is_charged_its_own_cost(+ model, message_id, sink, expected, tmp_path, monkeypatch+):+ # The session file the turn resumes says turn 1 cost _CALL_COST, so the+ # CLI reports this identical call as twice that.+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path))+ os.makedirs(os.path.dirname(cli_session_path(_SDK_CWD, _SESSION_ID)))+ transcript = build_test_transcript(+ [("user", "prior question"), ("assistant", "prior answer")]+ )+ cost_state = {+ "type": "cost-state",+ "sessionId": _SESSION_ID,+ "totalCostUSD": _CALL_COST,+ }+ transcript = transcript.rstrip("\n") + "\n" + json.dumps(cost_state) + "\n"+ messages = [+ AssistantMessage(+ content=[TextBlock(text="ok")], model=model, message_id=message_id+ ),+ ResultMessage(+ subtype="success",+ duration_ms=100,+ duration_api_ms=100,+ is_error=False,+ num_turns=1,+ session_id=_SESSION_ID,+ total_cost_usd=2 * _CALL_COST,+ usage=dict(_CALL_USAGE),+ result="ok",+ ),+ ]+ launched = []++ def _client_factory(*args, **kwargs):+ launched.append(kwargs["options"])++ async def _receive():+ for message in messages:+ yield message++ client = MagicMock()+ client.query = AsyncMock()+ client.receive_response = _receive+ cm = AsyncMock()+ cm.__aenter__.return_value = client+ cm.__aexit__.return_value = None+ return cm++ session = ChatSession(+ session_id=_SESSION_ID,+ user_id="test-user",+ usage=[],+ started_at=datetime.now(UTC),+ updated_at=datetime.now(UTC),+ messages=[+ ChatMessage(role="user", content="prior question"),+ ChatMessage(role="assistant", content="prior answer"),+ ChatMessage(role="user", content="hello"),+ ],+ )+ patches = _make_sdk_patches(+ session,+ original_transcript=transcript,+ compacted_transcript=None,+ client_side_effect=_client_factory,+ )+ with contextlib.ExitStack() as stack:+ for target, kwargs in patches:+ stack.enter_context(patch(target, **kwargs))+ # A resumed turn diffs the user's skills over a DB RPC.+ stack.enter_context(+ patch(+ f"{_SVC}.build_skills_update_notice",+ new_callable=AsyncMock,+ return_value=None,+ )+ )+ persist = stack.enter_context(+ patch(f"{_SVC}.persist_and_record_usage", new_callable=AsyncMock)+ )+ reconcile = stack.enter_context(+ patch(f"{_SVC}.record_turn_cost_from_openrouter", new_callable=AsyncMock)+ )+ async for _ in stream_chat_completion_sdk(+ session_id=_SESSION_ID,+ message="hello",+ is_user_message=True,+ user_id="test-user",+ session=session,+ ):+ pass++ assert [options.resume for options in launched] == [_SESSION_ID]+ if sink == "persist":+ reconcile.assert_not_called()+ persist.assert_awaited_once()+ assert persist.await_args.kwargs["cost_usd"] == pytest.approx(expected)+ else:+ persist.assert_not_awaited()+ reconcile.assert_called_once()+ cost = reconcile.call_args.kwargs["fallback_cost_usd"]+ assert cost == pytest.approx(expected)+++def test_every_query_of_one_cli_process_is_charged():+ # A re-prompt is a second query to the same CLI process, whose total+ # then counts both queries.+ state = _retry_state()+ for running_total in (_CALL_COST, 2 * _CALL_COST):+ _record_result_usage(_result(running_total), state, "")++ assert state.usage.cost_usd == pytest.approx(2 * _CALL_COST)+++ "models", [(_KIMI, "claude-sonnet-4-6"), ("claude-sonnet-4-6", _KIMI)]+)+def test_model_switch_preserves_the_price_of_each_result(models):+ state = _retry_state()+ for index, model in enumerate(models, start=1):+ state.observed_model = model+ _record_result_usage(_result(index * _CALL_COST), state, "")++ assert state.usage.cost_usd == pytest.approx(_CALL_COST + _kimi_rate_card_cost())+++def test_reset_cli_total_does_not_erase_already_billed_spend(caplog):+ state = _retry_state()+ state.usage.cli_session_total_usd = 2 * _CALL_COST+ with caplog.at_level(logging.ERROR):+ for running_total in (3 * _CALL_COST, _CALL_COST, 2 * _CALL_COST):+ _record_result_usage(_result(running_total), state, "")++ assert state.usage.cost_usd == pytest.approx(3 * _CALL_COST)+ assert [r.levelno for r in caplog.records] == [logging.ERROR]+++def test_a_total_below_the_baseline_is_charged_in_full_and_alerts(caplog):+ state = _retry_state()+ state.usage.cli_session_total_usd = 2 * _CALL_COST+ with caplog.at_level(logging.ERROR, logger="backend.copilot.sdk.cost_tracking"):+ _record_result_usage(_result(_CALL_COST), state, "")++ assert state.usage.cost_usd == pytest.approx(_CALL_COST)+ assert [r.levelno for r in caplog.records] == [logging.ERROR]+++def test_an_unreadable_session_file_is_charged_in_full_and_alerts(+ tmp_path, monkeypatch, caplog+):+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path)) # holds no session file+ state = _retry_state()+ with caplog.at_level(logging.ERROR, logger="backend.copilot.sdk.cost_tracking"):+ state.usage.start_cli_session(read_cli_session_usage(_SDK_CWD, _SESSION_ID, ""))+ _record_result_usage(_result(2 * _CALL_COST), state, "")++ assert state.usage.cost_usd == pytest.approx(2 * _CALL_COST)+ assert [r.levelno for r in caplog.records] == [logging.ERROR]+++def _retry_state() -> _RetryState:+ return _RetryState(+ options=MagicMock(model="claude-sonnet-4-6"),+ query_message="",+ compaction_stats=None,+ use_resume=False,+ resume_file=None,+ transcript_msg_count=0,+ adapter=MagicMock(),+ transcript_builder=MagicMock(),+ usage=_TokenUsage(),+ )+++def _result(total_cost_usd: float) -> ResultMessage:+ return ResultMessage(+ subtype="success",+ duration_ms=100,+ duration_api_ms=100,+ is_error=False,+ num_turns=1,+ session_id=_SESSION_ID,+ total_cost_usd=total_cost_usd,+ usage=dict(_CALL_USAGE),+ )diff --git a/autogpt_platform/backend/backend/copilot/transcript.py b/autogpt_platform/backend/backend/copilot/transcript.pyindex 11fc01d7af..936f8723cb 100644--- a/autogpt_platform/backend/backend/copilot/transcript.py+++ b/autogpt_platform/backend/backend/copilot/transcript.py@@ -20,7 +20,7 @@ import shutilimport timefrom dataclasses import dataclassfrom pathlib import Path-from typing import Literal+from typing import Any, Literalfrom uuid import uuid4from backend.data.db_accessors import chat_db@@ -656,6 +656,32 @@ def cli_session_path(sdk_cwd: str, session_id: str) -> str:return os.path.join(projects_base(), encoded_cwd, f"{safe_id}.jsonl")+def cli_session_cost_usd(content: str, session_id: str) -> float:+ """The running cost the CLI restores on ``--resume`` from this session file.++ The CLI resumes from the last ``cost-state`` row whose ``sessionId`` matches,+ so its ``total_cost_usd`` counts on from this figure (0 with no such row).+ """+ entry = cli_session_cost_state(content, session_id)+ return float(entry["totalCostUSD"]) if entry else 0.0+++def cli_session_cost_state(content: str, session_id: str) -> dict[str, Any] | None:+ """Return the last matching native CLI cost snapshot, including model usage."""+ for line in reversed(content.splitlines()):+ if "cost-state" not in line:+ continue+ entry = json.loads(line, fallback=None)+ if (+ isinstance(entry, dict)+ and entry.get("type") == "cost-state"+ and entry.get("sessionId") == session_id+ and isinstance(entry.get("totalCostUSD"), (int, float))+ ):+ return entry+ return None++def _cli_session_storage_path_parts(user_id: str, session_id: str) -> tuple[str, str, str]:diff --git a/autogpt_platform/backend/backend/copilot/transcript_test.py b/autogpt_platform/backend/backend/copilot/transcript_test.pyindex b46c4b9c41..4bc75ceea7 100644--- a/autogpt_platform/backend/backend/copilot/transcript_test.py+++ b/autogpt_platform/backend/backend/copilot/transcript_test.py@@ -21,6 +21,7 @@ from .transcript import (_rechain_tail,_sanitize_id,_transcript_to_messages,+ cli_session_cost_usd,detect_gap,extract_context_messages,strip_for_upload,@@ -1867,3 +1868,38 @@ class TestExtractContextMessages:"user", # gap user-6"assistant", # gap assistant-7]+++# ---------------------------------------------------------------------------+# cli_session_cost_usd+# ---------------------------------------------------------------------------++_SID = "5f0c2a4e-1b7d-4c9a-9e3f-2d8b6a1c4e70"+++ "lines,expected",+ [+ pytest.param(+ [+ {"type": "cost-state", "sessionId": _SID, "totalCostUSD": 0.028},+ "{not json cost-state",+ {"type": "cost-state", "sessionId": _SID, "totalCostUSD": 0.056},+ {"type": "cost-state", "sessionId": "another", "totalCostUSD": 5.0},+ {"type": "user", "message": {"content": "what is cost-state?"}},+ ],+ 0.056,+ id="last-row-of-this-session",+ ),+ pytest.param(+ [{"type": "user", "message": {"content": "hi"}}],+ 0.0,+ id="no-cost-state",+ ),+ ],+)+def test_cli_session_cost_usd_reads_what_the_cli_resumes_from(lines, expected):+ content = "\n".join(+ line if isinstance(line, str) else json.dumps(line) for line in lines+ )+ assert cli_session_cost_usd(content, _SID) == expected