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): Include node execution steps and cost of sub-graph execution (#10328)

Zamil Majdyc2eea593c0a8
diff --git a/autogpt_platform/backend/backend/blocks/agent.py b/autogpt_platform/backend/backend/blocks/agent.py
index 38c41aeb58..3ed720f87c 100644
--- a/autogpt_platform/backend/backend/blocks/agent.py
+++ b/autogpt_platform/backend/backend/blocks/agent.py
@@ -14,7 +14,7 @@ from backend.data.block import (
get_block,
)
from backend.data.execution import ExecutionStatus
-from backend.data.model import SchemaField
+from backend.data.model import NodeExecutionStats, SchemaField
from backend.util import json, retry
_logger = logging.getLogger(__name__)
@@ -151,6 +151,12 @@ class AgentExecutorBlock(Block):
if event.event_type == ExecutionEventType.GRAPH_EXEC_UPDATE:
# If the graph execution is COMPLETED, TERMINATED, or FAILED,
# we can stop listening for further events.
+ self.merge_stats(
+ NodeExecutionStats(
+ extra_cost=event.stats.cost if event.stats else 0,
+ extra_steps=event.stats.node_exec_count if event.stats else 0,
+ )
+ )
break
logger.debug(
diff --git a/autogpt_platform/backend/backend/data/block.py b/autogpt_platform/backend/backend/data/block.py
index 9836ce35a3..5f28fefc87 100644
--- a/autogpt_platform/backend/backend/data/block.py
+++ b/autogpt_platform/backend/backend/data/block.py
@@ -425,28 +425,7 @@ class Block(ABC, Generic[BlockSchemaInputType, BlockSchemaOutputType]):
raise ValueError(f"{self.name} did not produce any output for {output}")
def merge_stats(self, stats: NodeExecutionStats) -> NodeExecutionStats:
- stats_dict = stats.model_dump()
- current_stats = self.execution_stats.model_dump()
-
- for key, value in stats_dict.items():
- if key not in current_stats:
- # Field doesn't exist yet, just set it, but this will probably
- # not happen, just in case though so we throw for invalid when
- # converting back in
- current_stats[key] = value
- elif isinstance(value, dict) and isinstance(current_stats[key], dict):
- current_stats[key].update(value)
- elif isinstance(value, (int, float)) and isinstance(
- current_stats[key], (int, float)
- ):
- current_stats[key] += value
- elif isinstance(value, list) and isinstance(current_stats[key], list):
- current_stats[key].extend(value)
- else:
- current_stats[key] = value
-
- self.execution_stats = NodeExecutionStats(**current_stats)
-
+ self.execution_stats += stats
return self.execution_stats
@property
diff --git a/autogpt_platform/backend/backend/data/model.py b/autogpt_platform/backend/backend/data/model.py
index 90ec204904..17ba987f14 100644
--- a/autogpt_platform/backend/backend/data/model.py
+++ b/autogpt_platform/backend/backend/data/model.py
@@ -636,6 +636,35 @@ class NodeExecutionStats(BaseModel):
llm_retry_count: int = 0
input_token_count: int = 0
output_token_count: int = 0
+ extra_cost: int = 0
+ extra_steps: int = 0
+
+ def __iadd__(self, other: "NodeExecutionStats") -> "NodeExecutionStats":
+ """Mutate this instance by adding another NodeExecutionStats."""
+ if not isinstance(other, NodeExecutionStats):
+ return NotImplemented
+
+ stats_dict = other.model_dump()
+ current_stats = self.model_dump()
+
+ for key, value in stats_dict.items():
+ if key not in current_stats:
+ # Field doesn't exist yet, just set it
+ setattr(self, key, value)
+ elif isinstance(value, dict) and isinstance(current_stats[key], dict):
+ current_stats[key].update(value)
+ setattr(self, key, current_stats[key])
+ elif isinstance(value, (int, float)) and isinstance(
+ current_stats[key], (int, float)
+ ):
+ setattr(self, key, current_stats[key] + value)
+ elif isinstance(value, list) and isinstance(current_stats[key], list):
+ current_stats[key].extend(value)
+ setattr(self, key, current_stats[key])
+ else:
+ setattr(self, key, value)
+
+ return self
class GraphExecutionStats(BaseModel):
diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.py
index 7683de61bd..709b375604 100644
--- a/autogpt_platform/backend/backend/executor/manager.py
+++ b/autogpt_platform/backend/backend/executor/manager.py
@@ -207,9 +207,7 @@ async def execute_node(
# Update execution stats
if execution_stats is not None:
- execution_stats = execution_stats.model_copy(
- update=node_block.execution_stats.model_dump()
- )
+ execution_stats += node_block.execution_stats
execution_stats.input_size = input_size
execution_stats.output_size = output_size
@@ -648,9 +646,10 @@ class Executor:
return
nonlocal execution_stats
- execution_stats.node_count += 1
+ execution_stats.node_count += 1 + result.extra_steps
execution_stats.nodes_cputime += result.cputime
execution_stats.nodes_walltime += result.walltime
+ execution_stats.cost += result.extra_cost
if (err := result.error) and isinstance(err, Exception):
execution_stats.node_error_count += 1
update_node_execution_status(