Skip to main content

Overview

The OpenHands SDK provides metrics tracking at two levels: individual LLM metrics and aggregated conversation-level costs:
  • You can access detailed metrics from each LLM instance using the llm.metrics object to track token usage, costs, and latencies per API call.
  • For a complete view, use conversation.conversation_stats to get aggregated costs across all LLMs used in a conversation, including the primary agent LLM and any auxiliary LLMs (such as those used by the context condenser).

Getting Metrics from Individual LLMs

A ready-to-run example is available here!
Track token usage, costs, and performance metrics from LLM interactions:

Accessing Individual LLM Metrics

Access metrics directly from the LLM object after running the conversation:
The llm.metrics object is an instance of the Metrics class, which provides detailed information including:
  • accumulated_cost - Total accumulated cost across all API calls
  • accumulated_token_usage - Aggregated token usage with fields like:
    • prompt_tokens - Number of input tokens processed
    • completion_tokens - Number of output tokens generated
    • cache_read_tokens - Cache hits (if supported by the model)
    • cache_write_tokens - Cache writes (if supported by the model)
    • reasoning_tokens - Reasoning tokens (for models that support extended thinking)
    • context_window - Context window size used
  • costs - List of individual cost records per API call
  • token_usages - List of detailed token usage records per API call
  • response_latencies - List of response latency metrics per API call
For more details on the available metrics and methods, refer to the source code.

Ready-to-run Example (LLM metrics)

This example is available on GitHub: examples/01_standalone_sdk/13_get_llm_metrics.py
examples/01_standalone_sdk/13_get_llm_metrics.py
You can run the example code as-is.
The model name should follow the LiteLLM convention: provider/model_name (e.g., anthropic/claude-sonnet-4-5-20250929, openai/gpt-4o). The LLM_API_KEY should be the API key for your chosen provider.
ChatGPT Plus/Pro subscribers: You can use LLM.subscription_login() to authenticate with your ChatGPT account and access Codex models without consuming API credits. See the LLM Subscriptions guide for details.

Using LLM Registry for Cost Tracking

A ready-to-run example is available here!
The LLM Registry allows you to maintain a centralized registry of LLM instances, each identified by a unique usage_id. This is particularly useful for tracking costs across different LLMs used in your application.

How the LLM Registry Works

Each LLM is created with a unique usage_id (e.g., “agent”, “condenser”) that serves as its identifier in the registry. The registry maintains references to all LLM instances, allowing you to:
  1. Register LLMs: Add LLM instances to the registry with llm_registry.add(llm)
  2. Retrieve LLMs: Get LLM instances by their usage ID with llm_registry.get("usage_id")
  3. List Usage IDs: View all registered usage IDs with llm_registry.list_usage_ids()
  4. Track Costs Separately: Each LLM’s metrics are tracked independently by its usage ID
This pattern is essential when using multiple LLMs in your application, such as having a primary agent LLM and a separate LLM for context condensing.

Ready-to-run Example (LLM Registry)

This example is available on GitHub: examples/01_standalone_sdk/05_use_llm_registry.py
examples/01_standalone_sdk/05_use_llm_registry.py
You can run the example code as-is.
The model name should follow the LiteLLM convention: provider/model_name (e.g., anthropic/claude-sonnet-4-5-20250929, openai/gpt-4o). The LLM_API_KEY should be the API key for your chosen provider.
ChatGPT Plus/Pro subscribers: You can use LLM.subscription_login() to authenticate with your ChatGPT account and access Codex models without consuming API credits. See the LLM Subscriptions guide for details.

Getting Aggregated Conversation Costs

Beyond individual LLM metrics, you can access aggregated costs for an entire conversation using conversation.conversation_stats. This is particularly useful when your conversation involves multiple LLMs, such as the main agent LLM and auxiliary LLMs for tasks like context condensing.
examples/01_standalone_sdk/21_generate_extraneous_conversation_costs.py
You can run the example code as-is.
The model name should follow the LiteLLM convention: provider/model_name (e.g., anthropic/claude-sonnet-4-5-20250929, openai/gpt-4o). The LLM_API_KEY should be the API key for your chosen provider.
ChatGPT Plus/Pro subscribers: You can use LLM.subscription_login() to authenticate with your ChatGPT account and access Codex models without consuming API credits. See the LLM Subscriptions guide for details.

Understanding Conversation Stats

The conversation.conversation_stats object provides cost tracking across all LLMs used in a conversation. It is an instance of the ConversationStats class, which provides the following key features:

Key Methods and Properties

  • usage_to_metrics: A dictionary mapping usage IDs to their respective Metrics objects. This allows you to track costs separately for each LLM used in the conversation.
  • get_combined_metrics(): Returns a single Metrics object that aggregates costs across all LLMs used in the conversation. This gives you the total cost of the entire conversation.
  • get_metrics_for_usage(usage_id: str): Retrieves the Metrics object for a specific usage ID, allowing you to inspect costs for individual LLMs.

Next Steps

  • Context Condenser - Learn about context management and how it uses separate LLMs
  • LLM Routing - Optimize costs with smart routing between different models