Track and analyze your Anthropic API calls with AgentOps
AgentOps provides seamless integration with Anthropic’s Python SDK, allowing you to track and analyze all your Claude model interactions automatically.
Then load the environment variables in your Python code:
Copy
from dotenv import load_dotenvimport os# Load environment variables from .env fileload_dotenv()# Set up environment variables with fallback valuesos.environ["ANTHROPIC_API_KEY"] = os.getenv("ANTHROPIC_API_KEY")os.environ["AGENTOPS_API_KEY"] = os.getenv("AGENTOPS_API_KEY")
import agentopsimport anthropic# Initialize AgentOpsagentops.init()# Create Anthropic clientclient = anthropic.Anthropic()# Make a streaming requestwith 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()