Skip to main content

Available Decorators

AgentOps provides the following decorators:

Decorator Hierarchy

The decorators create spans that form a hierarchy:

Using Decorators

@session

The @session decorator tracks an entire user interaction from start to finish:
Each @session function call creates a new session span that contains all the agents, operations, and workflows used during that interaction.

@agent

The @agent decorator instruments a class to track its lifecycle and operations:
When an agent-decorated class is instantiated within a session, an AGENT span is created automatically.

@operation

The @operation decorator tracks discrete functions performed by an agent:
Operations represent the smallest meaningful units of work in your agent system. Each operation creates an OPERATION span with:
  • Inputs (function arguments)
  • Output (return value)
  • Duration
  • Success/failure status

@workflow

The @workflow decorator tracks a sequence of operations that work together:
Workflows help you organize related operations and see their collective performance.

@task

The @task decorator is similar to @operation but can be used for smaller units of work:
The @task and @operation decorators function identically (they are aliases in the codebase), and you can choose the one that best fits your semantic needs.

@tool

The @tool decorator tracks tool usage within agent operations and supports cost tracking. It works with all function types: synchronous, asynchronous, generator, and async generator.
The tool decorator provides:
  • Cost tracking for each tool call
  • Proper span creation and nesting
  • Support for all function types (sync, async, generator, async generator)
  • Cost accumulation in generator and async generator operations

@guardrail

The @guardrail decorator tracks guardrail input and output. You can specify the guardrail type ("input" or "output") with the spec parameter.

Decorator Attributes

You can pass additional attributes to decorators:
Common attributes include:

Complete Example

Here’s a complete example using all the decorators together:
In this example:
  1. The @session decorator wraps the entire interaction
  2. The @agent decorator defines multiple agent classes
  3. The @workflow decorator creates a workflow that coordinates agents
  4. The @operation and @task decorators track individual operations
  5. All spans are properly nested in the hierarchy
Note that LLM and TOOL spans are automatically created when you use compatible LLM libraries or tool integrations.

Best Practices

  • Use @session for top-level functions that represent complete user interactions
  • Apply @agent to classes that represent distinct components of your system
  • Use @operation for significant functions that represent complete units of work
  • Use @task for smaller functions that are part of larger operations
  • Apply @workflow to methods that coordinate multiple operations
  • Keep decorator nesting consistent with the logical hierarchy of your code
  • Add custom attributes to provide additional context for analysis
  • Use meaningful names for all decorated components

Dashboard Visualization

In the AgentOps dashboard, decorators create spans that appear in:
  1. Timeline View: Shows the execution sequence and duration
  2. Hierarchy View: Displays the parent-child relationships
  3. Detail Panels: Shows inputs, outputs, and attributes
  4. Performance Metrics: Tracks execution times and success rates
This visualization helps you understand the flow and performance of your agent system.