Skip to content

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

fix(backend): update tests to match new cost tracking behavior

Zamil Majdy34abaa5a763f
diff --git a/autogpt_platform/backend/backend/blocks/test/test_llm.py b/autogpt_platform/backend/backend/blocks/test/test_llm.py
index a6fb1dd448..2f12d6ab6e 100644
--- a/autogpt_platform/backend/backend/blocks/test/test_llm.py
+++ b/autogpt_platform/backend/backend/blocks/test/test_llm.py
@@ -200,12 +200,11 @@ class TestLLMStatsTracking:
assert block.execution_stats.llm_retry_count == 1
@pytest.mark.asyncio
- async def test_retry_cost_uses_last_attempt_only(self):
- """provider_cost is only merged from the final successful attempt.
+ async def test_retry_cost_accumulates_across_attempts(self):
+ """provider_cost accumulates across all retry attempts.
- Intermediate retry costs are intentionally dropped to avoid
- double-counting: the cost of failed attempts is captured in
- last_attempt_cost only when the loop eventually succeeds.
+ Each LLM call incurs a real cost, including failed validation attempts.
+ The total cost is the sum of all attempts so no billed USD is lost.
"""
import backend.blocks.llm as llm
@@ -253,8 +252,8 @@ class TestLLMStatsTracking:
async for _ in block.run(input_data, credentials=llm.TEST_CREDENTIALS):
pass
- # Only the final successful attempt's cost is merged
- assert block.execution_stats.provider_cost == pytest.approx(0.02)
+ # provider_cost accumulates across all attempts: $0.01 + $0.02 = $0.03
+ assert block.execution_stats.provider_cost == pytest.approx(0.03)
# Tokens from both attempts accumulate
assert block.execution_stats.input_token_count == 30
assert block.execution_stats.output_token_count == 15
diff --git a/autogpt_platform/backend/backend/blocks/test/test_orchestrator_dynamic_fields.py b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_dynamic_fields.py
index ac4fa0710b..1069fc8ad5 100644
--- a/autogpt_platform/backend/backend/blocks/test/test_orchestrator_dynamic_fields.py
+++ b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_dynamic_fields.py
@@ -306,6 +306,9 @@ async def test_output_yielding_with_dynamic_fields():
mock_response.raw_response = {"role": "assistant", "content": "test"}
mock_response.prompt_tokens = 100
mock_response.completion_tokens = 50
+ mock_response.cache_read_tokens = 0
+ mock_response.cache_creation_tokens = 0
+ mock_response.provider_cost = None
# Mock the LLM call
with patch(
diff --git a/autogpt_platform/backend/backend/data/platform_cost_test.py b/autogpt_platform/backend/backend/data/platform_cost_test.py
index af150346a5..169934377b 100644
--- a/autogpt_platform/backend/backend/data/platform_cost_test.py
+++ b/autogpt_platform/backend/backend/data/platform_cost_test.py
@@ -191,8 +191,12 @@ class TestGetPlatformCostDashboard:
"request_count": 3,
}
]
- # Dashboard runs 3 queries: by_provider, by_user, COUNT(DISTINCT userId).
- mock_query = AsyncMock(side_effect=[provider_rows, user_rows, [{"cnt": 1}]])
+ # Dashboard runs 4 queries: by_provider, by_user, COUNT(DISTINCT userId),
+ # and a separate total aggregate (total_cost + request_count with no LIMIT).
+ agg_rows = [{"total_cost": 5000, "request_count": 3}]
+ mock_query = AsyncMock(
+ side_effect=[provider_rows, user_rows, [{"cnt": 1}], agg_rows]
+ )
with patch("backend.data.platform_cost.query_raw_with_schema", new=mock_query):
dashboard = await get_platform_cost_dashboard()
assert dashboard.total_cost_microdollars == 5000
@@ -207,7 +211,7 @@ class TestGetPlatformCostDashboard:
@pytest.mark.asyncio
async def test_returns_empty_dashboard(self):
- mock_query = AsyncMock(side_effect=[[], [], []])
+ mock_query = AsyncMock(side_effect=[[], [], [], []])
with patch("backend.data.platform_cost.query_raw_with_schema", new=mock_query):
dashboard = await get_platform_cost_dashboard()
assert dashboard.total_cost_microdollars == 0
@@ -219,12 +223,12 @@ class TestGetPlatformCostDashboard:
@pytest.mark.asyncio
async def test_passes_filters_to_queries(self):
start = datetime(2026, 1, 1, tzinfo=timezone.utc)
- mock_query = AsyncMock(side_effect=[[], [], []])
+ mock_query = AsyncMock(side_effect=[[], [], [], []])
with patch("backend.data.platform_cost.query_raw_with_schema", new=mock_query):
await get_platform_cost_dashboard(
start=start, provider="openai", user_id="u1"
)
- assert mock_query.await_count == 3
+ assert mock_query.await_count == 4
first_call_sql = mock_query.call_args_list[0][0][0]
assert "createdAt" in first_call_sql