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.
Initialize AgentOps at the beginning of your application to automatically track all Anthropic API calls:
import agentopsimport anthropic# Initialize AgentOpsagentops.init(<INSERT YOUR API KEY HERE>)# Create Anthropic clientclient = anthropic.Anthropic(api_key="<YOUR_ANTHROPIC_API_KEY>")# Make a completion request - AgentOps will track it automaticallymessage = client.messages.create( model="claude-3-opus-20240229", max_tokens=1000, messages=[{"role":"user","content":"What is artificial intelligence?"}])print(message.content[0].text)
import agentopsimport anthropic# Initialize AgentOps agentops.init(<INSERT YOUR API KEY HERE>)# Create Anthropic clientclient = anthropic.Anthropic(api_key="<YOUR_ANTHROPIC_API_KEY>")# Make a streaming requestwith 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
import agentopsimport anthropic# Initialize AgentOpsagentops.init(<INSERT YOUR API KEY HERE>)# Create Anthropic clientclient = anthropic.Anthropic(api_key="<YOUR_ANTHROPIC_API_KEY>")# Make a request with a system promptmessage = 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)
import agentopsimport anthropicimport jsonfrom datetime import datetime# Initialize AgentOps agentops.init(<INSERT YOUR API KEY HERE>)# Create Anthropic clientclient = anthropic.Anthropic(api_key="<YOUR_ANTHROPIC_API_KEY>")# Define toolstools =[{"name":"get_current_time","description":"Get the current date and time","input_schema":{},}]defget_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 useif message.content[0].type=="tool_use": tool_use = message.content[0].tool_use tool_name = tool_use.nameif 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)