Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: Gracefully handling negative response latencies (#5660)

Calvin Smith8488dd2a03db
diff --git a/openhands/llm/metrics.py b/openhands/llm/metrics.py
index 5388dd0402..f3f2a4e4b4 100644
--- a/openhands/llm/metrics.py
+++ b/openhands/llm/metrics.py
@@ -55,11 +55,9 @@ class Metrics:
self._costs.append(Cost(cost=value, model=self.model_name))
def add_response_latency(self, value: float, response_id: str) -> None:
- if value < 0:
- raise ValueError('Response latency cannot be negative.')
self._response_latencies.append(
ResponseLatency(
- latency=value, model=self.model_name, response_id=response_id
+ latency=max(0.0, value), model=self.model_name, response_id=response_id
)
)
diff --git a/tests/unit/test_llm.py b/tests/unit/test_llm.py
index 4973fcf353..46923aa217 100644
--- a/tests/unit/test_llm.py
+++ b/tests/unit/test_llm.py
@@ -125,6 +125,15 @@ def test_response_latency_tracking(mock_time, mock_litellm_completion):
assert response['id'] == 'test-response-123'
assert response['choices'][0]['message']['content'] == 'Test response'
+ # To make sure the metrics fail gracefully, set the start/end time to go backwards.
+ mock_time.side_effect = [1000.0, 999.0]
+ llm.completion(messages=[{'role': 'user', 'content': 'Hello!'}])
+
+ # There should now be 2 latencies, the last of which has the value clipped to 0
+ assert len(llm.metrics.response_latencies) == 2
+ latency_record = llm.metrics.response_latencies[-1]
+ assert latency_record.latency == 0.0 # Should be lifted to 0 instead of being -1!
+
def test_llm_reset():
llm = LLM(LLMConfig(model='gpt-4o-mini', api_key='test_key'))