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/backend): dynamic BlockCostType (SECOND/ITEMS/COST_USD/TOKENS) + E2B/FAL migration (#12894)
Zamil Majdy0f6eea06c474
diff --git a/autogpt_platform/backend/backend/blocks/_base.py b/autogpt_platform/backend/backend/blocks/_base.pyindex dc503c7ae1..e2238bcae5 100644--- a/autogpt_platform/backend/backend/blocks/_base.py+++ b/autogpt_platform/backend/backend/blocks/_base.py@@ -96,27 +96,64 @@ class BlockCategory(Enum):class BlockCostType(str, Enum):- RUN = "run" # cost X credits per run- BYTE = "byte" # cost X credits per byte- SECOND = "second" # cost X credits per second+ # RUN : cost_amount credits per run.+ # BYTE : cost_amount credits per byte of input data.+ # SECOND : cost_amount credits per cost_divisor walltime seconds.+ # ITEMS : cost_amount credits per cost_divisor items (from stats).+ # COST_USD : cost_amount credits per USD of stats.provider_cost.+ # TOKENS : per-(model, provider) rate table lookup; see TOKEN_COST.+ RUN = "run"+ BYTE = "byte"+ SECOND = "second"+ ITEMS = "items"+ COST_USD = "cost_usd"+ TOKENS = "tokens"++ @property+ def is_dynamic(self) -> bool:+ """Real charge is computed post-flight from stats.++ Dynamic types (SECOND/ITEMS/COST_USD/TOKENS) return 0 pre-flight and+ settle against stats via charge_reconciled_usage once the block runs.+ """+ return self in _DYNAMIC_COST_TYPES+++_DYNAMIC_COST_TYPES: frozenset[BlockCostType] = frozenset(+ {+ BlockCostType.SECOND,+ BlockCostType.ITEMS,+ BlockCostType.COST_USD,+ BlockCostType.TOKENS,+ }+)class BlockCost(BaseModel):cost_amount: intcost_filter: BlockInputcost_type: BlockCostType+ # cost_divisor: interpret cost_amount as "credits per cost_divisor units".+ # Only meaningful for SECOND / ITEMS. TOKENS routes through TOKEN_COST+ # rate tables (per-model input/output/cache pricing) and ignores+ # cost_divisor entirely. Defaults to 1 so existing RUN/BYTE entries stay+ # point-wise. Example: cost_amount=1, cost_divisor=10 under SECOND means+ # "1 credit per 10 seconds of walltime".+ cost_divisor: int = 1def __init__(self,cost_amount: int,cost_type: BlockCostType = BlockCostType.RUN,cost_filter: Optional[BlockInput] = None,+ cost_divisor: int = 1,**data: Any,) -> None:super().__init__(cost_amount=cost_amount,cost_filter=cost_filter or {},cost_type=cost_type,+ cost_divisor=max(1, cost_divisor),**data,)@@ -445,19 +482,6 @@ class BlockWebhookConfig(BlockManualWebhookConfig):class Block(ABC, Generic[BlockSchemaInputType, BlockSchemaOutputType]):_optimized_description: ClassVar[str | None] = None- def extra_runtime_cost(self, execution_stats: NodeExecutionStats) -> int:- """Return extra runtime cost to charge after this block run completes.-- Called by the executor after a block finishes with COMPLETED status.- The return value is the number of additional base-cost credits to- charge beyond the single credit already collected by charge_usage- at the start of execution. Defaults to 0 (no extra charges).-- Override in blocks (e.g. OrchestratorBlock) that make multiple LLM- calls within one run and should be billed per call.- """- return 0-def __init__(self,id: str = "",diff --git a/autogpt_platform/backend/backend/blocks/agent.py b/autogpt_platform/backend/backend/blocks/agent.pyindex a4e5acff07..67eba1aa9c 100644--- a/autogpt_platform/backend/backend/blocks/agent.py+++ b/autogpt_platform/backend/backend/blocks/agent.py@@ -171,7 +171,10 @@ class AgentExecutorBlock(Block):)self.merge_stats(NodeExecutionStats(- extra_cost=event.stats.cost if event.stats else 0,+ # Sub-graph already debited each of its own nodes; we+ # roll up its total so graph_stats.cost reflects the+ # full sub-graph spend.+ reconciled_cost_delta=(event.stats.cost if event.stats else 0),extra_steps=event.stats.node_exec_count if event.stats else 0,))diff --git a/autogpt_platform/backend/backend/blocks/agent_mail/_config.py b/autogpt_platform/backend/backend/blocks/agent_mail/_config.pyindex 414b19536a..cf5c0d0ff1 100644--- a/autogpt_platform/backend/backend/blocks/agent_mail/_config.py+++ b/autogpt_platform/backend/backend/blocks/agent_mail/_config.py@@ -4,11 +4,16 @@ Shared configuration for all AgentMail blocks.from agentmail import AsyncAgentMail-from backend.sdk import APIKeyCredentials, ProviderBuilder, SecretStr+from backend.sdk import APIKeyCredentials, BlockCostType, ProviderBuilder, SecretStr+# AgentMail is in beta with no published paid tier yet, but ~37 blocks+# without any BLOCK_COSTS entry means they currently execute wallet-free.+# 1 cr/call is a conservative interim floor so no AgentMail work leaks+# past billing. Revisit once AgentMail publishes usage-based pricing.agent_mail = (ProviderBuilder("agent_mail").with_api_key("AGENTMAIL_API_KEY", "AgentMail API Key")+ .with_base_cost(1, BlockCostType.RUN).build())diff --git a/autogpt_platform/backend/backend/blocks/baas/bots.py b/autogpt_platform/backend/backend/blocks/baas/bots.pyindex 68af9a675e..4c5d4215fc 100644--- a/autogpt_platform/backend/backend/blocks/baas/bots.py+++ b/autogpt_platform/backend/backend/blocks/baas/bots.py@@ -8,17 +8,27 @@ from backend.sdk import (APIKeyCredentials,Block,BlockCategory,+ BlockCost,+ BlockCostType,BlockOutput,BlockSchemaInput,BlockSchemaOutput,CredentialsMetaInput,SchemaField,+ cost,)from ._api import MeetingBaasAPIfrom ._config import baas+# Meeting BaaS charges $0.69/hour of recording. The Join block is the+# trigger that starts the recording session; the meeting itself runs out+# of band (we don't get duration back from the FetchMeetingData response+# we use). 30 cr ≈ $0.30 covers a median 30-minute meeting with margin.+# Interim until FetchMeetingData surfaces duration for post-flight+# reconciliation.+@cost(BlockCost(cost_type=BlockCostType.RUN, cost_amount=30))class BaasBotJoinMeetingBlock(Block):"""Deploy a bot immediately or at a scheduled start_time to join and record a meeting.diff --git a/autogpt_platform/backend/backend/blocks/dataforseo/_api.py b/autogpt_platform/backend/backend/blocks/dataforseo/_api.pyindex 3b3190e66d..b4a30dda0d 100644--- a/autogpt_platform/backend/backend/blocks/dataforseo/_api.py+++ b/autogpt_platform/backend/backend/blocks/dataforseo/_api.py@@ -19,6 +19,10 @@ class DataForSeoClient:trusted_origins=["https://api.dataforseo.com"],raise_for_status=False,)+ # USD cost reported by DataForSEO on the most recent successful call.+ # Populated by keyword_suggestions / related_keywords so the caller+ # can surface it via NodeExecutionStats.provider_cost for billing.+ self.last_cost_usd: float = 0.0def _get_headers(self) -> Dict[str, str]:"""Generate the authorization header using Basic Auth."""@@ -97,6 +101,9 @@ class DataForSeoClient:if data.get("tasks") and len(data["tasks"]) > 0:task = data["tasks"][0]if task.get("status_code") == 20000: # Success code+ # DataForSEO reports per-task USD cost; stash it so callers+ # can populate NodeExecutionStats.provider_cost.+ self.last_cost_usd = float(task.get("cost") or 0.0)return task.get("result", [])else:error_msg = task.get("status_message", "Task failed")@@ -174,6 +181,9 @@ class DataForSeoClient:if data.get("tasks") and len(data["tasks"]) > 0:task = data["tasks"][0]if task.get("status_code") == 20000: # Success code+ # DataForSEO reports per-task USD cost; stash it so callers+ # can populate NodeExecutionStats.provider_cost.+ self.last_cost_usd = float(task.get("cost") or 0.0)return task.get("result", [])else:error_msg = task.get("status_message", "Task failed")diff --git a/autogpt_platform/backend/backend/blocks/dataforseo/_config.py b/autogpt_platform/backend/backend/blocks/dataforseo/_config.pyindex 10b2b91130..ec979de893 100644--- a/autogpt_platform/backend/backend/blocks/dataforseo/_config.py+++ b/autogpt_platform/backend/backend/blocks/dataforseo/_config.py@@ -12,6 +12,11 @@ dataforseo = (password_env_var="DATAFORSEO_PASSWORD",title="DataForSEO Credentials",)- .with_base_cost(1, BlockCostType.RUN)+ # DataForSEO reports USD cost per task (e.g. $0.001/keyword returned).+ # DataForSeoClient stashes it on last_cost_usd; each block emits it via+ # merge_stats so the COST_USD resolver bills against real spend.+ # 1000 platform credits per USD → 1 credit per $0.001 (≈ 1 credit/+ # returned keyword on the standard tier).+ .with_base_cost(1000, BlockCostType.COST_USD).build())diff --git a/autogpt_platform/backend/backend/blocks/dataforseo/keyword_suggestions.py b/autogpt_platform/backend/backend/blocks/dataforseo/keyword_suggestions.pyindex a1ecc86386..1c546615f7 100644--- a/autogpt_platform/backend/backend/blocks/dataforseo/keyword_suggestions.py+++ b/autogpt_platform/backend/backend/blocks/dataforseo/keyword_suggestions.py@@ -4,6 +4,7 @@ DataForSEO Google Keyword Suggestions block.from typing import Any, Dict, List, Optional+from backend.data.model import NodeExecutionStatsfrom backend.sdk import (Block,BlockCategory,@@ -110,8 +111,10 @@ class DataForSeoKeywordSuggestionsBlock(Block):test_output=[("suggestion",- lambda x: hasattr(x, "keyword")- and x.keyword == "digital marketing strategy",+ lambda x: (+ hasattr(x, "keyword")+ and x.keyword == "digital marketing strategy"+ ),),("suggestions", lambda x: isinstance(x, list) and len(x) == 1),("total_count", 1),@@ -167,6 +170,16 @@ class DataForSeoKeywordSuggestionsBlock(Block):results = await self._fetch_keyword_suggestions(client, input_data)+ # DataForSEO reports per-task USD cost on the response. Feed it+ # into NodeExecutionStats so the COST_USD resolver bills the+ # real provider spend at reconciliation time.+ self.merge_stats(+ NodeExecutionStats(+ provider_cost=client.last_cost_usd,+ provider_cost_type="cost_usd",+ )+ )+# Process and format the resultssuggestions = []if results and len(results) > 0:diff --git a/autogpt_platform/backend/backend/blocks/dataforseo/related_keywords.py b/autogpt_platform/backend/backend/blocks/dataforseo/related_keywords.pyindex 0757cb6507..711f5ea5ef 100644--- a/autogpt_platform/backend/backend/blocks/dataforseo/related_keywords.py+++ b/autogpt_platform/backend/backend/blocks/dataforseo/related_keywords.py@@ -4,6 +4,7 @@ DataForSEO Google Related Keywords block.from typing import Any, Dict, List, Optional+from backend.data.model import NodeExecutionStatsfrom backend.sdk import (Block,BlockCategory,@@ -177,6 +178,16 @@ class DataForSeoRelatedKeywordsBlock(Block):results = await self._fetch_related_keywords(client, input_data)+ # DataForSEO reports per-task USD cost on the response. Feed it+ # into NodeExecutionStats so the COST_USD resolver bills the+ # real provider spend at reconciliation time.+ self.merge_stats(+ NodeExecutionStats(+ provider_cost=client.last_cost_usd,+ provider_cost_type="cost_usd",+ )+ )+# Process and format the resultsrelated_keywords = []if results and len(results) > 0:diff --git a/autogpt_platform/backend/backend/blocks/exa/_config.py b/autogpt_platform/backend/backend/blocks/exa/_config.pyindex bca636b2a8..31a37ba93b 100644--- a/autogpt_platform/backend/backend/blocks/exa/_config.py+++ b/autogpt_platform/backend/backend/blocks/exa/_config.py@@ -11,6 +11,11 @@ exa = (ProviderBuilder("exa").with_api_key("EXA_API_KEY", "Exa API Key").with_webhook_manager(ExaWebhookManager)- .with_base_cost(1, BlockCostType.RUN)+ # Exa returns `cost_dollars.total` on every response and ExaSearchBlock+ # (plus ~45 sibling blocks that share this provider config) already+ # populates NodeExecutionStats.provider_cost with it. Bill 100 credits+ # per USD (~$0.01/credit): cheap searches stay at 1–2 credits, a Deep+ # Research run at $0.20 lands at 20 credits, matching provider spend.+ .with_base_cost(100, BlockCostType.COST_USD).build())diff --git a/autogpt_platform/backend/backend/blocks/firecrawl/_config.py b/autogpt_platform/backend/backend/blocks/firecrawl/_config.pyindex cc176c4a86..7a2ff95fe4 100644--- a/autogpt_platform/backend/backend/blocks/firecrawl/_config.py+++ b/autogpt_platform/backend/backend/blocks/firecrawl/_config.py@@ -1,8 +1,14 @@from backend.sdk import BlockCostType, ProviderBuilder+# Firecrawl bills in its own credits (1 credit ≈ $0.001). Each block's+# run() estimates USD spend from the operation (pages scraped, limit,+# credits_used on ExtractResponse) and merge_stats populates+# NodeExecutionStats.provider_cost before billing reconciliation. 1000+# platform credits per USD means 1 platform credit per Firecrawl credit+# — roughly matches our existing per-call tier for single-page scrape.firecrawl = (ProviderBuilder("firecrawl").with_api_key("FIRECRAWL_API_KEY", "Firecrawl API Key")- .with_base_cost(1, BlockCostType.RUN)+ .with_base_cost(1000, BlockCostType.COST_USD).build())diff --git a/autogpt_platform/backend/backend/blocks/firecrawl/crawl.py b/autogpt_platform/backend/backend/blocks/firecrawl/crawl.pyindex eced461a8a..0c88b85e59 100644--- a/autogpt_platform/backend/backend/blocks/firecrawl/crawl.py+++ b/autogpt_platform/backend/backend/blocks/firecrawl/crawl.py@@ -4,6 +4,7 @@ from firecrawl import FirecrawlAppfrom firecrawl.v2.types import ScrapeOptionsfrom backend.blocks.firecrawl._api import ScrapeFormat+from backend.data.model import NodeExecutionStatsfrom backend.sdk import (APIKeyCredentials,Block,@@ -86,6 +87,14 @@ class FirecrawlCrawlBlock(Block):wait_for=input_data.wait_for,),)+ # Firecrawl bills 1 credit (~$0.001) per crawled page. crawl_result.data+ # is the list of scraped pages actually returned.+ pages = len(crawl_result.data) if crawl_result.data else 0+ self.merge_stats(+ NodeExecutionStats(+ provider_cost=pages * 0.001, provider_cost_type="cost_usd"+ )+ )yield "data", crawl_result.datafor data in crawl_result.data:diff --git a/autogpt_platform/backend/backend/blocks/firecrawl/extract.py b/autogpt_platform/backend/backend/blocks/firecrawl/extract.pyindex e5fd5ec9f3..c86feb1b09 100755--- a/autogpt_platform/backend/backend/blocks/firecrawl/extract.py+++ b/autogpt_platform/backend/backend/blocks/firecrawl/extract.py@@ -2,25 +2,22 @@ from typing import Anyfrom firecrawl import FirecrawlApp+from backend.data.model import NodeExecutionStatsfrom backend.sdk import (APIKeyCredentials,Block,BlockCategory,- BlockCost,- BlockCostType,BlockOutput,BlockSchemaInput,BlockSchemaOutput,CredentialsMetaInput,SchemaField,- cost,)from backend.util.exceptions import BlockExecutionErrorfrom ._config import firecrawl-@cost(BlockCost(2, BlockCostType.RUN))class FirecrawlExtractBlock(Block):class Input(BlockSchemaInput):credentials: CredentialsMetaInput = firecrawl.credentials_field()@@ -74,4 +71,13 @@ class FirecrawlExtractBlock(Block):block_id=self.id,) from e+ # Firecrawl surfaces actual credit spend on extract responses+ # (credits_used). 1 Firecrawl credit ≈ $0.001.+ credits_used = getattr(extract_result, "credits_used", None) or 0+ self.merge_stats(+ NodeExecutionStats(+ provider_cost=credits_used * 0.001,+ provider_cost_type="cost_usd",+ )+ )yield "data", extract_result.datadiff --git a/autogpt_platform/backend/backend/blocks/firecrawl/map.py b/autogpt_platform/backend/backend/blocks/firecrawl/map.pyindex e2e04adac0..9d24da7237 100644--- a/autogpt_platform/backend/backend/blocks/firecrawl/map.py+++ b/autogpt_platform/backend/backend/blocks/firecrawl/map.py@@ -2,6 +2,7 @@ from typing import Anyfrom firecrawl import FirecrawlApp+from backend.data.model import NodeExecutionStatsfrom backend.sdk import (APIKeyCredentials,Block,@@ -50,6 +51,10 @@ class FirecrawlMapWebsiteBlock(Block):map_result = app.map(url=input_data.url,)+ # Firecrawl bills 1 credit (~$0.001) per map request.+ self.merge_stats(+ NodeExecutionStats(provider_cost=0.001, provider_cost_type="cost_usd")+ )# Convert SearchResult objects to dictsresults_data = [diff --git a/autogpt_platform/backend/backend/blocks/firecrawl/scrape.py b/autogpt_platform/backend/backend/blocks/firecrawl/scrape.pyindex 2c1a68d6d9..f7923cf07c 100644--- a/autogpt_platform/backend/backend/blocks/firecrawl/scrape.py+++ b/autogpt_platform/backend/backend/blocks/firecrawl/scrape.py@@ -3,6 +3,7 @@ from typing import Anyfrom firecrawl import FirecrawlAppfrom backend.blocks.firecrawl._api import ScrapeFormat+from backend.data.model import NodeExecutionStatsfrom backend.sdk import (APIKeyCredentials,Block,@@ -81,6 +82,11 @@ class FirecrawlScrapeBlock(Block):max_age=input_data.max_age,wait_for=input_data.wait_for,)+ # Firecrawl bills 1 credit (~$0.001) per scraped page; scrape is a+ # single-page operation.+ self.merge_stats(+ NodeExecutionStats(provider_cost=0.001, provider_cost_type="cost_usd")+ )yield "data", scrape_resultfor f in input_data.formats:diff --git a/autogpt_platform/backend/backend/blocks/firecrawl/search.py b/autogpt_platform/backend/backend/blocks/firecrawl/search.pyindex a2769a0f96..3c14bcf905 100644--- a/autogpt_platform/backend/backend/blocks/firecrawl/search.py+++ b/autogpt_platform/backend/backend/blocks/firecrawl/search.py@@ -4,6 +4,7 @@ from firecrawl import FirecrawlAppfrom firecrawl.v2.types import ScrapeOptionsfrom backend.blocks.firecrawl._api import ScrapeFormat+from backend.data.model import NodeExecutionStatsfrom backend.sdk import (APIKeyCredentials,Block,@@ -68,6 +69,17 @@ class FirecrawlSearchBlock(Block):wait_for=input_data.wait_for,),)+ # Firecrawl bills per returned web result (~1 credit each). The+ # SearchResponse structure exposes `.web` when scrape_options was+ # requested; fall back to `limit` as an upper bound estimate.+ web_results = getattr(scrape_result, "web", None) or []+ billed_units = max(len(web_results), 1)+ self.merge_stats(+ NodeExecutionStats(+ provider_cost=billed_units * 0.001,+ provider_cost_type="cost_usd",+ )+ )yield "data", scrape_resultif hasattr(scrape_result, "web") and scrape_result.web:for site in scrape_result.web:diff --git a/autogpt_platform/backend/backend/blocks/jina/search.py b/autogpt_platform/backend/backend/blocks/jina/search.pyindex 007dd5bc12..5c2ebfb39f 100644--- a/autogpt_platform/backend/backend/blocks/jina/search.py+++ b/autogpt_platform/backend/backend/blocks/jina/search.py@@ -15,7 +15,7 @@ from backend.blocks.jina._auth import (JinaCredentialsInput,)from backend.blocks.search import GetRequest-from backend.data.model import SchemaField+from backend.data.model import NodeExecutionStats, SchemaFieldfrom backend.util.exceptions import BlockExecutionErrorfrom backend.util.request import HTTPClientError, HTTPServerError, validate_url_host@@ -70,6 +70,13 @@ class SearchTheWebBlock(Block, GetRequest):block_id=self.id,) from e+ # Jina Reader Search: $0.01/query on the paid tier. Fixed per-query+ # cost; routed through COST_USD so the platform cost log records+ # real USD spend (costMicrodollars) alongside the credit charge.+ self.merge_stats(+ NodeExecutionStats(provider_cost=0.01, provider_cost_type="cost_usd")+ )+# Output the search resultsyield "results", results@@ -128,10 +135,16 @@ class ExtractWebsiteContentBlock(Block, GetRequest):try:content = await self.get_request(url, json=False, headers=headers)except HTTPClientError as e:- yield "error", f"Client error ({e.status_code}) fetching {input_data.url}: {e}"+ yield (+ "error",+ f"Client error ({e.status_code}) fetching {input_data.url}: {e}",+ )returnexcept HTTPServerError as e:- yield "error", f"Server error ({e.status_code}) fetching {input_data.url}: {e}"+ yield (+ "error",+ f"Server error ({e.status_code}) fetching {input_data.url}: {e}",+ )returnexcept Exception as e:yield "error", f"Failed to fetch {input_data.url}: {e}"diff --git a/autogpt_platform/backend/backend/blocks/orchestrator.py b/autogpt_platform/backend/backend/blocks/orchestrator.pyindex b2a6df8481..5979f90dde 100644--- a/autogpt_platform/backend/backend/blocks/orchestrator.py+++ b/autogpt_platform/backend/backend/blocks/orchestrator.py@@ -376,20 +376,12 @@ class OrchestratorBlock(Block):re-raise carve-out for this reason."""- def extra_runtime_cost(self, execution_stats: NodeExecutionStats) -> int:- """Charge one extra runtime cost per LLM call beyond the first.-- In agent mode each iteration makes one LLM call. The first is already- covered by charge_usage(); this returns the number of additional- credits so the executor can bill the remaining calls post-completion.-- SDK-mode exemption: when the block runs via _execute_tools_sdk_mode,- the SDK manages its own conversation loop and only exposes aggregate- usage. We hardcode llm_call_count=1 there (the SDK does not report a- per-turn call count), so this method always returns 0 for SDK-mode- executions. Per-iteration billing does not apply to SDK mode.- """- return max(0, execution_stats.llm_call_count - 1)+ # OrchestratorBlock bills via BlockCostType.TOKENS + compute_token_credits,+ # which aggregates input_token_count / output_token_count / cache_read /+ # cache_creation across every LLM iteration into one post-flight charge.+ # The per-iteration flat-fee path (Block.extra_runtime_cost →+ # charge_extra_runtime_cost) would double-bill the same tokens, so+ # OrchestratorBlock deliberately inherits the base-class no-op default.# MCP server name used by the Claude Code SDK execution mode. Keep in sync# with _create_graph_mcp_server and the MCP_PREFIX derivation in _execute_tools_sdk_mode.@@ -1189,10 +1181,14 @@ class OrchestratorBlock(Block):not execution_params.execution_context.dry_runand tool_node_stats.error is None):+ # Charge the sub-block for telemetry / wallet debit. The+ # return value is intentionally discarded: on_node_execution+ # above ran the sub-block against this graph's own+ # graph_stats_pair (manager.py:659-668), so its cost already+ # lands in graph_stats.cost on the sub-block's completion.+ # Re-merging here would double-count in telemetry / UI / audit.try:- tool_cost, _ = await execution_processor.charge_node_usage(- node_exec_entry,- )+ await execution_processor.charge_node_usage(node_exec_entry)except InsufficientBalanceError:# IBE must propagate — see OrchestratorBlock class docstring.# Log the billing failure here so the discarded tool result@@ -1214,9 +1210,6 @@ class OrchestratorBlock(Block):"tool execution was successful",sink_node_id,)- tool_cost = 0- if tool_cost > 0:- self.merge_stats(NodeExecutionStats(extra_cost=tool_cost))# Get outputs from database after execution completes using database manager clientnode_outputs = await db_client.get_execution_outputs_by_node_exec_id(diff --git a/autogpt_platform/backend/backend/blocks/stagehand/_config.py b/autogpt_platform/backend/backend/blocks/stagehand/_config.pyindex 43ec6cd5ac..0bb609d664 100644--- a/autogpt_platform/backend/backend/blocks/stagehand/_config.py+++ b/autogpt_platform/backend/backend/blocks/stagehand/_config.py@@ -1,8 +1,12 @@from backend.sdk import BlockCostType, ProviderBuilder+# 1 credit per 3 walltime seconds. Block walltime proxies for the+# Browserbase session lifetime + the LLM call it issues. Interim until+# the block emits real provider_cost (USD) via merge_stats and migrates+# to COST_USD.stagehand = (ProviderBuilder("stagehand").with_api_key("STAGEHAND_API_KEY", "Stagehand API Key")- .with_base_cost(1, BlockCostType.RUN)+ .with_base_cost(1, BlockCostType.SECOND, cost_divisor=3).build())diff --git a/autogpt_platform/backend/backend/blocks/test/test_orchestrator_per_iteration_cost.py b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_per_iteration_cost.pydeleted file mode 100644index 441bc08a42..0000000000--- a/autogpt_platform/backend/backend/blocks/test/test_orchestrator_per_iteration_cost.py+++ /dev/null@@ -1,1020 +0,0 @@-"""Tests for OrchestratorBlock per-iteration cost charging.--The OrchestratorBlock in agent mode makes multiple LLM calls in a single-node execution. The executor uses ``Block.extra_runtime_cost`` to detect-this and charge ``base_cost * (llm_call_count - 1)`` extra credits after-the block completes.-"""--import threading-from collections import defaultdict-from unittest.mock import AsyncMock, MagicMock, patch--import pytest--from backend.blocks._base import Block-from backend.blocks.orchestrator import ExecutionParams, OrchestratorBlock-from backend.data.execution import ExecutionContext, ExecutionStatus-from backend.data.model import NodeExecutionStats-from backend.executor import billing, manager-from backend.util.exceptions import InsufficientBalanceError--# ── extra_runtime_cost hook ────────────────────────────────────────---class _NoOpBlock(Block):- """Minimal concrete Block subclass that does not override extra_runtime_cost."""-- def __init__(self):- super().__init__(- id="00000000-0000-0000-0000-000000000001", description="No-op test block"- )-- def run(self, input_data, **kwargs): # type: ignore[override]- yield "out", {}---class TestExtraRuntimeCost:- """OrchestratorBlock opts into per-LLM-call billing via extra_runtime_cost."""-- def test_orchestrator_returns_nonzero_for_multiple_calls(self):- block = OrchestratorBlock()- stats = NodeExecutionStats(llm_call_count=3)- assert block.extra_runtime_cost(stats) == 2-- def test_orchestrator_returns_zero_for_single_call(self):- block = OrchestratorBlock()- stats = NodeExecutionStats(llm_call_count=1)- assert block.extra_runtime_cost(stats) == 0-- def test_orchestrator_returns_zero_for_zero_calls(self):- block = OrchestratorBlock()- stats = NodeExecutionStats(llm_call_count=0)- assert block.extra_runtime_cost(stats) == 0-- def test_default_block_returns_zero(self):- """A block that does not override extra_runtime_cost returns 0."""- block = _NoOpBlock()- stats = NodeExecutionStats(llm_call_count=10)- assert block.extra_runtime_cost(stats) == 0---# ── charge_extra_runtime_cost math ───────────────────────────────────---def fake_node_exec():- node_exec = MagicMock()- node_exec.user_id = "u"- node_exec.graph_exec_id = "g"- node_exec.graph_id = "g"- node_exec.node_exec_id = "ne"- node_exec.node_id = "n"- node_exec.block_id = "b"- node_exec.inputs = {}- return node_exec---def patched_processor(monkeypatch):- """ExecutionProcessor with stubbed db client / block lookup helpers.-- Returns the processor and a list of credit amounts spent so tests can- assert on what was charged.-- Note: ``ExecutionProcessor.__new__()`` bypasses ``__init__`` — if- ``__init__`` gains required state in the future this fixture will need- updating.- """- spent: list[int] = []-- class FakeDb:- def spend_credits(self, *, user_id, cost, metadata):- spent.append(cost)- return 1000 # remaining balance-- fake_block = MagicMock()- fake_block.name = "FakeBlock"-- monkeypatch.setattr(billing, "get_db_client", lambda: FakeDb())- monkeypatch.setattr(billing, "get_block", lambda block_id: fake_block)- monkeypatch.setattr(- billing,- "block_usage_cost",- lambda block, input_data, **_kw: (10, {"model": "claude-sonnet-4-6"}),- )-- proc = manager.ExecutionProcessor.__new__(manager.ExecutionProcessor)- return proc, spent---class TestChargeExtraRuntimeCost:- @pytest.mark.asyncio- async def test_zero_extra_iterations_charges_nothing(- self, patched_processor, fake_node_exec- ):- proc, spent = patched_processor- cost, balance = await proc.charge_extra_runtime_cost(- fake_node_exec, extra_count=0- )- assert cost == 0- assert balance == 0- assert spent == []-- @pytest.mark.asyncio- async def test_extra_iterations_multiplies_base_cost(- self, patched_processor, fake_node_exec- ):- proc, spent = patched_processor- cost, balance = await proc.charge_extra_runtime_cost(- fake_node_exec, extra_count=4- )- assert cost == 40 # 4 × 10- assert balance == 1000- assert spent == [40]-- @pytest.mark.asyncio- async def test_negative_extra_iterations_charges_nothing(- self, patched_processor, fake_node_exec- ):- proc, spent = patched_processor- cost, balance = await proc.charge_extra_runtime_cost(- fake_node_exec, extra_count=-1- )- assert cost == 0- assert balance == 0- assert spent == []-- @pytest.mark.asyncio- async def test_capped_at_max(self, monkeypatch, fake_node_exec):- """Runaway llm_call_count is capped at _MAX_EXTRA_RUNTIME_COST."""-- spent: list[int] = []-- class FakeDb:- def spend_credits(self, *, user_id, cost, metadata):- spent.append(cost)- return 1000-- fake_block = MagicMock()- fake_block.name = "FakeBlock"-- monkeypatch.setattr(billing, "get_db_client", lambda: FakeDb())- monkeypatch.setattr(billing, "get_block", lambda block_id: fake_block)- monkeypatch.setattr(- billing,- "block_usage_cost",- lambda block, input_data, **_kw: (10, {}),- )-- proc = manager.ExecutionProcessor.__new__(manager.ExecutionProcessor)- cap = billing._MAX_EXTRA_RUNTIME_COST- cost, _ = await proc.charge_extra_runtime_cost(- fake_node_exec, extra_count=cap * 100- )- # Charged at most cap × 10- assert cost == cap * 10- assert spent == [cap * 10]-- @pytest.mark.asyncio- async def test_zero_base_cost_skips_charge(self, monkeypatch, fake_node_exec):-- spent: list[int] = []-- class FakeDb:- def spend_credits(self, *, user_id, cost, metadata):- spent.append(cost)- return 0-- fake_block = MagicMock()- fake_block.name = "FakeBlock"-- monkeypatch.setattr(billing, "get_db_client", lambda: FakeDb())- monkeypatch.setattr(billing, "get_block", lambda block_id: fake_block)- monkeypatch.setattr(- billing, "block_usage_cost", lambda block, input_data, **_kw: (0, {})- )-- proc = manager.ExecutionProcessor.__new__(manager.ExecutionProcessor)- cost, balance = await proc.charge_extra_runtime_cost(- fake_node_exec, extra_count=4- )- assert cost == 0- assert balance == 0- assert spent == []-- @pytest.mark.asyncio- async def test_block_not_found_skips_charge(self, monkeypatch, fake_node_exec):-- spent: list[int] = []-- class FakeDb:- def spend_credits(self, *, user_id, cost, metadata):- spent.append(cost)- return 0-- monkeypatch.setattr(billing, "get_db_client", lambda: FakeDb())- monkeypatch.setattr(billing, "get_block", lambda block_id: None)- monkeypatch.setattr(- billing, "block_usage_cost", lambda block, input_data, **_kw: (10, {})- )-- proc = manager.ExecutionProcessor.__new__(manager.ExecutionProcessor)- cost, balance = await proc.charge_extra_runtime_cost(- fake_node_exec, extra_count=3- )- assert cost == 0- assert balance == 0- assert spent == []-- @pytest.mark.asyncio- async def test_propagates_insufficient_balance_error(- self, monkeypatch, fake_node_exec- ):- """Out-of-credits errors must propagate, not be silently swallowed."""-- class FakeDb:- def spend_credits(self, *, user_id, cost, metadata):- raise InsufficientBalanceError(- user_id=user_id,- message="Insufficient balance",- balance=0,- amount=cost,- )-- fake_block = MagicMock()- fake_block.name = "FakeBlock"-- monkeypatch.setattr(billing, "get_db_client", lambda: FakeDb())- monkeypatch.setattr(billing, "get_block", lambda block_id: fake_block)- monkeypatch.setattr(- billing, "block_usage_cost", lambda block, input_data, **_kw: (10, {})- )-- proc = manager.ExecutionProcessor.__new__(manager.ExecutionProcessor)- with pytest.raises(InsufficientBalanceError):- await proc.charge_extra_runtime_cost(fake_node_exec, extra_count=4)---# ── charge_node_usage ──────────────────────────────────────────────---class TestChargeNodeUsage:- """charge_node_usage delegates to billing.charge_usage with execution_count=0."""-- @pytest.mark.asyncio- async def test_delegates_with_zero_execution_count(- self, monkeypatch, fake_node_exec- ):- """Nested tool charges should NOT inflate the per-execution counter."""-- captured: dict = {}-- def fake_charge_usage(node_exec, execution_count):- captured["execution_count"] = execution_count- captured["node_exec"] = node_exec- return (5, 100)-- def fake_handle_low_balance(- db_client, user_id, current_balance, transaction_cost- ):- pass-- monkeypatch.setattr(billing, "charge_usage", fake_charge_usage)- monkeypatch.setattr(billing, "handle_low_balance", fake_handle_low_balance)- monkeypatch.setattr(billing, "get_db_client", lambda: MagicMock())-- proc = manager.ExecutionProcessor.__new__(manager.ExecutionProcessor)- cost, balance = await proc.charge_node_usage(fake_node_exec)- assert cost == 5- assert balance == 100- assert captured["execution_count"] == 0-- @pytest.mark.asyncio- async def test_calls_handle_low_balance_when_cost_nonzero(- self, monkeypatch, fake_node_exec- ):- """charge_node_usage should call handle_low_balance when total_cost > 0."""-- low_balance_calls: list[dict] = []-- def fake_charge_usage(node_exec, execution_count):- return (10, 50)-- def fake_handle_low_balance(- db_client, user_id, current_balance, transaction_cost- ):- low_balance_calls.append(- {- "user_id": user_id,- "current_balance": current_balance,- "transaction_cost": transaction_cost,- }- )-- monkeypatch.setattr(billing, "charge_usage", fake_charge_usage)- monkeypatch.setattr(billing, "handle_low_balance", fake_handle_low_balance)- monkeypatch.setattr(billing, "get_db_client", lambda: MagicMock())-- proc = manager.ExecutionProcessor.__new__(manager.ExecutionProcessor)- cost, balance = await proc.charge_node_usage(fake_node_exec)- assert cost == 10- assert balance == 50- assert len(low_balance_calls) == 1- assert low_balance_calls[0]["user_id"] == "u"- assert low_balance_calls[0]["current_balance"] == 50- assert low_balance_calls[0]["transaction_cost"] == 10-- @pytest.mark.asyncio- async def test_skips_handle_low_balance_when_cost_zero(- self, monkeypatch, fake_node_exec- ):- """charge_node_usage should NOT call handle_low_balance when cost is 0."""-- low_balance_calls: list = []-- def fake_charge_usage(node_exec, execution_count):- return (0, 200)-- def fake_handle_low_balance(- db_client, user_id, current_balance, transaction_cost- ):- low_balance_calls.append(True)-- monkeypatch.setattr(billing, "charge_usage", fake_charge_usage)- monkeypatch.setattr(billing, "handle_low_balance", fake_handle_low_balance)- monkeypatch.setattr(billing, "get_db_client", lambda: MagicMock())-- proc = manager.ExecutionProcessor.__new__(manager.ExecutionProcessor)- cost, balance = await proc.charge_node_usage(fake_node_exec)- assert cost == 0- assert low_balance_calls == []---# ── on_node_execution charging gate ────────────────────────────────---class _FakeNode:- """Minimal stand-in for a ``Node`` object with a block attribute."""-- def __init__(self, extra_charges: int = 0, block_name: str = "FakeBlock"):- self.block = MagicMock()- self.block.name = block_name- self.block.extra_runtime_cost = MagicMock(return_value=extra_charges)---class _FakeExecContext:- def __init__(self, dry_run: bool = False):- self.dry_run = dry_run---def _make_node_exec(dry_run: bool = False) -> MagicMock:- """Build a NodeExecutionEntry-like mock for on_node_execution tests."""- ne = MagicMock()- ne.user_id = "u"- ne.graph_id = "g"- ne.graph_exec_id = "ge"- ne.node_id = "n"- ne.node_exec_id = "ne"- ne.block_id = "b"- ne.inputs = {}- ne.execution_context = _FakeExecContext(dry_run=dry_run)- return ne---def gated_processor(monkeypatch):- """ExecutionProcessor with on_node_execution's downstream calls stubbed.-- Lets tests flip the gate conditions (status, extra_runtime_cost result,- llm_call_count, dry_run) and observe whether charge_extra_runtime_cost- was called.- """-- calls: dict[str, list] = {- "charge_extra_runtime_cost": [],- "handle_low_balance": [],- "handle_insufficient_funds_notif": [],- }-- # Stub node lookup + DB client so the wrapper doesn't touch real infra.- fake_db = MagicMock()- fake_db.get_node = AsyncMock(return_value=_FakeNode(extra_charges=2))- monkeypatch.setattr(manager, "get_db_async_client", lambda: fake_db)- monkeypatch.setattr(billing, "get_db_client", lambda: fake_db)- # get_block is called by LogMetadata construction in on_node_execution.- monkeypatch.setattr(- manager,- "get_block",- lambda block_id: MagicMock(name="FakeBlock"),- )- # Persistence + cost logging are not under test here.- monkeypatch.setattr(- manager,- "async_update_node_execution_status",- AsyncMock(return_value=None),- )- monkeypatch.setattr(- manager,- "async_update_graph_execution_state",- AsyncMock(return_value=None),- )- monkeypatch.setattr(- manager,- "log_system_credential_cost",- AsyncMock(return_value=None),- )-- proc = manager.ExecutionProcessor.__new__(manager.ExecutionProcessor)-- # Control the status returned by the inner execution call.- inner_result = {"status": ExecutionStatus.COMPLETED, "llm_call_count": 3}-- async def fake_inner(- self,- *,- node,- node_exec,- node_exec_progress,- stats,- db_client,- log_metadata,- nodes_input_masks=None,- nodes_to_skip=None,- ):- stats.llm_call_count = inner_result["llm_call_count"]- return MagicMock(wall_time=0.1, cpu_time=0.1), inner_result["status"]-- monkeypatch.setattr(- manager.ExecutionProcessor,- "_on_node_execution",- fake_inner,- )-- async def fake_charge_extra(node_exec, extra_count):- calls["charge_extra_runtime_cost"].append(extra_count)- return (extra_count * 10, 500)-- monkeypatch.setattr(billing, "charge_extra_runtime_cost", fake_charge_extra)-- def fake_low_balance(db_client, user_id, current_balance, transaction_cost):- calls["handle_low_balance"].append(- {- "user_id": user_id,- "current_balance": current_balance,- "transaction_cost": transaction_cost,- }- )-- monkeypatch.setattr(billing, "handle_low_balance", fake_low_balance)-- def fake_notif(db_client, user_id, graph_id, e):- calls["handle_insufficient_funds_notif"].append(- {"user_id": user_id, "graph_id": graph_id, "error": e}- )-- monkeypatch.setattr(billing, "handle_insufficient_funds_notif", fake_notif)-- return proc, calls, inner_result, fake_db, NodeExecutionStats---async def test_on_node_execution_charges_extra_iterations_when_gate_passes(- gated_processor,-):- """COMPLETED + extra_runtime_cost > 0 + not dry_run → charged."""-- proc, calls, inner, fake_db, _ = gated_processor- inner["status"] = ExecutionStatus.COMPLETED- inner["llm_call_count"] = 3 # → extra_charges = 2- fake_db.get_node = AsyncMock(return_value=_FakeNode(extra_charges=2))-- stats_pair = (- MagicMock(- node_count=0, nodes_cputime=0, nodes_walltime=0, cost=0, node_error_count=0- ),- threading.Lock(),- )- await proc.on_node_execution(- node_exec=_make_node_exec(dry_run=False),- node_exec_progress=MagicMock(),- nodes_input_masks=None,- graph_stats_pair=stats_pair,- )- assert calls["charge_extra_runtime_cost"] == [2]- # handle_low_balance must be called with the remaining balance returned by- # charge_extra_runtime_cost (500) so users are alerted when balance drops low.- assert len(calls["handle_low_balance"]) == 1---async def test_on_node_execution_skips_when_status_not_completed(gated_processor):-- proc, calls, inner, fake_db, _ = gated_processor- inner["status"] = ExecutionStatus.FAILED- inner["llm_call_count"] = 5- fake_db.get_node = AsyncMock(return_value=_FakeNode(extra_charges=4))-- stats_pair = (- MagicMock(- node_count=0, nodes_cputime=0, nodes_walltime=0, cost=0, node_error_count=0- ),- threading.Lock(),- )- await proc.on_node_execution(- node_exec=_make_node_exec(dry_run=False),- node_exec_progress=MagicMock(),- nodes_input_masks=None,- graph_stats_pair=stats_pair,- )- assert calls["charge_extra_runtime_cost"] == []---async def test_on_node_execution_skips_when_extra_charges_zero(gated_processor):-- proc, calls, inner, fake_db, _ = gated_processor- inner["status"] = ExecutionStatus.COMPLETED- inner["llm_call_count"] = 5- # Block returns 0 extra charges (base class default)- fake_db.get_node = AsyncMock(return_value=_FakeNode(extra_charges=0))-- stats_pair = (- MagicMock(- node_count=0, nodes_cputime=0, nodes_walltime=0, cost=0, node_error_count=0- ),- threading.Lock(),- )- await proc.on_node_execution(- node_exec=_make_node_exec(dry_run=False),- node_exec_progress=MagicMock(),- nodes_input_masks=None,- graph_stats_pair=stats_pair,- )- assert calls["charge_extra_runtime_cost"] == []---async def test_on_node_execution_skips_when_dry_run(gated_processor):-- proc, calls, inner, fake_db, _ = gated_processor- inner["status"] = ExecutionStatus.COMPLETED- inner["llm_call_count"] = 5- fake_db.get_node = AsyncMock(return_value=_FakeNode(extra_charges=4))-- stats_pair = (- MagicMock(- node_count=0, nodes_cputime=0, nodes_walltime=0, cost=0, node_error_count=0- ),- threading.Lock(),- )- await proc.on_node_execution(- node_exec=_make_node_exec(dry_run=True),- node_exec_progress=MagicMock(),- nodes_input_masks=None,- graph_stats_pair=stats_pair,- )- assert calls["charge_extra_runtime_cost"] == []---async def test_on_node_execution_insufficient_balance_records_error_and_notifies(- monkeypatch,- gated_processor,-):- """When extra-iteration charging fails with InsufficientBalanceError:-- - the run still reports COMPLETED (the work is already done)- - execution_stats.error is NOT set (would flip node_error_count and- leak balance amounts into persisted node_stats — see manager.py- comment in the IBE handler)- - _handle_insufficient_funds_notif is called so the user is notified- - the structured ERROR log is the alerting hook- """-- proc, calls, inner, fake_db, _ = gated_processor- inner["status"] = ExecutionStatus.COMPLETED- inner["llm_call_count"] = 4- fake_db.get_node = AsyncMock(return_value=_FakeNode(extra_charges=3))-- async def raise_ibe(node_exec, extra_count):- raise InsufficientBalanceError(- user_id=node_exec.user_id,- message="Insufficient balance",- balance=0,- amount=extra_count * 10,- )-- monkeypatch.setattr(billing, "charge_extra_runtime_cost", raise_ibe)-- stats_pair = (- MagicMock(- node_count=0, nodes_cputime=0, nodes_walltime=0, cost=0, node_error_count=0- ),- threading.Lock(),- )- result_stats = await proc.on_node_execution(- node_exec=_make_node_exec(dry_run=False),- node_exec_progress=MagicMock(),- nodes_input_masks=None,- graph_stats_pair=stats_pair,- )- # error stays None — node ran to completion, only the post-hoc- # charge failed. Setting .error would (a) flip node_error_count++- # creating an "errored COMPLETED node" inconsistency, and (b) leak- # balance amounts into persisted node_stats.- assert result_stats.error is None- # User notification fired.- assert len(calls["handle_insufficient_funds_notif"]) == 1- assert calls["handle_insufficient_funds_notif"][0]["user_id"] == "u"---# ── Orchestrator _execute_single_tool_with_manager charging gates ──---async def _run_tool_exec_with_stats(- *,- dry_run: bool,- tool_stats_error,- charge_node_usage_mock=None,-):- """Invoke _execute_single_tool_with_manager against fully mocked deps- and return (charge_call_count, merge_stats_calls).-- Used to prove the dry_run and error guards around charge_node_usage- behave as documented, and that InsufficientBalanceError propagates.- """- block = OrchestratorBlock()-- # Mocked async DB client used inside orchestrator.- mock_db_client = AsyncMock()- mock_target_node = MagicMock()- mock_target_node.block_id = "test-block-id"- mock_target_node.input_default = {}- mock_db_client.get_node.return_value = mock_target_node- mock_node_exec_result = MagicMock()- mock_node_exec_result.node_exec_id = "test-tool-exec-id"- mock_db_client.upsert_execution_input.return_value = (- mock_node_exec_result,- {"query": "t"},- )- mock_db_client.get_execution_outputs_by_node_exec_id.return_value = {"result": "ok"}-- # ExecutionProcessor mock: on_node_execution returns supplied error.- mock_processor = AsyncMock()- mock_processor.running_node_execution = defaultdict(MagicMock)- mock_processor.execution_stats = MagicMock()- mock_processor.execution_stats_lock = threading.Lock()- mock_node_stats = MagicMock()- mock_node_stats.error = tool_stats_error- mock_processor.on_node_execution = AsyncMock(return_value=mock_node_stats)- mock_processor.charge_node_usage = charge_node_usage_mock or AsyncMock(- return_value=(10, 990)- )-- # Build a tool_info shaped like _build_tool_info_from_args output.- tool_call = MagicMock()- tool_call.id = "call-1"- tool_call.name = "search_keywords"- tool_call.arguments = '{"query":"t"}'- tool_def = {- "type": "function",- "function": {- "name": "search_keywords",- "_sink_node_id": "test-sink-node-id",- "_field_mapping": {},- "parameters": {- "properties": {"query": {"type": "string"}},- "required": ["query"],- },- },- }- tool_info = OrchestratorBlock._build_tool_info_from_args(- tool_call_id="call-1",- tool_name="search_keywords",- tool_args={"query": "t"},- tool_def=tool_def,- )-- exec_params = ExecutionParams(- user_id="u",- graph_id="g",- node_id="n",- graph_version=1,- graph_exec_id="ge",- node_exec_id="ne",- execution_context=ExecutionContext(- human_in_the_loop_safe_mode=False, dry_run=dry_run- ),- )-- with patch(- "backend.blocks.orchestrator.get_database_manager_async_client",- return_value=mock_db_client,- ):- try:- await block._execute_single_tool_with_manager(- tool_info, exec_params, mock_processor, responses_api=False- )- raised = None- except Exception as e:- raised = e-- return mock_processor.charge_node_usage, raised---async def test_tool_execution_skips_charging_on_dry_run():- """dry_run=True → charge_node_usage is NOT called."""- charge_mock, raised = await _run_tool_exec_with_stats(- dry_run=True, tool_stats_error=None- )- assert raised is None- assert charge_mock.call_count == 0---async def test_tool_execution_skips_charging_on_failed_tool():- """tool_node_stats.error is an Exception → charge_node_usage NOT called."""- charge_mock, raised = await _run_tool_exec_with_stats(- dry_run=False, tool_stats_error=RuntimeError("tool blew up")- )- assert raised is None- assert charge_mock.call_count == 0---async def test_tool_execution_skips_charging_on_cancelled_tool():- """Cancellation (BaseException subclass) → charge_node_usage NOT called.-- Guards the fix for sentry's BaseException concern: the old- `isinstance(error, Exception)` check would have treated CancelledError- as "no error" and billed the user for a terminated run.- """- import asyncio as _asyncio-- charge_mock, raised = await _run_tool_exec_with_stats(- dry_run=False, tool_stats_error=_asyncio.CancelledError()- )- assert raised is None- assert charge_mock.call_count == 0---async def test_tool_execution_insufficient_balance_propagates():- """InsufficientBalanceError from charge_node_usage must propagate out.-- If this leaked into a ToolCallResult the LLM loop would keep running- with 'tool failed' errors and the user would get unpaid work.- """- raising_charge = AsyncMock(- side_effect=InsufficientBalanceError(- user_id="u", message="nope", balance=0, amount=10- )- )- _, raised = await _run_tool_exec_with_stats(- dry_run=False,- tool_stats_error=None,- charge_node_usage_mock=raising_charge,- )- assert isinstance(raised, InsufficientBalanceError)---async def test_tool_execution_on_node_execution_returns_none_sets_is_error():- """on_node_execution returning None (swallowed by @async_error_logged) must- result in a tool response with _is_error=True so the LLM loop knows the- tool failed and does not treat a silent error as a successful execution.- """- block = OrchestratorBlock()-- mock_db_client = AsyncMock()- mock_target_node = MagicMock()- mock_target_node.block_id = "test-block-id"- mock_target_node.input_default = {}- mock_db_client.get_node.return_value = mock_target_node- mock_node_exec_result = MagicMock()- mock_node_exec_result.node_exec_id = "test-tool-exec-id"- mock_db_client.upsert_execution_input.return_value = (- mock_node_exec_result,- {"query": "t"},- )-- mock_processor = AsyncMock()- mock_processor.running_node_execution = defaultdict(MagicMock)- mock_processor.execution_stats = MagicMock()- mock_processor.execution_stats_lock = threading.Lock()- # on_node_execution returns None — simulates @async_error_logged(swallow=True)- # swallowing an internal error- mock_processor.on_node_execution = AsyncMock(return_value=None)-- tool_call = MagicMock()- tool_call.id = "call-none"- tool_call.name = "search_keywords"- tool_call.arguments = '{"query":"t"}'- tool_def = {- "type": "function",- "function": {- "name": "search_keywords",- "_sink_node_id": "test-sink-node-id",- "_field_mapping": {},- "parameters": {- "properties": {"query": {"type": "string"}},- "required": ["query"],- },- },- }- tool_info = OrchestratorBlock._build_tool_info_from_args(- tool_call_id="call-none",- tool_name="search_keywords",- tool_args={"query": "t"},- tool_def=tool_def,- )-- exec_params = ExecutionParams(- user_id="u",- graph_id="g",- node_id="n",- graph_version=1,- graph_exec_id="ge",- node_exec_id="ne",- execution_context=ExecutionContext(- human_in_the_loop_safe_mode=False, dry_run=False- ),- )-- with patch(- "backend.blocks.orchestrator.get_database_manager_async_client",- return_value=mock_db_client,- ):- resp = await block._execute_single_tool_with_manager(- tool_info, exec_params, mock_processor, responses_api=False- )-- assert resp.get("_is_error") is True- # charge_node_usage must NOT be called for a failed tool execution- mock_processor.charge_node_usage.assert_not_called()---# ── on_node_execution FAILED + InsufficientBalanceError notification ──---async def test_on_node_execution_failed_ibe_sends_notification(- monkeypatch,- gated_processor,-):- """When status == FAILED and execution_stats.error is InsufficientBalanceError,- _handle_insufficient_funds_notif must be called.-- This path fires when a nested tool charge inside the orchestrator raises- InsufficientBalanceError, which propagates out of the block's run() generator- and is caught by _on_node_execution's broad except, setting status=FAILED and- execution_stats.error=IBE. on_node_execution's post-execution block then- sends the user notification so they understand why the run stopped.- """-- proc, calls, inner, fake_db, NodeExecutionStats = gated_processor- ibe = InsufficientBalanceError(- user_id="u",- message="Insufficient balance",- balance=0,- amount=30,- )-- # Simulate _on_node_execution returning FAILED with IBE in stats.error.- async def fake_inner_failed(- self,- *,- node,- node_exec,- node_exec_progress,- stats,- db_client,- log_metadata,- nodes_input_masks=None,- nodes_to_skip=None,- ):- stats.error = ibe- return MagicMock(wall_time=0.1, cpu_time=0.1), ExecutionStatus.FAILED-- monkeypatch.setattr(- manager.ExecutionProcessor,- "_on_node_execution",- fake_inner_failed,- )- fake_db.get_node = AsyncMock(return_value=_FakeNode(extra_charges=0))-- stats_pair = (- MagicMock(- node_count=0, nodes_cputime=0, nodes_walltime=0, cost=0, node_error_count=0- ),- threading.Lock(),- )- await proc.on_node_execution(- node_exec=_make_node_exec(dry_run=False),- node_exec_progress=MagicMock(),- nodes_input_masks=None,- graph_stats_pair=stats_pair,- )- # The notification must have fired so the user knows why their run stopped.- assert len(calls["handle_insufficient_funds_notif"]) == 1- assert calls["handle_insufficient_funds_notif"][0]["user_id"] == "u"- # charge_extra_runtime_cost must NOT be called — status is FAILED.- assert calls["charge_extra_runtime_cost"] == []---# ── Billing leak: non-IBE exception during extra-iteration charging ──---async def test_on_node_execution_non_ibe_billing_failure_keeps_completed(- monkeypatch,- gated_processor,-):- """When charge_extra_runtime_cost raises a non-IBE exception (e.g. DB outage):-- - execution_stats.error stays None (node ran to completion)- - status stays COMPLETED (work already done)- - the billing_leak error is logged but does not corrupt execution_stats- """- proc, calls, inner, fake_db, _ = gated_processor- inner["status"] = ExecutionStatus.COMPLETED- inner["llm_call_count"] = 4- fake_db.get_node = AsyncMock(return_value=_FakeNode(extra_charges=3))-- async def raise_conn_error(node_exec, extra_count):- raise ConnectionError("DB connection lost")-- monkeypatch.setattr(billing, "charge_extra_runtime_cost", raise_conn_error)-- stats_pair = (- MagicMock(- node_count=0,- nodes_cputime=0,- nodes_walltime=0,- cost=0,- node_error_count=0,- ),- threading.Lock(),- )- result_stats = await proc.on_node_execution(- node_exec=_make_node_exec(dry_run=False),- node_exec_progress=MagicMock(),- nodes_input_masks=None,- graph_stats_pair=stats_pair,- )- # error stays None — node completed, only billing failed.- assert result_stats.error is None- # No notification was sent (only IBE triggers notification).- assert len(calls["handle_insufficient_funds_notif"]) == 0---# ── _charge_usage with execution_count=0 ──---class TestChargeUsageZeroExecutionCount:- """Verify _charge_usage(node_exec, 0) does not invoke execution_usage_cost."""-- def test_execution_count_zero_skips_execution_tier(self, monkeypatch):- """_charge_usage with execution_count=0 must not call execution_usage_cost."""- execution_tier_called = []-- def fake_execution_usage_cost(count):- execution_tier_called.append(count)- return (100, count)-- spent: list[int] = []-- class FakeDb:- def spend_credits(self, *, user_id, cost, metadata):- spent.append(cost)- return 500-- fake_block = MagicMock()- fake_block.name = "FakeBlock"-- monkeypatch.setattr(billing, "get_db_client", lambda: FakeDb())- monkeypatch.setattr(billing, "get_block", lambda block_id: fake_block)- monkeypatch.setattr(- billing,- "block_usage_cost",- lambda block, input_data, **_kw: (10, {}),- )- monkeypatch.setattr(billing, "execution_usage_cost", fake_execution_usage_cost)-- ne = MagicMock()- ne.user_id = "u"- ne.graph_exec_id = "ge"- ne.graph_id = "g"- ne.node_exec_id = "ne"- ne.node_id = "n"- ne.block_id = "b"- ne.inputs = {}-- total_cost, remaining = billing.charge_usage(ne, 0)- assert total_cost == 10 # block cost only- assert remaining == 500- assert spent == [10]- # execution_usage_cost must NOT have been called- assert execution_tier_called == []diff --git a/autogpt_platform/backend/backend/blocks/zerobounce/validate_emails.py b/autogpt_platform/backend/backend/blocks/zerobounce/validate_emails.pyindex 6a461b4aa8..57c0ed3ef7 100644--- a/autogpt_platform/backend/backend/blocks/zerobounce/validate_emails.py+++ b/autogpt_platform/backend/backend/blocks/zerobounce/validate_emails.py@@ -21,7 +21,7 @@ from backend.blocks.zerobounce._auth import (ZeroBounceCredentials,ZeroBounceCredentialsInput,)-from backend.data.model import CredentialsField, SchemaField+from backend.data.model import CredentialsField, NodeExecutionStats, SchemaFieldclass Response(BaseModel):@@ -140,20 +140,22 @@ class ValidateEmailsBlock(Block):)],test_mock={- "validate_email": lambda email, ip_address, credentials: ZBValidateResponse(- data={- "address": email,- "status": ZBValidateStatus.valid,- "sub_status": ZBValidateSubStatus.allowed,- "account": "test",- "domain": "test.com",- "did_you_mean": None,- "domain_age_days": None,- "free_email": False,- "mx_found": False,- "mx_record": None,- "smtp_provider": None,- }+ "validate_email": lambda email, ip_address, credentials: (+ ZBValidateResponse(+ data={+ "address": email,+ "status": ZBValidateStatus.valid,+ "sub_status": ZBValidateSubStatus.allowed,+ "account": "test",+ "domain": "test.com",+ "did_you_mean": None,+ "domain_age_days": None,+ "free_email": False,+ "mx_found": False,+ "mx_record": None,+ "smtp_provider": None,+ }+ ))},)@@ -176,6 +178,13 @@ class ValidateEmailsBlock(Block):input_data.email, input_data.ip_address, credentials)+ # ZeroBounce bills $0.008 per validated email on the paid tier.+ # Routed through COST_USD so platform cost telemetry captures real+ # USD spend; the resolver still bills 2 credits per call.+ self.merge_stats(+ NodeExecutionStats(provider_cost=0.008, provider_cost_type="cost_usd")+ )+response_model = Response(**response.__dict__)yield "response", response_modeldiff --git a/autogpt_platform/backend/backend/copilot/tools/helpers_test.py b/autogpt_platform/backend/backend/copilot/tools/helpers_test.pyindex 8494271d93..ee513e22ef 100644--- a/autogpt_platform/backend/backend/copilot/tools/helpers_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/helpers_test.py@@ -332,11 +332,15 @@ class TestNewlyRegisteredBlockCosts:assert BLOCK_COSTS[SaveCampaignSequencesBlock][0].cost_amount == 1def test_zerobounce_validate_block_registered(self):+ from backend.blocks._base import BlockCostTypefrom backend.blocks.zerobounce.validate_emails import ValidateEmailsBlockfrom backend.data.block_cost_config import BLOCK_COSTSassert ValidateEmailsBlock in BLOCK_COSTS- assert BLOCK_COSTS[ValidateEmailsBlock][0].cost_amount == 2+ # COST_USD with multiplier 250 → ceil(provider_cost_usd * 250) credits.+ # Block reports $0.008/call via merge_stats, so effective charge is 2.+ assert BLOCK_COSTS[ValidateEmailsBlock][0].cost_type == BlockCostType.COST_USD+ assert BLOCK_COSTS[ValidateEmailsBlock][0].cost_amount == 250def test_claude_code_block_registered(self):"""ClaudeCodeBlock spawns an E2B sandbox + runs Claude inside it.diff --git a/autogpt_platform/backend/backend/data/block_cost_config.py b/autogpt_platform/backend/backend/data/block_cost_config.pyindex 9659662004..aa5a110089 100644--- a/autogpt_platform/backend/backend/data/block_cost_config.py+++ b/autogpt_platform/backend/backend/data/block_cost_config.py@@ -1,6 +1,13 @@-from typing import Type+import math+from typing import TYPE_CHECKING, Type++from pydantic import BaseModelfrom backend.blocks._base import Block, BlockCost, BlockCostType+from backend.data.block import BlockInput++if TYPE_CHECKING:+ from backend.data.model import NodeExecutionStatsfrom backend.blocks.ai_image_customizer import AIImageCustomizerBlock, GeminiImageModelfrom backend.blocks.ai_image_generator_block import AIImageGeneratorBlock, ImageGenModelfrom backend.blocks.ai_music_generator import AIMusicGeneratorBlock@@ -210,11 +217,169 @@ for model in LlmModel:raise ValueError(f"Missing MODEL_COST for model: {model}")+class TokenRate(BaseModel):+ """Per-token credit rates for a specific model.++ Each field is credits per 1,000,000 tokens of the corresponding kind.+ Cache-read and cache-write are 0 by default for providers that don't+ surface them (most non-Anthropic). Amounts use float so small rates+ (e.g. 0.2 credits / 1M Gemini Flash input) don't round away.+ """++ input: float+ output: float+ cache_read: float = 0.0+ cache_creation: float = 0.0+++# TOKEN_COST populates gradually as we migrate LLM blocks to the TOKENS+# cost type. Entries not yet listed fall back to the flat MODEL_COST tier+# via the RUN-based LLM_COST list. Rates below are credits/1M tokens at the+# current credit-to-USD conversion (1 credit ≈ $0.01), with a 1.5x margin+# over the published provider price.+TOKEN_COST: dict[LlmModel, TokenRate] = {+ # Anthropic Opus ($15/$75/$1.50/$18.75 per 1M).+ LlmModel.CLAUDE_4_1_OPUS: TokenRate(+ input=2250, output=11250, cache_read=225, cache_creation=2812+ ),+ LlmModel.CLAUDE_4_OPUS: TokenRate(+ input=2250, output=11250, cache_read=225, cache_creation=2812+ ),+ LlmModel.CLAUDE_4_6_OPUS: TokenRate(+ input=2250, output=11250, cache_read=225, cache_creation=2812+ ),+ LlmModel.CLAUDE_4_5_OPUS: TokenRate(+ input=2250, output=11250, cache_read=225, cache_creation=2812+ ),+ # Anthropic Sonnet ($3/$15/$0.30/$3.75).+ LlmModel.CLAUDE_4_SONNET: TokenRate(+ input=450, output=2250, cache_read=45, cache_creation=562+ ),+ LlmModel.CLAUDE_4_6_SONNET: TokenRate(+ input=450, output=2250, cache_read=45, cache_creation=562+ ),+ LlmModel.CLAUDE_4_5_SONNET: TokenRate(+ input=450, output=2250, cache_read=45, cache_creation=562+ ),+ # Anthropic Haiku ($0.80/$4/$0.08/$1).+ LlmModel.CLAUDE_4_5_HAIKU: TokenRate(+ input=120, output=600, cache_read=12, cache_creation=150+ ),+ LlmModel.CLAUDE_3_HAIKU: TokenRate(input=37, output=187),+ # OpenAI+ LlmModel.GPT5_2: TokenRate(input=600, output=2400),+ LlmModel.GPT5_1: TokenRate(input=450, output=1800),+ LlmModel.GPT5: TokenRate(input=375, output=1500),+ LlmModel.GPT5_MINI: TokenRate(input=22, output=90),+ LlmModel.GPT5_NANO: TokenRate(input=7, output=30),+ LlmModel.GPT5_CHAT: TokenRate(input=375, output=1500),+ LlmModel.GPT4O: TokenRate(input=375, output=1500),+ LlmModel.GPT4O_MINI: TokenRate(input=22, output=90),+ LlmModel.GPT41: TokenRate(input=300, output=1200),+ LlmModel.GPT41_MINI: TokenRate(input=60, output=240),+ LlmModel.GPT4_TURBO: TokenRate(input=1500, output=4500),+ LlmModel.O3: TokenRate(input=1500, output=6000),+ LlmModel.O3_MINI: TokenRate(input=165, output=660),+ LlmModel.O1: TokenRate(input=2250, output=9000),+ LlmModel.O1_MINI: TokenRate(input=165, output=660),+ # Google Gemini+ LlmModel.GEMINI_2_5_PRO: TokenRate(input=187, output=750),+ LlmModel.GEMINI_2_5_PRO_PREVIEW: TokenRate(input=187, output=750),+ LlmModel.GEMINI_2_5_FLASH: TokenRate(input=11, output=45),+ LlmModel.GEMINI_2_5_FLASH_LITE_PREVIEW: TokenRate(input=5, output=22),+ LlmModel.GEMINI_2_0_FLASH: TokenRate(input=11, output=45),+ LlmModel.GEMINI_2_0_FLASH_LITE: TokenRate(input=5, output=22),+ LlmModel.GEMINI_3_1_PRO_PREVIEW: TokenRate(input=750, output=3000),+ LlmModel.GEMINI_3_FLASH_PREVIEW: TokenRate(input=15, output=60),+ LlmModel.GEMINI_3_1_FLASH_LITE_PREVIEW: TokenRate(input=5, output=22),+ # xAI Grok+ LlmModel.GROK_3: TokenRate(input=450, output=2250),+ LlmModel.GROK_4: TokenRate(input=2250, output=11250),+ LlmModel.GROK_4_FAST: TokenRate(input=37, output=150),+ LlmModel.GROK_4_1_FAST: TokenRate(input=37, output=150),+ LlmModel.GROK_4_20: TokenRate(input=750, output=3000),+ LlmModel.GROK_CODE_FAST_1: TokenRate(input=37, output=150),+ # DeepSeek+ LlmModel.DEEPSEEK_CHAT: TokenRate(input=40, output=165),+ LlmModel.DEEPSEEK_R1_0528: TokenRate(input=82, output=328),+ # Mistral+ LlmModel.MISTRAL_LARGE_3: TokenRate(input=300, output=900),+ LlmModel.MISTRAL_MEDIUM_3_1: TokenRate(input=405, output=1215),+ LlmModel.MISTRAL_SMALL_3_2: TokenRate(input=15, output=45),+ LlmModel.MISTRAL_NEMO: TokenRate(input=15, output=45),+ LlmModel.CODESTRAL: TokenRate(input=22, output=67),+ # Cohere+ LlmModel.COHERE_COMMAND_R_08_2024: TokenRate(input=22, output=90),+ LlmModel.COHERE_COMMAND_R_PLUS_08_2024: TokenRate(input=375, output=1500),+ LlmModel.COHERE_COMMAND_A_03_2025: TokenRate(input=187, output=750),+ # Moonshot Kimi+ LlmModel.KIMI_K2: TokenRate(input=90, output=375),+ LlmModel.KIMI_K2_0905: TokenRate(input=90, output=375),+ LlmModel.KIMI_K2_5: TokenRate(input=90, output=375),+ LlmModel.KIMI_K2_6: TokenRate(input=225, output=900),+ LlmModel.KIMI_K2_THINKING: TokenRate(input=225, output=900),+ # Perplexity Sonar+ LlmModel.PERPLEXITY_SONAR: TokenRate(input=30, output=30),+ LlmModel.PERPLEXITY_SONAR_PRO: TokenRate(input=150, output=150),+ LlmModel.PERPLEXITY_SONAR_REASONING_PRO: TokenRate(input=150, output=750),+ LlmModel.PERPLEXITY_SONAR_DEEP_RESEARCH: TokenRate(input=750, output=3750),+ # Groq (LLama + OpenAI OSS)+ LlmModel.LLAMA3_3_70B: TokenRate(input=88, output=118),+ LlmModel.LLAMA3_1_8B: TokenRate(input=7, output=12),+ LlmModel.META_LLAMA_4_SCOUT: TokenRate(input=22, output=75),+ LlmModel.META_LLAMA_4_MAVERICK: TokenRate(input=45, output=97),+ LlmModel.OPENAI_GPT_OSS_120B: TokenRate(input=22, output=67),+ LlmModel.OPENAI_GPT_OSS_20B: TokenRate(input=15, output=45),+}+++def compute_token_credits(+ input_data: BlockInput, stats: "NodeExecutionStats | None"+) -> int:+ """Compute the credit charge for a TOKENS-billed LLM call from stats.++ Falls back to MODEL_COST[model] (the per-model flat tier) when the+ model has no TOKEN_COST entry or stats haven't been populated yet+ (pre-flight). Callers in block_usage_cost handle the TOKENS branch.+ """+ if stats is None:+ # Pre-flight — use the flat MODEL_COST entry as an estimate.+ raw_model = input_data.get("model")+ model = _lookup_llm_model(raw_model)+ return MODEL_COST.get(model, 0) if model else 0++ raw_model = input_data.get("model")+ model = _lookup_llm_model(raw_model)+ rate = TOKEN_COST.get(model) if model else None+ if rate is None:+ # Unmapped model — charge the per-call flat tier so we don't under-bill.+ return MODEL_COST.get(model, 0) if model else 0++ total = (+ stats.input_token_count * rate.input+ + stats.output_token_count * rate.output+ + stats.cache_read_token_count * rate.cache_read+ + stats.cache_creation_token_count * rate.cache_creation+ )+ return max(0, math.ceil(total / 1_000_000))+++def _lookup_llm_model(raw: "str | LlmModel | None") -> "LlmModel | None":+ if raw is None:+ return None+ if isinstance(raw, LlmModel):+ return raw+ try:+ return LlmModel(raw)+ except ValueError:+ return None++LLM_COST = (# Anthropic Models[BlockCost(- cost_type=BlockCostType.RUN,+ cost_type=BlockCostType.TOKENS,cost_filter={"model": model,"credentials": {@@ -231,7 +396,7 @@ LLM_COST = (# OpenAI Models+ [BlockCost(- cost_type=BlockCostType.RUN,+ cost_type=BlockCostType.TOKENS,cost_filter={"model": model,"credentials": {@@ -248,7 +413,7 @@ LLM_COST = (# Groq Models+ [BlockCost(- cost_type=BlockCostType.RUN,+ cost_type=BlockCostType.TOKENS,cost_filter={"model": model,"credentials": {"id": groq_credentials.id},@@ -261,7 +426,7 @@ LLM_COST = (# Open Router Models+ [BlockCost(- cost_type=BlockCostType.RUN,+ cost_type=BlockCostType.TOKENS,cost_filter={"model": model,"credentials": {@@ -278,7 +443,7 @@ LLM_COST = (# Llama API Models+ [BlockCost(- cost_type=BlockCostType.RUN,+ cost_type=BlockCostType.TOKENS,cost_filter={"model": model,"credentials": {@@ -295,7 +460,7 @@ LLM_COST = (# v0 by Vercel Models+ [BlockCost(- cost_type=BlockCostType.RUN,+ cost_type=BlockCostType.TOKENS,cost_filter={"model": model,"credentials": {@@ -312,7 +477,7 @@ LLM_COST = (# AI/ML Api Models+ [BlockCost(- cost_type=BlockCostType.RUN,+ cost_type=BlockCostType.TOKENS,cost_filter={"model": model,"credentials": {@@ -379,9 +544,14 @@ BLOCK_COSTS: dict[Type[Block], list[BlockCost]] = {},)],+ # Jina Reader Search: $0.01/query on the paid tier. Block emits+ # merge_stats(provider_cost=0.01, cost_usd) so the COST_USD resolver+ # bills 1 platform credit per call AND the platform cost telemetry+ # captures real USD spend (RUN wouldn't populate costMicrodollars).SearchTheWebBlock: [BlockCost(- cost_amount=1,+ cost_amount=100,+ cost_type=BlockCostType.COST_USD,cost_filter={"credentials": {"id": jina_credentials.id,@@ -617,9 +787,17 @@ BLOCK_COSTS: dict[Type[Block], list[BlockCost]] = {},)],+ # Apollo Search blocks: bill per returned record. Blocks already emit+ # provider_cost=float(len(people/organizations)) with+ # provider_cost_type="items" via merge_stats, so ITEMS multiplies the+ # count by cost_amount post-flight. Pre-flight returns 0 (unknown+ # result count). enrich_info=True doubles the provider-side unit cost+ # (email enrichment), so we bill 2cr/person vs 1cr/person.SearchOrganizationsBlock: [BlockCost(- cost_amount=2,+ cost_amount=1,+ cost_type=BlockCostType.ITEMS,+ cost_divisor=2,cost_filter={"credentials": {"id": apollo_credentials.id,@@ -631,9 +809,10 @@ BLOCK_COSTS: dict[Type[Block], list[BlockCost]] = {],SearchPeopleBlock: [BlockCost(- cost_amount=10,+ cost_amount=2,+ cost_type=BlockCostType.ITEMS,cost_filter={- "enrich_info": False,+ "enrich_info": True,"credentials": {"id": apollo_credentials.id,"provider": apollo_credentials.provider,@@ -642,9 +821,10 @@ BLOCK_COSTS: dict[Type[Block], list[BlockCost]] = {},),BlockCost(- cost_amount=20,+ cost_amount=1,+ cost_type=BlockCostType.ITEMS,cost_filter={- "enrich_info": True,+ "enrich_info": False,"credentials": {"id": apollo_credentials.id,"provider": apollo_credentials.provider,@@ -954,9 +1134,13 @@ BLOCK_COSTS: dict[Type[Block], list[BlockCost]] = {)],# ZeroBounce: $16 / 2K validations = $0.008 per email. One email per call.+ # Block emits merge_stats(provider_cost=0.008, cost_usd) so the platform+ # cost telemetry records real USD; resolver bills 2 credits per call+ # (ceil(0.008 * 250)).ValidateEmailsBlock: [BlockCost(- cost_amount=2,+ cost_amount=250,+ cost_type=BlockCostType.COST_USD,cost_filter={"credentials": {"id": zerobounce_credentials.id,@@ -966,32 +1150,17 @@ BLOCK_COSTS: dict[Type[Block], list[BlockCost]] = {},)],- # ClaudeCodeBlock runs an E2B sandbox AND executes Claude Sonnet inside it.- # Real cost $0.50-$2/run; flat 100 credits is conservative until we pipe- # x-total-cost from the in-sandbox Claude calls into provider_cost.- ClaudeCodeBlock: [- BlockCost(- cost_amount=100,- cost_filter={- "e2b_credentials": {- "id": e2b_credentials.id,- "provider": e2b_credentials.provider,- "type": e2b_credentials.type,- }- },- )- ],- # Ayrshare post blocks use the @cost(...) decorator directly on each block- # class (see backend/blocks/ayrshare/_cost.py). They can't be listed here- # because post_to_*.py imports from backend.sdk, which imports from this- # module — registering via decorator avoids the circular import.- # E2B code-execution blocks: Hobby tier ~$0.000014/vCPU-s. A typical 30s- # sandbox with 2 vCPU is ~$0.00084. Flat 2 credits covers the floor with- # margin; accurate per-second billing happens via walltime-based resolver- # in the dynamic-pricing follow-up.+ # E2B code-execution blocks: Hobby tier ~$0.000014/vCPU-s × 2 vCPU ≈+ # $0.000028/s. Charge 1 credit per 10 seconds of walltime (~$0.0003)+ # — recovers infra cost with margin and scales with session length.+ # Pre-flight returns 0 (walltime unknown); reconciliation charges the+ # true walltime after the block finishes (manager.py calls+ # billing.charge_reconciled_usage on completion).ExecuteCodeBlock: [BlockCost(- cost_amount=2,+ cost_amount=1,+ cost_type=BlockCostType.SECOND,+ cost_divisor=10,cost_filter={"credentials": {"id": e2b_credentials.id,@@ -1003,7 +1172,9 @@ BLOCK_COSTS: dict[Type[Block], list[BlockCost]] = {],InstantiateCodeSandboxBlock: [BlockCost(- cost_amount=2,+ cost_amount=1,+ cost_type=BlockCostType.SECOND,+ cost_divisor=10,cost_filter={"credentials": {"id": e2b_credentials.id,@@ -1015,7 +1186,9 @@ BLOCK_COSTS: dict[Type[Block], list[BlockCost]] = {],ExecuteCodeStepBlock: [BlockCost(- cost_amount=2,+ cost_amount=1,+ cost_type=BlockCostType.SECOND,+ cost_divisor=10,cost_filter={"credentials": {"id": e2b_credentials.id,@@ -1025,12 +1198,13 @@ BLOCK_COSTS: dict[Type[Block], list[BlockCost]] = {},)],- # FAL video generation: $0.001-$0.02 per output second. A 5s clip costs- # us ~$0.05-$0.10 in practice. 10 credits is a safe floor until walltime- # billing lands.+ # FAL video generation: $0.001–$0.02 per output second. Charge 3 credits+ # per walltime second (~$0.03) — covers the median tier with margin and+ # scales fairly across short clips vs long renders.AIVideoGeneratorBlock: [BlockCost(- cost_amount=10,+ cost_amount=3,+ cost_type=BlockCostType.SECOND,cost_filter={"credentials": {"id": fal_credentials.id,@@ -1055,6 +1229,25 @@ BLOCK_COSTS: dict[Type[Block], list[BlockCost]] = {},)],+ # ClaudeCodeBlock runs an E2B sandbox AND executes Claude Sonnet inside it.+ # Real cost $0.50-$2/run; flat 100 credits is conservative until we pipe+ # x-total-cost from the in-sandbox Claude calls into provider_cost.+ ClaudeCodeBlock: [+ BlockCost(+ cost_amount=100,+ cost_filter={+ "e2b_credentials": {+ "id": e2b_credentials.id,+ "provider": e2b_credentials.provider,+ "type": e2b_credentials.type,+ }+ },+ )+ ],+ # Ayrshare post blocks use the @cost(...) decorator directly on each block+ # class (see backend/blocks/ayrshare/_cost.py). They can't be listed here+ # because post_to_*.py imports from backend.sdk, which imports from this+ # module — registering via decorator avoids the circular import.# Jina chunking: $0.02/1M tokens. Flat 1 credit floor so the block is not# wallet-free; embedding/search already have their own entries.JinaChunkingBlock: [diff --git a/autogpt_platform/backend/backend/data/block_cost_config_test.py b/autogpt_platform/backend/backend/data/block_cost_config_test.pyindex 7b69517c16..88c5261f15 100644--- a/autogpt_platform/backend/backend/data/block_cost_config_test.py+++ b/autogpt_platform/backend/backend/data/block_cost_config_test.py@@ -111,7 +111,14 @@ def test_bannerbear_base_cost_is_three_credits():assert cost == 3-def test_e2b_sandbox_blocks_have_two_credit_floor():+def test_e2b_sandbox_blocks_bill_per_walltime_second():+ """E2B uses SECOND cost_type with cost_divisor=10 (1 credit per 10s).++ Pre-flight (no stats) returns 0 — walltime unknown until the block runs.+ Post-flight bills at the real walltime via charge_reconciled_usage.+ """+ from backend.data.model import NodeExecutionStats+creds = {"credentials": {"id": e2b_credentials.id,@@ -124,22 +131,34 @@ def test_e2b_sandbox_blocks_have_two_credit_floor():InstantiateCodeSandboxBlock,ExecuteCodeStepBlock,):+ # Pre-flight: unknown walltime ⇒ 0 credits (no gating on future cost).cost, _ = block_usage_cost(block_cls(), creds)- assert cost == 2, f"{block_cls.__name__} floor must be 2 credits, got {cost}"+ assert cost == 0, f"{block_cls.__name__} pre-flight must be 0, got {cost}"+ # Post-flight: 25s ⇒ ceil(25/10) = 3 credits.+ stats = NodeExecutionStats(walltime=25.0)+ cost, _ = block_usage_cost(block_cls(), creds, stats=stats)+ assert cost == 3, f"{block_cls.__name__} @ 25s must be 3 credits, got {cost}"++def test_fal_video_generator_bills_per_walltime_second():+ """FAL AIVideoGeneratorBlock uses SECOND with cost_amount=3 (3 credits/s)."""+ from backend.data.model import NodeExecutionStats-def test_fal_video_generator_has_ten_credit_floor():+ creds = {+ "credentials": {+ "id": fal_credentials.id,+ "provider": fal_credentials.provider,+ "type": fal_credentials.type,+ }+ }+ # Pre-flight: unknown walltime ⇒ 0 credits.+ cost, _ = block_usage_cost(AIVideoGeneratorBlock(), creds)+ assert cost == 0+ # Post-flight: 5s clip ⇒ 3 * 5 = 15 credits.cost, _ = block_usage_cost(- AIVideoGeneratorBlock(),- {- "credentials": {- "id": fal_credentials.id,- "provider": fal_credentials.provider,- "type": fal_credentials.type,- }- },+ AIVideoGeneratorBlock(), creds, stats=NodeExecutionStats(walltime=5.0))- assert cost == 10+ assert cost == 15def test_transcribe_youtube_has_one_credit_tooling_floor():diff --git a/autogpt_platform/backend/backend/data/model.py b/autogpt_platform/backend/backend/data/model.pyindex a8cc6e8b16..f1d3e1630e 100644--- a/autogpt_platform/backend/backend/data/model.py+++ b/autogpt_platform/backend/backend/data/model.py@@ -487,7 +487,6 @@ class UserMetadataRaw(TypedDict, total=False):class UserIntegrations(BaseModel):-class ManagedCredentials(BaseModel):"""Integration credentials managed by us, rather than by the user"""@@ -864,8 +863,16 @@ class NodeExecutionStats(BaseModel):cache_read_token_count: int = 0cache_creation_token_count: int = 0cost: int = 0- extra_cost: int = 0+ # Post-flight adjustment to the pre-flight ``cost`` estimate. Three writers:+ # 1. charge_reconciled_usage — dynamic cost delta (TOKENS/SECOND/ITEMS/+ # COST_USD); can be negative when a TOKENS floor over-estimated.+ # 2. OrchestratorBlock — sub-block cost roll-up for run_block tool calls+ # (debit already happened on the child; this is reporting-only).+ # 3. AgentExecutorBlock — sub-graph total roll-up.+ # Readers aggregating into graph_stats.cost should add this to cost.+ reconciled_cost_delta: int = 0extra_steps: int = 0+provider_cost: float | None = None# Type of the provider-reported cost/usage captured above. When set# by a block, resolve_tracking honors this directly instead ofdiff --git a/autogpt_platform/backend/backend/executor/billing.py b/autogpt_platform/backend/backend/executor/billing.pyindex 24bdec2c5c..e8979ccba3 100644--- a/autogpt_platform/backend/backend/executor/billing.py+++ b/autogpt_platform/backend/backend/executor/billing.py@@ -1,18 +1,13 @@import asyncioimport logging-from typing import TYPE_CHECKING, Any, cast+from typing import TYPE_CHECKING, Anyfrom backend.blocks import get_blockfrom backend.blocks._base import Blockfrom backend.blocks.io import AgentOutputBlockfrom backend.data import redis_client as redisfrom backend.data.credit import UsageTransactionMetadata-from backend.data.execution import (- ExecutionStatus,- GraphExecutionEntry,- NodeExecutionEntry,-)-from backend.data.graph import Node+from backend.data.execution import GraphExecutionEntry, NodeExecutionEntryfrom backend.data.model import GraphExecutionStats, NodeExecutionStatsfrom backend.data.notifications import (AgentRunData,@@ -23,12 +18,13 @@ from backend.data.notifications import ()from backend.notifications.notifications import queue_notificationfrom backend.util.clients import (+ get_database_manager_async_client,get_database_manager_client,get_notification_manager_client,)from backend.util.exceptions import InsufficientBalanceErrorfrom backend.util.logging import TruncatedLogger-from backend.util.metrics import DiscordChannel+from backend.util.metrics import DiscordChannel, discord_send_alertfrom backend.util.settings import Settingsfrom .utils import LogMetadata, block_usage_cost, execution_usage_cost@@ -46,12 +42,6 @@ INSUFFICIENT_FUNDS_NOTIFIED_PREFIX = "insufficient_funds_discord_notified"# TTL for the notification flag (30 days) - acts as a fallback cleanupINSUFFICIENT_FUNDS_NOTIFIED_TTL_SECONDS = 30 * 24 * 60 * 60-# Hard cap on the multiplier passed to charge_extra_runtime_cost to-# protect against a corrupted llm_call_count draining a user's balance.-# Real agent-mode runs are bounded by agent_mode_max_iterations (~50);-# 200 leaves headroom while preventing runaway charges.-_MAX_EXTRA_RUNTIME_COST = 200-def get_db_client() -> "DatabaseManagerClient":return get_database_manager_client()@@ -85,13 +75,31 @@ async def clear_insufficient_funds_notifications(user_id: str) -> int:return 0+def _block_has_paid_cost_entry(block: Block, input_data: "dict[str, Any]") -> bool:+ """Whether any BLOCK_COSTS entry matches this input — even if pre-flight is 0.++ Used to guard dynamic-cost blocks (SECOND/ITEMS/COST_USD) whose+ pre-flight cost is 0 but whose post-flight reconciliation will debit+ a real amount. A user with non-positive balance must not be allowed+ to start such a block.+ """+ from backend.data.block_cost_config import BLOCK_COSTS++ from .utils import _is_cost_filter_match++ block_costs = BLOCK_COSTS.get(type(block))+ if not block_costs:+ return False+ return any(_is_cost_filter_match(bc.cost_filter, input_data) for bc in block_costs)++def resolve_block_cost(node_exec: NodeExecutionEntry,) -> tuple["Block | None", int, dict[str, Any]]:- """Look up the block and compute its base usage cost for an exec.+ """Look up the block and compute its pre-flight usage cost for an exec.- Shared by charge_usage and charge_extra_runtime_cost so the- (get_block, block_usage_cost) lookup lives in exactly one place.+ Shared by ``charge_usage`` and ``charge_reconciled_usage`` so the+ ``(get_block, block_usage_cost)`` lookup lives in exactly one place.Returns ``(block, cost, matching_filter)``. ``block`` is ``None`` ifthe block id can't be resolved — callers should treat that as"nothing to charge".@@ -131,6 +139,23 @@ def charge_usage(),)total_cost += cost+ elif _block_has_paid_cost_entry(block, node_exec.inputs):+ # Dynamic-cost blocks (SECOND/ITEMS/COST_USD) compute 0 pre-flight+ # because the real charge is settled post-flight against stats. Guard+ # execution here: a user with non-positive balance cannot start a+ # paid block even if the pre-flight estimate is zero, otherwise+ # reconciliation leaks real provider spend as an uncollectable debit.+ remaining_balance = db_client.get_credits(user_id=node_exec.user_id)+ if remaining_balance <= 0:+ raise InsufficientBalanceError(+ user_id=node_exec.user_id,+ message=(+ f"Insufficient balance to run {block.name}: "+ "dynamic-cost blocks require a positive balance."+ ),+ balance=remaining_balance,+ amount=0,+ )# execution_count=0 is used by charge_node_usage for nested tool calls# which must not be pushed into higher execution-count tiers.@@ -158,74 +183,109 @@ def charge_usage(return total_cost, remaining_balance-def _charge_extra_runtime_cost_sync(+async def charge_reconciled_usage(node_exec: NodeExecutionEntry,- capped_count: int,+ stats: NodeExecutionStats,) -> tuple[int, int]:- """Synchronous implementation — runs in a thread-pool worker.-- Called only from charge_extra_runtime_cost. Do not call directly from- async code.-- Note: ``resolve_block_cost`` is called again here (rather than reusing- the result from ``charge_usage`` at the start of execution) because the- two calls happen in separate thread-pool workers and sharing mutable- state across workers would require locks. The block config is immutable- during a run, so the repeated lookup is safe and produces the same cost;- the only overhead is an extra registry lookup.+ """Charge the dynamic portion of a block's cost from its execution stats.++ Computes post-flight cost from the execution stats and settles the delta+ against the pre-flight estimate. Positive delta → charge the user; negative+ delta → refund the overcharge (happens when a TOKENS block's flat+ MODEL_COST floor exceeds the real token-metered cost). Zero delta is a+ no-op — common for RUN-only blocks and any balanced estimate.++ Called once per node execution AFTER the block has finished running and+ stats (walltime, tokens, provider_cost) are populated. Swallows its own+ InsufficientBalanceError because reconciliation must never poison the+ success path — log and move on; the shortfall is captured in telemetry."""- db_client = get_db_client()- block, cost, matching_filter = resolve_block_cost(node_exec)- if not block or cost <= 0:- return 0, 0- total_extra_cost = cost * capped_count- remaining_balance = db_client.spend_credits(- user_id=node_exec.user_id,- cost=total_extra_cost,- metadata=UsageTransactionMetadata(- graph_exec_id=node_exec.graph_exec_id,- graph_id=node_exec.graph_id,- node_exec_id=node_exec.node_exec_id,- node_id=node_exec.node_id,- block_id=node_exec.block_id,- block=block.name,- input={- **matching_filter,- "extra_runtime_cost_count": capped_count,- },- reason=(- f"Extra agent-mode iterations for {block.name} "- f"({capped_count} additional LLM calls)"+ try:+ db_client = get_database_manager_async_client()+ block = get_block(node_exec.block_id)+ if not block:+ return 0, 0++ pre_flight, _ = block_usage_cost(block=block, input_data=node_exec.inputs)+ post_flight, matching_filter = block_usage_cost(+ block=block, input_data=node_exec.inputs, stats=stats+ )+ delta = post_flight - pre_flight+ if delta == 0:+ return 0, 0++ # spend_credits with a negative cost posts a USAGE transaction whose+ # amount is positive (i.e. credits back to the wallet). We reuse the+ # USAGE type so the refund is attributable to the same graph+ # execution in credit history.+ remaining_balance = await db_client.spend_credits(+ user_id=node_exec.user_id,+ cost=delta,+ metadata=UsageTransactionMetadata(+ graph_exec_id=node_exec.graph_exec_id,+ graph_id=node_exec.graph_id,+ node_exec_id=node_exec.node_exec_id,+ node_id=node_exec.node_id,+ block_id=node_exec.block_id,+ block=block.name,+ input={**matching_filter, "reconciled_delta": delta},+ reason=(+ f"Post-flight reconciliation for {block.name}: "+ f"actual={post_flight} credits, pre-flight={pre_flight}"+ ),),- ),- )- return total_extra_cost, remaining_balance---async def charge_extra_runtime_cost(- node_exec: NodeExecutionEntry,- extra_count: int,-) -> tuple[int, int]:- """Charge a block extra runtime cost beyond the initial run.-- Used by agent-mode blocks (e.g. OrchestratorBlock) that make multiple- LLM calls within a single node execution. The first iteration is already- charged by charge_usage; this method charges *extra_count* additional- copies of the block's base cost.-- Returns ``(total_extra_cost, remaining_balance)``. May raise- ``InsufficientBalanceError`` if the user can't afford the charge.- """- if extra_count <= 0:+ )+ # Refunds can't push the balance below the threshold — skip.+ if delta > 0:+ # handle_low_balance is sync + does a blocking RPC; dispatch to+ # thread so we don't block the event loop. Rare path (threshold+ # crossings only).+ await asyncio.to_thread(+ handle_low_balance,+ get_db_client(),+ node_exec.user_id,+ remaining_balance,+ delta,+ )+ return delta, remaining_balance+ except InsufficientBalanceError as e:+ # Billing leak: work is already done, but the user cannot pay. Emit a+ # structured ERROR so alerting can pick it up and ping the platform+ # channel so the leak is visible in real time.+ logger.error(+ "billing_leak: insufficient balance after post-flight reconciliation",+ extra={+ "billing_leak": True,+ "user_id": node_exec.user_id,+ "graph_id": node_exec.graph_id,+ "graph_exec_id": node_exec.graph_exec_id,+ "node_exec_id": node_exec.node_exec_id,+ "block_id": node_exec.block_id,+ "cost": abs(e.amount),+ "balance": e.balance,+ "error": str(e),+ },+ )+ try:+ await discord_send_alert(+ f"⚠️ billing_leak (post-flight reconciliation)\n"+ f"user={node_exec.user_id} graph={node_exec.graph_id} "+ f"exec={node_exec.graph_exec_id}\n"+ f"block={node_exec.block_id} needed={abs(e.amount)} "+ f"balance={e.balance}",+ DiscordChannel.PLATFORM,+ )+ except Exception:+ # Discord dispatch is best-effort; never let alert failure poison+ # the success path of a completed block.+ logger.exception("billing_leak discord alert dispatch failed")return 0, 0- # Cap to protect against a corrupted llm_call_count.- capped = min(extra_count, _MAX_EXTRA_RUNTIME_COST)- if extra_count > _MAX_EXTRA_RUNTIME_COST:- logger.warning(- f"extra_count {extra_count} exceeds cap {_MAX_EXTRA_RUNTIME_COST};"- f" charging {_MAX_EXTRA_RUNTIME_COST} (llm_call_count may be corrupted)"+ except Exception:+ logger.exception(+ f"charge_reconciled_usage failed unexpectedly for block "+ f"{node_exec.block_id}")- return await asyncio.to_thread(_charge_extra_runtime_cost_sync, node_exec, capped)+ return 0, 0async def charge_node_usage(node_exec: NodeExecutionEntry) -> tuple[int, int]:@@ -276,84 +336,6 @@ async def try_send_insufficient_funds_notif()-async def handle_post_execution_billing(- node: Node,- node_exec: NodeExecutionEntry,- execution_stats: NodeExecutionStats,- status: ExecutionStatus,- log_metadata: LogMetadata,-) -> None:- """Charge extra runtime cost for blocks that opt into per-LLM-call billing.-- The first LLM call is already covered by charge_usage(); each additional- call costs another base_cost. Skipped for dry runs and failed runs.-- InsufficientBalanceError here is a post-hoc billing leak: the work is- already done but the user can no longer pay. The run stays COMPLETED and- the error is logged with ``billing_leak: True`` for alerting.- """- extra_iterations = (- cast(Block, node.block).extra_runtime_cost(execution_stats)- if status == ExecutionStatus.COMPLETED- and not node_exec.execution_context.dry_run- else 0- )- if extra_iterations <= 0:- return-- try:- extra_cost, remaining_balance = await charge_extra_runtime_cost(- node_exec,- extra_iterations,- )- if extra_cost > 0:- execution_stats.extra_cost += extra_cost- await asyncio.to_thread(- handle_low_balance,- get_db_client(),- node_exec.user_id,- remaining_balance,- extra_cost,- )- except InsufficientBalanceError as e:- log_metadata.error(- "billing_leak: insufficient balance after "- f"{node.block.name} completed {extra_iterations} "- f"extra iterations",- extra={- "billing_leak": True,- "user_id": node_exec.user_id,- "graph_id": node_exec.graph_id,- "block_id": node_exec.block_id,- "extra_runtime_cost_count": extra_iterations,- "error": str(e),- },- )- # Do NOT set execution_stats.error — the node ran to completion,- # only the post-hoc charge failed. See class-level billing-leak- # contract documentation.- await try_send_insufficient_funds_notif(- node_exec.user_id,- node_exec.graph_id,- e,- log_metadata,- )- except Exception as e:- log_metadata.error(- f"billing_leak: failed to charge extra iterations for {node.block.name}",- extra={- "billing_leak": True,- "user_id": node_exec.user_id,- "graph_id": node_exec.graph_id,- "block_id": node_exec.block_id,- "extra_runtime_cost_count": extra_iterations,- "error_type": type(e).__name__,- "error": str(e),- },- exc_info=True,- )--def handle_agent_run_notif(db_client: "DatabaseManagerClient",graph_exec: GraphExecutionEntry,diff --git a/autogpt_platform/backend/backend/executor/billing_reconciliation_test.py b/autogpt_platform/backend/backend/executor/billing_reconciliation_test.pynew file mode 100644index 0000000000..58e3510fe8--- /dev/null+++ b/autogpt_platform/backend/backend/executor/billing_reconciliation_test.py@@ -0,0 +1,297 @@+"""Tests for charge_reconciled_usage post-flight dynamic-cost charging."""++from unittest.mock import AsyncMock, MagicMock, patch+from uuid import uuid4++import pytest++from backend.blocks._base import BlockCost, BlockCostType+from backend.blocks.jina.search import SearchTheWebBlock+from backend.data.block_cost_config import BLOCK_COSTS+from backend.data.execution import ExecutionContext, NodeExecutionEntry+from backend.data.model import NodeExecutionStats+from backend.executor.billing import charge_reconciled_usage+++def tmp_block_costs_override():+ original = BLOCK_COSTS.get(SearchTheWebBlock)+ yield lambda costs: BLOCK_COSTS.__setitem__(SearchTheWebBlock, costs)+ if original is None:+ BLOCK_COSTS.pop(SearchTheWebBlock, None)+ else:+ BLOCK_COSTS[SearchTheWebBlock] = original+++def _node_exec(block_id: str):+ return NodeExecutionEntry(+ user_id="test-user",+ graph_exec_id=str(uuid4()),+ graph_id=str(uuid4()),+ graph_version=1,+ node_exec_id=str(uuid4()),+ node_id=str(uuid4()),+ block_id=block_id,+ inputs={},+ execution_context=ExecutionContext(),+ )+++def _async_db_client(spend_credits_return: int = 0) -> MagicMock:+ """Build an AsyncMock-backed db client stand-in for the reconciliation path.++ ``spend_credits`` is awaited in the production code, so it must be an+ AsyncMock. ``get_credits`` is only read by the sync pre-flight path.+ """+ client = MagicMock()+ client.spend_credits = AsyncMock(return_value=spend_credits_return)+ client.get_credits = MagicMock(return_value=0)+ return client+++def test_dynamic_cost_block_with_zero_balance_raises_ibe_preflight(+ tmp_block_costs_override,+):+ """Sentry-flagged bug: dynamic-cost blocks (SECOND/ITEMS/COST_USD) have+ pre-flight cost 0, so without a guard a zero-balance user could run the+ block and leak the post-flight provider spend as an uncollectable+ debit. Verify charge_usage raises InsufficientBalanceError when the+ user has no balance and the block has a dynamic cost entry.+ """+ from backend.executor.billing import charge_usage+ from backend.util.exceptions import InsufficientBalanceError++ tmp_block_costs_override(+ [BlockCost(cost_amount=100, cost_type=BlockCostType.COST_USD)]+ )+ exec_entry = _node_exec(SearchTheWebBlock().id)++ db_client = MagicMock()+ db_client.get_credits.return_value = 0 # empty wallet++ with (+ patch("backend.executor.billing.get_db_client", return_value=db_client),+ patch("backend.executor.billing.get_block", return_value=SearchTheWebBlock()),+ ):+ with pytest.raises(InsufficientBalanceError):+ charge_usage(exec_entry, execution_count=0)++ db_client.get_credits.assert_called_once_with(user_id=exec_entry.user_id)+ db_client.spend_credits.assert_not_called()+++def test_dynamic_cost_block_with_positive_balance_starts(tmp_block_costs_override):+ """The guard must only fire when balance is non-positive. A user with any+ positive balance may start a dynamic-cost block; reconciliation settles+ the actual charge afterward.+ """+ from backend.executor.billing import charge_usage++ tmp_block_costs_override(+ [BlockCost(cost_amount=100, cost_type=BlockCostType.COST_USD)]+ )+ exec_entry = _node_exec(SearchTheWebBlock().id)++ db_client = MagicMock()+ db_client.get_credits.return_value = 50 # has balance++ with (+ patch("backend.executor.billing.get_db_client", return_value=db_client),+ patch("backend.executor.billing.get_block", return_value=SearchTheWebBlock()),+ ):+ total_cost, remaining = charge_usage(exec_entry, execution_count=0)++ assert total_cost == 0+ assert remaining == 50+ db_client.spend_credits.assert_not_called()+++async def test_run_cost_produces_zero_delta_noop(tmp_block_costs_override):+ tmp_block_costs_override([BlockCost(cost_amount=7, cost_type=BlockCostType.RUN)])+ exec_entry = _node_exec(SearchTheWebBlock().id)+ stats = NodeExecutionStats(walltime=25.0)++ db_client = _async_db_client()+ with patch(+ "backend.executor.billing.get_database_manager_async_client",+ return_value=db_client,+ ):+ delta, _ = await charge_reconciled_usage(exec_entry, stats)++ # RUN type: pre == post == 7, so reconciliation charges nothing.+ assert delta == 0+ db_client.spend_credits.assert_not_awaited()+++async def test_cost_usd_charges_post_flight_delta(tmp_block_costs_override):+ tmp_block_costs_override(+ [BlockCost(cost_amount=100, cost_type=BlockCostType.COST_USD)]+ )+ exec_entry = _node_exec(SearchTheWebBlock().id)+ stats = NodeExecutionStats(provider_cost=0.05, provider_cost_type="cost_usd")++ db_client = _async_db_client(spend_credits_return=42)+ with (+ patch(+ "backend.executor.billing.get_database_manager_async_client",+ return_value=db_client,+ ),+ patch("backend.executor.billing.get_db_client", return_value=MagicMock()),+ patch("backend.executor.billing.handle_low_balance") as handle_lb,+ ):+ delta, remaining = await charge_reconciled_usage(exec_entry, stats)++ # Pre-flight COST_USD returns 0 (no stats). Post-flight: ceil(0.05 * 100) = 5.+ assert delta == 5+ assert remaining == 42+ db_client.spend_credits.assert_awaited_once()+ call_kwargs = db_client.spend_credits.await_args.kwargs+ assert call_kwargs["cost"] == 5+ # Positive delta should also fire the low-balance notification so users+ # get alerted when reconciliation crosses the threshold.+ handle_lb.assert_called_once()+ lb_args = handle_lb.call_args.args+ assert lb_args[1] == exec_entry.user_id+ assert lb_args[2] == 42+ assert lb_args[3] == 5+++async def test_missing_block_returns_zero():+ exec_entry = _node_exec("deadbeef-0000-0000-0000-000000000000")+ stats = NodeExecutionStats(walltime=10)+ with patch("backend.executor.billing.get_block", return_value=None):+ delta, _ = await charge_reconciled_usage(exec_entry, stats)+ assert delta == 0+++async def test_items_cost_scales_linearly_with_result_count(tmp_block_costs_override):+ """ITEMS with cost_divisor=2 bills 1 credit per 2 returned items.++ Apollo SearchOrganizationsBlock uses this exact config. Verifies the+ divisor path in the resolver + post-flight charge.+ """+ tmp_block_costs_override(+ [+ BlockCost(+ cost_amount=1,+ cost_type=BlockCostType.ITEMS,+ cost_divisor=2,+ )+ ]+ )+ exec_entry = _node_exec(SearchTheWebBlock().id)+ # Simulate 20 returned organizations.+ stats = NodeExecutionStats(provider_cost=20, provider_cost_type="items")++ db_client = _async_db_client(spend_credits_return=500)+ with (+ patch(+ "backend.executor.billing.get_database_manager_async_client",+ return_value=db_client,+ ),+ patch("backend.executor.billing.get_db_client", return_value=MagicMock()),+ patch("backend.executor.billing.handle_low_balance"),+ ):+ delta, _ = await charge_reconciled_usage(exec_entry, stats)++ # 20 items / cost_divisor=2 * cost_amount=1 = 10 credits.+ assert delta == 10+ call_kwargs = db_client.spend_credits.await_args.kwargs+ assert call_kwargs["cost"] == 10+ meta_input = call_kwargs["metadata"].input+ assert meta_input.get("reconciled_delta") == 10+++async def test_items_cost_bills_zero_when_no_items_returned(tmp_block_costs_override):+ """An ITEMS block that returns 0 results should bill 0, not the floor."""+ tmp_block_costs_override(+ [BlockCost(cost_amount=1, cost_type=BlockCostType.ITEMS, cost_divisor=2)]+ )+ exec_entry = _node_exec(SearchTheWebBlock().id)+ stats = NodeExecutionStats(provider_cost=0, provider_cost_type="items")++ db_client = _async_db_client()+ with patch(+ "backend.executor.billing.get_database_manager_async_client",+ return_value=db_client,+ ):+ delta, _ = await charge_reconciled_usage(exec_entry, stats)++ assert delta == 0+ db_client.spend_credits.assert_not_awaited()+++async def test_cost_usd_with_larger_spend_bills_full_delta(tmp_block_costs_override):+ """Exa deep-research: $0.20 provider spend × 100 credits/USD = 20 credits.++ Verifies ceil-semantics on fractional USD amounts and that the full+ post-flight charge lands (no refund / no clamping).+ """+ tmp_block_costs_override(+ [BlockCost(cost_amount=100, cost_type=BlockCostType.COST_USD)]+ )+ exec_entry = _node_exec(SearchTheWebBlock().id)+ # $0.207 spend: ceil(0.207 * 100) = 21+ stats = NodeExecutionStats(provider_cost=0.207, provider_cost_type="cost_usd")++ db_client = _async_db_client(spend_credits_return=100)+ with (+ patch(+ "backend.executor.billing.get_database_manager_async_client",+ return_value=db_client,+ ),+ patch("backend.executor.billing.get_db_client", return_value=MagicMock()),+ patch("backend.executor.billing.handle_low_balance"),+ ):+ delta, _ = await charge_reconciled_usage(exec_entry, stats)++ assert delta == 21+++async def test_tokens_cost_refunds_when_actual_below_estimate(tmp_block_costs_override):+ """TOKENS pre-flight uses MODEL_COST floor; if real token usage is cheaper,+ the user is refunded the overcharge via a negative-delta spend_credits."""+ from backend.blocks.llm import LlmModel++ tmp_block_costs_override(+ [+ BlockCost(+ cost_amount=1,+ cost_type=BlockCostType.TOKENS,+ cost_filter={"model": LlmModel.GPT5},+ )+ ]+ )+ exec_entry = _node_exec(SearchTheWebBlock().id)+ exec_entry = exec_entry.model_copy(update={"inputs": {"model": LlmModel.GPT5}})+ # Minimal real usage → post-flight < pre-flight MODEL_COST floor.+ stats = NodeExecutionStats(+ input_token_count=1,+ output_token_count=1,+ )++ db_client = _async_db_client(spend_credits_return=999)+ with (+ patch(+ "backend.executor.billing.get_database_manager_async_client",+ return_value=db_client,+ ),+ patch("backend.executor.billing.handle_low_balance") as handle_lb,+ ):+ delta, remaining = await charge_reconciled_usage(exec_entry, stats)++ assert delta < 0+ assert remaining == 999+ db_client.spend_credits.assert_awaited_once()+ call_kwargs = db_client.spend_credits.await_args.kwargs+ assert call_kwargs["cost"] == delta # negative cost ⇒ credit back+ # Refunds can't push the user below the low-balance threshold, so+ # handle_low_balance must not fire here.+ handle_lb.assert_not_called()diff --git a/autogpt_platform/backend/backend/executor/block_usage_cost_test.py b/autogpt_platform/backend/backend/executor/block_usage_cost_test.pynew file mode 100644index 0000000000..2b1d8a181c--- /dev/null+++ b/autogpt_platform/backend/backend/executor/block_usage_cost_test.py@@ -0,0 +1,284 @@+"""Tests for the dynamic-pricing branches of block_usage_cost."""++import math++import pytest++from backend.blocks._base import BlockCost, BlockCostType+from backend.blocks.code_executor import (+ ExecuteCodeBlock,+ ExecuteCodeStepBlock,+ InstantiateCodeSandboxBlock,+)+from backend.blocks.exa.search import ExaSearchBlock+from backend.blocks.fal.ai_video_generator import AIVideoGeneratorBlock+from backend.blocks.jina.search import SearchTheWebBlock+from backend.blocks.llm import AITextGeneratorBlock, LlmModel+from backend.data.block_cost_config import (+ BLOCK_COSTS,+ MODEL_COST,+ TOKEN_COST,+ TokenRate,+)+from backend.data.model import NodeExecutionStats+from backend.executor.utils import block_usage_cost+from backend.integrations.credentials_store import (+ anthropic_credentials,+ e2b_credentials,+ exa_credentials,+ fal_credentials,+ openai_credentials,+)+++def tmp_block_costs_override(monkeypatch):+ """Swap out BLOCK_COSTS[SearchTheWebBlock] for the duration of a test."""+ return lambda costs: monkeypatch.setitem(BLOCK_COSTS, SearchTheWebBlock, costs)+++def test_second_cost_type_uses_stats_walltime_with_divisor(tmp_block_costs_override):+ tmp_block_costs_override(+ [BlockCost(cost_amount=1, cost_type=BlockCostType.SECOND, cost_divisor=10)]+ )+ block = SearchTheWebBlock()+ # 25 seconds of walltime / 10 sec-per-credit = ceil(2.5) = 3 credits.+ stats = NodeExecutionStats(walltime=25.0)+ cost, _ = block_usage_cost(block, {}, stats=stats)+ assert cost == 3+++def test_second_cost_type_sub_divisor_bills_one_credit(tmp_block_costs_override):+ """Sub-divisor walltime still bills 1cr — no 0-credit leak."""+ tmp_block_costs_override(+ [BlockCost(cost_amount=1, cost_type=BlockCostType.SECOND, cost_divisor=3)]+ )+ block = SearchTheWebBlock()+ # 1s walltime on a 1cr/3s block → ceil(1/3) * 1 = 1 credit.+ stats = NodeExecutionStats(walltime=1.0)+ cost, _ = block_usage_cost(block, {}, stats=stats)+ assert cost == 1+++def test_second_cost_type_returns_zero_without_stats_or_runtime(+ tmp_block_costs_override,+):+ tmp_block_costs_override([BlockCost(cost_amount=1, cost_type=BlockCostType.SECOND)])+ block = SearchTheWebBlock()+ # Pre-flight: no stats, no run_time. SECOND should return 0 credits.+ cost, _ = block_usage_cost(block, {})+ assert cost == 0+++def test_items_cost_type_multiplies_provider_cost(tmp_block_costs_override):+ tmp_block_costs_override(+ [BlockCost(cost_amount=1, cost_type=BlockCostType.ITEMS, cost_divisor=10)]+ )+ block = SearchTheWebBlock()+ stats = NodeExecutionStats(provider_cost=45, provider_cost_type="items")+ # 45 items / 10 = ceil(4.5) = 5 credits.+ cost, _ = block_usage_cost(block, {}, stats=stats)+ assert cost == 5+++def test_items_cost_type_sub_divisor_bills_one_credit(tmp_block_costs_override):+ """A single item under cost_divisor=2 still bills 1cr — no 0-credit leak."""+ tmp_block_costs_override(+ [BlockCost(cost_amount=1, cost_type=BlockCostType.ITEMS, cost_divisor=2)]+ )+ block = SearchTheWebBlock()+ # Apollo SearchOrganizationsBlock shape: 1 result returned on a 1cr/2-item+ # block should bill ceil(1/2) * 1 = 1 credit (floor division would bill 0).+ stats = NodeExecutionStats(provider_cost=1, provider_cost_type="items")+ cost, _ = block_usage_cost(block, {}, stats=stats)+ assert cost == 1+++def test_items_cost_type_ignores_non_items_provider_cost(tmp_block_costs_override):+ tmp_block_costs_override([BlockCost(cost_amount=1, cost_type=BlockCostType.ITEMS)])+ block = SearchTheWebBlock()+ # provider_cost is USD, not item count — don't misread as items.+ stats = NodeExecutionStats(provider_cost=0.05, provider_cost_type="cost_usd")+ cost, _ = block_usage_cost(block, {}, stats=stats)+ assert cost == 0+++def test_cost_usd_ceils_multiplier(tmp_block_costs_override):+ tmp_block_costs_override(+ # 100 credits per USD, so $0.023 provider spend → 3 credits (ceil of 2.3).+ [BlockCost(cost_amount=100, cost_type=BlockCostType.COST_USD)]+ )+ block = SearchTheWebBlock()+ stats = NodeExecutionStats(provider_cost=0.023, provider_cost_type="cost_usd")+ cost, _ = block_usage_cost(block, {}, stats=stats)+ assert cost == 3+++def test_cost_usd_zero_when_no_stats(tmp_block_costs_override):+ tmp_block_costs_override(+ [BlockCost(cost_amount=100, cost_type=BlockCostType.COST_USD)]+ )+ block = SearchTheWebBlock()+ cost, _ = block_usage_cost(block, {})+ assert cost == 0+++def test_cost_usd_ignores_non_usd_provider_cost(tmp_block_costs_override):+ """provider_cost_type='items' should not be mistaken for dollars."""+ tmp_block_costs_override(+ [BlockCost(cost_amount=100, cost_type=BlockCostType.COST_USD)]+ )+ block = SearchTheWebBlock()+ # An items-typed provider_cost of 45 would otherwise be billed as $45.+ stats = NodeExecutionStats(provider_cost=45, provider_cost_type="items")+ cost, _ = block_usage_cost(block, {}, stats=stats)+ assert cost == 0+++def test_tokens_cost_type_uses_token_rate_table(tmp_block_costs_override, monkeypatch):+ tmp_block_costs_override([BlockCost(cost_amount=0, cost_type=BlockCostType.TOKENS)])+ # Override TOKEN_COST for a predictable rate: 1000 credits/1M input,+ # 2000 credits/1M output.+ monkeypatch.setitem(+ TOKEN_COST,+ LlmModel.GPT4O_MINI,+ TokenRate(input=1000, output=2000),+ )+ block = SearchTheWebBlock()+ stats = NodeExecutionStats(+ input_token_count=500_000,+ output_token_count=250_000,+ )+ cost, _ = block_usage_cost(+ block,+ {"model": LlmModel.GPT4O_MINI.value},+ stats=stats,+ )+ # 0.5 * 1000 + 0.25 * 2000 = 500 + 500 = 1000 credits.+ assert cost == 1000+++def test_tokens_falls_back_to_flat_model_cost_when_rate_missing(+ tmp_block_costs_override,+):+ tmp_block_costs_override([BlockCost(cost_amount=0, cost_type=BlockCostType.TOKENS)])+ block = SearchTheWebBlock()+ # Ollama models aren't in TOKEN_COST but are in MODEL_COST.+ ollama_model = LlmModel.OLLAMA_LLAMA3_2+ expected = MODEL_COST[ollama_model]+ cost, _ = block_usage_cost(+ block,+ {"model": ollama_model.value},+ stats=NodeExecutionStats(input_token_count=10_000, output_token_count=10_000),+ )+ assert cost == expected+++def test_e2b_sandbox_blocks_bill_one_credit_per_ten_seconds():+ """End-to-end: E2B blocks use the real SECOND/divisor=10 BlockCost entry."""+ creds = {+ "credentials": {+ "id": e2b_credentials.id,+ "provider": e2b_credentials.provider,+ "type": e2b_credentials.type,+ }+ }+ for block_cls in (+ ExecuteCodeBlock,+ InstantiateCodeSandboxBlock,+ ExecuteCodeStepBlock,+ ):+ # 45s walltime → ceil(45/10) = 5 credits.+ stats = NodeExecutionStats(walltime=45.0)+ cost, _ = block_usage_cost(block_cls(), creds, stats=stats)+ assert cost == 5, f"{block_cls.__name__} expected 5 credits, got {cost}"+ # Pre-flight (no stats) → 0.+ cost, _ = block_usage_cost(block_cls(), creds)+ assert cost == 0+++def test_fal_video_block_bills_three_credits_per_second():+ block = AIVideoGeneratorBlock()+ creds = {+ "credentials": {+ "id": fal_credentials.id,+ "provider": fal_credentials.provider,+ "type": fal_credentials.type,+ }+ }+ # 8s clip → 3 * 8 = 24 credits.+ cost, _ = block_usage_cost(block, creds, stats=NodeExecutionStats(walltime=8.0))+ assert cost == 24+ # Pre-flight → 0.+ cost, _ = block_usage_cost(block, creds)+ assert cost == 0+++def test_exa_blocks_bill_cost_usd_via_sdk_config():+ """End-to-end: Exa's ProviderBuilder.with_base_cost(100, COST_USD) is live."""+ block = ExaSearchBlock()+ creds = {+ "credentials": {+ "id": exa_credentials.id,+ "provider": exa_credentials.provider,+ "type": exa_credentials.type,+ }+ }+ # $0.05 provider spend * 100 credits/USD = 5 credits.+ stats = NodeExecutionStats(provider_cost=0.05, provider_cost_type="cost_usd")+ cost, _ = block_usage_cost(block, creds, stats=stats)+ assert cost == 5+ # Pre-flight: unknown cost → 0.+ cost, _ = block_usage_cost(block, creds)+ assert cost == 0+++def test_llm_block_charges_per_token_post_flight():+ """AITextGeneratorBlock with Claude 4.6 Sonnet bills by real token counts."""+ block = AITextGeneratorBlock()+ input_data = {+ "model": LlmModel.CLAUDE_4_6_SONNET,+ "credentials": {+ "id": anthropic_credentials.id,+ "provider": anthropic_credentials.provider,+ "type": anthropic_credentials.type,+ },+ }+ rate = TOKEN_COST[LlmModel.CLAUDE_4_6_SONNET]+ stats = NodeExecutionStats(+ input_token_count=200_000,+ output_token_count=50_000,+ cache_read_token_count=100_000,+ )+ expected = math.ceil(+ (200_000 * rate.input + 50_000 * rate.output + 100_000 * rate.cache_read)+ / 1_000_000+ )+ cost, _ = block_usage_cost(block, input_data, stats=stats)+ assert cost == expected+++def test_llm_block_pre_flight_falls_back_to_model_cost():+ """Pre-flight charge of an LLM block uses the flat MODEL_COST floor."""+ block = AITextGeneratorBlock()+ cost, _ = block_usage_cost(+ block,+ {+ "model": LlmModel.GPT5,+ "credentials": {+ "id": openai_credentials.id,+ "provider": openai_credentials.provider,+ "type": openai_credentials.type,+ },+ },+ )+ assert cost == MODEL_COST[LlmModel.GPT5]+++def test_run_cost_type_remains_unchanged(tmp_block_costs_override):+ tmp_block_costs_override([BlockCost(cost_amount=7, cost_type=BlockCostType.RUN)])+ block = SearchTheWebBlock()+ # Stats shouldn't affect RUN charge.+ stats = NodeExecutionStats(walltime=999, input_token_count=999)+ cost, _ = block_usage_cost(block, {}, stats=stats)+ assert cost == 7diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.pyindex ce198b972c..ba3dc49eb6 100644--- a/autogpt_platform/backend/backend/executor/manager.py+++ b/autogpt_platform/backend/backend/executor/manager.py@@ -725,16 +725,35 @@ class ExecutionProcessor:execution_stats.walltime = timing_info.wall_timeexecution_stats.cputime = timing_info.cpu_time- await billing.handle_post_execution_billing(- node, node_exec, execution_stats, status, log_metadata- )+ # Log platform cost + reconcile dynamic billing BEFORE graph/node stats+ # are aggregated and persisted — otherwise the reconciled delta never+ # lands in `graph_stats.cost` or the persisted node stats. RUN-only+ # blocks produce a zero delta; dynamic types (SECOND/ITEMS/COST_USD/+ # TOKENS) settle their post-flight charge or refund here. Dry runs+ # skip reconciliation so simulation never touches the user's wallet.+ if status == ExecutionStatus.COMPLETED:+ await log_system_credential_cost(+ node_exec=node_exec,+ block=node.block,+ stats=execution_stats,+ db_client=db_client,+ )+ if not node_exec.execution_context.dry_run:+ reconciled_delta, _ = await billing.charge_reconciled_usage(+ node_exec=node_exec,+ stats=execution_stats,+ )+ if reconciled_delta != 0:+ execution_stats.reconciled_cost_delta += reconciled_deltagraph_stats, graph_stats_lock = graph_stats_pairwith graph_stats_lock:graph_stats.node_count += 1 + execution_stats.extra_stepsgraph_stats.nodes_cputime += execution_stats.cputimegraph_stats.nodes_walltime += execution_stats.walltime- graph_stats.cost += execution_stats.cost + execution_stats.extra_cost+ graph_stats.cost += (+ execution_stats.cost + execution_stats.reconciled_cost_delta+ )if isinstance(execution_stats.error, Exception):graph_stats.node_error_count += 1@@ -755,15 +774,6 @@ class ExecutionProcessor:stats=graph_stats,)- # Log platform cost if system credentials were used (only on success)- if status == ExecutionStatus.COMPLETED:- await log_system_credential_cost(- node_exec=node_exec,- block=node.block,- stats=execution_stats,- db_client=db_client,- )-# If the node failed because a nested tool charge raised IBE,# send the user notification so they understand why the run stopped.if status == ExecutionStatus.FAILED and isinstance(@@ -1010,13 +1020,6 @@ class ExecutionProcessor:) -> tuple[int, int]:return await billing.charge_node_usage(node_exec)- async def charge_extra_runtime_cost(- self,- node_exec: NodeExecutionEntry,- extra_count: int,- ) -> tuple[int, int]:- return await billing.charge_extra_runtime_cost(node_exec, extra_count)-@time_measureddef _on_graph_execution(self,diff --git a/autogpt_platform/backend/backend/executor/utils.py b/autogpt_platform/backend/backend/executor/utils.pyindex 497565ab13..e14e410edb 100644--- a/autogpt_platform/backend/backend/executor/utils.py+++ b/autogpt_platform/backend/backend/executor/utils.py@@ -1,5 +1,6 @@import asyncioimport logging+import mathimport threadingimport timefrom collections import defaultdict@@ -19,7 +20,7 @@ from backend.data import workspace as workspace_db# Import dynamic field utilities from centralized locationfrom backend.data.block import BlockInput, BlockOutputEntry-from backend.data.block_cost_config import BLOCK_COSTS+from backend.data.block_cost_config import BLOCK_COSTS, compute_token_creditsfrom backend.data.db import prismafrom backend.data.dynamic_fields import merge_execution_inputfrom backend.data.execution import (@@ -31,7 +32,12 @@ from backend.data.execution import (NodesInputMasks,)from backend.data.graph import GraphModel, Node-from backend.data.model import USER_TIMEZONE_NOT_SET, CredentialsMetaInput, GraphInput+from backend.data.model import (+ USER_TIMEZONE_NOT_SET,+ CredentialsMetaInput,+ GraphInput,+ NodeExecutionStats,+)from backend.data.rabbitmq import Exchange, ExchangeType, Queue, RabbitMQConfigfrom backend.util.clients import (get_async_execution_event_bus,@@ -116,18 +122,20 @@ def block_usage_cost(input_data: BlockInput,data_size: float = 0,run_time: float = 0,+ stats: NodeExecutionStats | None = None,) -> tuple[int, BlockInput]:- """- Calculate the cost of using a block based on the input data and the block type.-- Args:- block: Block object- input_data: Input data for the block- data_size: Size of the input data in bytes- run_time: Execution time of the block in seconds-- Returns:- Tuple of cost amount and cost filter+ """Calculate the credit charge for a block invocation.++ Two calling contexts:+ - Pre-flight (no stats): charge the fixed floor / estimate. Dynamic cost+ types (ITEMS/COST_USD/TOKENS) return 0 here so they don't block+ execution on a balance check when we don't yet know the true cost.+ - Post-flight (stats populated): dynamic types consume the captured+ stats to compute the actual charge.++ For SECOND/ITEMS/TOKENS cost entries, ``cost_amount`` is interpreted as+ "credits per ``cost_divisor`` units" — e.g. ``cost_amount=1,+ cost_divisor=10`` under SECOND means "1 credit per 10 seconds"."""block_costs = BLOCK_COSTS.get(type(block))if not block_costs:@@ -140,21 +148,79 @@ def block_usage_cost(if block_cost.cost_type == BlockCostType.RUN:return block_cost.cost_amount, block_cost.cost_filter+ if block_cost.cost_type == BlockCostType.BYTE:+ return (+ int(data_size * block_cost.cost_amount),+ block_cost.cost_filter,+ )+if block_cost.cost_type == BlockCostType.SECOND:+ # Ceil so partial divisor-units still bill — avoids 0-credit leaks+ # on sub-divisor runs (e.g. 1s on a `1cr / 3s` block).+ seconds = _coerce_seconds(run_time, stats)+ credits = (+ math.ceil(seconds / block_cost.cost_divisor) * block_cost.cost_amount+ if seconds > 0+ else 0+ )+ return credits, block_cost.cost_filter++ if block_cost.cost_type == BlockCostType.ITEMS:+ # Ceil so partial buckets still bill — avoids 0-credit leaks on+ # single-item returns under a >1 divisor (e.g. Apollo 1cr/2-items).+ items = _coerce_items(stats)+ credits = (+ math.ceil(items / block_cost.cost_divisor) * block_cost.cost_amount+ if items > 0+ else 0+ )+ return credits, block_cost.cost_filter++ if block_cost.cost_type == BlockCostType.COST_USD:+ usd = _coerce_usd(stats)return (- int(run_time * block_cost.cost_amount),+ max(0, math.ceil(usd * block_cost.cost_amount)),block_cost.cost_filter,)- if block_cost.cost_type == BlockCostType.BYTE:+ if block_cost.cost_type == BlockCostType.TOKENS:return (- int(data_size * block_cost.cost_amount),+ compute_token_credits(input_data, stats),block_cost.cost_filter,)return 0, {}+def _coerce_seconds(run_time: float, stats: NodeExecutionStats | None) -> float:+ if run_time > 0:+ return run_time+ if stats and stats.walltime > 0:+ return stats.walltime+ return 0.0+++def _coerce_items(stats: NodeExecutionStats | None) -> int:+ if not stats or stats.provider_cost is None:+ return 0+ # provider_cost is a raw item count only when explicitly typed 'items';+ # a None type likely means USD (resolve_tracking defaults), so reject it+ # here to avoid misreading a fractional dollar amount as an item count.+ if stats.provider_cost_type != "items":+ return 0+ return max(0, int(stats.provider_cost))+++def _coerce_usd(stats: NodeExecutionStats | None) -> float:+ if not stats or stats.provider_cost is None:+ return 0.0+ # provider_cost is billable only when tagged as cost_usd — otherwise it+ # encodes a non-dollar quantity (e.g. items) that would wildly over-bill.+ if stats.provider_cost_type and stats.provider_cost_type != "cost_usd":+ return 0.0+ return max(0.0, float(stats.provider_cost))++def _is_cost_filter_match(cost_filter: BlockInput, input_data: BlockInput) -> bool:"""Filter rules:@@ -551,7 +617,7 @@ async def _validate_node_input_credentials(and node.id not in credential_errors):logger.info(- f"Node #{node.id}: optional credentials not configured, " "skipping"+ f"Node #{node.id}: optional credentials not configured, skipping")nodes_to_skip.add(node.id)@@ -615,9 +681,10 @@ async def validate_graph_with_credentials()# Get credential input/availability/validation errors and nodes to skip- node_credential_input_errors, nodes_to_skip = (- await _validate_node_input_credentials(graph, user_id, nodes_input_masks)- )+ (+ node_credential_input_errors,+ nodes_to_skip,+ ) = await _validate_node_input_credentials(graph, user_id, nodes_input_masks)# Merge credential errors with structural errorsfor node_id, field_errors in node_credential_input_errors.items():@@ -799,14 +866,15 @@ async def validate_and_construct_node_execution_input(nodes_input_masks or {},)- starting_nodes_input, nodes_to_skip = (- await _construct_starting_node_execution_input(- graph=graph,- user_id=user_id,- graph_inputs=graph_inputs,- nodes_input_masks=nodes_input_masks,- dry_run=dry_run,- )+ (+ starting_nodes_input,+ nodes_to_skip,+ ) = await _construct_starting_node_execution_input(+ graph=graph,+ user_id=user_id,+ graph_inputs=graph_inputs,+ nodes_input_masks=nodes_input_masks,+ dry_run=dry_run,)return graph, starting_nodes_input, nodes_input_masks, nodes_to_skip@@ -1108,17 +1176,20 @@ async def add_graph_execution(dry_run = True# Create new execution- graph, starting_nodes_input, compiled_nodes_input_masks, nodes_to_skip = (- await validate_and_construct_node_execution_input(- graph_id=graph_id,- user_id=user_id,- graph_inputs=inputs or {},- graph_version=graph_version,- graph_credentials_inputs=graph_credentials_inputs,- nodes_input_masks=nodes_input_masks,- is_sub_graph=parent_exec_id is not None,- dry_run=dry_run,- )+ (+ graph,+ starting_nodes_input,+ compiled_nodes_input_masks,+ nodes_to_skip,+ ) = await validate_and_construct_node_execution_input(+ graph_id=graph_id,+ user_id=user_id,+ graph_inputs=inputs or {},+ graph_version=graph_version,+ graph_credentials_inputs=graph_credentials_inputs,+ nodes_input_masks=nodes_input_masks,+ is_sub_graph=parent_exec_id is not None,+ dry_run=dry_run,)graph_exec = await edb.create_graph_execution(diff --git a/autogpt_platform/backend/backend/sdk/builder.py b/autogpt_platform/backend/backend/sdk/builder.pyindex 28dd4023f0..ff903b730b 100644--- a/autogpt_platform/backend/backend/sdk/builder.py+++ b/autogpt_platform/backend/backend/sdk/builder.py@@ -140,10 +140,25 @@ class ProviderBuilder:return selfdef with_base_cost(- self, amount: int, cost_type: BlockCostType+ self,+ amount: int,+ cost_type: BlockCostType,+ cost_divisor: int = 1,) -> "ProviderBuilder":- """Set base cost for all blocks using this provider."""- self._base_costs.append(BlockCost(cost_amount=amount, cost_type=cost_type))+ """Set base cost for all blocks using this provider.++ ``cost_divisor`` only applies to SECOND / ITEMS. TOKENS is billed via+ the TOKEN_COST rate table (per-model pricing) and ignores the divisor.+ Example: ``with_base_cost(1, BlockCostType.SECOND, cost_divisor=10)``+ bills 1 credit per 10 walltime seconds.+ """+ self._base_costs.append(+ BlockCost(+ cost_amount=amount,+ cost_type=cost_type,+ cost_divisor=cost_divisor,+ )+ )return selfdef with_api_client(self, factory: Callable) -> "ProviderBuilder":diff --git a/autogpt_platform/frontend/pnpm-lock.yaml b/autogpt_platform/frontend/pnpm-lock.yamlindex 82a76350af..e0d4c590fb 100644--- a/autogpt_platform/frontend/pnpm-lock.yaml+++ b/autogpt_platform/frontend/pnpm-lock.yaml@@ -14461,8 +14461,8 @@ snapshots:'@typescript-eslint/parser': 8.52.0([email protected])([email protected])eslint: 8.57.1eslint-import-resolver-node: 0.3.9- eslint-import-resolver-typescript: 3.10.1([email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected]))([email protected])- eslint-plugin-import: 2.32.0(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected]([email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected]))([email protected]))([email protected])+ eslint-import-resolver-typescript: 3.10.1([email protected])([email protected])+ eslint-plugin-import: 2.32.0(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected])eslint-plugin-jsx-a11y: 6.10.2([email protected])eslint-plugin-react: 7.37.5([email protected])eslint-plugin-react-hooks: 5.2.0([email protected])@@ -14481,7 +14481,7 @@ snapshots:transitivePeerDependencies:- supports-color- [email protected]([email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected]))([email protected]):dependencies:'@nolyfill/is-core-module': 1.0.39debug: 4.4.3@@ -14492,22 +14492,22 @@ snapshots:tinyglobby: 0.2.15unrs-resolver: 1.11.1optionalDependencies:- eslint-plugin-import: 2.32.0(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected]([email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected]))([email protected]))([email protected])+ eslint-plugin-import: 2.32.0(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected])transitivePeerDependencies:- supports-color- [email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected]([email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected]))([email protected]))([email protected]):+ [email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected])([email protected]):dependencies:debug: 3.2.7optionalDependencies:'@typescript-eslint/parser': 8.52.0([email protected])([email protected])eslint: 8.57.1eslint-import-resolver-node: 0.3.9- eslint-import-resolver-typescript: 3.10.1([email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected]))([email protected])+ eslint-import-resolver-typescript: 3.10.1([email protected])([email protected])transitivePeerDependencies:- supports-color- [email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected]([email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected]))([email protected]))([email protected]):+ [email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected]):dependencies:'@rtsao/scc': 1.1.0array-includes: 3.1.9@@ -14518,7 +14518,7 @@ snapshots:doctrine: 2.1.0eslint: 8.57.1eslint-import-resolver-node: 0.3.9- eslint-module-utils: 2.12.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected]([email protected](@typescript-eslint/[email protected]([email protected])([email protected]))([email protected]))([email protected]))([email protected])+ eslint-module-utils: 2.12.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected])([email protected])hasown: 2.0.2is-core-module: 2.16.1is-glob: 4.0.3diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/nodes/CustomNode/components/NodeCost.tsx b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/nodes/CustomNode/components/NodeCost.tsxindex 4a3ef40d4d..b47bcf0540 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/nodes/CustomNode/components/NodeCost.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/nodes/CustomNode/components/NodeCost.tsx@@ -1,4 +1,5 @@import { BlockCost } from "@/app/api/__generated__/models/blockCost";+import { BlockCostType } from "@/app/api/__generated__/models/blockCostType";import { Text } from "@/components/atoms/Text/Text";import useCredits from "@/hooks/useCredits";import { CoinIcon } from "@phosphor-icons/react";@@ -6,6 +7,48 @@ import { isCostFilterMatch } from "../../../../helper";import { useNodeStore } from "@/app/(platform)/build/stores/nodeStore";import { useShallow } from "zustand/react/shallow";+type CostLabelKind = "fixed" | "per-unit" | "dynamic";++interface CostLabel {+ kind: CostLabelKind;+ unitSuffix: string;+ note?: string;+}++function getCostLabel(blockCost: BlockCost): CostLabel {+ const divisor = blockCost.cost_divisor;+ switch (blockCost.cost_type) {+ case BlockCostType.run:+ return { kind: "fixed", unitSuffix: "/run" };+ case BlockCostType.byte:+ return { kind: "per-unit", unitSuffix: "/byte" };+ case BlockCostType.second:+ return {+ kind: "per-unit",+ unitSuffix: divisor && divisor > 1 ? `/ ${divisor}s` : "/sec",+ };+ case BlockCostType.items:+ return {+ kind: "per-unit",+ unitSuffix: divisor && divisor > 1 ? `/ ${divisor} items` : "/item",+ };+ case BlockCostType.cost_usd:+ return {+ kind: "dynamic",+ unitSuffix: "· by USD",+ note: "Final charge scales with provider-reported USD spend",+ };+ case BlockCostType.tokens:+ return {+ kind: "dynamic",+ unitSuffix: "· by tokens",+ note: "Floor shown; final charge scales with real token usage (cached tokens discounted)",+ };+ default:+ return { kind: "fixed", unitSuffix: `/${blockCost.cost_type}` };+ }+}+export const NodeCost = ({blockCosts,nodeId,@@ -26,16 +69,24 @@ export const NodeCost = ({if (!blockCost) return null;+ const label = getCostLabel(blockCost);+ const amountText =+ label.kind === "fixed"+ ? formatCredits(blockCost.cost_amount)+ : blockCost.cost_amount > 0+ ? `~${formatCredits(blockCost.cost_amount)}`+ : "—";+return (- <div className="mr-3 flex items-center gap-1 text-base font-light">+ <div+ className="mr-3 flex items-center gap-1 text-base font-light"+ title={label.note}+ ><CoinIcon className="h-3 w-3" /><Text variant="small" className="!font-medium">- {formatCredits(blockCost.cost_amount)}- </Text>- <Text variant="small">- {" \/"}- {blockCost.cost_type}+ {amountText}</Text>+ <Text variant="small">{` ${label.unitSuffix}`}</Text></div>);};diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/nodes/CustomNode/components/__tests__/NodeCost.test.tsx b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/nodes/CustomNode/components/__tests__/NodeCost.test.tsxnew file mode 100644index 0000000000..da78dcd87e--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/nodes/CustomNode/components/__tests__/NodeCost.test.tsx@@ -0,0 +1,173 @@+import { describe, it, expect, vi, beforeEach } from "vitest";+import { render, screen } from "@/tests/integrations/test-utils";+import { NodeCost } from "../NodeCost";+import { BlockCost } from "@/app/api/__generated__/models/blockCost";+import { BlockCostType } from "@/app/api/__generated__/models/blockCostType";+import { useNodeStore } from "@/app/(platform)/build/stores/nodeStore";++vi.mock("@/hooks/useCredits", () => ({+ default: () => ({+ formatCredits: (credit: number | null) =>+ credit === null ? "-" : `$${(Math.abs(credit) / 100).toFixed(2)}`,+ }),+}));++function cost(overrides: Partial<BlockCost>): BlockCost {+ return {+ cost_amount: 100,+ cost_filter: {},+ cost_type: BlockCostType.run,+ ...overrides,+ } as BlockCost;+}++describe("NodeCost", () => {+ beforeEach(() => {+ useNodeStore.setState({ getHardCodedValues: () => ({}) } as any);+ });++ it("renders fixed /run label for RUN cost type", () => {+ render(<NodeCost blockCosts={[cost({})]} nodeId="n1" />);+ expect(screen.getByText("$1.00")).toBeTruthy();+ expect(screen.getByText(/\/run/)).toBeTruthy();+ });++ it("renders /sec for SECOND cost with divisor<=1", () => {+ render(+ <NodeCost+ blockCosts={[+ cost({+ cost_type: BlockCostType.second,+ cost_divisor: 1,+ cost_amount: 50,+ }),+ ]}+ nodeId="n1"+ />,+ );+ expect(screen.getByText(/\/sec/)).toBeTruthy();+ });++ it("renders '/ Ns' for SECOND cost with divisor>1", () => {+ render(+ <NodeCost+ blockCosts={[+ cost({+ cost_type: BlockCostType.second,+ cost_divisor: 10,+ cost_amount: 25,+ }),+ ]}+ nodeId="n1"+ />,+ );+ expect(screen.getByText(/\/ 10s/)).toBeTruthy();+ });++ it("renders /item for ITEMS with divisor<=1", () => {+ render(+ <NodeCost+ blockCosts={[cost({ cost_type: BlockCostType.items, cost_amount: 10 })]}+ nodeId="n1"+ />,+ );+ expect(screen.getByText(/\/item/)).toBeTruthy();+ });++ it("renders '/ N items' for ITEMS with divisor>1", () => {+ render(+ <NodeCost+ blockCosts={[+ cost({+ cost_type: BlockCostType.items,+ cost_divisor: 5,+ cost_amount: 40,+ }),+ ]}+ nodeId="n1"+ />,+ );+ expect(screen.getByText(/\/ 5 items/)).toBeTruthy();+ });++ it("renders '· by USD' for COST_USD and shows approximate prefix for positive floor", () => {+ render(+ <NodeCost+ blockCosts={[+ cost({ cost_type: BlockCostType.cost_usd, cost_amount: 200 }),+ ]}+ nodeId="n1"+ />,+ );+ expect(screen.getByText(/~\$2\.00/)).toBeTruthy();+ expect(screen.getByText(/· by USD/)).toBeTruthy();+ });++ it("renders em-dash when dynamic cost has zero floor", () => {+ render(+ <NodeCost+ blockCosts={[+ cost({ cost_type: BlockCostType.cost_usd, cost_amount: 0 }),+ ]}+ nodeId="n1"+ />,+ );+ expect(screen.getByText("—")).toBeTruthy();+ });++ it("renders '· by tokens' with floor hint for TOKENS", () => {+ render(+ <NodeCost+ blockCosts={[+ cost({ cost_type: BlockCostType.tokens, cost_amount: 150 }),+ ]}+ nodeId="n1"+ />,+ );+ expect(screen.getByText(/~\$1\.50/)).toBeTruthy();+ expect(screen.getByText(/· by tokens/)).toBeTruthy();+ });++ it("renders /byte suffix for BYTE cost type", () => {+ render(+ <NodeCost+ blockCosts={[cost({ cost_type: BlockCostType.byte, cost_amount: 1 })]}+ nodeId="n1"+ />,+ );+ expect(screen.getByText(/\/byte/)).toBeTruthy();+ });++ it("falls back to /<raw> suffix for unknown cost type", () => {+ render(+ <NodeCost+ blockCosts={[+ cost({+ // Simulate a future enum value not handled by the switch.+ cost_type: "experimental" as unknown as BlockCostType,+ cost_amount: 99,+ }),+ ]}+ nodeId="n1"+ />,+ );+ expect(screen.getByText(/\/experimental/)).toBeTruthy();+ });++ it("returns null when no matching blockCost found", () => {+ useNodeStore.setState({+ getHardCodedValues: () => ({ provider: "openai" }),+ } as any);+ const { container } = render(+ <NodeCost+ blockCosts={[+ cost({+ cost_filter: { provider: "anthropic" },+ }),+ ]}+ nodeId="n1"+ />,+ );+ expect(container.textContent).toBe("");+ });+});diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 43861d98a2..6f2d29bb29 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -9776,7 +9776,12 @@"type": "object","title": "Cost Filter"},- "cost_type": { "$ref": "#/components/schemas/BlockCostType" }+ "cost_type": { "$ref": "#/components/schemas/BlockCostType" },+ "cost_divisor": {+ "type": "integer",+ "title": "Cost Divisor",+ "default": 1+ }},"type": "object","required": ["cost_amount", "cost_filter", "cost_type"],@@ -9784,7 +9789,7 @@},"BlockCostType": {"type": "string",- "enum": ["run", "byte", "second"],+ "enum": ["run", "byte", "second", "items", "cost_usd", "tokens"],"title": "BlockCostType"},"BlockDetails": {