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(blocks): propagate cache tokens and provider_cost in AIConditionBlock
Zamil Majdy4e4aafca4557
diff --git a/autogpt_platform/backend/backend/blocks/ai_condition.py b/autogpt_platform/backend/backend/blocks/ai_condition.pyindex 6d62d4ab77..db8c023b99 100644--- a/autogpt_platform/backend/backend/blocks/ai_condition.py+++ b/autogpt_platform/backend/backend/blocks/ai_condition.py@@ -207,6 +207,9 @@ class AIConditionBlock(AIBlockBase):NodeExecutionStats(input_token_count=response.prompt_tokens,output_token_count=response.completion_tokens,+ cache_read_token_count=response.cache_read_tokens,+ cache_creation_token_count=response.cache_creation_tokens,+ provider_cost=response.provider_cost,))self.prompt = response.promptdiff --git a/autogpt_platform/backend/backend/blocks/ai_condition_test.py b/autogpt_platform/backend/backend/blocks/ai_condition_test.pyindex babb1eb4cf..38d39a406a 100644--- a/autogpt_platform/backend/backend/blocks/ai_condition_test.py+++ b/autogpt_platform/backend/backend/blocks/ai_condition_test.py@@ -47,7 +47,13 @@ def _make_input(**overrides) -> AIConditionBlock.Input:return AIConditionBlock.Input(**defaults)-def _mock_llm_response(response_text: str) -> LLMResponse:+def _mock_llm_response(+ response_text: str,+ *,+ cache_read_tokens: int = 0,+ cache_creation_tokens: int = 0,+ provider_cost: float | None = None,+) -> LLMResponse:return LLMResponse(raw_response="",prompt=[],@@ -56,6 +62,9 @@ def _mock_llm_response(response_text: str) -> LLMResponse:prompt_tokens=10,completion_tokens=5,reasoning=None,+ cache_read_tokens=cache_read_tokens,+ cache_creation_tokens=cache_creation_tokens,+ provider_cost=provider_cost,)@@ -145,3 +154,33 @@ class TestExceptionPropagation:input_data = _make_input()with pytest.raises(RuntimeError, match="LLM provider error"):await _collect_outputs(block, input_data, credentials=TEST_CREDENTIALS)+++# ---------------------------------------------------------------------------+# Regression: cache tokens and provider_cost must be propagated to stats+# ---------------------------------------------------------------------------+++class TestCacheTokenPropagation:+ @pytest.mark.asyncio+ async def test_cache_tokens_propagated_to_stats(self):+ """cache_read_tokens and cache_creation_tokens must be forwarded to+ NodeExecutionStats so that usage dashboards count cached tokens."""+ block = AIConditionBlock()++ async def spy_llm(**kwargs):+ return _mock_llm_response(+ "true",+ cache_read_tokens=7,+ cache_creation_tokens=3,+ provider_cost=0.0012,+ )++ block.llm_call = spy_llm # type: ignore[assignment]++ input_data = _make_input()+ await _collect_outputs(block, input_data, credentials=TEST_CREDENTIALS)++ assert block.execution_stats.cache_read_token_count == 7+ assert block.execution_stats.cache_creation_token_count == 3+ assert block.execution_stats.provider_cost == 0.0012