Study 70 files ยท 12 subfolders
Copy to Workspace 10_TRACING Shared from "Study" on Inkdown
Tracing - Comprehensive Deep Dive
Overview
Tracing in the OpenAI Agents SDK provides comprehensive observability into agent runs. Think of tracing as a "flight recorder" or "audit log" that captures everything that happens during an agent execution - from the initial input to the final output, including all model calls, tool executions, handoffs, and more. This is essential for debugging, monitoring, and understanding agent behavior.
Core Concepts
What is Tracing?
Tracing is the systematic recording of events that occur during an agent run. It captures:
Timing - When each operation occurred
Duration - How long each operation took
Inputs/Outputs - What data was passed (unless sensitive)
Relationships - How operations relate to each other
Metadata - Additional context about the run
Why Tracing Matters
basic-ques
Debugging - Understand what went wrong in a failed run
Performance - Identify bottlenecks and slow operations
Observability - Monitor agent behavior in production
Analysis - Analyze patterns and optimize workflows
Compliance - Maintain audit trails for regulatory requirements
Testing - Verify agent behavior matches expectations
Trace Structure
Trace Hierarchy Traces are organized hierarchically:
Analogy: Think of it like a tree where the Trace is the trunk and Spans are branches.
Trace Class
Span Class
Span Types
Agent Span Represents an agent's execution:
Agent name
Agent instructions
Input items
Output items
Tool calls
Handoffs
Generation Span Represents a model generation:
GenerationSpanData includes:
Model name
System instructions
Input items
Output items
Token usage
Tool calls
Function Span Represents a function execution:
FunctionSpanData includes:
Function name
Arguments
Return value
Duration
Guardrail Span Represents a guardrail execution:
GuardrailSpanData includes:
Guardrail name
Guardrail type (input/output/tool)
Input data
Output data
Tripwire triggered
Handoff Span Represents a handoff between agents:
HandoffSpanData includes:
From agent name
To agent name
Handoff arguments
Handoff result
Custom Span Represents a custom operation:
Tool Span Represents a tool execution:
MCP Tools Span Represents MCP tool operations:
Tracing Configuration
RunConfig Tracing Settings Configure tracing for a run:
Global Tracing Settings Set global tracing defaults:
Environment Variables Configure tracing via environment:
Trace Processors
TracingProcessor Interface Custom trace processors allow you to handle traces:
Adding Trace Processors Add processors to handle traces:
Multiple Processors You can have multiple processors:
Built-in Processors
Console Processor
File Processor
OpenAI Processor
Manual Tracing
Creating Traces Manually Create traces for custom operations:
Getting Current Trace Access the current trace context:
Getting Current Span Access the current span context:
Span Data Types
AgentSpanData
GenerationSpanData
FunctionSpanData
GuardrailSpanData
Trace Errors
SpanError Errors in spans are captured:
Error Handling in Spans Errors are automatically captured:
Manual Error Recording
Trace Export
OpenAI Trace Export
Custom Export
Trace Analysis
Analyzing Trace Duration Calculate total duration:
Analyzing Token Usage Aggregate token usage across spans:
Analyzing Tool Usage
Analyzing Handoff Patterns
Tracing Best Practices
1. Use Descriptive Span Names Use clear, descriptive span names:
2. Include Relevant Metadata
3. Handle Sensitive Data Be careful with sensitive data:
4. Use Appropriate Granularity Choose the right level of detail:
5. Clean Up Old Traces
Common Tracing Patterns
1. Distributed Tracing Link traces across services:
2. Performance Monitoring Track performance metrics:
3. Error Tracking
4. Compliance Logging Maintain compliance logs:
5. Cost Tracking Estimate costs from traces:
Tracing and Streaming
Streaming Traces Traces are built incrementally during streaming:
Real-time Span Updates Spans are updated as operations complete:
Tracing and Sessions
Session-Aware Tracing Traces can include session information:
Cross-Session Tracing Link traces across sessions:
Tracing Security
Sensitive Data Protection Protect sensitive data in traces:
Access Control Control who can access traces:
Tracing Performance
Performance Impact Tracing has minimal performance impact:
Optimizing Tracing Optimize for performance:
Tracing Debugging
Debugging with Traces Use traces to debug issues:
Visualizing Traces Create visual representations:
Summary Tracing provides comprehensive observability. Key takeaways:
Traces capture entire agent runs
Spans represent individual operations
Span hierarchy shows operation relationships
Agent spans track agent execution
Generation spans track model calls
Function spans track function execution
Guardrail spans track guardrail execution
Handoff spans track agent delegation
Custom spans track custom operations
Trace processors handle completed traces
Configuration controls tracing behavior
Sensitive data can be excluded
Manual tracing for custom operations
Current trace/span context access
Span data provides operation details
Span errors capture failures
Trace export to external systems
Trace analysis enables insights
Performance monitoring via traces
Compliance via audit trails
Tracing is essential for building observable, debuggable, and monitorable agent systems.