fix(metrics) Merge metrics of agent LLM and condenser LLM (#7890)

Co-authored-by: Carlos Freund <carlosfreund@gmail.com>
This commit is contained in:
Carlos Freund
2025-04-17 19:15:14 +02:00
committed by GitHub
parent 78e3f82de1
commit cc8b677f3e
3 changed files with 112 additions and 6 deletions

View File

@@ -67,7 +67,6 @@ from openhands.events.observation import (
)
from openhands.events.serialization.event import event_to_trajectory, truncate_content
from openhands.llm.llm import LLM
from openhands.llm.metrics import Metrics
# note: RESUME is only available on web GUI
TRAFFIC_CONTROL_REMINDER = (
@@ -1142,15 +1141,21 @@ class AgentController:
- accumulated_cost: The current total cost
- accumulated_token_usage: Accumulated token statistics across all API calls
This includes metrics from both the agent's LLM and the condenser's LLM if it exists.
Args:
action: The action to attach metrics to
"""
metrics = self.agent.llm.metrics.copy()
# Include condenser metrics if they exist
if hasattr(self.agent, 'condenser') and hasattr(self.agent.condenser, 'llm'):
metrics.merge(self.agent.condenser.llm.metrics)
# Create a minimal metrics object with just what the frontend needs
metrics = Metrics(model_name=self.agent.llm.metrics.model_name)
metrics.accumulated_cost = self.agent.llm.metrics.accumulated_cost
metrics._accumulated_token_usage = (
self.agent.llm.metrics.accumulated_token_usage
)
metrics._token_usages = []
metrics._response_latencies = []
metrics._costs = []
action.llm_metrics = metrics