Skip to content

Commit e1d861c

Browse files
authored
docs: updates for #2844 changes (#2845)
1 parent b0ab25c commit e1d861c

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

docs/tracing.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,57 @@ By default, the trace is named "Agent workflow". You can set this name if you us
4444

4545
In addition, you can set up [custom trace processors](#custom-tracing-processors) to push traces to other destinations (as a replacement, or secondary destination).
4646

47+
## Long-running workers and immediate exports
48+
49+
The default [`BatchTraceProcessor`][agents.tracing.processors.BatchTraceProcessor] exports traces
50+
in the background every few seconds, or sooner when the in-memory queue reaches its size trigger,
51+
and also performs a final flush when the process exits. In long-running workers such as Celery,
52+
RQ, Dramatiq, or FastAPI background tasks, this means traces are usually exported automatically
53+
without any extra code, but they may not appear in the Traces dashboard immediately after each job
54+
finishes.
55+
56+
If you need an immediate delivery guarantee at the end of a unit of work, call
57+
[`flush_traces()`][agents.tracing.flush_traces] after the trace context exits.
58+
59+
```python
60+
from agents import Runner, flush_traces, trace
61+
62+
63+
@celery_app.task
64+
def run_agent_task(prompt: str):
65+
try:
66+
with trace("celery_task"):
67+
result = Runner.run_sync(agent, prompt)
68+
return result.final_output
69+
finally:
70+
flush_traces()
71+
```
72+
73+
```python
74+
from fastapi import BackgroundTasks, FastAPI
75+
from agents import Runner, flush_traces, trace
76+
77+
app = FastAPI()
78+
79+
80+
def process_in_background(prompt: str) -> None:
81+
try:
82+
with trace("background_job"):
83+
Runner.run_sync(agent, prompt)
84+
finally:
85+
flush_traces()
86+
87+
88+
@app.post("/run")
89+
async def run(prompt: str, background_tasks: BackgroundTasks):
90+
background_tasks.add_task(process_in_background, prompt)
91+
return {"status": "queued"}
92+
```
93+
94+
[`flush_traces()`][agents.tracing.flush_traces] blocks until currently buffered traces and spans are
95+
exported, so call it after `trace()` closes to avoid flushing a partially built trace. You can skip
96+
this call when the default export latency is acceptable.
97+
4798
## Higher level traces
4899

49100
Sometimes, you might want multiple calls to `run()` to be part of a single trace. You can do this by wrapping the entire code in a `trace()`.

0 commit comments

Comments
 (0)