OpenObserve Docs
IntegrationAITools

PostHog → OpenObserve

Updated View MarkdownOpen in ChatGPTOpen in Claude

Capture LLM generation events in PostHog for product analytics while simultaneously exporting structured OTel traces to OpenObserve for observability. The two platforms complement each other: PostHog tracks user behaviour and aggregates LLM usage; OpenObserve stores the full span tree with token counts and latency for debugging.

Prerequisites

  • Python 3.9+
  • An OpenObserve account (cloud or self-hosted)
  • Your OpenObserve organisation ID and Base64-encoded auth token
  • A PostHog project API key
  • An OpenAI API key (or another LLM provider)

Installation

pip install openobserve-python-sdk openinference-instrumentation-openai posthog openai python-dotenv

Configuration

Create a .env file in your project root:

OPENOBSERVE_URL=https://api.openobserve.ai/
OPENOBSERVE_ORG=your_org_id
OPENOBSERVE_AUTH_TOKEN=Basic <your_base64_token>
OPENAI_API_KEY=your-openai-api-key
POSTHOG_API_KEY=phc_your_project_api_key
POSTHOG_HOST=https://app.posthog.com

Instrumentation

Call OpenAIInstrumentor().instrument() and openobserve_init() before creating any clients. Track each LLM generation in both PostHog and OpenObserve.

from dotenv import load_dotenv
load_dotenv()

from openinference.instrumentation.openai import OpenAIInstrumentor
from openobserve import openobserve_init

OpenAIInstrumentor().instrument()
openobserve_init()

from opentelemetry import trace
import os
import uuid
import posthog
from openai import OpenAI

tracer = trace.get_tracer(__name__)

posthog.api_key = os.environ["POSTHOG_API_KEY"]
posthog.host = os.environ.get("POSTHOG_HOST", "https://app.posthog.com")

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

def generate(prompt: str, user_id: str = None) -> str:
    with tracer.start_as_current_span("posthog.llm_generation") as span:
        span.set_attribute("posthog.model", "gpt-4o-mini")
        span.set_attribute("posthog.prompt", prompt[:200])
        response = client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[{"role": "user", "content": prompt}],
            max_tokens=200,
        )
        reply = response.choices[0].message.content
        trace_id = format(span.get_span_context().trace_id, "032x")

        posthog.capture(user_id or str(uuid.uuid4()), "$ai_generation", {
            "$ai_provider": "openai",
            "$ai_model": "gpt-4o-mini",
            "$ai_input_tokens": response.usage.prompt_tokens,
            "$ai_output_tokens": response.usage.completion_tokens,
            "$ai_trace_id": trace_id,
        })
        return reply

result = generate("Explain distributed tracing in one sentence.", user_id="user-123")
print(result)
posthog.shutdown()

What Gets Captured

In OpenObserve (OTel traces):

AttributeDescription
posthog_modelThe model used
posthog_promptThe input prompt
posthog_completion_lengthCharacter length of the model response
llm_token_count_promptPrompt tokens (from OpenAI instrumentor child span)
llm_token_count_completionCompletion tokens (from OpenAI instrumentor child span)
durationRequest latency
span_statusOK or error status

In PostHog ($ai_generation event):

PropertyDescription
$ai_providerLLM provider name
$ai_modelModel used
$ai_input_tokensPrompt token count
$ai_output_tokensCompletion token count
$ai_trace_idTrace ID for cross-referencing with OpenObserve

Viewing Traces

  1. Log in to OpenObserve and navigate to Traces
  2. Filter by span name posthog.llm_generation to see all generations
  3. Use $ai_trace_id in PostHog to cross-reference with the full OTel trace in OpenObserve

PostHog trace in OpenObserve, Waterfall view with the posthog.llm_generation span attributes

Next Steps

With PostHog and OpenObserve both instrumented, you get product-level analytics in PostHog and deep observability in OpenObserve. Set alerts in OpenObserve on latency spikes and use PostHog dashboards to track per-user token spend.

Read More

Was this page helpful?

On this page