Skip to main content

Signature

Parameters

string
default:"function name"
Name for the trace. Defaults to the decorated function’s name if not provided.Example: "chat-completion", "customer-support", "process-order"
list[string]
default:"[]"
Tags to attach to all spans in this trace.Example: ["production", "high-priority"]
dict
default:"{}"
Custom key-value pairs to attach to the trace.Example: {"order_id": "123", "plan": "enterprise"}
string | Callable[[], str | None]
default:"None"
User identifier. Can be a static string or a zero-argument callable that returns a string.Example: "user_123" or lambda: get_current_user_id()
string | Callable[[], str | None]
default:"None"
Session identifier for grouping related traces. Accepts a string or callable.
string
default:"None"
Version string stored in trace metadata (e.g., model version, app version).Example: "1.0.0", "claude-sonnet-4-6"
string | Callable[[], str | None]
default:"auto-generated"
Explicit trace ID. If not provided, a UUID is generated. Accepts a string or callable.Use this to correlate traces across services.
boolean
default:false
If True, serialize the function’s arguments and store them in trace.metadata["input"].
boolean
default:false
If True, serialize the function’s return value and store it in trace.metadata["output"].

Examples

Basic

With metadata

Dynamic user and session

Capture inputs and outputs

Sync and async

Works with both sync and async functions:

Nesting

Nested @observe decorators skip the inner trace — only the outermost creates a trace context. All LLM calls within nested functions are attributed to the outermost trace.

Context manager alternative

If decorators aren’t convenient (notebooks, scripts), use trace():
See trace() for details.

See Also