Skip to main content
Use @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.
Decorator-First: Use @trace decorators as your primary pattern. Context managers are for special cases like loops or conditional tracing.

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:
Benefits:
  • ✅ 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 @trace instead
Creates spans with automatic HoneyHive namespacing:

tracer.start_span() (Low-Level)

For raw OpenTelemetry-style control:

Comparison


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