AgentOps provides seamless integration with Anthropic’s Python SDK, allowing you to track and analyze all your Claude model interactions automatically.

Installation

pip install agentops anthropic

Setting Up API Keys

Before using Anthropic with AgentOps, you need to set up your API keys. You can obtain:

Then to set them up, you can either export them as environment variables or set them in a .env file.

export ANTHROPIC_API_KEY="your_anthropic_api_key_here"
export AGENTOPS_API_KEY="your_agentops_api_key_here"

Then load the environment variables in your Python code:

from dotenv import load_dotenv
import os

# Load environment variables from .env file
load_dotenv()

# Set up environment variables with fallback values
os.environ["ANTHROPIC_API_KEY"] = os.getenv("ANTHROPIC_API_KEY")
os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY")

Usage

Initialize AgentOps at the beginning of your application to automatically track all Anthropic API calls:

import agentops
import anthropic

# Initialize AgentOps
agentops.init()

# Create Anthropic client
client = anthropic.Anthropic()

# Make a completion request - AgentOps will track it automatically
message = client.messages.create(
    model="claude-sonnet-4-20250514",
    messages=[
        {"role": "user", "content": "What is artificial intelligence?"}
    ]
)

# Print the response received
print(message.content[0].text)

Examples

import agentops
import anthropic
      
# Initialize AgentOps
agentops.init()

# Create Anthropic client
client = anthropic.Anthropic()

# Make a streaming request
with client.messages.stream(
    model="claude-sonnet-4-20250514",
    messages=[
        {"role": "user", "content": "Write a short poem about artificial intelligence."}
    ]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
    print()

More Examples