Skip to content

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

fix(backend/copilot): charge a credit-billed turn as its cost lands (#15593)

Nicholas Tindleb3109ee42941
diff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/audit.py b/autogpt_platform/backend/backend/copilot/billing_e2e/audit.py
index 31e5eeb52f..bf20142afb 100644
--- a/autogpt_platform/backend/backend/copilot/billing_e2e/audit.py
+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/audit.py
@@ -5,6 +5,7 @@ from pydantic import BaseModel
from backend.copilot.model import ChatSession, Usage
from backend.data.platform_cost import PlatformCostEntry, usd_to_microdollars
+from .credit_audit import credit_charge_problems, debit_problems
from .invoice import ANTHROPIC_PRICES, KIMI, MOONSHOT_FALLBACK, Call
from .ledger import PlatformLedger
from .provider import FakeProvider
@@ -37,6 +38,10 @@ def audit_thread(
"""
findings = _audit_path(provider, cli_resumed, turns=len(usage_by_turn))
findings += _audit_attribution(session, ledger)
+ findings += [
+ Finding(check="charge", message=problem)
+ for problem in debit_problems(scenario.credits, session, ledger)
+ ]
for turn, session_usage in enumerate(usage_by_turn):
calls = provider.calls_in(turn)
findings += _audit_turn(scenario, turn, calls, ledger, session_usage)
@@ -127,13 +132,14 @@ def _audit_unbilled(
"""A turn that owes nothing leaves no charge, cost-log row or usage."""
spend = [charge.microdollars for charge in ledger.spend_in(turn)]
tree = [charge.microdollars for charge in ledger.tree_in(turn)]
+ debits = [debit.credits for debit in ledger.debits_in(turn)]
rows = ledger.rows_in(turn)
- if not (spend or tree or rows or session_usage):
+ if not (spend or tree or debits or rows or session_usage):
return []
message = (
- f"turn {turn} owes nothing, yet charged {spend} to spend and {tree} to "
- f"the tree, logged {len(rows)} cost-log row(s) and recorded "
- f"{len(session_usage)} session usage entries"
+ f"turn {turn} owes nothing, yet charged {spend} to spend, {tree} to "
+ f"the tree and {debits} credits, logged {len(rows)} cost-log row(s) "
+ f"and recorded {len(session_usage)} session usage entries"
)
return [Finding(check="charge", message=message)]
@@ -155,6 +161,11 @@ def _audit_spend(
sum(expected_charge_usd(scenario.charge, call) for call in calls)
)
tolerance = _MICRODOLLAR_TOLERANCE_PER_CALL * len(calls)
+ if scenario.credits is not None:
+ problems = credit_charge_problems(
+ scenario.credits, turn, expected or 0, tolerance, ledger
+ )
+ return [Finding(check="charge", message=problem) for problem in problems]
messages = []
if len(spend) != 1 or abs(spend[0] - (expected or 0)) > tolerance:
messages.append(
@@ -179,12 +190,24 @@ def _audit_row(
f"for a route the platform does not pay for"
)
else:
- charged = [charge.microdollars for charge in ledger.spend_in(turn)]
+ # A turn billed to credits charges no spend window: the tree has the
+ # cost it recorded.
+ charges = (
+ ledger.spend_in(turn) if scenario.credits is None else ledger.tree_in(turn)
+ )
+ charged = [charge.microdollars for charge in charges]
if row.tracking_type != "cost_usd" or [row.cost_microdollars] != charged:
messages.append(
f"turn {turn}: logged {row.tracking_type} {row.cost_microdollars}, "
f"charged {charged}"
)
+ mode = (row.metadata or {}).get("billing_mode")
+ billed_to_credits = scenario.credits is not None
+ if (mode == "credits") != billed_to_credits:
+ messages.append(
+ f"turn {turn}: cost-log billing mode {mode!r}, though the turn is "
+ f"{'' if billed_to_credits else 'not '}billed to credits"
+ )
return [Finding(check="charge", message=message) for message in messages]
diff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/audit_test.py b/autogpt_platform/backend/backend/copilot/billing_e2e/audit_test.py
index 4a86f0f89e..527c4006a7 100644
--- a/autogpt_platform/backend/backend/copilot/billing_e2e/audit_test.py
+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/audit_test.py
@@ -1,16 +1,25 @@
"""The audit on its own: a relaunched turn owes only the attempt that
-succeeded, a turn the failure ended owes nothing, and a governor meters each
-call at list price."""
+succeeded, a turn the failure ended owes nothing, a turn billed to credits is
+debited as its cost lands, and a governor meters each call at list price."""
from claude_agent_sdk import AssistantMessage, TextBlock
from backend.copilot.model import Usage
from backend.copilot.sdk.usage_meter import UsageMeter
+from backend.data.credit import UsageTransactionMetadata
from backend.data.platform_cost import PlatformCostEntry, usd_to_microdollars
from .audit import _audit_meter, _audit_turn
from .invoice import ANTHROPIC_PRICES, SONNET, Call, Price, scripted_usage
-from .ledger import CostLogRow, PlatformLedger, SpendCharge, TreeCharge
+from .ledger import (
+ CostLogRow,
+ CreditBilling,
+ CreditDebit,
+ PlannedDrain,
+ PlatformLedger,
+ SpendCharge,
+ TreeCharge,
+)
from .provider import PlannedFault
from .scenarios import SCENARIOS, _sdk_anthropic_failing
@@ -77,6 +86,64 @@ def test_charging_a_turn_the_failure_ended_is_reported():
assert "owes nothing" in findings[0].message
+_CREDIT_BILLED = next(s for s in SCENARIOS if s.id == "sdk-anthropic-credits")
+# 42,127 microdollars: 4 whole credits and part of a fifth.
+_TOOL_ROUND = [_call(1, request=1), _call(2, request=2)]
+
+
+def test_a_turn_debited_its_whole_credits_then_the_part_credit_passes():
+ ledger, usage = _credit_billed_turn(debits=[4, 1])
+
+ assert _audit_turn(_CREDIT_BILLED, 1, _TOOL_ROUND, ledger, usage) == []
+
+
+def test_a_turn_debited_its_whole_cost_at_once_is_reported():
+ """What a charge that rounds up as the cost lands takes, and one that
+ took the cost after the turn had ended."""
+ ledger, usage = _credit_billed_turn(debits=[5])
+
+ findings = _audit_turn(_CREDIT_BILLED, 1, _TOOL_ROUND, ledger, usage)
+
+ assert [finding.check for finding in findings] == ["charge"]
+ assert "owes [4, 1]" in findings[0].message
+
+
+def test_a_turn_billed_to_credits_charging_the_spend_windows_is_reported():
+ ledger, usage = _credit_billed_turn(debits=[4, 1])
+ ledger.spend.append(
+ SpendCharge(turn=1, user_id="user", microdollars=42_127, skip_daily=False)
+ )
+
+ findings = _audit_turn(_CREDIT_BILLED, 1, _TOOL_ROUND, ledger, usage)
+
+ assert [finding.check for finding in findings] == ["charge"]
+ assert "spend windows" in findings[0].message
+
+
+def test_a_turn_the_balance_left_a_credit_owes_that_credit():
+ drained = _CREDIT_BILLED.model_copy(
+ update={"credits": CreditBilling(drain=PlannedDrain(turn=1, left=1))}
+ )
+ ledger, usage = _credit_billed_turn(debits=[1])
+
+ assert _audit_turn(drained, 1, _TOOL_ROUND, ledger, usage) == []
+
+
+def _credit_billed_turn(debits: list[int]) -> tuple[PlatformLedger, list[Usage]]:
+ """Turn 1's ledger and usage, as if the platform billed ``_TOOL_ROUND``
+ to credits in ``debits``."""
+ ledger, usage = _turn_charged_for(*_TOOL_ROUND)
+ ledger.spend.clear()
+ ledger.cost_log[0].entry.metadata = {"billing_mode": "credits"}
+ ledger.debits = [
+ CreditDebit(
+ turn=1, user_id="user", credits=debit, metadata=UsageTransactionMetadata()
+ )
+ for debit in debits
+ ]
+ return ledger, usage
+
+
# Two calls on OpenRouter, which names the model ``anthropic/claude-sonnet-5-5``.
_OPENROUTER_CALLS = [
call.model_copy(update={"route": "openrouter", "model": f"anthropic/{SONNET}"})
diff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/credit_audit.py b/autogpt_platform/backend/backend/copilot/billing_e2e/credit_audit.py
new file mode 100644
index 0000000000..1febd7cf6c
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/credit_audit.py
@@ -0,0 +1,85 @@
+"""A turn billed to credits, checked against its invoice.
+
+The AutoPilot block's turn past the plan's used-up allowance pays from the
+credit balance instead of the plan's spend windows, and its tree is charged as
+any turn's is. The SDK engine records the turn's cost once, as its stream
+ends: the turn is debited the whole credits then, and the part credit when it
+ends, never more in all than the balance leaves it. On OpenRouter that cost
+lands from the reconcile task, so a charge that didn't wait for it would find
+the turn over and take it all at once.
+"""
+
+from backend.copilot.constants import AUTOPILOT_BLOCK_ID
+from backend.copilot.model import ChatSession
+
+from .ledger import CreditBilling, PlatformLedger
+
+# A credit is a cent. Written out rather than imported, like the invoice's
+# prices, so the rounding under test can't set its own expectation.
+_MICRODOLLARS_PER_CREDIT = 10_000
+_AUTOPILOT_BLOCK = (AUTOPILOT_BLOCK_ID, "AutoPilotBlock")
+
+
+def credit_charge_problems(
+ credits: CreditBilling,
+ turn: int,
+ owed: int,
+ tolerance: int,
+ ledger: PlatformLedger,
+) -> list[str]:
+ """How the turn's charges strayed from the ``owed`` microdollars its
+ invoice comes to, give or take ``tolerance``."""
+ spend = [charge.microdollars for charge in ledger.spend_in(turn)]
+ tree = [charge.microdollars for charge in ledger.tree_in(turn)]
+ debits = [debit.credits for debit in ledger.debits_in(turn)]
+ owed_debits = [
+ _debits_for(owed + bound, credits.available(turn))
+ for bound in (-tolerance, tolerance)
+ ]
+ problems = []
+ if spend:
+ problems.append(
+ f"turn {turn}: charged {spend} to the plan's spend windows, though "
+ f"the turn is billed to credits"
+ )
+ if len(tree) != 1 or abs(tree[0] - owed) > tolerance:
+ problems.append(f"turn {turn}: the tree was charged {tree}; it owes {owed}")
+ if debits not in owed_debits:
+ problems.append(
+ f"turn {turn}: debited {debits} credits for {owed} microdollars; it "
+ f"owes {owed_debits[-1]}, the whole credits as the cost landed, "
+ f"then the part credit as the turn ended"
+ )
+ return problems
+
+
+def debit_problems(
+ credits: CreditBilling | None, session: ChatSession, ledger: PlatformLedger
+) -> list[str]:
+ """Every debit on this chat's user, its chat and the AutoPilot block, and
+ none where the plan's allowance covers the turns."""
+ if credits is None:
+ return [
+ f"turn {debit.turn}: debited {debit.credits} credits, though the "
+ f"plan's allowance covers the turn"
+ for debit in ledger.debits
+ ]
+ return [
+ f"turn {debit.turn}: debited user {debit.user_id!r} for chat "
+ f"{debit.metadata.chat_session_id!r} and block "
+ f"{(debit.metadata.block_id, debit.metadata.block)}"
+ for debit in ledger.debits
+ if (debit.user_id, debit.metadata.chat_session_id)
+ != (session.user_id, session.session_id)
+ or (debit.metadata.block_id, debit.metadata.block) != _AUTOPILOT_BLOCK
+ ]
+
+
+def _debits_for(microdollars: int, available: int | None) -> list[int]:
+ """The debits for a cost recorded once: its whole credits, then the part
+ credit rounded up, never more in all than ``available``."""
+ whole = microdollars // _MICRODOLLARS_PER_CREDIT
+ rounded_up = -(-microdollars // _MICRODOLLARS_PER_CREDIT)
+ if available is not None:
+ whole, rounded_up = min(whole, available), min(rounded_up, available)
+ return [debit for debit in (whole, rounded_up - whole) if debit > 0]
diff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/credit_turns.py b/autogpt_platform/backend/backend/copilot/billing_e2e/credit_turns.py
new file mode 100644
index 0000000000..cd559e3bb8
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/credit_turns.py
@@ -0,0 +1,63 @@
+"""SDK turns billed to the credit balance once the plan's allowance is used up.
+
+Each turn is the AutoPilot block's, wrapped as ``CoPilotProcessor._run_turn``
+wraps such a turn: ``bill_to_credits`` puts it on the balance, and the engine
+runs inside ``credit_billing``, which charges each cost as it lands and the
+rest when the turn ends. The processor's other work around the turn (the
+allowance lookup, the stream registry, the turn's lock) is not repeated here;
+``processor_budget_gate_test`` checks that wiring.
+"""
+
+from datetime import UTC, datetime
+from pathlib import Path
+
+import pytest
+
+from backend.copilot import credit_billing as billing
+from backend.copilot.config import ChatConfig
+from backend.copilot.constants import AUTOPILOT_BLOCK_TOOL_NAME
+from backend.copilot.model import ChatSession
+from backend.copilot.turn_pause import TurnPause
+
+from .provider import FakeProvider
+from .sdk_turns import SdkTurns
+
+# What ``refusal_for`` answers once the plan's daily allowance is used up.
+_ALLOWANCE_USED_UP = TurnPause(
+ reason="daily",
+ resets_at=datetime(2026, 10, 17, tzinfo=UTC),
+ checkpoint_saved=False,
+ started=False,
+)
+
+
+class CreditBilledTurns(SdkTurns):
+ def __init__(
+ self,
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ provider: FakeProvider,
+ config: ChatConfig,
+ *,
+ cli_max_retries: int | None = None,
+ ) -> None:
+ super().__init__(
+ tmp_path, monkeypatch, provider, config, cli_max_retries=cli_max_retries
+ )
+ # A deployment that bills credits, with the per-turn ceiling and the
+ # transport its SDK engine runs on.
+ monkeypatch.setattr(billing.settings.config, "enable_credit", True)
+ monkeypatch.setattr(billing, "config", config)
+
+ async def __call__(
+ self, session: ChatSession, prompt: str, *, new_pod: bool
+ ) -> None:
+ turn = await billing.bill_to_credits(
+ user_id=session.user_id,
+ session_id=session.session_id,
+ tool_name=AUTOPILOT_BLOCK_TOOL_NAME,
+ refusal=_ALLOWANCE_USED_UP,
+ )
+ assert turn is not None, "the balance didn't cover the turn, so it was refused"
+ async with billing.credit_billing(turn):
+ await super().__call__(session, prompt, new_pod=new_pod)
diff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/ledger.py b/autogpt_platform/backend/backend/copilot/billing_e2e/ledger.py
index 2af593b7ca..16908d775d 100644
--- a/autogpt_platform/backend/backend/copilot/billing_e2e/ledger.py
+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/ledger.py
@@ -1,9 +1,11 @@
"""What the platform charged, captured where ``persist_and_record_usage`` writes.
Each write lands in one of three places: the user's daily and weekly spend
-counters, the turn tree's budget, and a ``PlatformCostLog`` row. The ledger
-replaces exactly those three writes, so every decision about what to charge
-still runs for real, and files each write under the turn that made it.
+counters, the turn tree's budget, and a ``PlatformCostLog`` row. A turn billed
+to credits debits the credit balance instead of the spend counters. The ledger
+replaces exactly those writes, and stands in for the credit balance, so every
+decision about what to charge still runs for real, and files each write under
+the turn that made it.
"""
import asyncio
@@ -11,11 +13,13 @@ import asyncio
import pytest
from pydantic import BaseModel
-from backend.copilot import token_tracking
+from backend.copilot import credit_billing, token_tracking
from backend.copilot.baseline import service as baseline_service
from backend.copilot.sdk import service as sdk_service
from backend.copilot.tree import TurnEnvelope
+from backend.data.credit import UsageTransactionMetadata
from backend.data.platform_cost import PlatformCostEntry
+from backend.util.exceptions import InsufficientBalanceError
class SpendCharge(BaseModel):
@@ -36,17 +40,57 @@ class CostLogRow(BaseModel):
entry: PlatformCostEntry
+class CreditDebit(BaseModel):
+ turn: int
+ user_id: str
+ credits: int
+ metadata: UsageTransactionMetadata
+
+
+class PlannedDrain(BaseModel):
+ """Other runs spending the credit balance while one turn runs."""
+
+ turn: int
+ # What they leave once the turn has read the balance.
+ left: int
+
+
+class CreditBilling(BaseModel):
+ """The plan's allowance is used up, and each turn, the AutoPilot block's,
+ is billed to this credit balance instead."""
+
+ # Over the per-turn spend floor, so each turn runs on it, and far over a
+ # whole chat's cost here, so it never runs short.
+ balance: int = 500
+ drain: PlannedDrain | None = None
+
+ def available(self, turn: int) -> int | None:
+ """What the balance leaves ``turn`` to pay; None where it covers the
+ turn's whole cost."""
+ if self.drain is None or self.drain.turn != turn:
+ return None
+ return self.drain.left
+
+
class PlatformLedger:
def __init__(self) -> None:
self.turn = 0
self.spend: list[SpendCharge] = []
self.tree: list[TreeCharge] = []
self.cost_log: list[CostLogRow] = []
+ self.debits: list[CreditDebit] = []
+ self.balance = 0
+ self.drain: PlannedDrain | None = None
def install(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(token_tracking, "record_cost_usage", self._record_spend)
monkeypatch.setattr(token_tracking, "charge_turn", self._charge_tree)
monkeypatch.setattr(token_tracking, "platform_cost_db", lambda: self)
+ monkeypatch.setattr(credit_billing, "credit_db", lambda: self)
+
+ def fund(self, credits: CreditBilling) -> None:
+ self.balance = credits.balance
+ self.drain = credits.drain
def spend_in(self, turn: int) -> list[SpendCharge]:
return [charge for charge in self.spend if charge.turn == turn]
@@ -57,9 +101,35 @@ class PlatformLedger:
def rows_in(self, turn: int) -> list[PlatformCostEntry]:
return [row.entry for row in self.cost_log if row.turn == turn]
+ def debits_in(self, turn: int) -> list[CreditDebit]:
+ return [debit for debit in self.debits if debit.turn == turn]
+
async def log_platform_cost(self, entry: PlatformCostEntry) -> None:
self.cost_log.append(CostLogRow(turn=self.turn, entry=entry))
+ async def get_credits(self, user_id: str) -> int:
+ balance = self.balance
+ if self.drain is not None and self.drain.turn == self.turn:
+ # Other runs spend it down as soon as the turn has read it.
+ self.balance = min(self.balance, self.drain.left)
+ return balance
+
+ async def spend_credits(
+ self, *, user_id: str, cost: int, metadata: UsageTransactionMetadata
+ ) -> int:
+ if cost > self.balance:
+ # Refused whole, as the credit model refuses a debit it can't cover.
+ raise InsufficientBalanceError(
+ "Insufficient balance", user_id, self.balance, cost
+ )
+ self.balance -= cost
+ self.debits.append(
+ CreditDebit(
+ turn=self.turn, user_id=user_id, credits=cost, metadata=metadata
+ )
+ )
+ return self.balance
+
async def settle(self) -> None:
"""Wait for the writes a turn defers past its own end.
diff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/scenarios.py b/autogpt_platform/backend/backend/copilot/billing_e2e/scenarios.py
index 289243bf78..1badf6ebe5 100644
--- a/autogpt_platform/backend/backend/copilot/billing_e2e/scenarios.py
+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/scenarios.py
@@ -22,6 +22,7 @@ from typing import Any, Literal
from pydantic import BaseModel
from .invoice import ANTHROPIC_PRICES, KIMI, MOONSHOT_FALLBACK, SONNET, Call
+from .ledger import CreditBilling, PlannedDrain
from .provider import PlannedFault
ChargeBasis = Literal["invoice", "cli_estimate", "moonshot_estimate", "none"]
@@ -68,6 +69,10 @@ class Scenario(BaseModel):
fault_ends_turn: bool = False
# CLAUDE_CODE_MAX_RETRIES for the CLI; None keeps its own default.
cli_max_retries: int | None = None
+ # Each turn the AutoPilot block's past the plan's used-up allowance: it
+ # pays its charge from this credit balance, rounded up to whole credits,
+ # instead of the plan's spend windows. None where the allowance covers it.
+ credits: CreditBilling | None = None
known_gap: KnownGap | None = None
def owed_calls(self, turn: int, calls: list[Call]) -> list[Call]:
@@ -109,6 +114,14 @@ _SDK_ANTHROPIC = Scenario(
cost_log_provider="anthropic",
config={**_DIRECT, "claude_agent_model": SONNET},
)
+_SDK_OPENROUTER = Scenario(
+ id="sdk-openrouter",
+ engine="sdk",
+ model=f"anthropic/{SONNET}",
+ charge="invoice",
+ cost_log_provider="open_router",
+ config={**_OPENROUTER, "claude_agent_model": f"anthropic/{SONNET}"},
+)
def _sdk_anthropic_failing(
@@ -167,14 +180,7 @@ SCENARIOS = [
PlannedFault(kind="overloaded", turn=2, request=1),
calls_per_turn=(1, 2, 1),
),
- Scenario(
- id="sdk-openrouter",
- engine="sdk",
- model=f"anthropic/{SONNET}",
- charge="invoice",
- cost_log_provider="open_router",
- config={**_OPENROUTER, "claude_agent_model": f"anthropic/{SONNET}"},
- ),
+ _SDK_OPENROUTER,
Scenario(
id="sdk-openrouter-lookups-refused",
engine="sdk",
@@ -225,6 +231,22 @@ SCENARIOS = [
config=_DIRECT,
codex=True,
),
+ # On OpenRouter, the default route, the turn's charge waits for the
+ # reconcile's invoice; direct, the CLI's own price lands as the stream ends.
+ _SDK_OPENROUTER.model_copy(
+ update={"id": "sdk-openrouter-credits", "credits": CreditBilling()}
+ ),
+ _SDK_ANTHROPIC.model_copy(
+ update={"id": "sdk-anthropic-credits", "credits": CreditBilling()}
+ ),
+ # Other runs spend all but a credit once the resumed turn has read the
+ # balance: it takes the credit that is left and never overdraws.
+ _SDK_OPENROUTER.model_copy(
+ update={
+ "id": "sdk-openrouter-credits-drained",
+ "credits": CreditBilling(drain=PlannedDrain(turn=2, left=1)),
+ }
+ ),
Scenario(
id="baseline-openrouter",
engine="baseline",
diff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.py b/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.py
index 1c1a965a3d..2954e8bfa0 100644
--- a/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.py
+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.py
@@ -21,6 +21,7 @@ from claude_agent_sdk import ClaudeAgentOptions, ClaudeSDKClient
from openai import AsyncOpenAI
from pydantic import SecretStr
+from backend.copilot.budget_gate import AutoContinueGate
from backend.copilot.config import ChatConfig
from backend.copilot.model import ChatSession
from backend.copilot.sdk import env as sdk_env
@@ -147,6 +148,10 @@ class SdkTurns:
"build_turn_budget_block": AsyncMock(return_value=""),
"drain_pending_safe": AsyncMock(return_value=[]),
"drain_pending_for_persist": AsyncMock(return_value=[]),
+ # Nothing is queued mid-turn, so a finished turn simply ends.
+ "gate_auto_continue": AsyncMock(
+ return_value=AutoContinueGate(proceed=True)
+ ),
"resolve_answered": AsyncMock(return_value=[]),
"clear_pending_question": AsyncMock(),
}
diff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/thread_billing_test.py b/autogpt_platform/backend/backend/copilot/billing_e2e/thread_billing_test.py
index 0b85bee35a..2dbd0608e8 100644
--- a/autogpt_platform/backend/backend/copilot/billing_e2e/thread_billing_test.py
+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/thread_billing_test.py
@@ -8,7 +8,8 @@ charged the whole chat's spend so far until #15342.
The provider keeps its own invoice; the ledger records what the platform
charged. Each turn must charge the user's spend counters and the turn tree
once, for the turn's own calls, and write one cost-log row whose tokens are
-the provider's.
+the provider's. A turn billed to credits charges the credit balance instead
+of the spend counters.
The SDK scenarios run the bundled Claude Code CLI, so they skip where there is
none (the Windows wheel ships without one); CI runs them on Linux.
@@ -26,6 +27,7 @@ from backend.copilot.sdk.cli_openrouter_compat_test import _resolve_cli_path
from .audit import audit_thread
from .baseline_turns import BaselineTurns
+from .credit_turns import CreditBilledTurns
from .ledger import PlatformLedger
from .m365_turns import MICROSOFT_365_CREDENTIAL_ID, Microsoft365Turns
from .provider import FakeProvider
@@ -52,6 +54,8 @@ async def test_each_turn_is_charged_what_its_provider_billed(
pytest.skip("No Claude Code CLI binary available")
provider.generation_lookups_fail = scenario.generation_lookups_fail
provider.faults = [scenario.fault] if scenario.fault else []
+ if scenario.credits is not None:
+ ledger.fund(scenario.credits)
run_turn = _turn_runner(scenario, provider, tmp_path, monkeypatch)
session = _session(scenario)
usage_by_turn: list[list[Usage]] = []
@@ -98,11 +102,20 @@ def _turn_runner(
base_url=f"{provider.url}{scenario.client_path}",
model=scenario.model,
)
+ config = _config(sdk_service.config, scenario, provider)
+ if scenario.credits is not None:
+ return CreditBilledTurns(
+ tmp_path,
+ monkeypatch,
+ provider,
+ config,
+ cli_max_retries=scenario.cli_max_retries,
+ )
return SdkTurns(
tmp_path,
monkeypatch,
provider,
- _config(sdk_service.config, scenario, provider),
+ config,
codex=scenario.codex,
cli_max_retries=scenario.cli_max_retries,
)
diff --git a/autogpt_platform/backend/backend/copilot/credit_billing.py b/autogpt_platform/backend/backend/copilot/credit_billing.py
index fc2abfe084..02debe3889 100644
--- a/autogpt_platform/backend/backend/copilot/credit_billing.py
+++ b/autogpt_platform/backend/backend/copilot/credit_billing.py
@@ -16,9 +16,18 @@ A credit-billed turn:
so its CLI backstop sits exactly on it;
- runs on the SDK engine, the one that enforces a budget;
- records its cost here rather than in the plan's daily and weekly windows;
-- is charged once when it ends: the cost it recorded, rounded up to whole
- credits, never past its cap. A turn that ended without a result is charged
- what it recorded too, through the same accounting as any other turn.
+- is charged as its cost lands: each whole credit as soon as the cost it
+ recorded reaches it, and the rest, rounded up to a whole credit, when the
+ turn ends. In all it pays the cost it recorded, rounded up, never past its
+ cap, and a process that dies mid-turn leaves under a credit of what it
+ recorded unpaid. A turn that ended without a result is charged what it
+ recorded too, through the same accounting as any other turn;
+- counts a debit that failed for a reason other than the balance as charged,
+ and logs it as a possible leak. Such a debit may still have landed: the
+ credit model commits it before its auto top-up runs, and a reply can be
+ lost, so taking it again could charge it twice. The database client already
+ retries what fails before reaching the database. When other runs spent the
+ balance meanwhile, the turn takes what is left and never overdraws it.
"""
import asyncio
@@ -109,13 +118,16 @@ class CreditBilledTurn:
self._recordings.add(recording)
async def record(self, cost_microdollars: int) -> None:
+ """Put a cost on the turn and charge what it completes: whole credits
+ while the turn runs, the rest rounded up once it has ended. Shielded
+ like the end's charge, so a debit already sent is counted even if the
+ caller is cancelled, and no later charge takes it again."""
self.spent_microdollars += cost_microdollars
- if self._ended:
- # Recorded after the turn was charged: charged as it lands.
- await self._charge_due()
+ await asyncio.shield(self._charge_due())
async def end(self) -> None:
- """Charge what the turn recorded, once its late recordings are in.
+ """Charge the rest of what the turn recorded, rounded up, once its
+ late recordings are in.
A cancel during the wait doesn't skip the charge: the turn still pays
for what it spent, and cost recorded later is charged as it lands.
@@ -131,38 +143,60 @@ class CreditBilledTurn:
async def _charge_due(self) -> None:
async with self._charging:
- owed = min(credits_for(self.spent_microdollars), self.cap_credits)
- due = owed - self.charged_credits
+ final = self._ended
+ due = self._owed_credits(final) - self.charged_credits
if due <= 0:
return
self.charged_credits += await self._spend(due)
+ def _owed_credits(self, final: bool) -> int:
+ """Whole credits while the turn runs, the part credit too once it has
+ ended; never past the cap."""
+ spent = self.spent_microdollars
+ owed = credits_for(spent) if final else spent // MICRODOLLARS_PER_CREDIT
+ return min(owed, self.cap_credits)
+
async def _spend(self, credits: int) -> int:
- """Debit up to ``credits``, never below zero; returns what was debited."""
+ """Debit up to ``credits``, never below zero; returns what the turn
+ counts as charged, which a debit that may have landed is."""
try:
await self._debit(credits)
- return credits
except InsufficientBalanceError:
- # Other runs spent the balance while this turn ran: it takes what
- # is left rather than overdrawing the account.
- try:
- left = min(credits, await _balance(self.user_id, self.organization_id))
- if left > 0:
- await self._debit(left)
- except Exception:
- left = 0
- logger.warning(
- f"Credit-billed turn in session {self.session_id[:12]} owed "
- f"{credits} credits; the balance covered {max(left, 0)}"
- )
- return max(left, 0)
+ return await self._spend_what_is_left(credits)
except Exception:
- logger.error(
- f"Billing leak: could not charge {credits} credits for the "
- f"credit-billed turn in session {self.session_id[:12]}",
- exc_info=True,
+ self._log_unconfirmed(credits)
+ return credits
+
+ async def _spend_what_is_left(self, credits: int) -> int:
+ # Other runs spent the balance while this turn ran: it takes what is
+ # left rather than overdrawing the account. A balance it can't read
+ # took nothing, so what is due stays due.
+ try:
+ left = max(
+ min(credits, await _balance(self.user_id, self.organization_id)), 0
)
- return 0
+ except Exception:
+ left = 0
+ if left > 0:
+ try:
+ await self._debit(left)
+ except InsufficientBalanceError:
+ left = 0
+ except Exception:
+ self._log_unconfirmed(left)
+ logger.warning(
+ f"Credit-billed turn in session {self.session_id[:12]} owed "
+ f"{credits} credits; the balance covered {left}"
+ )
+ return left
+
+ def _log_unconfirmed(self, credits: int) -> None:
+ logger.error(
+ f"Billing leak: could not confirm a charge of {credits} credits for "
+ f"the credit-billed turn in session {self.session_id[:12]}; it "
+ "counts as charged, since taking it again could charge it twice",
+ exc_info=True,
+ )
async def _debit(self, credits: int) -> None:
metadata = UsageTransactionMetadata(
@@ -238,7 +272,7 @@ async def _balance(user_id: str, organization_id: str | None) -> int:
@asynccontextmanager
async def credit_billing(turn: CreditBilledTurn) -> AsyncIterator[None]:
- """Run what's inside billed to credits, and charge it when it ends."""
+ """Run what's inside billed to credits, and charge the rest when it ends."""
token = _credit_billed_turn.set(turn)
try:
yield
diff --git a/autogpt_platform/backend/backend/copilot/credit_billing_test.py b/autogpt_platform/backend/backend/copilot/credit_billing_test.py
index 7e95b3069b..e76a00050c 100644
--- a/autogpt_platform/backend/backend/copilot/credit_billing_test.py
+++ b/autogpt_platform/backend/backend/copilot/credit_billing_test.py
@@ -2,10 +2,12 @@
The executor runs a block turn on the credit balance instead of refusing it
when the balance covers the per-turn spend floor. The turn is capped at what
-the balance covers and charged the cost it recorded when it ends.
+the balance covers and charged the cost it records as it lands: each whole
+credit as the cost reaches it, and the rest, rounded up, when it ends.
"""
import asyncio
+import logging
from datetime import UTC, datetime
from unittest.mock import AsyncMock, MagicMock, PropertyMock
@@ -25,6 +27,7 @@ from backend.copilot.rate_limit import RateLimitUnavailable
from backend.copilot.sdk.governor import TurnGovernor
from backend.copilot.sdk.usage_meter import UsageMeter, cli_price_ratio
from backend.copilot.turn_pause import TurnPause
+from backend.data.credit import UsageTransactionMetadata
from backend.util.exceptions import InsufficientBalanceError
OUT_OF_TODAY = TurnPause(
@@ -54,6 +57,38 @@ def credits(mocker) -> MagicMock:
return db
+class _Wallet:
+ """The account's balance. A debit it can't cover is refused whole, as the
+ credit model refuses it."""
+
+ def __init__(self, balance: int) -> None:
+ self.balance = balance
+ self.debits: list[int] = []
+
+ async def get_credits(self, user_id: str) -> int:
+ return self.balance
+
+ async def spend_credits(
+ self, *, user_id: str, cost: int, metadata: UsageTransactionMetadata
+ ) -> int:
+ # A debit is a round trip: other charges run while it is in flight.
+ await asyncio.sleep(0)
+ if cost > self.balance:
+ raise InsufficientBalanceError("too low", user_id, self.balance, cost)
+ self.balance -= cost
+ self.debits.append(cost)
+ return self.balance
+
+
+def wallet(credits: MagicMock) -> _Wallet:
+ """500 credits for the turn's debits to draw on."""
+ wallet = _Wallet(balance=500)
+ credits.get_credits.side_effect = wallet.get_credits
+ credits.spend_credits.side_effect = wallet.spend_credits
+ return wallet
+
+
async def _billed(credits: MagicMock, balance: int) -> CreditBilledTurn:
credits.get_credits.return_value = balance
turn = await bill_to_credits(
@@ -70,6 +105,10 @@ def _charged(credits: MagicMock) -> list[int]:
return [call.kwargs["cost"] for call in credits.spend_credits.await_args_list]
+def _leaks(caplog: pytest.LogCaptureFixture) -> list[logging.LogRecord]:
+ return [r for r in caplog.records if r.getMessage().startswith("Billing leak")]
+
+
class _Meter(UsageMeter):
def __init__(self, spent: float, last: float) -> None:
super().__init__()
@@ -161,7 +200,7 @@ async def test_an_organizations_run_bills_the_organizations_balance(credits):
@pytest.mark.asyncio
async def test_a_turn_cancelled_while_its_charge_waits_is_still_charged(credits):
turn = await _billed(credits, balance=500)
- await turn.record(1_000_000)
+ await turn.record(1_005_000)
reconcile = asyncio.create_task(asyncio.sleep(3600))
turn.wait_for(reconcile)
@@ -172,7 +211,8 @@ async def test_a_turn_cancelled_while_its_charge_waits_is_still_charged(credits)
await ending
reconcile.cancel()
- assert _charged(credits) == [100]
+ # The 100 whole credits as the cost landed, and the half credit left.
+ assert _charged(credits) == [100, 1]
async def _bill(**tenancy) -> CreditBilledTurn | None:
@@ -185,6 +225,12 @@ async def _bill(**tenancy) -> CreditBilledTurn | None:
)
+async def _turn() -> CreditBilledTurn:
+ turn = await _bill()
+ assert turn is not None
+ return turn
+
+
@pytest.mark.asyncio
async def test_an_unreadable_balance_fails_closed(credits):
credits.get_credits.side_effect = ConnectionError("db down")
@@ -230,21 +276,248 @@ async def test_a_turn_costing_more_than_the_balance_stops_at_the_cap(credits):
@pytest.mark.asyncio
-async def test_the_turn_is_charged_its_recorded_cost_when_it_ends(credits):
+async def test_the_turn_is_charged_whole_credits_as_its_cost_lands(credits):
turn = await _billed(credits, balance=500)
async with credit_billing(turn):
assert await record_credit_billed_cost("user-1", 123_400)
+ assert _charged(credits) == [12]
assert await record_credit_billed_cost("user-1", 1)
- credits.spend_credits.assert_not_awaited()
+ assert _charged(credits) == [12]
+
+ # 123,401 microdollars: the 12 whole credits as they landed, and the part
+ # credit left, rounded up, when the turn ended.
+ assert _charged(credits) == [12, 1]
+ for call in credits.spend_credits.await_args_list:
+ assert call.kwargs["user_id"] == "user-1"
+ assert call.kwargs["metadata"].chat_session_id == "sess-1"
+ assert call.kwargs["metadata"].block_id == AUTOPILOT_BLOCK_ID
+ assert call.kwargs["metadata"].block == "AutoPilotBlock"
+
+
+async def test_part_credits_are_charged_once_they_add_up_to_a_whole_one(credits):
+ turn = await _billed(credits, balance=500)
+
+ for _ in range(7):
+ await turn.record(5_000)
+ assert _charged(credits) == [1, 1, 1]
+
+ await turn.end()
+ # 3.5 credits in all, rounded up.
+ assert _charged(credits) == [1, 1, 1, 1]
+
+
+ "costs, balance",
+ [
+ ([1], 500),
+ ([9_999, 1], 500),
+ ([123_400, 1], 500),
+ ([5_000] * 7, 500),
+ ([33_333, 33_333, 33_334], 500),
+ ([123_456, 250_000, 99_999], 500),
+ # Past the cap: the balance the turn started with.
+ ([250_001, 499_999, 3], 60),
+ ([600_001], 60),
+ ],
+)
+async def test_the_turn_pays_what_one_charge_at_its_end_would_have(
+ credits, wallet, costs, balance
+):
+ wallet.balance = balance
+ turn = await _turn()
+
+ async with credit_billing(turn):
+ for cost in costs:
+ await record_credit_billed_cost("user-1", cost)
+ # Every whole credit is in before the turn ends.
+ assert sum(wallet.debits) == min(sum(costs) // 10_000, balance)
+
+ # The recorded cost rounded up to whole credits, within the cap: what the
+ # turn's one charge took when nothing was charged before its end.
+ assert sum(wallet.debits) == turn.charged_credits
+ assert turn.charged_credits == min(-(-sum(costs) // 10_000), balance)
+
+
+async def test_concurrent_records_never_charge_more_than_the_turn_spent(
+ credits, wallet
+):
+ turn = await _turn()
+
+ await asyncio.gather(*(turn.record(12_500) for _ in range(8)))
+ # 100,000 microdollars: 10 credits, however the charges interleaved.
+ assert sum(wallet.debits) == turn.charged_credits == 10
+
+ await asyncio.gather(turn.record(12_500), turn.end(), turn.record(12_500))
+ # 125,000 in all, with the end racing the last two: 12.5, rounded up once.
+ assert sum(wallet.debits) == turn.charged_credits == 13
+
+
+async def test_a_turn_that_never_ends_has_paid_all_but_a_part_credit(credits, wallet):
+ """A process that dies mid-turn never reaches the charge at the end."""
+ turn = await _turn()
+
+ for cost in (123_456, 250_000, 99_999):
+ await turn.record(cost)
+
+ # 473,455 microdollars recorded: 47 credits paid, 3,455 unpaid.
+ assert wallet.debits == [12, 25, 10]
+ assert 473_455 - 10_000 * sum(wallet.debits) < 10_000
+
+
+async def test_a_charge_still_counts_when_its_recorder_is_cancelled(credits):
+ """A debit the ledger took can't be taken back: it counts, and no later
+ charge takes it again."""
+ taken = asyncio.Event()
+ replied = asyncio.Event()
+
+ async def debit_awaiting_its_reply(**charge) -> int:
+ taken.set()
+ await replied.wait()
+ return 0
+
+ turn = await _billed(credits, balance=500)
+ credits.spend_credits.side_effect = debit_awaiting_its_reply
+ recording = asyncio.create_task(turn.record(25_000))
+ await asyncio.wait_for(taken.wait(), timeout=5)
+ recording.cancel()
+ with pytest.raises(asyncio.CancelledError):
+ await recording
+ replied.set()
+ await turn.end()
- # 123,401 microdollars, rounded up to whole credits, in one charge.
- credits.spend_credits.assert_awaited_once()
- charge = credits.spend_credits.await_args.kwargs
- assert (charge["user_id"], charge["cost"]) == ("user-1", 13)
- assert charge["metadata"].chat_session_id == "sess-1"
- assert charge["metadata"].block_id == AUTOPILOT_BLOCK_ID
- assert charge["metadata"].block == "AutoPilotBlock"
+ assert _charged(credits) == [2, 1]
+ assert turn.charged_credits == 3
+
+
+async def test_a_failed_debit_counts_as_charged(credits, caplog):
+ # It may have landed, and charging it again could take it twice.
+ caplog.set_level(logging.WARNING, logger="backend.copilot.credit_billing")
+ turn = await _billed(credits, balance=500)
+ credits.spend_credits.side_effect = [ConnectionError("db blip"), 0, 0]
+
+ await turn.record(25_000)
+ assert turn.charged_credits == 2
+ await turn.record(10_000)
+ assert turn.charged_credits == 3
+ await turn.end()
+
+ assert _charged(credits) == [2, 1, 1]
+ assert turn.charged_credits == 4
+ assert [leak.levelno for leak in _leaks(caplog)] == [logging.ERROR]
+
+
+async def test_a_debit_that_lands_then_raises_is_not_taken_again(
+ credits, wallet, caplog
+):
+ # The credit model commits a debit before it looks up the user's auto
+ # top-up, which can still raise: the credits are gone, but the call failed.
+ caplog.set_level(logging.WARNING, logger="backend.copilot.credit_billing")
+ turn = await _billed(credits, balance=500)
+
+ async def lands_then_raises(**debit) -> int:
+ await wallet.spend_credits(**debit)
+ raise RuntimeError("auto top-up settings unreadable")
+
+ credits.spend_credits.side_effect = lands_then_raises
+ await turn.record(25_000)
+ credits.spend_credits.side_effect = wallet.spend_credits
+ await turn.end()
+
+ # 25,000 microdollars rounds up to 3 credits, taken once.
+ assert wallet.debits == [2, 1]
+ assert wallet.balance == 497
+ assert [leak.levelno for leak in _leaks(caplog)] == [logging.ERROR]
+
+
+async def test_what_is_left_counts_as_charged_once_its_debit_may_have_landed(
+ credits, wallet, caplog
+):
+ caplog.set_level(logging.WARNING, logger="backend.copilot.credit_billing")
+ turn = await _billed(credits, balance=500)
+ # Other runs spent all but a credit meanwhile.
+ wallet.balance = 1
+
+ async def lands_then_raises(**debit) -> int:
+ await wallet.spend_credits(**debit)
+ raise RuntimeError("reply lost")
+
+ credits.spend_credits.side_effect = lands_then_raises
+ await turn.record(25_000)
+ # A top-up lands before the turn ends.
+ wallet.balance += 100
+ credits.spend_credits.side_effect = wallet.spend_credits
+ await turn.end()
+
+ # The credit that was left, then the 2 more the 3 owed come to.
+ assert wallet.debits == [1, 2]
+ assert turn.charged_credits == 3
+ assert [leak.levelno for leak in _leaks(caplog)] == [logging.ERROR]
+
+
+async def test_a_balance_it_cannot_read_takes_nothing_and_stays_due(credits, wallet):
+ turn = await _billed(credits, balance=500)
+ # Other runs spent all but a credit, and the balance can't be read.
+ wallet.balance = 1
+ credits.get_credits.side_effect = ConnectionError("db down")
+
+ await turn.record(25_000)
+ assert (turn.charged_credits, wallet.debits) == (0, [])
+
+ # Once it reads again, after a top-up, the end takes all that is due.
+ credits.get_credits.side_effect = wallet.get_credits
+ wallet.balance += 100
+ await turn.end()
+ assert (turn.charged_credits, wallet.debits) == (3, [3])
+
+
+async def test_what_is_left_that_goes_before_it_is_taken_takes_nothing(credits, wallet):
+ turn = await _billed(credits, balance=500)
+ wallet.balance = 1
+
+ async def spent_right_after_the_read(user_id: str) -> int:
+ # Another run takes the last credit between the read and the debit.
+ left, wallet.balance = wallet.balance, 0
+ return left
+
+ credits.get_credits.side_effect = spent_right_after_the_read
+ await turn.record(25_000)
+
+ assert (turn.charged_credits, wallet.debits, wallet.balance) == (0, [], 0)
+
+
+async def test_a_debit_that_keeps_failing_is_never_asked_for_twice(credits, caplog):
+ caplog.set_level(logging.WARNING, logger="backend.copilot.credit_billing")
+ turn = await _billed(credits, balance=500)
+ credits.spend_credits.side_effect = ConnectionError("db down")
+
+ async with credit_billing(turn):
+ await record_credit_billed_cost("user-1", 25_000)
+ await record_credit_billed_cost("user-1", 20_000)
+
+ # Each charge asks only for what the ones before it didn't, 5 credits in
+ # all, and each failure is logged as a possible leak.
+ assert _charged(credits) == [2, 2, 1]
+ assert turn.charged_credits == 5
+ assert [leak.levelno for leak in _leaks(caplog)] == [logging.ERROR] * 3
+
+ # Once debits go through again, a cost landing late charges only its own
+ # part.
+ credits.spend_credits.side_effect = None
+ await turn.record(10_000)
+ assert _charged(credits)[3:] == [1]
+ assert turn.charged_credits == 6
@pytest.mark.asyncio
@@ -269,25 +542,28 @@ async def test_cost_recorded_after_the_charge_is_charged_when_it_lands(credits):
await turn.record(30_000)
- # 55,000 microdollars in all: 6 credits, not 3 + 3.
- assert _charged(credits) == [3, 3]
+ # 2 as the first cost landed and 1 rounding it up at the end; then 55,000
+ # microdollars in all is 6 credits, not 3 + 3.
+ assert _charged(credits) == [2, 1, 3]
assert turn.charged_credits == 6
@pytest.mark.asyncio
-async def test_a_balance_spent_meanwhile_is_charged_only_what_is_left(credits):
- turn = await _billed(credits, balance=500)
- credits.spend_credits.side_effect = [
- InsufficientBalanceError("too low", "user-1", 20, 40),
- 0,
- ]
+async def test_a_balance_spent_meanwhile_pays_only_what_is_left(credits, wallet):
+ turn = await _turn()
async with credit_billing(turn):
await record_credit_billed_cost("user-1", 400_000)
- credits.get_credits.return_value = 20
-
- assert _charged(credits) == [40, 20]
- assert turn.charged_credits == 20
+ assert wallet.debits == [40]
+ # Other runs spent all but 20 credits meanwhile.
+ wallet.balance = 20
+ await record_credit_billed_cost("user-1", 300_000)
+ # 30 credits due and 20 left: the turn takes the 20.
+ assert wallet.debits == [40, 20]
+
+ # 70 owed in all; the 10 still due when the turn ended found nothing left.
+ assert _charged(credits) == [40, 30, 20, 10]
+ assert (turn.charged_credits, wallet.balance) == (60, 0)
@pytest.mark.asyncio
diff --git a/autogpt_platform/backend/backend/copilot/executor/processor_budget_gate_test.py b/autogpt_platform/backend/backend/copilot/executor/processor_budget_gate_test.py
index b5eacbf5ef..15c07b7e2d 100644
--- a/autogpt_platform/backend/backend/copilot/executor/processor_budget_gate_test.py
+++ b/autogpt_platform/backend/backend/copilot/executor/processor_budget_gate_test.py
@@ -276,12 +276,14 @@ async def test_a_block_turn_past_its_allowance_runs_on_credits_and_pays_its_cost
turn.engine.assert_called_once()
assert _paused(turn) == []
- # $0.1234 is 1,234 tenths of a cent: 13 credits, rounded up.
- credits.spend_credits.assert_awaited_once()
- charge = credits.spend_credits.await_args.kwargs
- assert (charge["user_id"], charge["cost"]) == ("user-1", 13)
- assert charge["metadata"].chat_session_id == "sess-1"
- assert charge["metadata"].block == "AutoPilotBlock"
+ # $0.1234 is 1,234 tenths of a cent: the 12 whole credits as the cost
+ # landed, and 1 more rounding it up when the turn ended.
+ charges = [call.kwargs for call in credits.spend_credits.await_args_list]
+ assert [charge["cost"] for charge in charges] == [12, 1]
+ for charge in charges:
+ assert charge["user_id"] == "user-1"
+ assert charge["metadata"].chat_session_id == "sess-1"
+ assert charge["metadata"].block == "AutoPilotBlock"
windows.assert_not_awaited()
turn.complete.assert_awaited_once_with(
"sess-1", error_message=None, error_code=None, turn_id="turn-1"
@@ -303,12 +305,11 @@ async def test_an_organizations_block_turn_is_charged_to_the_organization(
credits.get_org_credits.assert_awaited_once_with(org_id="org-1")
credits.spend_credits.assert_not_awaited()
- charge = credits.spend_org_credits.await_args.kwargs
- assert (charge["org_id"], charge["team_id"], charge["cost"]) == (
- "org-1",
- "team-1",
- 13,
- )
+ charges = [call.kwargs for call in credits.spend_org_credits.await_args_list]
+ assert [(c["org_id"], c["team_id"], c["cost"]) for c in charges] == [
+ ("org-1", "team-1", 12),
+ ("org-1", "team-1", 1),
+ ]
@pytest.mark.asyncio
diff --git a/autogpt_platform/backend/backend/copilot/token_tracking.py b/autogpt_platform/backend/backend/copilot/token_tracking.py
index 5b525d1b91..9fdec613b8 100644
--- a/autogpt_platform/backend/backend/copilot/token_tracking.py
+++ b/autogpt_platform/backend/backend/copilot/token_tracking.py
@@ -227,8 +227,8 @@ async def persist_and_record_usage(
cost_microdollars = usd_to_microdollars(cost_float)
if cost_microdollars is not None and cost_microdollars > 0:
- # A turn billed to credits is charged to the credit balance when it
- # ends, and spends nothing from the plan's daily and weekly windows.
+ # A turn billed to credits is charged to the credit balance as its cost
+ # lands, and spends nothing from the plan's daily and weekly windows.
if user_id and not await record_credit_billed_cost(user_id, cost_microdollars):
# record_cost_usage() owns its fail-open handling for Redis/network
# errors. Don't wrap with a broad except here — unexpected accounting