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 PostHog Logger

From Leeroopedia
Revision as of 12:10, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/BerriAI_Litellm_PostHog_Logger.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Attribute Value
Sources litellm/integrations/posthog.py
Domains Logging, Analytics, LLM Monitoring, Integrations
Last Updated 2026-02-15 16:00 GMT

Overview

The PostHogLogger sends LLM analytics events to PostHog following the PostHog LLM Analytics specification.

Description

PostHogLogger extends CustomBatchLogger to capture LLM call data as PostHog events ($ai_generation or $ai_embedding). It constructs event payloads with properties following PostHog's LLM Analytics format, including model information, input/output data, token counts, costs, latency, error information, trace IDs, and custom metadata. The logger supports both sync (immediate send) and async (batched) modes, per-request credential overrides via standard_callback_dynamic_params, distinct user ID resolution (from metadata, end_user, or trace_id), and graceful shutdown with atexit flush. Events are sent to PostHog's /batch/ endpoint grouped by API credentials. A mock mode is available via POSTHOG_MOCK environment variable.

Usage

Import and register PostHogLogger when you want to send LLM analytics to PostHog. Requires POSTHOG_API_KEY environment variable.

Code Reference

Source Location

litellm/integrations/posthog.py

Signature

class PostHogLogger(CustomBatchLogger):
    def __init__(self, **kwargs)

Import

from litellm.integrations.posthog import PostHogLogger

I/O Contract

Inputs

Parameter Type Required Description
kwargs dict No Additional arguments passed to CustomBatchLogger.

Environment Variables:

Variable Required Description
POSTHOG_API_KEY Yes PostHog project API key.
POSTHOG_API_URL No PostHog API URL. Defaults to "https://us.i.posthog.com".
POSTHOG_MOCK No Enable mock mode for testing.

Key Methods

Method Returns Description
log_success_event(kwargs, response_obj, start_time, end_time) None Sync logging - sends event immediately.
async_log_success_event(kwargs, response_obj, start_time, end_time) None Async logging - queues event for batch send.
async_log_failure_event(kwargs, response_obj, start_time, end_time) None Async failure logging - queues event for batch send.
create_posthog_event_payload(kwargs) PostHogEventPayload Creates a PostHog event payload from request kwargs.
async_send_batch() None Sends batched events grouped by credentials to PostHog.

Outputs

Output Type Description
Side effect HTTP POST Sends events to {posthog_host}/batch/ endpoint with $ai_generation or $ai_embedding event types.

Usage Examples

import litellm

# Register the callback
litellm.success_callback = ["posthog"]
litellm.failure_callback = ["posthog"]

response = litellm.completion(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}],
    metadata={
        "user_id": "user-123",  # Used as PostHog distinct_id
    },
)
# Direct instantiation
from litellm.integrations.posthog import PostHogLogger

logger = PostHogLogger()

Related Pages

Page Connections

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