Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:BerriAI Litellm OpenMeter Logger

From Leeroopedia
Attribute Value
Sources litellm/integrations/openmeter.py
Domains Logging, Metering, Usage Tracking, Integrations
Last Updated 2026-02-15 16:00 GMT

Overview

The OpenMeterLogger sends LLM usage events (token counts, cost, model) to OpenMeter for metering and billing purposes, using the CloudEvents specification.

Description

OpenMeterLogger extends CustomLogger to post CloudEvents-formatted usage data to the OpenMeter API (/api/v1/events). Each event includes the model name, token usage (prompt, completion, total), cost, a subject identifier (derived from the user parameter or user_api_key_user_id metadata), and a timestamp. The event type defaults to "litellm_tokens" and can be overridden with the OPENMETER_EVENT_TYPE environment variable. Both sync and async logging methods are supported. The logger requires a user identifier -- either passed directly in the completion call or available from the API key metadata.

Usage

Import and register OpenMeterLogger when you need to meter LLM usage for billing. Requires OPENMETER_API_KEY environment variable.

Code Reference

Source Location

litellm/integrations/openmeter.py

Signature

class OpenMeterLogger(CustomLogger):
    def __init__(self) -> None

Import

from litellm.integrations.openmeter import OpenMeterLogger

I/O Contract

Inputs

Parameter Type Required Description
(none) -- -- Constructor takes no arguments. Validates environment variables on init.

Environment Variables:

Variable Required Description
OPENMETER_API_KEY Yes OpenMeter API key for authentication.
OPENMETER_API_ENDPOINT No API base URL. Defaults to "https://openmeter.cloud".
OPENMETER_EVENT_TYPE No CloudEvent type string. Defaults to "litellm_tokens".

Key Methods

Method Returns Description
log_success_event(kwargs, response_obj, start_time, end_time) None Sync logging of usage event to OpenMeter.
async_log_success_event(kwargs, response_obj, start_time, end_time) None Async logging of usage event to OpenMeter.

Outputs

Output Type Description
Side effect HTTP POST Sends a CloudEvents JSON payload to {endpoint}/api/v1/events.

CloudEvent payload structure:

{
    "specversion": "1.0",
    "type": "litellm_tokens",
    "id": "<call_id>",
    "time": "<ISO 8601 timestamp>",
    "subject": "<user_id>",
    "source": "litellm-proxy",
    "data": {
        "model": "<model_name>",
        "cost": 0.001,
        "prompt_tokens": 10,
        "completion_tokens": 20,
        "total_tokens": 30
    }
}

Usage Examples

import litellm

# Register the callback
litellm.success_callback = ["openmeter"]

response = litellm.completion(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}],
    user="user-123",  # Required: identifies the subject for metering
)
# Direct instantiation
from litellm.integrations.openmeter import OpenMeterLogger

logger = OpenMeterLogger()

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment