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

Installation

Make sure to call agentops.init before calling any openai, cohere, crew, etc models.

pip install agentops anthropic

Usage

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

import agentops
import anthropic

# Initialize AgentOps
agentops.init(<INSERT YOUR API KEY HERE>)

# Create Anthropic client
client = anthropic.Anthropic(api_key="<YOUR_ANTHROPIC_API_KEY>")

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

print(message.content[0].text)

Advanced Examples

Streaming Example

AgentOps automatically tracks streaming completions:

import agentops
import anthropic
      
# Initialize AgentOps
      agentops.init(<INSERT YOUR API KEY HERE>)

# Create Anthropic client
client = anthropic.Anthropic(api_key="<YOUR_ANTHROPIC_API_KEY>")

# Make a streaming request
with client.messages.stream(
    model="claude-3-haiku-20240307",
    max_tokens=1000,
    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()  # Add a newline at the end

System Prompt Example

AgentOps tracks interactions with system prompts:

import agentops
import anthropic

# Initialize AgentOps
agentops.init(<INSERT YOUR API KEY HERE>)

# Create Anthropic client
client = anthropic.Anthropic(api_key="<YOUR_ANTHROPIC_API_KEY>")

# Make a request with a system prompt
message = client.messages.create(
    model="claude-3-sonnet-20240229",
    max_tokens=1000,
    system="You are a helpful AI assistant with expertise in science and technology. Keep your answers concise and accurate.",
    messages=[
        {"role": "user", "content": "Explain quantum computing in simple terms."}
    ]
)

print(message.content[0].text)

Tool Use Example

AgentOps tracks tool use with Claude:

import agentops
import anthropic
import json
from datetime import datetime

# Initialize AgentOps
  agentops.init(<INSERT YOUR API KEY HERE>)

# Create Anthropic client
client = anthropic.Anthropic(api_key="<YOUR_ANTHROPIC_API_KEY>")

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

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

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

# Handle tool use
if message.content[0].type == "tool_use":
    tool_use = message.content[0].tool_use
    tool_name = tool_use.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-3-opus-20240229",
            max_tokens=1000,
            messages=[
                {"role": "user", "content": "What time is it now?"},
                {"role": "assistant", "content": [
                    {"type": "tool_use", "tool_use": {
                        "name": "get_current_time",
                        "input": {}
                    }}
                ]},
                {"role": "user", "content": [
                    {"type": "tool_result", "tool_result": {
                        "tool_name": "get_current_time",
                        "content": json.dumps(tool_response)
                    }}
                ]}
            ]
      )

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

Environment Variables

Set your API key as an .env variable for easy access.

AGENTOPS_API_KEY=<YOUR API KEY>
ANTHROPIC_API_KEY=<YOUR ANTHROPIC API KEY>

Read more about environment variables in Advanced Configuration