How to beginner · 3 min read

How to track tool calls with AgentOps

Quick answer
Use agentops.init() to initialize AgentOps for automatic tracking of all AI calls, including tool calls. For manual tracking of tool calls, use agentops.start_session() and agentops.end_session() to create sessions that capture tool usage and metadata.

PREREQUISITES

  • Python 3.8+
  • AgentOps API key
  • pip install agentops
  • OpenAI API key (optional, if using OpenAI models)

Setup

Install the agentops Python package and set your API key as an environment variable. This enables automatic instrumentation of AI calls and tool usage tracking.

bash
pip install agentops
output
Collecting agentops
  Downloading agentops-1.0.0-py3-none-any.whl (15 kB)
Installing collected packages: agentops
Successfully installed agentops-1.0.0

Step by step

Initialize AgentOps in your Python script to automatically track AI calls and tool usage. Use start_session() and end_session() to manually track tool call sessions with tags and metadata.

python
import os
import agentops
import json

# Set your AgentOps API key in environment variable AGENTOPS_API_KEY
agentops.init(api_key=os.environ["AGENTOPS_API_KEY"])

# Start a manual session to track tool calls
session = agentops.start_session(tags=["tool-call", "weather"])

# Simulate a tool call (e.g., calling a weather API)
def call_weather_tool(location):
    # Example tool call logic
    return f"Weather for {location}: Sunny, 75F"

result = call_weather_tool("New York City")
print("Tool call result:", result)

# End the session with a success status
agentops.end_session("Success")
output
Tool call result: Weather for New York City: Sunny, 75F

Common variations

You can combine AgentOps with OpenAI or other AI clients for automatic tracking of tool calls embedded in AI workflows. For async code, use asyncio with AgentOps. Streaming outputs are also tracked automatically.

python
import os
import asyncio
import agentops
from openai import OpenAI

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

async def async_tool_call():
    session = agentops.start_session(tags=["async-tool-call"])
    response = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Call a tool to get current time"}]
    )
    print("AI response:", response.choices[0].message.content)
    agentops.end_session("Success")

asyncio.run(async_tool_call())
output
AI response: The current time is 3:45 PM.

Troubleshooting

  • If tool calls are not appearing in your AgentOps dashboard, verify that agentops.init() is called before any AI or tool calls.
  • Ensure your AGENTOPS_API_KEY environment variable is set correctly.
  • For manual sessions, always call agentops.end_session() to close the session and send data.

Key Takeaways

  • Initialize AgentOps with agentops.init() to enable automatic tracking of AI and tool calls.
  • Use start_session() and end_session() for manual tracking of tool call sessions with metadata.
  • AgentOps integrates seamlessly with OpenAI and other AI clients for comprehensive observability.
  • Always set your AGENTOPS_API_KEY environment variable before running your code.
  • Troubleshoot missing data by ensuring proper initialization and session closure.
Verified 2026-04 · gpt-4o-mini
Verify ↗