The Problem: Claude Gets Math Wrong
response = client.messages.create(
model="claude-3-haiku-20240307",
messages=[{"role": "user", "content": "Multiply 1984135 by 9343116. Only respond with the result"}],
max_tokens=400
)
# Claude replied: 18593367726060
# Correct answer: 18538003464660
# Off by 55,364,261,400!
Solution: Give Claude a Calculator
def calculator(operation, operand1, operand2):
if operation == "add":
return operand1 + operand2
elif operation == "subtract":
return operand1 - operand2
elif operation == "multiply":
return operand1 * operand2
elif operation == "divide":
if operand2 == 0:
raise ValueError("Cannot divide by zero.")
return operand1 / operand2
calculator_tool = {
"name": "calculator",
"description": "A simple calculator that performs basic arithmetic operations.",
"input_schema": {
"type": "object",
"properties": {
"operation": {
"type": "string",
"enum": ["add", "subtract", "multiply", "divide"],
"description": "The arithmetic operation to perform."
},
"operand1": {"type": "number", "description": "The first operand."},
"operand2": {"type": "number", "description": "The second operand."}
},
"required": ["operation", "operand1", "operand2"]
}
}
response = client.messages.create(
model="claude-3-haiku-20240307",
messages=[{"role": "user", "content": "Multiply 1984135 by 9343116. Only respond with the result"}],
max_tokens=300,
tools=[calculator_tool]
)
# response.stop_reason == "tool_use"
tool_name = response.content[0].name # "calculator"
tool_inputs = response.content[0].input # {"operation": "multiply", "operand1": 1984135, "operand2": 9343116}
result = calculator(tool_inputs["operation"], tool_inputs["operand1"], tool_inputs["operand2"])
# RESULT: 18538003464660 — correct!
If you ask "What color are emeralds?" with a calculator attached, Claude might try to call it. Fix:
system="You have access to tools, but only use them when necessary."