Skip to main content
Context managers give you full control over trace and span lifecycle. Use them when you need to set metadata, token usage, or model information imperatively.
Prefer context managers over decorators when inputs and outputs are not simple function arguments/returns, or when you must attach token counts and costs after the fact.

Starting a trace

pandaprobe.start_trace() parameters: Returns a TraceContext with:
  • trace_id property — the auto-generated trace UUID (read-only)
  • span() method — creates child spans
  • set_input(data) — update trace input
  • set_output(data) — set trace output
  • set_metadata(dict) — merge metadata
  • set_status(status) — set TraceStatus (PENDING, RUNNING, COMPLETED, ERROR)

Creating spans

t.span() parameters:

SpanContext methods

span_id property — read-only UUID of the span.
set_token_usage and set_cost accept additional keyword arguments so you can record provider-specific breakdowns without losing structured data in the UI.

Nested spans

Spans can be nested to form a tree. Parent-child relationships are tracked automatically via a context-var span stack:
The llm-call and tool-call spans are automatically parented to the agent span.

Error handling

On exception within a span, the status is automatically set to ERROR and the error message is captured. The exception is re-raised.
Because exceptions propagate, you can rely on normal try / except boundaries around your instrumentation while still recording span-level failures.

Sync and async support

Both TraceContext and SpanContext work as sync or async context managers.