
PostHog AI Observability: Track LLM Cost Per User and Per Customer
Also available in Hindi: यह लेख हिंदी में पढ़ें
Your OpenAI invoice shows one total. It does not tell you that three customers cause most of it. PostHog's AI Observability (earlier called LLM analytics) records every model call as an event with tokens, latency and a dollar cost, linked to the user who triggered it. Here is the Python setup for an app that already uses the OpenAI SDK.
Install and wrap the client
pip install "posthog~=8.0" openai
from posthog import Posthog
from posthog.ai.openai import OpenAI
posthog = Posthog("<ph_project_token>", host="https://us.i.posthog.com")
client = OpenAI(api_key="sk-...", posthog_client=posthog)
The wrapper replaces openai.OpenAI; the rest of your code stays the same. EU projects use https://eu.i.posthog.com. Prefer Node? Install @posthog/ai and posthog-node and do the same.
Tag every call with who made it
response = client.responses.create(
model="gpt-5-mini",
input=[{"role": "user", "content": question}],
posthog_distinct_id=user.id,
posthog_groups={"company": user.company_id},
posthog_trace_id=request_id,
)
Each call now creates an $ai_generation event. posthog_distinct_id ties it to a person; posthog_groups ties it to a company, which is what B2B apps bill by. posthog_trace_id groups the calls of one request, so a multi-step agent shows up as one trace. Leave out the distinct ID and the events are captured anonymously.
What gets recorded
The event holds the model, input and output tokens, latency in seconds and $ai_total_cost_usd. PostHog prices calls with OpenRouter's pricing data, falling back to its own table, and counts cached and reasoning tokens. If you pay negotiated rates, send $ai_input_token_price and $ai_output_token_price per token, not per million.
Read cost per user
Open the AI Observability dashboard. Out of the box it shows users, traces, costs, generations and latency, and you can filter it by person or group properties. That answers the real questions: which customers cost the most, which model is driving spend, and whether latency spiked after a prompt change. Tool calls are not captured automatically; send them as $ai_span events if you need them in the trace.
Keep prompts private
Set privacy_mode=True on the PostHog client and the prompt ($ai_input) and reply ($ai_output_choices) are not sent, while tokens, cost and latency still are.
The free tier includes 100K AI events a month, shared with evaluations. For price context see how much an AI API costs, AI chatbot cost for a website and Langfuse cost tracking per developer, or visit the PostHog tool page.
Sources
- OpenAI observability installation, PostHog docs
- Calculating LLM costs, Privacy mode and Dashboard, PostHog docs
- Pricing, PostHog
- posthog-python, GitHub and @posthog/ai, npm
FAQ
Is PostHog AI Observability free?
Yes up to 100K AI events a month, shared with evaluations, on the free plan.
Does it work with Anthropic or LangChain?
Yes. PostHog has wrappers and integrations for several providers and frameworks.
Will my prompts be stored?
By default yes. Turn on privacy mode to send only metadata like tokens and cost.
How is cost calculated?
From tokens and the model's price, using OpenRouter pricing data or your custom prices.