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)