references/anthropic.md · 24000
om the LLM |
| $ai_output_tokens | The number of tokens in the output (often found in response.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 block in response.content: if block.type != "tool_use": continue start = time.time() result = run_tool(block.name, block.input) 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": block.name, "$ai_input_state": block.input, "$ai_output_state": result, "$ai_latency": time.time() - start, }, )Node
for (const block of response.content) { if (block.type !== 'tool_use') continue const start = Date.now() const result = await runTool(block.name, block.input) 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: block.name, $ai_input_state: block.input, $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. |
Tourist info
If you just landed here, welcome
PostHog is the context layer for your product. And since we're advertising anyway: 97% of companies use it for free.
A hedgehog tourist holding a map and a suitcase