@trace decorators to trace business logic, workflow steps, and application components that automatic LLM instrumentation does not cover. Initialize your tracer first (see tracer initialization), then add trace enrichments for metadata on each span.
When should you use custom spans?
Custom spans let you trace specific business logic, workflow steps, and application components beyond just LLM calls. For the tracing data model behind spans, see tracing concepts. Use Cases:- Business process tracking
- Performance bottleneck identification
- Complex workflow visualization
- Custom error tracking
How do you trace functions with @trace?
The recommended approach for function-level tracing:- ✅ Automatic inputs/outputs capture
- ✅ Nested calls create proper trace hierarchy
- ✅ Clean code without span management clutter
For details on adding metadata with
enrich_span(), see Enriching Traces.How do you trace async functions?
The@trace decorator works with both sync and async functions automatically:
No separate
@atrace needed. The decorator detects async functions automatically.When should you use context managers?
Use context managers for scenarios where decorators don’t fit:When to use context managers
- ✅ Loop iterations - Tracing individual items in batch processing
- ✅ Conditional spans - Dynamic span creation based on runtime conditions
- ✅ Non-function blocks - Setup, cleanup, or configuration phases
- ❌ Regular functions - Use
@traceinstead
enrich_span_context() (Recommended)
Creates spans with automatic HoneyHive namespacing:
tracer.start_span() (Low-Level)
For raw OpenTelemetry-style control:
Comparison
| Feature | enrich_span_context() | tracer.start_span() |
|---|---|---|
| Auto namespacing | ✅ Automatic | ❌ Manual |
| HoneyHive enrichment | ✅ Built-in | ❌ Manual attributes |
| Best for | Business logic | Low-level control |
How do you create conditional spans?
Create spans only when conditions are met:What are custom span best practices?
Span naming
Avoid Over-Instrumentation
Where should you go next?
Enriching Traces
Add metadata with enrich_span and enrich_session
Enrichment Schema
Namespaces, data types, backend attributes
Distributed Tracing
Cross-service tracing
Python SDK Reference
Full API documentation

