diff --git a/agenta-backend/agenta_backend/services/llm_apps_service.py b/agenta-backend/agenta_backend/services/llm_apps_service.py index d8ce93cb5e..93a17d3d54 100644 --- a/agenta-backend/agenta_backend/services/llm_apps_service.py +++ b/agenta-backend/agenta_backend/services/llm_apps_service.py @@ -58,12 +58,24 @@ def extract_result_from_response(response: dict): if "tree" in response: trace_tree = response.get("tree", {}).get("nodes", [])[0] - latency = ( - get_nested_value( - trace_tree, ["metrics", "acc", "duration", "total"] - ) - / 1000 + duration_ms = get_nested_value( + trace_tree, ["metrics", "acc", "duration", "total"] ) + if duration_ms: + duration_seconds = duration_ms / 1000 + else: + start_time = get_nested_value(trace_tree, ["time", "start"]) + end_time = get_nested_value(trace_tree, ["time", "end"]) + + if start_time and end_time: + duration_seconds = ( + datetime.fromisoformat(end_time) + - datetime.fromisoformat(start_time) + ).total_seconds() + else: + duration_seconds = None + + latency = duration_seconds cost = get_nested_value( trace_tree, ["metrics", "acc", "costs", "total"] )