:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { OnboardingComponentsContext, createInstallation } from 'scenes/onboarding/shared/OnboardingDocsContentWrapper'
import { StepDefinition } from '../steps'
import { getOtelRequestContextStep } from './_snippets/otel-request-context'
export const getPydanticAISteps = (ctx: OnboardingComponentsContext): StepDefinition[] => {
const { CodeBlock, CalloutBox, Markdown, Blockquote, dedent, snippets } = ctx
const NotableGenerationProperties = snippets?.NotableGenerationProperties
return [
{
title: 'Install dependencies',
badge: 'required',
content: (
<>
<CalloutBox type="info" icon="IconInfo" title="Full working examples">
<Markdown>
See the complete [Python
example](https://github.com/PostHog/posthog-python/tree/master/examples/example-ai-pydantic-ai)
on GitHub. If you use the PostHog SDK wrapper instead of OpenTelemetry, see the [Python
wrapper
example](https://github.com/PostHog/posthog-python/tree/7223c52/examples/example-ai-pydantic-ai).
</Markdown>
</CalloutBox>
<Markdown>Install the OpenTelemetry SDK and Pydantic AI.</Markdown>
<CodeBlock
language="bash"
code={dedent`
pip install "pydantic-ai[openai]" opentelemetry-sdk "posthog[otel]"
`}
/>
</>
),
},
{
title: 'Set up OpenTelemetry tracing',
badge: 'required',
content: (
<>
<Markdown>
Configure OpenTelemetry to export traces to PostHog and enable Pydantic AI's built-in OTel
instrumentation. PostHog converts `gen_ai.*` spans into `$ai_generation` events automatically.
</Markdown>
<CodeBlock
language="python"
code={dedent`
import os
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource, SERVICE_NAME
from posthog.ai.otel import PostHogSpanProcessor
from pydantic_ai import Agent
resource = Resource(attributes={
SERVICE_NAME: "my-app",
"posthog.distinct_id": "user_123", # optional: identifies the user in PostHog
"foo": "bar", # custom properties are passed through
})
provider = TracerProvider(resource=resource)
provider.add_span_processor(
PostHogSpanProcessor(
api_key="<ph_project_token>",
host="<ph_client_api_host>",
)
)
trace.set_tracer_provider(provider)
# Enable automatic OTel instrumentation for all Pydantic AI agents
Agent.instrument_all()
`}
/>
</>
),
},
{
title: 'Run your agent',
badge: 'required',
content: (
<>
<Markdown>
Create a Pydantic AI agent and run it. PostHog automatically captures an `$ai_generation` event
for each LLM call via the OTel instrumentation.
</Markdown>
<CodeBlock
language="python"
code={dedent`
from pydantic_ai import Agent
from pydantic_ai.models.openai import OpenAIModel
model = OpenAIModel("gpt-4o-mini")
agent = Agent(model, system_prompt="You are a helpful assistant.")
result = agent.run_sync("Tell me a fun fact about hedgehogs.")
print(result.output)
`}
/>
<Blockquote>
<Markdown>
**Note:** To capture LLM events anonymously, omit the `posthog.distinct_id` resource
attribute and don't set a distinct ID in the PostHog context. See our docs on [anonymous vs
identified events](https://posthog.com/docs/data/anonymous-vs-identified-events) to learn
more.
</Markdown>
</Blockquote>
<Markdown>
{dedent`
You can expect captured \`$ai_generation\` events to have the following properties:
`}
</Markdown>
{NotableGenerationProperties && <NotableGenerationProperties />}
</>
),
},
getOtelRequestContextStep(ctx, { languages: ['Python'] }),
]
}
export const PydanticAIInstallation = createInstallation(getPydanticAISteps)