Traces
Effectively manage traces in your agent workflow
Automatic Trace Management
The simplest way to create and manage traces is to use the init
function with automatic trace creation:
This approach:
- Creates a trace automatically when you initialize the SDK
- Tracks all events in the context of this trace
- Manages the trace throughout the lifecycle of your application
Manual Trace Creation
For more control, you can disable automatic trace creation and start traces manually:
Manual trace management is useful when:
- You want to control exactly when trace tracking begins
- You need to associate different traces with different sets of tags
- Your application has distinct workflows that should be tracked separately
Using the Trace Decorator
You can use the @trace
decorator to create a trace for a specific function:
Trace Context Manager
TraceContext objects support Python’s context manager protocol, making it easy to manage trace lifecycles:
Trace States
Every trace has an associated state that indicates its completion status. AgentOps provides multiple ways to specify trace end states for flexibility and backward compatibility.
AgentOps TraceState Enum (Recommended)
The recommended approach is to use the TraceState
enum from AgentOps:
OpenTelemetry StatusCode
For advanced users familiar with OpenTelemetry, you can use StatusCode directly:
String Values
String values are also supported for convenience:
State Mapping
All state representations map to the same underlying OpenTelemetry StatusCode:
AgentOps TraceState | OpenTelemetry StatusCode | String Values | Description |
---|---|---|---|
TraceState.SUCCESS | StatusCode.OK | ”Success” | Trace completed successfully |
TraceState.ERROR | StatusCode.ERROR | ”Error” | Trace encountered an error |
TraceState.UNSET | StatusCode.UNSET | ”Indeterminate” | Trace state is not determined |
Default Behavior
If no end state is provided, the default is TraceState.SUCCESS
:
Trace Attributes
Every trace collects comprehensive metadata to provide rich context for analysis. Trace attributes are automatically captured by AgentOps and fall into several categories:
Core Trace Attributes
Identity and Timing:
- Trace ID: A unique identifier for the trace
- Span ID: Identifier for the root span of the trace
- Start Time: When the trace began
- End Time: When the trace completed (set automatically)
- Duration: Total execution time (calculated automatically)
User-Defined Attributes:
- Trace Name: Custom name provided when starting the trace
- Tags: Labels for filtering and grouping (list of strings or dictionary)
- End State: Success, error, or unset status
Resource Attributes
AgentOps automatically captures system and environment information:
Project and Service:
- Project ID: AgentOps project identifier
- Service Name: Service name (defaults to “agentops”)
- Service Version: Version of your service
- Environment: Deployment environment (dev, staging, prod)
- SDK Version: AgentOps SDK version being used
Host System Information:
- Host Name: Machine hostname
- Host System: Operating system (Windows, macOS, Linux)
- Host Version: OS version details
- Host Processor: CPU architecture information
- Host Machine: Machine type identifier
Performance Metrics:
- CPU Count: Number of available CPU cores
- CPU Percent: CPU utilization at trace start
- Memory Total: Total system memory
- Memory Available: Available system memory
- Memory Used: Currently used memory
- Memory Percent: Memory utilization percentage
Dependencies:
- Imported Libraries: List of Python packages imported in your environment
Span Hierarchy
Nested Operations:
- Spans: All spans (operations, agents, tools, workflows) recorded during the trace
- Parent-Child Relationships: Hierarchical structure of operations
- Span Kinds: Types of operations (agents, tools, workflows, tasks)
Accessing Trace Attributes
While most attributes are automatically captured, you can access trace information programmatically:
Custom Attributes
You can add custom attributes to spans within your trace:
Attribute Naming Conventions
AgentOps follows OpenTelemetry semantic conventions for attribute naming:
- AgentOps Specific:
agentops.*
(e.g.,agentops.span.kind
) - GenAI Operations:
gen_ai.*
(e.g.,gen_ai.request.model
) - System Resources: Standard names (e.g.,
host.name
,service.name
) - Custom Attributes: Use your own namespace (e.g.,
myapp.user.id
)
Trace Context
Traces create a context for all span recording. When a span is recorded:
- It’s associated with the current active trace
- It’s automatically included in the trace’s timeline
- It inherits the trace’s tags for filtering and analysis
Viewing Traces in the Dashboard
The AgentOps dashboard provides several views for analyzing your traces:
- Trace List: Overview of all traces with filtering options
- Trace Details: In-depth view of a single trace
- Timeline View: Chronological display of all spans in a trace
- Tree View: Hierarchical representation of agents, operations, and events
- Analytics: Aggregated metrics across traces
Best Practices
- Start traces at logical boundaries in your application workflow
- Use descriptive trace names to easily identify them in the dashboard
- Apply consistent tags to group related traces
- Use fewer, longer traces rather than many short ones for better analysis
- Use automatic trace management unless you have specific needs for manual control
- Leverage context managers for automatic trace lifecycle management
- Set appropriate end states to track success/failure rates