Fix AgentOps not tracking calls
Quick answer
To fix AgentOps not tracking calls, ensure you call agentops.init(api_key=os.environ["AGENTOPS_API_KEY"]) before any LLM client usage. This initializes automatic instrumentation. Also, verify your API key is correctly set in the environment and that you are using the latest agentops package.
PREREQUISITES
Python 3.8+AgentOps API keypip install agentopsOpenAI API key (if using OpenAI LLMs)
Setup
Install the agentops package and set your API keys as environment variables. This enables automatic tracking of LLM calls.
- Install AgentOps SDK:
pip install agentops - Set environment variables in your shell or .env file:
export AGENTOPS_API_KEY="your_agentops_api_key"export OPENAI_API_KEY="your_openai_api_key"
pip install agentops output
Collecting agentops Downloading agentops-1.0.0-py3-none-any.whl (10 kB) Installing collected packages: agentops Successfully installed agentops-1.0.0
Step by step
Initialize agentops at the start of your application to enable automatic tracking. Then create and use your LLM client as usual.
import os
from openai import OpenAI
import agentops
# Initialize AgentOps for automatic tracking
agentops.init(api_key=os.environ["AGENTOPS_API_KEY"])
# Create OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Make a chat completion call
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello, AgentOps!"}]
)
print("Response:", response.choices[0].message.content)
# Optionally, manually start and end a session
session = agentops.start_session(tags=["example-session"])
# ... your code here ...
agentops.end_session("Success") output
Response: Hello, AgentOps! # (No errors, AgentOps tracks this call automatically)
Common variations
You can use agentops with other LLM providers by initializing before client creation. For async code, agentops.init() remains the same. To manually track sessions, use start_session() and end_session(). Ensure you do not create LLM clients before calling agentops.init().
import asyncio
import agentops
from openai import OpenAI
async def main():
agentops.init(api_key=os.environ["AGENTOPS_API_KEY"])
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = await client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Async call with AgentOps"}]
)
print("Async response:", response.choices[0].message.content)
asyncio.run(main()) output
Async response: Async call with AgentOps # (Tracked automatically by AgentOps)
Troubleshooting
- If calls are not tracked: Confirm
agentops.init()is called before any LLM client instantiation. - Check API key: Ensure
AGENTOPS_API_KEYis set correctly in your environment. - Package version: Upgrade
agentopsto the latest version withpip install --upgrade agentops. - Conflicts: Avoid multiple initializations or creating clients before
agentops.init().
Key Takeaways
- Always call agentops.init() before creating any LLM clients to enable automatic tracking.
- Set your AGENTOPS_API_KEY environment variable correctly to authenticate tracking.
- Use agentops.start_session() and agentops.end_session() for manual session control.
- Keep the agentops package updated to avoid bugs affecting tracking.
- Do not instantiate LLM clients before initializing agentops.