# Practical and Useful Patterns for OpenAI Agent SDK
🌍 Customize where your traces go. Send alongside OpenAI to Langfuse or Datadog, or replace the backend entirely -- seamlessly integrate with your existing observability stack.
Custom processors give you full control over trace routing, transformation, and filtering.
📌 Title: Tracing -- Custom processors / Ecosystem
🔗 URL:
🧩 Overview
The OpenAI Agent SDK's tracing is fully extensible through custom processors. Use `add_trace_processor()` to add custom processing alongside the OpenAI backend, or `set_trace_processors()` to completely replace the destination with Langfuse, Datadog, MLflow, Arize, or Braintrust. Even when using non-OpenAI models, `set_tracing_export_api_key()` enables free trace visualization.
🛠 How to Use
Import `add_trace_processor` and `TracingProcessor` from `agents`. Define a `MyProcessor` class extending `TracingProcessor` with `on_trace_start`, `on_trace_end`, `on_span_start`, `on_span_end`, and `shutdown` methods. Access ` `trace.duration_ms`, and ` for trace data. Register with `add_trace_processor(MyProcessor())` to run custom processing alongside the OpenAI backend.
🏗 Practical Usage Patterns
**Parallel Export with add_trace_processor()**
Keep the OpenAI Traces dashboard while also sending data to your own observability platform.
Extend `TracingProcessor` to create a `DatadogProcessor`. In `on_trace_start`, call `dd_client.start_span(operation_name="agent.trace", resource= In `on_trace_end`, call `dd_client.finish_span(metadata={"duration_ms": trace.duration_ms})`. In `on_span_start`, use `operation_name=f"agent.{span.type}"`. In `shutdown`, call `dd_client.flush()`. Register with `add_trace_processor(DatadogProcessor(dd_client))` to send traces to Datadog in parallel with OpenAI.
**Complete Backend Replacement with set_trace_processors()**
When you don't want traces going to OpenAI, replace the processor entirely.
Import `set_trace_processors` from `agents` to completely replace the trace destination. Use `set_trace_processors([LangfuseTracingProcessor()])` for Langfuse, `[MLflowTracingProcessor()]` for MLflow, `[ArizeTracingProcessor()]` for Arize, or `[BraintrustTracingProcessor()]` for Braintrust. This replaces all existing processors including the OpenAI backend.
**Free Traces for Non-OpenAI Models with set_tracing_export_api_key()**
Use trace visualization for free even when running non-OpenAI models (Gemini, Claude, etc.).
Import `set_tracing_export_api_key` from `agents` and call `set_tracing_export_api_key("your-tracing-export-key")`. This enables free trace visualization even when using non-OpenAI models like Gemini or Claude.
**Combining Multiple Processors**
In production, it's common to send traces to multiple observability tools.
Pass multiple processors as a list to `set_trace_processors`: `LangfuseTracingProcessor()` for detailed LLM analytics, `DatadogProcessor(dd_client)` for infrastructure monitoring integration, and `CustomAuditProcessor()` for internal audit logging -- all running simultaneously.
💡 Use Cases
📡 Use add_trace_processor() to send traces to Datadog in parallel while keeping the OpenAI dashboard
🔀 Switch destinations entirely to Langfuse/MLflow/Arize/Braintrust with set_trace_processors()
🆓 Visualize non-OpenAI model (Gemini, Claude) traces for free with set_tracing_export_api_key()
🔧 Build production observability pipelines combining multiple processors (Langfuse + Datadog + audit logs)
⚠️ Caveats
- `set_trace_processors()` **replaces all** existing processors, including the OpenAI backend. Use `add_trace_processor()` if you want to keep it.
- Always implement the `shutdown()` method in custom processors to flush any buffered data.
- Multiple processors add overhead to trace delivery. Consider asynchronous sending to minimize impact.
✨ Custom processors let you pipe trace data directly into your existing observability stack. Start with add for parallel export, then optimize destinations with set once you're confident!
#
OpenAIAgentSDK# #
AIAgent#