Skip to content

feat(llma): OpenAI Agents SDK integration #45072

@andrewm4894

Description

@andrewm4894

Summary

Add PostHog LLM analytics integration for the OpenAI Agents SDK, enabling automatic tracking of agent execution, tool calls, handoffs, and LLM generations.

Implementation

The integration uses the SDK's official TracingProcessor interface to capture:

  • Agent spans ($ai_span type=agent)
  • LLM generations ($ai_generation) with token counts and costs
  • Tool/function calls ($ai_span type=tool)
  • Agent handoffs ($ai_span type=handoff)
  • Guardrail checks ($ai_span type=guardrail)
  • Custom spans ($ai_span type=custom)
  • Full trace hierarchy ($ai_trace)
  • Conversation linking via $ai_group_id (maps to SDK's group_id in RunConfig)

Properties

  • $ai_provider: "openai" (the underlying LLM provider)
  • $ai_framework: "openai-agents" (identifies the framework)
  • $ai_total_tokens: Sum of input + output tokens
  • $ai_error_type: Error categorization (model_behavior_error, user_error, input_guardrail_triggered, output_guardrail_triggered, max_turns_exceeded, unknown)

Usage

from posthog.ai.openai_agents import instrument

instrument(
    client=posthog,
    distinct_id="user_123",
    privacy_mode=False,
)

# Run agents as normal - traces automatically sent to PostHog

Pull Requests

Branches

Real-World Example

Follow-up

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions