How to track agent costs with AgentOps
Quick answer
Use the
agentops Python package to initialize cost tracking by calling agentops.init() with your API key. Wrap your agent calls within sessions using agentops.start_session() and agentops.end_session() to automatically monitor and log usage and costs.PREREQUISITES
Python 3.8+AgentOps API keypip install agentopsOpenAI API key (if using OpenAI models)
Setup
Install the agentops package and set your environment variables for AGENTOPS_API_KEY and your AI provider API key (e.g., OPENAI_API_KEY).
- Run
pip install agentops - Export environment variables in your shell or set in your IDE:
export AGENTOPS_API_KEY=<your_agentops_key>export OPENAI_API_KEY=<your_openai_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 in your Python script, start a session to track an agent's execution, and end the session to finalize cost logging.
import os
import agentops
from openai import OpenAI
# Initialize AgentOps with your API key
agentops.init(api_key=os.environ["AGENTOPS_API_KEY"])
# Create OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Start a session to track costs and usage
session = agentops.start_session(tags=["example-agent"])
try:
# Make a chat completion call
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello, how much does this cost?"}]
)
print("Response:", response.choices[0].message.content)
finally:
# End the session and log costs
agentops.end_session("Completed successfully") output
Response: The cost depends on the tokens used, but this call is tracked by AgentOps. Session ended: Completed successfully
Common variations
You can use agentops with async code by awaiting agentops.start_session() and agentops.end_session(). AgentOps also auto-instruments OpenAI calls if agentops.init() is called before client usage. You can track multiple sessions or tag sessions for grouping costs.
import asyncio
import os
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"])
session = await agentops.start_session(tags=["async-agent"])
try:
response = await client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Async cost tracking example."}]
)
print("Async response:", response.choices[0].message.content)
finally:
await agentops.end_session("Async session complete")
asyncio.run(main()) output
Async response: This is an example of async cost tracking with AgentOps. Session ended: Async session complete
Troubleshooting
- If you see no cost data in your AgentOps dashboard, ensure
agentops.init()is called before any AI client usage. - Verify your
AGENTOPS_API_KEYis correct and has permissions. - For missing session logs, always call
agentops.end_session()to flush data.
Key Takeaways
- Initialize AgentOps early in your app with
agentops.init()to enable automatic cost tracking. - Wrap your AI calls within
agentops.start_session()andagentops.end_session()to monitor usage and expenses. - AgentOps supports both synchronous and asynchronous Python workflows for flexible integration.