Signature
Parameters
Name for the trace. Defaults to the decorated function’s name if not provided.Example:
"chat-completion", "customer-support", "process-order"Tags to attach to all spans in this trace.Example:
["production", "high-priority"]Custom key-value pairs to attach to the trace.Example:
{"order_id": "123", "plan": "enterprise"}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()Session identifier for grouping related traces. Accepts a string or callable.
Version string stored in trace metadata (e.g., model version, app version).Example:
"1.0.0", "claude-sonnet-4-6"Explicit trace ID. If not provided, a UUID is generated. Accepts a string or callable.Use this to correlate traces across services.
If
True, serialize the function’s arguments and store them in trace.metadata["input"].If
True, serialize the function’s return value and store it in trace.metadata["output"].Mapping of tool names to mock outputs or callables. Active only within this function call.See mock_tools() for details.
Default mock response for any tool not in
tool_mocks. Falls back to the process-wide default set by test_mode.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), usetrace():
See Also
- trace() — Context manager alternative
- span() — Create manual spans within a trace
- Custom Metadata — Adding custom data to traces

