Traces & Spans
What is a Trace?
A trace represents a complete unit of work — like handling a user request, processing a document, or running an agent workflow. Think of it as a timeline:What is a Span?
A span represents a single operation within a trace — like an LLM call, tool execution, or retrieval step.Span Types
See the JavaScript SDK or Python SDK for code examples of each span type.
Trace Hierarchy
Traces contain spans organized in a tree structure:Best Practices
One trace per user request
One trace per user request
Create a new trace for each distinct user interaction. Don’t reuse trace IDs across requests.
Name traces descriptively
Name traces descriptively
Use names that describe the workflow:
process-customer-query, generate-report, analyze-document.Use appropriate span types
Use appropriate span types
Choose the span type that best represents the operation. This enables better filtering and analytics in the dashboard.
Always record tokens for LLM spans
Always record tokens for LLM spans
Token counts are essential for cost tracking and optimization.
End spans properly
End spans properly
Always call
span.end() even on errors. Use try/finally or convenience methods like ctx.llmCall() for automatic handling.Don't over-nest
Don't over-nest
Keep span depth reasonable (typically 3-5 levels max). Deep nesting makes traces hard to read.
Next Steps
Signals
Add feedback and metrics to traces
Alerting
Get notified on quality issues