wrap_agent as a context manager when you want to add tracing to code that already exists without restructuring it. Drop a with block around the body you want to trace — no function extraction required.
This pattern is Python-specific. For TypeScript, use the callable wrapper pattern in
Async agent or Sync agent.
Async agent
Sync agent
Accessing run_id after the block
Theas run variable stays in scope after the block exits:
Tool-calling loop
For multi-turn loops, put the entire loop inside the block. The span stays open for all turns and closes when the block exits.Error handling
Unhandled exceptions are recorded on the span automatically — notry/except needed for the common case:
run.record_error manually:
What happens if on_complete is not called
The span ends cleanly when the block exits, butai.agent.output is not set. The run appears in the dashboard without an output value. Call run.on_complete(result) before the block exits to record the output.
Callable wrapper vs context manager
| Callable wrapper | Context manager | |
|---|---|---|
| Syntax | wrap_agent("name", fn) | wrap_agent("name", input=...) |
| Requires extracting a function | Yes | No |
| Output auto-captured from return value | Yes | No — call run.on_complete |
| Reusable wrapped callable | Yes | No — inline only |
| Async and sync support | Yes | Yes |
run_id access | From return tuple (result, run_id, span) | From run.run_id after block |

