Add comprehensive API for conversation metrics data #11059
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
End-user friendly description of the problem this fixes or functionality this introduces.
This PR introduces a new API endpoint that exposes comprehensive metrics data for OpenHands conversations, building on the existing "Display Cost" functionality. Users and developers can now programmatically access detailed cost, token usage, and performance metrics for any conversation through a REST API.
Key Features:
GET /api/conversations/{conversation_id}/metrics
Summarize what the PR does, explaining any non-trivial design decisions.
This PR creates a comprehensive API for exposing conversation metrics data that was previously only accessible through the "Display Cost" menu item in the UI.
Backend Changes:
Pydantic Response Models (
openhands/server/data_models/metrics_response.py
):TokenUsageResponse
: Token breakdown with cache hit/write trackingCostResponse
: Cost data with model and timestamp informationResponseLatencyResponse
: Performance metrics with response IDsMetricsResponse
: Comprehensive metrics containerConversationMetricsResponse
: Main response with service breakdownAPI Endpoint (
openhands/server/routes/conversation.py
):GET /api/conversations/{conversation_id}/metrics
endpointData Model Updates:
ConversationInfo
to include basic metrics fieldsFrontend Changes:
TypeScript Types (
frontend/src/api/open-hands.types.ts
):API Client (
frontend/src/api/conversation-service/conversation-service.api.ts
):getConversationMetrics()
method with proper authentication handlingReact Query Hook (
frontend/src/hooks/query/use-conversation-metrics.ts
):useConversationMetrics()
hook with configurable caching and refetch optionsDesign Decisions:
Testing:
Link of any specific issues this addresses:
This addresses the user request to create an API for exposing metrics data, building on previous analysis of the Display Cost functionality.
To run this PR locally, use the following command:
GUI with Docker:
CLI with uvx: