Skip to main content
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
poetry add agentops anthropic
uv 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"
ANTHROPIC_API_KEY="your_anthropic_api_key_here"
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()
import agentops
import anthropic
import json
from datetime import datetime

# Initialize AgentOps
agentops.init()

# Create Anthropic client
client = anthropic.Anthropic()

# Define tools
tools = [
    {
        "type": "custom",
        "name": "get_current_time",
        "description": "Get the current date and time",
        "input_schema": {
            "type": "object",
            "properties": {},
            "required": []
        }
    }
]

def get_current_time():
    return {"current_time": datetime.now().isoformat()}

# Make a request with tools
  message = client.messages.create(
    model="claude-opus-4-20250514",
    tools=tools,
    messages=[
        {"role": "user", "content": "What time is it now?"}
    ]
  )

# Handle tool use
if message.content[0].type == "tool_calls":
    tool_call = message.content[0].tool_calls[0]
    tool_name = tool_call.name
    
    if tool_name == "get_current_time":
        tool_response = get_current_time()
        
        # Continue the conversation with the tool response
        second_message = client.messages.create(
          model="claude-opus-4-20250514",
            messages=[
                {"role": "user", "content": "What time is it now?"},
                {
                    "role": "assistant",
                    "content": [
                        {
                            "type": "tool_calls",
                            "tool_calls": [
                                {
                                    "type": "custom",
                                    "name": "get_current_time",
                                    "input": {}
                                }
                            ]
                        }
                    ]
                },
                {
                    "role": "tool",
                    "content": json.dumps(tool_response),
                    "tool_call_id": tool_call.id
                }
            ]
      )

        print(second_message.content[0].text)
else:
    print(message.content[0].text)

More Examples

Understanding Tools

Claude integration with tool usage and advanced features

Sync Example

Shows synchronous calls with the Anthropic SDK.

Async Example

Demonstrates asynchronous calls with the Anthropic SDK.