Skip to main content

View source on GitHub

CLI tool to retrieve cost and token usage metrics for OpenHands conversations. Supports both V0 and V1 APIs, automatically selecting the appropriate one based on the conversation version.

Features

  • Auto-detects conversation version (V0 vs V1) and uses the appropriate API
  • Graceful fallback chain for metric retrieval
  • Displays cost (USD), token counts, cache stats, and context window
  • JSON output for programmatic use
  • API call logging for debugging and development
  • Fixture-based testing - high coverage via recorded API responses
  • Zero dependencies - uses only Python standard library

Installation

No installation required - just make the script executable:
Requires Python 3.10+.

Usage

Set your API key

Get metrics for a conversation

Example output (V0 conversation):
Example output (V1 conversation):

JSON output

Options

Logging API Calls

For debugging or development, you can log all API requests and responses:
This creates timestamped files in .oh/api-logs/YYYYMMDD-HHMMSS/:
Each request/response pair is numbered sequentially. The Authorization header is redacted from logged requests.

API Endpoints Used

For V1 Conversations

For V0 Conversations

How Metrics Are Retrieved

The tool uses a fallback chain to find metrics:
  1. Check conversation version via /api/conversations/{id}
  2. For V1 conversations:
    • First try /api/v1/app-conversations?ids={id} which includes a metrics object
    • If metrics are all zeros, fall back to /api/v1/conversation/{id}/events/search and extract metrics from ConversationStateUpdateEvent at value.stats.usage_to_metrics.agent
  3. For V0 conversations (or if V1 fails): Use /api/conversations/{id}/events and find the latest event with llm_metrics
  4. Last resort: Use /api/conversations/{id}/trajectory and scan for llm_metrics
Note: Some V1 conversations have metrics stored only in events (not in the app-conversations response). The fallback chain ensures these are still retrieved correctly.

Metrics Explained

Architecture

The library is organized into separate modules:

Using the Library Programmatically

Using the V0/V1 Drivers Directly

Testing

The test suite uses recorded API fixtures to achieve high coverage without making real API calls:
Current coverage: 89% of library code.

Creating New Fixtures

  1. Run the CLI with --log-api-calls to capture real API responses
  2. Copy relevant response files to tests/fixtures/
  3. Rename following the pattern: GET__api_path_q_param=value.json

License

MIT License - see LICENSE for details.