references/azure-openai.md
AI agents: this is one page from PostHog's docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt
Azure OpenAI observability installation
Canonical URL: https://posthog.com/docs/ai-observability/installation/azure-openai
Cloud region: This page uses US Cloud hosts. For an EU Cloud project, use
https://eu.i.posthog.cominstead ofhttps://us.i.posthog.com(ingestion host), and usehttps://eu.posthog.cominstead ofhttps://app.posthog.com(app host for links).
On this page
- Install dependencies
- Configure PostHog
- Call Azure OpenAI
- Capture tool calls as spans
- Verify traces and generations
- Next steps
- .NET support
Let AI instrument your LLM calls for you
Skip the manual setup — run this in your project and the wizard installs the SDK and wires up AI Observability for you.
npx @posthog/wizard ai-observability
PostHog Wizard hedgehog
Let AI instrument your LLM calls for you
-
1
Install dependencies
Required
Full working examples
See the complete Node.js and Python examples on GitHub.
Install the PostHog SDK and the OpenAI SDK.
Python
pip install posthog openaiNode
npm install @posthog/ai posthog-node openaiGo
PostHog doesn't have a Go wrapper for this SDK yet. Use the [OpenTelemetry integration](https://posthog.com/docs/ai-observability/installation/opentelemetry) instead. -
2
Configure PostHog
Required
Create a PostHog client, then swap in PostHog's Azure OpenAI wrapper.
Python
from posthog import Posthog from posthog.ai.openai import AzureOpenAI import time, uuid, json posthog = Posthog("<ph_project_token>", host="https://us.i.posthog.com") client = AzureOpenAI( api_key="<azure_openai_api_key>", api_version="2024-10-21", azure_endpoint="https://<your-resource>.openai.azure.com", posthog_client=posthog, )Node
import { AzureOpenAI } from '@posthog/ai/openai' import { PostHog } from 'posthog-node' const posthog = new PostHog('<ph_project_token>', { host: 'https://us.i.posthog.com' }) const client = new AzureOpenAI({ apiKey: '<azure_openai_api_key>', apiVersion: '2024-10-21', endpoint: 'https://<your-resource>.openai.azure.com', posthog, }) -
3
Call Azure OpenAI
Required
When you use the wrapped client to call Azure OpenAI, PostHog automatically captures an
$ai_generationevent.Python
trace_id = str(uuid.uuid4()) response = client.responses.create( model="<your-deployment-name>", input=[{"role": "user", "content": "What's the weather in Paris?"}], tools=tools, posthog_distinct_id="user_123", posthog_trace_id=trace_id, posthog_properties={ "$ai_session_id": "conversation-abc", "$ai_provider": "azure", }, )Node
const traceId = crypto.randomUUID() const response = await client.responses.create({ model: '<your-deployment-name>', input: [{ role: 'user', content: "What's the weather in Paris?" }], tools, posthogDistinctId: 'user_123', posthogTraceId: traceId, posthogProperties: { $ai_session_id: 'conversation-abc', $ai_provider: 'azure', }, })Note: If you want to capture LLM events anonymously, omit
posthog_distinct_idfrom the call. See our docs on anonymous vs identified events to learn more.You can expect captured
$ai_generationevents to have the following properties:| Property | Description | | --- | --- | |
$ai_model| The specific model, likegpt-5-miniorclaude-4-sonnet| |$ai_latency| The latency of the LLM call in seconds | |$ai_time_to_first_token| Time to first token in seconds (streaming only) | |$ai_tools| Tools and functions available to the LLM | |$ai_input| List of messages sent to the LLM | |$ai_input_tokens| The number of tokens in the input (often found in response.usage) | |$ai_output_choices| List of response choices from the LLM | |$ai_output_tokens| The number of tokens in the output (often found inresponse.usage) | |$ai_total_cost_usd| The total cost in USD (input + output) | | [...] | See full list of properties | -
4
Capture tool calls as spans
Optional
For standard responses, the posthog client captures it as a generation. For all tool calls, you must manually capture them as
$ai_spanevents.Python
for item in response.output: if item.type != "function_call": continue start = time.time() result = run_tool(item.name, json.loads(item.arguments)) posthog.capture( distinct_id="user_123", event="$ai_span", properties={ "$ai_trace_id": trace_id, "$ai_session_id": "conversation-abc", "$ai_span_id": str(uuid.uuid4()), "$ai_span_name": item.name, "$ai_input_state": item.arguments, "$ai_output_state": result, "$ai_latency": time.time() - start, }, )Node
for (const item of response.output) { if (item.type !== 'function_call') continue const start = Date.now() const result = await runTool(item.name, JSON.parse(item.arguments)) posthog.capture({ distinctId: 'user_123', event: '$ai_span', properties: { $ai_trace_id: traceId, $ai_session_id: 'conversation-abc', $ai_span_id: crypto.randomUUID(), $ai_span_name: item.name, $ai_input_state: item.arguments, $ai_output_state: result, $ai_latency: (Date.now() - start) / 1000, }, }) }See spans for the full list of span properties.
-
Verify traces and generations
Recommended
Confirm LLM events are being sent to PostHog
Let's make sure LLM events are being captured and sent to PostHog. Under AI Observability, you should see rows of data appear in the Traces and Generations tabs.
LLM generations in PostHog
-
5
Next steps
Recommended
Now that you're capturing AI conversations, continue with the resources below to learn what else AI Observability enables within the PostHog platform.
| Resource | Description | | --- | --- | | Basics | Learn the basics of how LLM calls become events in PostHog. | | Generations | Read about the
$ai_generationevent and its properties. | | Traces | Explore the trace hierarchy and how to use it to debug LLM calls. | | Spans | Review spans and their role in representing individual operations. | | Anaylze LLM performance | Learn how to create dashboards to analyze LLM performance. |
.NET support
PostHog.AI adds AI observability for .NET applications using Azure OpenAI. It is currently pre-release, so expect breaking changes before a stable release.
Install the packages:
dotnet add package PostHog.AI
dotnet add package Azure.AI.OpenAI
When using dependency injection, register PostHog first, then register an Azure OpenAI client with the PostHog handler:
using System.ClientModel.Primitives;
using Azure;
using Azure.AI.OpenAI;
using Microsoft.Extensions.DependencyInjection;
using PostHog.AI;
using PostHog.Config;
var services = new ServiceCollection();
services.AddPostHog(options =>
{
options.PostConfigure(posthogOptions =>
{
posthogOptions.ProjectToken = "<ph_project_token>";
posthogOptions.HostUrl = new Uri("https://us.i.posthog.com");
});
});
services.AddPostHogAI();
services
.AddHttpClient("PostHogAzureOpenAIClient")
.AddPostHogOpenAIHandler();
services.AddSingleton<AzureOpenAIClient>(sp =>
{
var httpClientFactory = sp.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient("PostHogAzureOpenAIClient");
var options = new AzureOpenAIClientOptions
{
Transport = new HttpClientPipelineTransport(httpClient),
};
return new AzureOpenAIClient(
new Uri("<azure_openai_endpoint>"),
new AzureKeyCredential("<azure_openai_api_key>"),
options);
});
var serviceProvider = services.BuildServiceProvider();
var azureOpenAIClient = serviceProvider.GetRequiredService<AzureOpenAIClient>();
Use PostHogAIContext to attach trace, session, span, and user context to AI calls made inside a scope:
using PostHog.AI;
using (PostHogAIContext.BeginScope(
distinctId: "user-123",
traceId: "trace-abc",
sessionId: "session-xyz",
spanId: "span-1",
spanName: "summarize_text",
parentId: null))
{
var chatClient = azureOpenAIClient.GetChatClient("<deployment_name>");
await chatClient.CompleteChatAsync("Summarize this text");
}
The integration captures $ai_generation and $ai_embedding events with model, latency, token, error, trace, session, and span properties. For more .NET SDK details, see the .NET library docs.