🚧 Note: LLM observability is currently considered in
beta
. To access it, enable the feature preview in your PostHog account.We are keen to gather as much feedback as possible so if you try this out please let us know. You can email tim@posthog.com, send feedback via the in-app support panel, or use one of our other support options.
LLM observability enables you to capture and analyze LLM usage and performance data. Specifically, it captures:
- Input and output content and tokens
- Latency
- Model
- Traces
- Cost of each generation
All of this data is captured into PostHog to be used in insights, dashboards, alerts, and more. LLM events are charged at the same rate as other PostHog events, and so in practice are roughly an order of magnitude cheaper than most other LLM providers.
Observability installation
Setting up observability starts with installing the PostHog Python SDK.
pip install posthog
The rest of the setup depends on the LLM platform you're using. These SDKs do not proxy your calls, they only fire off an async call to PostHog in the background to send the data.
Start by installing the OpenAI Python SDK:
pip install openai
In the spot where you initialize the OpenAI SDK, import PostHog and our OpenAI wrapper, initialize PostHog with your project API key and host (from your project settings), and pass it to our OpenAI wrapper.
from posthog.ai.openai import OpenAIimport posthogposthog.project_api_key = "<ph_project_api_key>"posthog.host = "https://us.i.posthog.com"client = OpenAI(api_key="your_openai_api_key",posthog_client=posthog)
Note: This also works with the
AsyncOpenAI
client.
Now, when you use the OpenAI SDK, it automatically captures many properties into PostHog including $ai_input
, $ai_input_tokens
, $ai_latency
, $ai_model
, $ai_model_parameters
, $ai_output
, and $ai_output_tokens
.
You can also capture additional properties like posthog_distinct_id
, posthog_trace_id
, posthog_properties
, posthog_groups
, and posthog_privacy_mode
.
response = client.chat.completions.create(model="gpt-3.5-turbo",messages=[{"role": "user", "content": "Tell me a fun fact about hedgehogs"}],posthog_distinct_id="user_123", # optionalposthog_trace_id="trace_123", # optionalposthog_properties={"conversation_id": "abc123", "paid": True} # optionalposthog_groups={"company": "company_id_in_your_db"} # optionalposthog_privacy_mode=False # optional)print(response.choices[0].message.content)
Notes:
- This also works with responses where
stream=True
.- If you want to capture LLM events anonymously, don't pass a distinct ID to the request. See our docs on anonymous vs identified events to learn more.
Embeddings
PostHog can also capture embedding generations as $ai_embedding
events. Just make sure to use the same posthog.ai.openai
client to do so:
response = client.embeddings.create(input="The quick brown fox",model="text-embedding-3-small",posthog_distinct_id="user_123", # optionalposthog_trace_id="trace_123", # optionalposthog_properties={"key": "value"} # optionalposthog_groups={"company": "company_id_in_your_db"} # optionalposthog_privacy_mode=False # optional)
Privacy mode
To avoid storing potentially sensitive prompt and completion data, you can enable privacy mode. This excludes the $ai_input
and $ai_output
properties from being captured.
This can be done either by setting the privacy_mode
config option in the Python SDK like this:
import posthogposthog.project_api_key = "<ph_project_api_key>"posthog.host = "https://us.i.posthog.com"posthog.privacy_mode = True
It can also be on at the request level by including setting the privacy_mode
parameter to True
in the request. The exact setup depends on the LLM platform you're using:
client.chat.completions.create(messages=[...],posthog_privacy_mode=True)