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(platform): credit history and the cost log name the AutoPilot chat, not a made-up graph execution (#14878)
Reinier van der Leer21ca870d7f20
diff --git a/autogpt_platform/backend/backend/api/features/chat/speech_test.py b/autogpt_platform/backend/backend/api/features/chat/speech_test.pyindex c64825b23c..8095bb0168 100644--- a/autogpt_platform/backend/backend/api/features/chat/speech_test.py+++ b/autogpt_platform/backend/backend/api/features/chat/speech_test.py@@ -39,7 +39,7 @@ def test_speech_meters_the_synthesis_against_the_users_plan(assert kwargs["user_id"] == test_user_idassert kwargs["provider"] == "openai"assert kwargs["block_name_override"] == speech_module.TTS_BLOCK_NAME- assert kwargs["graph_exec_id_override"] == "sess-1"+ assert kwargs["chat_session_id_override"] == "sess-1"assert kwargs["cost_usd"] == speech_module.speech_cost_usd(len("Two sentences here."))diff --git a/autogpt_platform/backend/backend/copilot/speech.py b/autogpt_platform/backend/backend/copilot/speech.pyindex c0d6af675f..973d358733 100644--- a/autogpt_platform/backend/backend/copilot/speech.py+++ b/autogpt_platform/backend/backend/copilot/speech.py@@ -116,7 +116,7 @@ async def _meter_speech(model=config.voice_tts_model,provider="openai",block_name_override=TTS_BLOCK_NAME,- graph_exec_id_override=session_id,+ chat_session_id_override=session_id,extra_metadata={"surface": "voice_mode","characters": characters,diff --git a/autogpt_platform/backend/backend/copilot/token_tracking.py b/autogpt_platform/backend/backend/copilot/token_tracking.pyindex 0930e5a1c1..e34d65dff4 100644--- a/autogpt_platform/backend/backend/copilot/token_tracking.py+++ b/autogpt_platform/backend/backend/copilot/token_tracking.py@@ -125,6 +125,7 @@ async def persist_and_record_usage(block_name_override: str | None = None,extra_metadata: dict | None = None,graph_exec_id_override: str | None = None,+ chat_session_id_override: str | None = None,credential_id_override: str | None = None,skip_daily: bool = False,execution_path: str = "sync",@@ -247,10 +248,10 @@ async def persist_and_record_usage(# (e.g. fully-cached Anthropic responses where only cache tokens# accumulate a charge without incrementing total_tokens).if user_id and (total_tokens > 0 or cost_float is not None):- session_id = (- graph_exec_id_override- if graph_exec_id_override is not None- else (session.session_id if session else None)+ # A dream pass logs under its own id as graph_exec_id; only a chat's+ # usage names the chat.+ chat_session_id = chat_session_id_override or (+ session.session_id if session and graph_exec_id_override is None else None)if cost_float is not None:@@ -286,7 +287,8 @@ async def persist_and_record_usage(_schedule_cost_log(PlatformCostEntry(user_id=user_id,- graph_exec_id=session_id,+ graph_exec_id=graph_exec_id_override,+ chat_session_id=chat_session_id,block_id=COPILOT_BLOCK_ID,block_name=(block_name_overridediff --git a/autogpt_platform/backend/backend/copilot/token_tracking_test.py b/autogpt_platform/backend/backend/copilot/token_tracking_test.pyindex aa1c7374ea..3cc8ccd201 100644--- a/autogpt_platform/backend/backend/copilot/token_tracking_test.py+++ b/autogpt_platform/backend/backend/copilot/token_tracking_test.py@@ -373,7 +373,45 @@ class TestPlatformCostLogging:assert entry.metadata["tracking_type"] == "cost_usd"assert entry.metadata["tracking_amount"] == 0.005assert entry.block_name == "copilot:SDK"- assert entry.graph_exec_id == "sess-test"+ assert entry.chat_session_id == "sess-test"+ assert entry.graph_exec_id is None++ @pytest.mark.parametrize(+ "overrides,expected",+ [+ ({"graph_exec_id_override": "pass-1"}, ("pass-1", None)),+ ({"chat_session_id_override": "chat-9"}, (None, "chat-9")),+ ],+ ids=["dream-pass", "voice"],+ )+ @pytest.mark.asyncio+ async def test_a_dream_pass_is_logged_under_its_own_id_not_as_a_chat(+ self, overrides, expected+ ):+ mock_log = AsyncMock()+ with (+ patch(+ "backend.copilot.token_tracking.record_cost_usage",+ new_callable=AsyncMock,+ ),+ patch(+ "backend.copilot.token_tracking.platform_cost_db",+ return_value=type(+ "FakePlatformCostDb", (), {"log_platform_cost": mock_log}+ )(),+ ),+ ):+ await persist_and_record_usage(+ session=None,+ user_id="user-cost",+ prompt_tokens=10,+ completion_tokens=5,+ cost_usd=0.001,+ **overrides,+ )+ await asyncio.sleep(0)+ entry = mock_log.call_args[0][0]+ assert (entry.graph_exec_id, entry.chat_session_id) == expected@pytest.mark.asyncioasync def test_logs_cost_entry_without_cost_usd(self):diff --git a/autogpt_platform/backend/backend/copilot/tools/helpers.py b/autogpt_platform/backend/backend/copilot/tools/helpers.pyindex ae53842a87..3ce46a99c8 100644--- a/autogpt_platform/backend/backend/copilot/tools/helpers.py+++ b/autogpt_platform/backend/backend/copilot/tools/helpers.py@@ -161,8 +161,7 @@ async def _charge_block_credits(node_exec_id: str,cost: int,cost_filter: dict[str, Any],- synthetic_graph_id: str,- synthetic_node_id: str,+ session_id: str,expert_id: str | None = None,) -> None:"""Charge credits for a block execution and log any billing leak.@@ -177,9 +176,7 @@ async def _charge_block_credits(user_id=user_id,cost=cost,metadata=UsageTransactionMetadata(- graph_exec_id=synthetic_graph_id,- graph_id=synthetic_graph_id,- node_id=synthetic_node_id,+ chat_session_id=session_id,node_exec_id=node_exec_id,block_id=block_id,block=block_name,@@ -522,8 +519,7 @@ async def execute_block(node_exec_id=node_exec_id,cost=cost,cost_filter=cost_filter,- synthetic_graph_id=synthetic_graph_id,- synthetic_node_id=synthetic_node_id,+ session_id=session_id,expert_id=await metered_expert_id(user_id, expert_id),))@@ -582,8 +578,7 @@ async def execute_block(node_exec_id=node_exec_id,cost=cost,cost_filter=cost_filter,- synthetic_graph_id=synthetic_graph_id,- synthetic_node_id=synthetic_node_id,+ session_id=session_id,expert_id=await metered_expert_id(user_id, expert_id),))diff --git a/autogpt_platform/backend/backend/data/credit.py b/autogpt_platform/backend/backend/data/credit.pyindex c3b34cf307..fe956ce3e9 100644--- a/autogpt_platform/backend/backend/data/credit.py+++ b/autogpt_platform/backend/backend/data/credit.py@@ -76,6 +76,8 @@ BillingCycle = Literal["monthly", "yearly"]class UsageTransactionMetadata(BaseModel):graph_exec_id: str | None = Nonegraph_id: str | None = None+ # Set instead of the graph fields when the usage came from an AutoPilot chat.+ chat_session_id: str | None = Nonenode_id: str | None = Nonenode_exec_id: str | None = Noneblock_id: str | None = None@@ -801,13 +803,16 @@ class UserCredit(UserCreditBase):# Auto top-up if balance is below threshold.auto_top_up = await get_auto_top_up(user_id)- if auto_top_up.threshold and balance < auto_top_up.threshold:+ if (+ auto_top_up.threshold+ and balance < auto_top_up.threshold+ and not await _legacy_chat_top_up_exists(user_id, metadata)+ ):try:await self._top_up_credits(user_id=user_id,amount=auto_top_up.amount,- # Avoid multiple auto top-ups within the same graph execution.- key=f"AUTO-TOP-UP-{user_id}-{metadata.graph_exec_id}",+ key=_auto_top_up_key(user_id, metadata),ceiling_balance=auto_top_up.threshold,top_up_type=TopUpType.AUTO,)@@ -1400,6 +1405,28 @@ class UserCredit(UserCreditBase):]+def _auto_top_up_key(user_id: str, metadata: UsageTransactionMetadata) -> str:+ """One auto top-up per graph execution or chat. A top-up stays inactive+ until its charge succeeds, so the key is what stops a repeat charge."""+ return f"AUTO-TOP-UP-{user_id}-{metadata.graph_exec_id or metadata.chat_session_id}"+++async def _legacy_chat_top_up_exists(+ user_id: str, metadata: UsageTransactionMetadata+) -> bool:+ """A chat's auto top-up keyed before chats had their own id (only a failed+ or still-pending one keeps its key; a charged one takes the payment's)."""+ if not metadata.chat_session_id:+ return False+ legacy_key = f"AUTO-TOP-UP-{user_id}-copilot-session-{metadata.chat_session_id}"+ return (+ await CreditTransaction.prisma().find_first(+ where={"transactionKey": legacy_key, "userId": user_id}+ )+ is not None+ )++class DisabledUserCredit(UserCreditBase):async def get_credits(self, *args, **kwargs) -> int:return 100diff --git a/autogpt_platform/backend/backend/data/credit_auto_top_up_test.py b/autogpt_platform/backend/backend/data/credit_auto_top_up_test.pynew file mode 100644index 0000000000..e8e2156211--- /dev/null+++ b/autogpt_platform/backend/backend/data/credit_auto_top_up_test.py@@ -0,0 +1,85 @@+from unittest.mock import AsyncMock, MagicMock+from uuid import uuid4++import pytest+from prisma.enums import CreditTransactionType+from prisma.models import CreditTransaction, User++from backend.data import credit+from backend.data.credit import UsageTransactionMetadata, UserCredit+++def top_ups(mocker):+ mocker.patch.object(+ credit,+ "get_auto_top_up",+ AsyncMock(return_value=MagicMock(threshold=100, amount=500)),+ )+ mocker.patch.object(+ UserCredit, "_add_transaction", AsyncMock(return_value=(10, "key"))+ )+ top_up = AsyncMock()+ mocker.patch.object(UserCredit, "_top_up_credits", top_up)+ return top_up+++async def _spend(metadata: UsageTransactionMetadata) -> None:+ await UserCredit().spend_credits(user_id="u1", cost=5, metadata=metadata)+++async def test_each_chat_gets_its_own_auto_top_up(top_ups):+ await _spend(UsageTransactionMetadata(chat_session_id="chat-1"))+ await _spend(UsageTransactionMetadata(chat_session_id="chat-2"))+ await _spend(UsageTransactionMetadata(graph_exec_id="run-1", graph_id="g"))++ keys = [call.kwargs["key"] for call in top_ups.await_args_list]+ assert keys == [+ "AUTO-TOP-UP-u1-chat-1",+ "AUTO-TOP-UP-u1-chat-2",+ "AUTO-TOP-UP-u1-run-1",+ ]+++async def test_usage_outside_a_run_or_chat_still_has_a_key(top_ups):+ # Without one, every spend below the threshold could start another charge+ # while the first top-up is still inactive.+ await _spend(UsageTransactionMetadata(reason="CoPilot daily rate limit reset"))++ assert top_ups.await_args.kwargs["key"] == "AUTO-TOP-UP-u1-None"++[email protected](loop_scope="session")+async def test_a_chat_with_an_uncharged_legacy_top_up_is_not_charged_again(+ server, top_ups+):+ # Before chats had their own id, a chat's top-up was keyed on+ # copilot-session-<id>; only a failed or pending one keeps that key.+ user_id = f"top-up-{uuid4()}"+ await User.prisma().create(data={"id": user_id, "email": f"{user_id}@example.com"})+ try:+ await CreditTransaction.prisma().create(+ data={+ "userId": user_id,+ "transactionKey": f"AUTO-TOP-UP-{user_id}-copilot-session-chat-1",+ "amount": 500,+ "type": CreditTransactionType.TOP_UP,+ "isActive": False,+ }+ )+ spend = UserCredit().spend_credits++ await spend(user_id=user_id, cost=5, metadata=_chat("chat-1"))+ top_ups.assert_not_awaited()++ await spend(user_id=user_id, cost=5, metadata=_chat("chat-2"))+ assert top_ups.await_args.kwargs["key"] == f"AUTO-TOP-UP-{user_id}-chat-2"+ finally:+ await CreditTransaction.prisma().delete_many(where={"userId": user_id})+ await User.prisma().delete_many(where={"id": user_id})+++def _chat(chat_session_id: str) -> UsageTransactionMetadata:+ return UsageTransactionMetadata(chat_session_id=chat_session_id)diff --git a/autogpt_platform/backend/backend/data/credit_history/enrichment.py b/autogpt_platform/backend/backend/data/credit_history/enrichment.pyindex 4442d42ebd..d06c50cd08 100644--- a/autogpt_platform/backend/backend/data/credit_history/enrichment.py+++ b/autogpt_platform/backend/backend/data/credit_history/enrichment.py@@ -16,7 +16,6 @@ from prisma.types import ()from pydantic import BaseModel-from backend.copilot.constants import COPILOT_SESSION_PREFIXfrom backend.data.model import CreditHistoryRelatedExecution, CreditTransactionItemfrom backend.data.tenancy import get_user_team_ids, visibility_filter@@ -32,7 +31,10 @@ async def enrich_credit_history(user_id: str,organization_id: str | None = None,) -> list[CreditTransactionItem]:- if not any(item.usage_execution_id or item.usage_graph_id for item in items):+ if not any(+ item.usage_execution_id or item.usage_graph_id or item.usage_chat_session_id+ for item in items+ ):return itemsteam_ids = (await get_user_team_ids(user_id, organization_id) if organization_id else []@@ -58,14 +60,7 @@ async def _load_executions(user_id: str,scope: AgentGraphExecutionWhereInput,) -> tuple[list[AgentGraphExecution], dict[str, int]]:- ids = sorted(- {- item.usage_execution_id- for item in items- if item.usage_execution_id- and not item.usage_execution_id.startswith(COPILOT_SESSION_PREFIX)- }- )+ ids = sorted({item.usage_execution_id for item in items if item.usage_execution_id})if not ids:return [], {}# Match execution detail's expert guard as well as its org/team predicate.@@ -123,12 +118,7 @@ async def _load_sessions(items: list[CreditTransactionItem], user_id: str, organization_id: str | None) -> dict[str, ChatSession]:ids = sorted(- {- item.usage_execution_id.removeprefix(COPILOT_SESSION_PREFIX)- for item in items- if item.usage_execution_id- and item.usage_execution_id.startswith(COPILOT_SESSION_PREFIX)- }+ {item.usage_chat_session_id for item in items if item.usage_chat_session_id})if not ids:return {}@@ -156,12 +146,7 @@ async def _load_agent_refs() -> tuple[dict[tuple[str, int], _AgentRef], dict[str, _AgentRef]]:graph_ids = sorted({e.agentGraphId for e in executions}- | {- item.usage_graph_id- for item in items- if item.usage_graph_id- and not (item.usage_execution_id or "").startswith(COPILOT_SESSION_PREFIX)- }+ | {item.usage_graph_id for item in items if item.usage_graph_id})if not graph_ids:return {}, {}@@ -276,13 +261,12 @@ def _enrich_item(child_counts: dict[str, int],) -> CreditTransactionItem:item = original.model_copy(deep=True)- execution_id = item.usage_execution_id or ""- if execution_id.startswith(COPILOT_SESSION_PREFIX):- session = sessions.get(execution_id.removeprefix(COPILOT_SESSION_PREFIX))+ if item.usage_chat_session_id:+ session = sessions.get(item.usage_chat_session_id)item.conversation_id = session.id if session else Noneitem.conversation_title = session.title if session else Nonereturn item- execution = executions.get(execution_id)+ execution = executions.get(item.usage_execution_id or "")ref = (refs.get((execution.agentGraphId, execution.agentGraphVersion))if executiondiff --git a/autogpt_platform/backend/backend/data/credit_history/enrichment_test.py b/autogpt_platform/backend/backend/data/credit_history/enrichment_test.pyindex 8be2837709..a5a641bea4 100644--- a/autogpt_platform/backend/backend/data/credit_history/enrichment_test.py+++ b/autogpt_platform/backend/backend/data/credit_history/enrichment_test.py@@ -53,11 +53,12 @@ def _library(graph=None, *, title=None, archived=False, library_id="library"):return library-def _item(execution_id="run", graph_id="graph"):+def _item(execution_id="run", graph_id="graph", session_id=None):return CreditTransactionItem(user_id="user",usage_graph_id=graph_id,usage_execution_id=execution_id,+ usage_chat_session_id=session_id,amount=-12,)@@ -246,7 +247,12 @@ async def test_copilot_links_only_existing_owned_conversations(mocker):session = MagicMock(spec=ChatSession, id="chat", title="Research leads")actions = _database(mocker, sessions=[session])existing, unavailable = await history.enrich_credit_history(- [_item("copilot-session-chat"), _item("copilot-session-missing")], "user", "org"+ [+ _item(None, None, session_id="chat"),+ _item(None, None, session_id="missing"),+ ],+ "user",+ "org",)assert existing.conversation_id == "chat"assert existing.conversation_title == "Research leads"diff --git a/autogpt_platform/backend/backend/data/credit_history/history_test.py b/autogpt_platform/backend/backend/data/credit_history/history_test.pyindex e9cda4a877..4b8c317aa7 100644--- a/autogpt_platform/backend/backend/data/credit_history/history_test.py+++ b/autogpt_platform/backend/backend/data/credit_history/history_test.py@@ -1,5 +1,5 @@from datetime import datetime, timedelta, timezone-from unittest.mock import AsyncMock+from unittest.mock import AsyncMock, MagicMockfrom uuid import uuid4import pytest@@ -7,6 +7,7 @@ import pytest_asynciofrom prisma.enums import CreditTransactionTypefrom prisma.models import CreditTransaction, Organization, OrgCreditTransaction, User+from backend.copilot.tools.helpers import _charge_block_creditsfrom backend.data.credit_history import get_credit_historyfrom backend.data.credit_history.queries import credit_history_queryfrom backend.data.db import get_database_schema, prisma@@ -139,6 +140,56 @@ async def test_copilot_and_orphan_usage_are_distinct(history_wallet):"block_usage","copilot_tools",]+ # A row written before chat usage had its own field still names the chat.+ [chat] = [r for r in page.transactions if r.activity_type == "copilot_tools"]+ assert (chat.usage_chat_session_id, chat.usage_execution_id) == ("chat-1", None)+ assert chat.usage_graph_id is None++[email protected](loop_scope="session")+async def test_a_chat_block_charge_is_attributed_to_its_chat(history_wallet):+ spend = AsyncMock()+ await _charge_block_credits(+ MagicMock(spend_credits=spend),+ user_id=history_wallet,+ block_name="Create Folder",+ block_id="blk",+ node_exec_id="copilot-node-blk:ab12",+ cost=7,+ cost_filter={},+ session_id="chat-9",+ )+ metadata = spend.await_args.kwargs["metadata"]+ assert (metadata.chat_session_id, metadata.graph_exec_id, metadata.graph_id) == (+ "chat-9",+ None,+ None,+ )+ time = datetime.now(timezone.utc) - timedelta(minutes=5)+ for key in ("first", "second"):+ await CreditTransaction.prisma().create(+ data={+ "userId": history_wallet,+ "transactionKey": key,+ "createdAt": time,+ "amount": -7,+ "type": CreditTransactionType.USAGE,+ "metadata": SafeJson(metadata.model_dump(exclude_none=True)),+ }+ )+ await add_charge(history_wallet, "run-charge", -3, time, "run-1")++ page = await get_credit_history(history_wallet)++ by_type = {row.activity_type: row for row in page.transactions}+ assert set(by_type) == {"copilot_tools", "agent_run"}+ chat = by_type["copilot_tools"]+ assert chat.amount == -14 and chat.transaction_key == "chat:chat-9"+ assert (chat.usage_chat_session_id, chat.usage_execution_id) == ("chat-9", None)+ assert chat.usage_graph_id is None+ run = by_type["agent_run"]+ assert (run.usage_execution_id, run.usage_chat_session_id) == ("run-1", None)+ assert run.usage_graph_id == "graph-1"@pytest.mark.asyncio(loop_scope="session")diff --git a/autogpt_platform/backend/backend/data/credit_history/queries.py b/autogpt_platform/backend/backend/data/credit_history/queries.pyindex 37cf284fc7..c0797bf040 100644--- a/autogpt_platform/backend/backend/data/credit_history/queries.py+++ b/autogpt_platform/backend/backend/data/credit_history/queries.py@@ -6,17 +6,30 @@ WITH wallet AS (WHERE __OWNER__ = $1 AND "isActive" = TRUEAND "createdAt" <= ($2::timestamptz AT TIME ZONE 'UTC')AND ($3::text IS NULL OR type::text = $3::text)+), scoped AS (+ -- Chat usage names its session; rows written before that carried it as a+ -- "copilot-session-<id>" graph execution id.+ SELECT *,+ CASE WHEN metadata->>'graph_exec_id' LIKE 'copilot-session-%' THEN NULL+ ELSE NULLIF(metadata->>'graph_exec_id', '') END AS execution_id,+ COALESCE(+ NULLIF(metadata->>'chat_session_id', ''),+ CASE WHEN metadata->>'graph_exec_id' LIKE 'copilot-session-%'+ THEN substr(metadata->>'graph_exec_id', length('copilot-session-') + 1)+ END) AS chat_session_id+ FROM wallet), classified AS MATERIALIZED (SELECT *,- CASE WHEN transaction_type = 'USAGE'- AND NULLIF(metadata->>'graph_exec_id', '') IS NOT NULL- THEN 'execution:' || (metadata->>'graph_exec_id')+ CASE WHEN transaction_type = 'USAGE' AND execution_id IS NOT NULL+ THEN 'execution:' || execution_id+ WHEN transaction_type = 'USAGE' AND chat_session_id IS NOT NULL+ THEN 'chat:' || chat_session_idELSE 'transaction:' || "transactionKey" END AS group_id,CASE WHEN transaction_type != 'USAGE' THEN 'transaction'WHEN metadata->'input' ? 'reconciled_delta' THEN 'adjustment'WHEN metadata->'input'->>'charge' = 'Execution Cost' THEN 'execution_fee'ELSE 'usage' END AS charge_type- FROM wallet+ FROM scoped), heads AS (SELECT group_id AS id, MAX("createdAt") AS transaction_timeFROM classified@@ -34,16 +47,18 @@ WITH wallet AS (FROM classified INNER JOIN page ON page.id = classified.group_id), grouped AS (SELECT group_id AS id,- CASE WHEN group_id LIKE 'execution:%' THEN group_id- ELSE MAX("transactionKey") END AS transaction_key,+ CASE WHEN group_id LIKE 'transaction:%' THEN MAX("transactionKey")+ ELSE group_id END AS transaction_key,MAX("createdAt") AS transaction_time,MIN("createdAt") AS usage_start_time,MAX(transaction_type) AS transaction_type,SUM(amount)::bigint AS amount,- MAX(CASE WHEN transaction_type = 'USAGE'+ MAX(CASE WHEN transaction_type = 'USAGE' AND chat_session_id IS NULLTHEN NULLIF(metadata->>'graph_id', '') END) AS usage_graph_id,MAX(CASE WHEN transaction_type = 'USAGE'- THEN NULLIF(metadata->>'graph_exec_id', '') END) AS usage_execution_id,+ THEN execution_id END) AS usage_execution_id,+ MAX(CASE WHEN transaction_type = 'USAGE'+ THEN chat_session_id END) AS usage_chat_session_id,COUNT(DISTINCT CASE WHEN transaction_type = 'USAGE'THEN NULLIF(metadata->>'node_exec_id', '') END)::intAS usage_node_count,diff --git a/autogpt_platform/backend/backend/data/credit_history/service.py b/autogpt_platform/backend/backend/data/credit_history/service.pyindex bf2f06a5b8..88a50457c5 100644--- a/autogpt_platform/backend/backend/data/credit_history/service.py+++ b/autogpt_platform/backend/backend/data/credit_history/service.py@@ -113,9 +113,7 @@ def _to_item(row: _HistoryRow, user_id: str) -> CreditTransactionItem:)description = _TRANSACTION_DESCRIPTIONS[row.transaction_type]if row.transaction_type == CreditTransactionType.USAGE:- if row.usage_execution_id and row.usage_execution_id.startswith(- "copilot-session-"- ):+ if row.usage_chat_session_id:activity_type, description = "copilot_tools", "Expert tool use"elif row.usage_execution_id:activity_type, description = "agent_run", "Agent run"diff --git a/autogpt_platform/backend/backend/data/credit_history/service_test.py b/autogpt_platform/backend/backend/data/credit_history/service_test.pyindex 3a8b1b785d..41c633cb4e 100644--- a/autogpt_platform/backend/backend/data/credit_history/service_test.py+++ b/autogpt_platform/backend/backend/data/credit_history/service_test.py@@ -16,7 +16,9 @@ from backend.data.credit_history.service import (from backend.data.model import CreditHistoryCharge, CreditTransactionItem-def history_row(key: str, execution_id: str | None = "run-1") -> _HistoryRow:+def history_row(+ key: str, execution_id: str | None = "run-1", session_id: str | None = None+) -> _HistoryRow:return _HistoryRow(id=key,transaction_key=key,@@ -24,6 +26,7 @@ def history_row(key: str, execution_id: str | None = "run-1") -> _HistoryRow:transaction_time=datetime(2026, 9, 4, tzinfo=timezone.utc),usage_start_time=datetime(2026, 9, 3, tzinfo=timezone.utc),usage_execution_id=execution_id,+ usage_chat_session_id=session_id,usage_graph_id="graph-1",usage_has_block=True,usage_charge_amount=-100,@@ -139,15 +142,17 @@ async def test_invalid_page_limit_is_rejected_before_query(limit):@pytest.mark.parametrize(- "execution_id,activity,description",+ "execution_id,session_id,activity,description",[- ("run-1", "agent_run", "Agent run"),- ("copilot-session-chat", "copilot_tools", "Expert tool use"),- (None, "block_usage", "Block usage"),+ ("run-1", None, "agent_run", "Agent run"),+ (None, "chat", "copilot_tools", "Expert tool use"),+ (None, None, "block_usage", "Block usage"),],)-def test_usage_types_are_not_mislabelled(execution_id, activity, description):- item = _to_item(history_row("id", execution_id), "user")+def test_usage_types_are_not_mislabelled(+ execution_id, session_id, activity, description+):+ item = _to_item(history_row("id", execution_id, session_id), "user")assert item.activity_type == activityassert item.description == descriptionassert (diff --git a/autogpt_platform/backend/backend/data/model.py b/autogpt_platform/backend/backend/data/model.pyindex ba2fa070ff..8a517f107a 100644--- a/autogpt_platform/backend/backend/data/model.py+++ b/autogpt_platform/backend/backend/data/model.py@@ -900,6 +900,9 @@ class CreditTransactionItem(BaseModel):description: str | None = Noneusage_graph_id: str | None = Noneusage_execution_id: str | None = None+ usage_chat_session_id: str | None = Field(+ default=None, description="The AutoPilot chat the usage came from"+ )usage_node_count: int = 0usage_start_time: datetime = datetime.max.replace(tzinfo=timezone.utc)user_id: strdiff --git a/autogpt_platform/backend/backend/data/platform_cost.py b/autogpt_platform/backend/backend/data/platform_cost.pyindex f8b25e338c..8b09af7afc 100644--- a/autogpt_platform/backend/backend/data/platform_cost.py+++ b/autogpt_platform/backend/backend/data/platform_cost.py@@ -35,6 +35,7 @@ def usd_to_microdollars(cost_usd: float | None) -> int | None:class PlatformCostEntry(BaseModel):user_id: strgraph_exec_id: str | None = None+ chat_session_id: str | None = Nonenode_exec_id: str | None = Nonegraph_id: str | None = Nonenode_id: str | None = None@@ -60,6 +61,7 @@ async def log_platform_cost(entry: PlatformCostEntry) -> None:data=PlatformCostLogCreateInput(userId=entry.user_id,graphExecId=entry.graph_exec_id,+ chatSessionId=entry.chat_session_id,nodeExecId=entry.node_exec_id,graphId=entry.graph_id,nodeId=entry.node_id,@@ -167,6 +169,7 @@ class CostLogRow(BaseModel):user_id: str | None = Noneemail: str | None = Nonegraph_exec_id: str | None = None+ chat_session_id: str | None = Nonenode_exec_id: str | None = Noneblock_name: strprovider: str@@ -858,7 +861,7 @@ async def get_platform_cost_logs(created_at=r.createdAt,user_id=r.userId,email=_mask_email(r.User.email if r.User else None),- graph_exec_id=r.graphExecId,+ **_run_or_chat(r),node_exec_id=r.nodeExecId,block_name=r.blockName or "",provider=r.provider,@@ -1079,7 +1082,7 @@ async def get_platform_cost_logs_for_export(created_at=r.createdAt,user_id=r.userId,email=_mask_email(r.User.email if r.User else None),- graph_exec_id=r.graphExecId,+ **_run_or_chat(r),node_exec_id=r.nodeExecId,block_name=r.blockName or "",provider=r.provider,@@ -1104,6 +1107,20 @@ async def get_platform_cost_logs_for_export(# ---------------------------------------------------------------------------+def _run_or_chat(row: Any) -> dict[str, str | None]:+ """A row's graph execution or chat. Chat rows written before+ ``chatSessionId`` existed carry the chat in ``graphExecId``."""+ if row.chatSessionId or row.blockId != _COPILOT_BLOCK_ID:+ return {"graph_exec_id": row.graphExecId, "chat_session_id": row.chatSessionId}+ if _md_str(row, "source") == "dream_pass":+ return {"graph_exec_id": row.graphExecId, "chat_session_id": None}+ return {"graph_exec_id": None, "chat_session_id": row.graphExecId}+++# The block id AutoPilot's own model usage is logged under (token_tracking).+_COPILOT_BLOCK_ID = "copilot"++def _build_where(start: datetime | None,end: datetime | None,diff --git a/autogpt_platform/backend/backend/data/platform_cost_test.py b/autogpt_platform/backend/backend/data/platform_cost_test.pyindex 714d9d74ca..a10dfed0f3 100644--- a/autogpt_platform/backend/backend/data/platform_cost_test.py+++ b/autogpt_platform/backend/backend/data/platform_cost_test.py@@ -15,6 +15,7 @@ from .platform_cost import (_build_where,_ids_matching_metadata_filter,_mask_email,+ _run_or_chat,get_platform_cost_dashboard,get_platform_cost_logs,get_platform_cost_logs_for_export,@@ -823,6 +824,7 @@ def _make_prisma_log_row(row.createdAt = datetime(2026, 3, 1, tzinfo=timezone.utc)row.userId = "u1"row.graphExecId = None+ row.chatSessionId = Nonerow.nodeExecId = Nonerow.blockName = "TestBlock"row.provider = "openai"@@ -1046,3 +1048,34 @@ class TestGetPlatformCostLogsForExport:assert logs == []assert truncated is Falseassert ids_mock.await_args.kwargs["start"] == start+++ "row,expected",+ [+ (+ {"blockId": "copilot", "graphExecId": None, "chatSessionId": "c1"},+ {"graph_exec_id": None, "chat_session_id": "c1"},+ ),+ (+ {"blockId": "copilot", "graphExecId": "c2", "chatSessionId": None},+ {"graph_exec_id": None, "chat_session_id": "c2"},+ ),+ (+ {+ "blockId": "copilot",+ "graphExecId": "pass-1",+ "chatSessionId": None,+ "metadata": {"source": "dream_pass"},+ },+ {"graph_exec_id": "pass-1", "chat_session_id": None},+ ),+ (+ {"blockId": "block-uuid", "graphExecId": "ge-1", "chatSessionId": None},+ {"graph_exec_id": "ge-1", "chat_session_id": None},+ ),+ ],+ ids=["chat", "chat-before-the-column", "dream-pass", "graph-run"],+)+def test_a_cost_row_names_its_run_or_its_chat(row, expected):+ assert _run_or_chat(MagicMock(**{"metadata": None, **row})) == expecteddiff --git a/autogpt_platform/backend/migrations/20260924120000_usage_names_the_chat_session/migration.sql b/autogpt_platform/backend/migrations/20260924120000_usage_names_the_chat_session/migration.sqlnew file mode 100644index 0000000000..388951242b--- /dev/null+++ b/autogpt_platform/backend/migrations/20260924120000_usage_names_the_chat_session/migration.sql@@ -0,0 +1,20 @@+-- AutoPilot chat usage was recorded under a synthetic graph execution+-- ("copilot-session-<session id>" in credit metadata, the bare session id in+-- the cost log); both now name the chat's session directly.++-- New rows only: the cost log holds a row per model call, so older chat rows+-- are read as chats by the admin view instead of being rewritten here. Nothing+-- filters on the column, so it has no index.+ALTER TABLE "PlatformCostLog" ADD COLUMN "chatSessionId" TEXT;++UPDATE "CreditTransaction"+SET metadata = (metadata - 'graph_exec_id' - 'graph_id'+ - CASE WHEN metadata->>'node_id' LIKE 'copilot-node-%' THEN 'node_id' ELSE '' END)+ || jsonb_build_object('chat_session_id', substr(metadata->>'graph_exec_id', length('copilot-session-') + 1))+WHERE metadata->>'graph_exec_id' LIKE 'copilot-session-%';++UPDATE "OrgCreditTransaction"+SET metadata = (metadata - 'graph_exec_id' - 'graph_id'+ - CASE WHEN metadata->>'node_id' LIKE 'copilot-node-%' THEN 'node_id' ELSE '' END)+ || jsonb_build_object('chat_session_id', substr(metadata->>'graph_exec_id', length('copilot-session-') + 1))+WHERE metadata->>'graph_exec_id' LIKE 'copilot-session-%';diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prismaindex 1222180a0a..97c7376fd5 100644--- a/autogpt_platform/backend/schema.prisma+++ b/autogpt_platform/backend/schema.prisma@@ -1906,6 +1906,8 @@ model PlatformCostLog {userId String?User User? @relation(fields: [userId], references: [id], onDelete: SetNull)graphExecId String?+ // The AutoPilot chat the cost came from, when it did not come from a graph run.+ chatSessionId String?nodeExecId String?graphId String?nodeId String?diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/__tests__/PlatformCostContent.test.tsx b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/__tests__/PlatformCostContent.test.tsxindex 8808f1280d..60665e7308 100644--- a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/__tests__/PlatformCostContent.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/__tests__/PlatformCostContent.test.tsx@@ -621,4 +621,33 @@ describe("PlatformCostContent", () => {// getAllByText to avoid the single-match constraint.expect(screen.getAllByText("-").length).toBeGreaterThan(0);});++ it("labels a chat's cost row as a chat, and a graph run's by its id", async () => {+ mockUseGetDashboard.mockReturnValue({+ data: emptyDashboard,+ isLoading: false,+ });+ mockUseGetLogs.mockReturnValue({+ data: {+ ...logsWithData,+ logs: [+ logsWithData.logs[0],+ {+ ...logsWithData.logs[0],+ id: "log-chat",+ graph_exec_id: null,+ chat_session_id: "5e551011-aaaa-bbbb-cccc-000000000000",+ block_name: "copilot:SDK",+ },+ ],+ },+ isLoading: false,+ });+ renderComponent({ tab: "logs" });+ await waitFor(() =>+ expect(document.querySelector(".animate-pulse")).toBeNull(),+ );+ expect(screen.getByText("Chat 5e551011")).toBeDefined();+ expect(screen.getByText("gx-123")).toBeDefined();+ });});diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/LogsTable.tsx b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/LogsTable.tsxindex 29eb9bda1b..69204ae365 100644--- a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/LogsTable.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/components/LogsTable.tsx@@ -131,28 +131,7 @@ function LogsTable({? formatDuration(Number(log.duration)): "-"}</td>- <td- className={[- "px-3 py-2 text-xs text-muted-foreground",- log.graph_exec_id ? "cursor-pointer" : "",- ].join(" ")}- title={- log.graph_exec_id ? String(log.graph_exec_id) : undefined- }- onClick={- log.graph_exec_id- ? () => {- navigator.clipboard- .writeText(String(log.graph_exec_id))- .catch(() => {});- }- : undefined- }- >- {log.graph_exec_id- ? String(log.graph_exec_id).slice(0, 8)- : "-"}- </td>+ <ExecutionCell log={log} /></tr>))}{logs.length === 0 && (@@ -198,3 +177,27 @@ function LogsTable({}export { LogsTable };++function ExecutionCell({ log }: { log: CostLogRow }) {+ const id = log.graph_exec_id ?? log.chat_session_id;+ return (+ <td+ className={[+ "px-3 py-2 text-xs text-muted-foreground",+ id ? "cursor-pointer" : "",+ ].join(" ")}+ title={id ? String(id) : undefined}+ onClick={+ id+ ? () => {+ navigator.clipboard.writeText(String(id)).catch(() => {});+ }+ : undefined+ }+ >+ {id+ ? `${log.graph_exec_id ? "" : "Chat "}${String(id).slice(0, 8)}`+ : "-"}+ </td>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/helpers.tsindex 9883a5a952..efdbe1f69f 100644--- a/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/helpers.ts+++ b/autogpt_platform/frontend/src/app/(platform)/admin/platform-costs/helpers.ts@@ -242,6 +242,7 @@ const CSV_HEADERS = ["Cache Creation Tokens","Duration (s)","Graph Exec ID",+ "Chat Session ID","Node Exec ID",];@@ -270,6 +271,7 @@ export function buildCostLogsCsv(logs: CostLogRow[]): string {log.cache_creation_tokens,log.duration,log.graph_exec_id,+ log.chat_session_id,log.node_exec_id,].map(csvEscape)diff --git a/autogpt_platform/frontend/src/app/(platform)/settings/billing/components/AutomationCreditsTab/TransactionHistoryCard/__tests__/activity-types.test.tsx b/autogpt_platform/frontend/src/app/(platform)/settings/billing/components/AutomationCreditsTab/TransactionHistoryCard/__tests__/activity-types.test.tsxindex 3adb7f46ba..156bf249ad 100644--- a/autogpt_platform/frontend/src/app/(platform)/settings/billing/components/AutomationCreditsTab/TransactionHistoryCard/__tests__/activity-types.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/settings/billing/components/AutomationCreditsTab/TransactionHistoryCard/__tests__/activity-types.test.tsx@@ -75,7 +75,11 @@ describe("Transaction history activity and states", () => {transactions: [{...run,+ id: "chat:chat-123",+ transaction_key: "chat:chat-123",activity_type: "copilot_tools",+ usage_execution_id: null,+ usage_chat_session_id: "chat-123",conversation_id: "chat-123",conversation_title: "Market landscape",library_agent_id: null,@@ -102,6 +106,9 @@ describe("Transaction history activity and states", () => {expect(screen.getByText(/Subscription usage is tracked separately/),).toBeDefined();+ fireEvent.click(screen.getByRole("button", { name: "Reference" }));+ expect(screen.getByText("Chat ID")).toBeDefined();+ expect(screen.getByText("chat-123")).toBeDefined();});it("opens related receipts without adding child charges to the parent amount", async () => {diff --git a/autogpt_platform/frontend/src/app/(platform)/settings/billing/components/AutomationCreditsTab/TransactionHistoryCard/components/ReceiptEntries.tsx b/autogpt_platform/frontend/src/app/(platform)/settings/billing/components/AutomationCreditsTab/TransactionHistoryCard/components/ReceiptEntries.tsxindex 15b675262b..c66edfde73 100644--- a/autogpt_platform/frontend/src/app/(platform)/settings/billing/components/AutomationCreditsTab/TransactionHistoryCard/components/ReceiptEntries.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/settings/billing/components/AutomationCreditsTab/TransactionHistoryCard/components/ReceiptEntries.tsx@@ -14,7 +14,9 @@ export function ReceiptEntries({ transaction }: Props) {const entriesID = useId();const referenceID = useId();const reference =- transaction.usage_execution_id || transaction.transaction_key;+ transaction.usage_execution_id ||+ transaction.usage_chat_session_id ||+ transaction.transaction_key;return (<div className="mt-3"><div className="flex flex-wrap items-center justify-between gap-x-4">@@ -57,7 +59,7 @@ export function ReceiptEntries({ transaction }: Props) {{referenceOpen && (<div id={referenceID} className="mt-2"><Text variant="small" className="text-zinc-600">- {transaction.activity_type === "agent_run" ? "Run ID" : "Reference"}+ {referenceLabel(transaction)}</Text><Textvariant="small"@@ -73,6 +75,12 @@ export function ReceiptEntries({ transaction }: Props) {);}+function referenceLabel(transaction: Transaction) {+ if (transaction.usage_execution_id) return "Run ID";+ if (transaction.usage_chat_session_id) return "Chat ID";+ return "Reference";+}+function ChargeEntries({ transaction }: Props) {const charges = transaction.charges ?? [];return (diff --git a/autogpt_platform/frontend/src/app/(platform)/settings/billing/components/AutomationCreditsTab/TransactionHistoryCard/useTransactionHistoryCard.ts b/autogpt_platform/frontend/src/app/(platform)/settings/billing/components/AutomationCreditsTab/TransactionHistoryCard/useTransactionHistoryCard.tsindex be65e2c813..5ba2b6acbd 100644--- a/autogpt_platform/frontend/src/app/(platform)/settings/billing/components/AutomationCreditsTab/TransactionHistoryCard/useTransactionHistoryCard.ts+++ b/autogpt_platform/frontend/src/app/(platform)/settings/billing/components/AutomationCreditsTab/TransactionHistoryCard/useTransactionHistoryCard.ts@@ -47,7 +47,9 @@ function mergeTransactions(pages: TransactionHistory[]): Transaction[] {const id =item.id ||item.transaction_key ||- `execution:${item.usage_execution_id}`;+ (item.usage_chat_session_id+ ? `chat:${item.usage_chat_session_id}`+ : `execution:${item.usage_execution_id}`);if (!transactions.has(id))transactions.set(id, {...item,diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 7b8ce81a94..952ec72ad1 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -19728,6 +19728,10 @@"anyOf": [{ "type": "string" }, { "type": "null" }],"title": "Graph Exec Id"},+ "chat_session_id": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Chat Session Id"+ },"node_exec_id": {"anyOf": [{ "type": "string" }, { "type": "null" }],"title": "Node Exec Id"@@ -20396,6 +20400,11 @@"anyOf": [{ "type": "string" }, { "type": "null" }],"title": "Usage Execution Id"},+ "usage_chat_session_id": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Usage Chat Session Id",+ "description": "The AutoPilot chat the usage came from"+ },"usage_node_count": {"type": "integer","title": "Usage Node Count",