Improve LLM call metadata (#10004)

Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
Xingyao Wang
2025-07-30 19:02:49 -04:00
committed by GitHub
parent c2fc84e6ea
commit c2e860fe92
6 changed files with 35 additions and 5 deletions

View File

@@ -8,6 +8,7 @@ from pydantic import BaseModel
from openhands.controller.state.state import State
from openhands.core.config.condenser_config import CondenserConfig
from openhands.core.logger import openhands_logger as logger
from openhands.events.action.agent import CondensationAction
from openhands.memory.view import View
@@ -101,10 +102,29 @@ class Condenser(ABC):
def condensed_history(self, state: State) -> View | Condensation:
"""Condense the state's history."""
self._llm_metadata = state.to_llm_metadata('condenser')
if hasattr(self, 'llm'):
model_name = self.llm.config.model
else:
model_name = 'unknown'
self._llm_metadata = state.to_llm_metadata(
model_name=model_name, agent_name='condenser'
)
with self.metadata_batch(state):
return self.condense(state.view)
@property
def llm_metadata(self) -> dict[str, Any]:
"""Metadata to be passed to the LLM when using this condenser.
This metadata is used to provide context about the condensation process and can be used by the LLM to understand how the history was condensed.
"""
if not self._llm_metadata:
logger.warning(
'LLM metadata is empty. Ensure to set it in the condenser implementation.'
)
return self._llm_metadata
@classmethod
def register_config(cls, configuration_type: type[CondenserConfig]) -> None:
"""Register a new condenser configuration type.

View File

@@ -133,7 +133,7 @@ CURRENT_STATE: Last flip: Heads, Haiku count: 15/20"""
response = self.llm.completion(
messages=self.llm.format_messages_for_llm(messages),
extra_body={'metadata': self._llm_metadata},
extra_body={'metadata': self.llm_metadata},
)
summary = response.choices[0].message.content