Session Management in AgentOps

AgentOps supports running multiple concurrent traces (sessions) without any special mode switching or restrictions. The modern approach uses the trace-based API with start_trace() and end_trace(), while legacy session functions remain available for backwards compatibility.

Modern Trace-Based Approach

The recommended way to manage sessions is using the trace-based API:

import agentops

agentops.init()
trace_context = agentops.start_trace("my_workflow")
# Your agent logic here
agentops.end_trace(trace_context, "Success")

Legacy Session API

For backwards compatibility, the legacy session functions are still available:

import agentops
agentops.init()
agentops.end_session(end_state='Success')

Managing Multiple Traces

Starting Traces

You can start multiple traces concurrently without any restrictions:

agentops.init(auto_start_session=False)
trace_1 = agentops.start_trace("user_query_1")
trace_2 = agentops.start_trace("user_query_2")
trace_3 = agentops.start_trace("background_task")

Ending Traces

End traces individually or all at once:

# End specific trace
agentops.end_trace(trace_1, "Success")

# End all active traces
agentops.end_trace(end_state="Success")

Using Decorators

The modern approach also supports decorators for automatic trace management:

import agentops

@agentops.trace
def my_workflow():
    # Your agent logic here
    return "result"

@agentops.agent
class MyAgent:
    def run(self):
        # Agent logic here
        pass

LLM Call Tracking

LLM calls are automatically tracked when using the modern instrumentation. No special handling is needed for multiple concurrent traces:

import agentops
import openai

agentops.init()
client = openai.OpenAI()

trace_1 = agentops.start_trace("query_1")
response_1 = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Hello from trace 1"}]
)

trace_2 = agentops.start_trace("query_2")
response_2 = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Hello from trace 2"}]
)

agentops.end_trace(trace_1, "Success")
agentops.end_trace(trace_2, "Success")

Migration from Legacy Multi-Session Mode

If you’re migrating from older AgentOps versions that had multi-session mode restrictions:

  1. Remove multi-session mode checks - These are no longer needed
  2. Update to trace-based API - Use start_trace() and end_trace() for new code
  3. Simplify LLM tracking - Automatic instrumentation handles LLM calls without special session assignment
  4. Use decorators - Consider using @trace, @agent, and @tool decorators for cleaner code

Examples