Debug Fix easy · 3 min read

How to track agent errors with AgentOps

Quick answer
Use agentops.init() to enable automatic tracking of all AI agent calls and wrap your agent logic within agentops.start_session() and agentops.end_session() to capture errors explicitly. This setup logs errors and session metadata for comprehensive observability with AgentOps.
ERROR TYPE code_error
⚡ QUICK FIX
Wrap your agent code with agentops.start_session() and agentops.end_session() to capture and track errors automatically.

Why this happens

Without explicit session management, AgentOps may not capture detailed error context or session metadata, leading to incomplete error tracking. Developers often call agentops.init() but omit session boundaries, causing missed error logs and lack of observability.

Example broken code that initializes AgentOps but does not track errors explicitly:

python
import agentops
import os

agentops.init(api_key=os.environ["AGENTOPS_API_KEY"])

def run_agent():
    # Agent logic that may raise exceptions
    raise ValueError("Simulated error")

run_agent()
output
Traceback (most recent call last):
  File "agent.py", line 9, in <module>
    run_agent()
  File "agent.py", line 7, in run_agent
    raise ValueError("Simulated error")
ValueError: Simulated error

# No error tracked in AgentOps dashboard

The fix

Wrap your agent execution within agentops.start_session() and agentops.end_session() to ensure errors and session metadata are captured and sent to AgentOps. This provides automatic error tracking and observability.

The start_session() returns a session object that you can end with a status, including error states.

python
import agentops
import os

agentops.init(api_key=os.environ["AGENTOPS_API_KEY"])

def run_agent():
    # Agent logic that may raise exceptions
    raise ValueError("Simulated error")

session = agentops.start_session(tags=["example-agent"])
try:
    run_agent()
except Exception as e:
    agentops.end_session("Error")
    print(f"Agent error tracked: {e}")
else:
    agentops.end_session("Success")
output
Agent error tracked: Simulated error

# Error details and session info sent to AgentOps dashboard

Preventing it in production

Implement robust error handling with retries and fallbacks around your agent calls. Always use agentops.start_session() and agentops.end_session() to track the full lifecycle of agent executions. Validate inputs and catch exceptions to avoid silent failures.

Consider integrating agentops.init() early in your app startup to enable automatic instrumentation of OpenAI or other SDK calls.

Key Takeaways

  • Always initialize AgentOps with agentops.init() before running agents.
  • Use agentops.start_session() and agentops.end_session() to track errors and session metadata.
  • Catch exceptions in your agent code to report errors explicitly to AgentOps.
  • Early initialization enables automatic instrumentation of supported AI SDKs.
  • Proper session management ensures full observability and debugging capability.
Verified 2026-04
Verify ↗