|
1 | | -# `Setup` |
| 1 | +# Tracing Setup |
2 | 2 |
|
3 | | -::: agents.tracing.setup |
| 3 | +The tracing setup module provides functions to configure and initialize tracing for OpenAI Agents. |
| 4 | + |
| 5 | +## Functions |
| 6 | + |
| 7 | +### `set_tracing_disabled(disabled: bool) -> None` |
| 8 | + |
| 9 | +Disables or enables tracing globally. |
| 10 | + |
| 11 | +```python |
| 12 | +from agents import set_tracing_disabled |
| 13 | + |
| 14 | +# Disable tracing globally |
| 15 | +set_tracing_disabled(True) |
| 16 | + |
| 17 | +# Re-enable tracing |
| 18 | +set_tracing_disabled(False) |
| 19 | +``` |
| 20 | + |
| 21 | +### `set_tracing_export_api_key(api_key: str) -> None` |
| 22 | + |
| 23 | +Sets the API key used for exporting traces to OpenAI's backend. |
| 24 | + |
| 25 | +```python |
| 26 | +from agents import set_tracing_export_api_key |
| 27 | + |
| 28 | +# Set a specific API key for tracing |
| 29 | +set_tracing_export_api_key("sk-your-openai-api-key") |
| 30 | +``` |
| 31 | + |
| 32 | +### `add_trace_processor(processor: TraceProcessor) -> None` |
| 33 | + |
| 34 | +Adds an additional trace processor to the existing processors. |
| 35 | + |
| 36 | +```python |
| 37 | +from agents import add_trace_processor |
| 38 | +from agents.tracing.processors import TraceProcessor |
| 39 | + |
| 40 | +class CustomTraceProcessor(TraceProcessor): |
| 41 | + def process_trace(self, trace): |
| 42 | + # Custom processing logic |
| 43 | + print(f"Processing trace: {trace.trace_id}") |
| 44 | + |
| 45 | +# Add custom processor |
| 46 | +add_trace_processor(CustomTraceProcessor()) |
| 47 | +``` |
| 48 | + |
| 49 | +### `set_trace_processors(processors: list[TraceProcessor]) -> None` |
| 50 | + |
| 51 | +Replaces all existing trace processors with the provided list. |
| 52 | + |
| 53 | +```python |
| 54 | +from agents import set_trace_processors |
| 55 | +from agents.tracing.processors import BatchTraceProcessor, BackendSpanExporter |
| 56 | + |
| 57 | +# Replace with custom processors |
| 58 | +custom_processors = [ |
| 59 | + BatchTraceProcessor(BackendSpanExporter()), |
| 60 | + CustomTraceProcessor() |
| 61 | +] |
| 62 | +set_trace_processors(custom_processors) |
| 63 | +``` |
| 64 | + |
| 65 | +## Environment Variables |
| 66 | + |
| 67 | +### `OPENAI_AGENTS_DISABLE_TRACING` |
| 68 | + |
| 69 | +Set to `1` to disable tracing globally. |
| 70 | + |
| 71 | +```bash |
| 72 | +export OPENAI_AGENTS_DISABLE_TRACING=1 |
| 73 | +``` |
| 74 | + |
| 75 | +## Common Setup Patterns |
| 76 | + |
| 77 | +### Basic Setup with Custom API Key |
| 78 | + |
| 79 | +```python |
| 80 | +import os |
| 81 | +from agents import set_tracing_export_api_key, Agent, Runner |
| 82 | + |
| 83 | +# Set up tracing with custom API key |
| 84 | +tracing_api_key = os.environ.get("OPENAI_TRACING_API_KEY") |
| 85 | +if tracing_api_key: |
| 86 | + set_tracing_export_api_key(tracing_api_key) |
| 87 | + |
| 88 | +agent = Agent(name="Assistant", instructions="You are helpful") |
| 89 | +result = await Runner.run(agent, "Hello world") |
| 90 | +``` |
| 91 | + |
| 92 | +### Setup with Custom Processor |
| 93 | + |
| 94 | +```python |
| 95 | +from agents import add_trace_processor, Agent, Runner |
| 96 | +from agents.tracing.processors import TraceProcessor |
| 97 | +import json |
| 98 | + |
| 99 | +class LoggingTraceProcessor(TraceProcessor): |
| 100 | + def process_trace(self, trace): |
| 101 | + # Log trace to file |
| 102 | + with open("traces.log", "a") as f: |
| 103 | + f.write(json.dumps({ |
| 104 | + "trace_id": trace.trace_id, |
| 105 | + "workflow_name": trace.workflow_name, |
| 106 | + "timestamp": trace.started_at.isoformat() |
| 107 | + }) + "\n") |
| 108 | + |
| 109 | +# Add logging processor |
| 110 | +add_trace_processor(LoggingTraceProcessor()) |
| 111 | + |
| 112 | +agent = Agent(name="Assistant") |
| 113 | +result = await Runner.run(agent, "Test message") |
| 114 | +``` |
| 115 | + |
| 116 | +### Disable Tracing for Specific Runs |
| 117 | + |
| 118 | +```python |
| 119 | +from agents import Agent, Runner, RunConfig |
| 120 | + |
| 121 | +agent = Agent(name="Assistant") |
| 122 | + |
| 123 | +# Disable tracing for this specific run |
| 124 | +result = await Runner.run( |
| 125 | + agent, |
| 126 | + "Hello world", |
| 127 | + run_config=RunConfig(tracing_disabled=True) |
| 128 | +) |
| 129 | +``` |
| 130 | + |
| 131 | +## Troubleshooting |
| 132 | + |
| 133 | +### Common Issues |
| 134 | + |
| 135 | +1. **401 Unauthorized Error**: Make sure you have a valid OpenAI API key set |
| 136 | +2. **Tracing Not Working**: Check if `OPENAI_AGENTS_DISABLE_TRACING=1` is set |
| 137 | +3. **Custom Processors Not Receiving Data**: Ensure processors are added before running agents |
| 138 | + |
| 139 | +### Debug Mode |
| 140 | + |
| 141 | +Enable verbose logging to debug tracing issues: |
| 142 | + |
| 143 | +```python |
| 144 | +from agents import enable_verbose_stdout_logging |
| 145 | + |
| 146 | +enable_verbose_stdout_logging() |
| 147 | +``` |
0 commit comments