Example: Adding a Calculator to Our Agent › Step 4: Test the Calculator Agent

Michael BrenndoerferJune 29, 20252 min read
AI Agent HandbookAll chapters
Part 5 of 15Tool Use - Extending the Agent's Abilities
Chapter 14Example: Adding a Calculator to Our Agent › Step 4: Test the Calculator Agent
Reading section 6 of 16 · Step 4: Test the Calculator Agent · 2 minSection 6 of 16 · 2 min

Book contents

Section 6 of 16 · 2 min read

Example: Adding a Calculator to Our Agent › Step 4: Test the Calculator AgentOne section · Sections in this chapter6 / 16 ↓

Let's try it out with various math problems:

In[7]:
Code
## Test 1: The original problem
print("Test 1: Large multiplication")
print(run_agent_with_calculator("What's 1234 times 5678?"))
print()

## Test 2: Multiple operations
print("Test 2: Multiple operations")
print(run_agent_with_calculator("If I have $\$100$ and spend $\$23.50$ on lunch and $\$15.75$ on coffee, how much do I have left?"))
print()

## Test 3: Complex calculation
print("Test 3: Complex calculation")
print(run_agent_with_calculator("What's 2 to the power of 16?"))
Out[7]:
Console
Test 1: Large multiplication
Agent is using tool: calculate
Tool input: {'expression': '1234 * 5678'}
Tool result: {'success': True, 'result': 7006652, 'expression': '1234 * 5678'}
1234 times 5678 equals **7,006,652**.

Test 2: Multiple operations
Agent is using tool: calculate
Tool input: {'expression': '100 - 23.50 - 15.75'}
Tool result: {'success': True, 'result': 60.75, 'expression': '100 - 23.50 - 15.75'}
You have **$60.75** left.

Here's the breakdown:
- Starting amount: $100.00
- Lunch: -$23.50
- Coffee: -$15.75
- Remaining: **$60.75**

Test 3: Complex calculation
Agent is using tool: calculate
Tool input: {'expression': '2 ** 16'}
Tool result: {'success': True, 'result': 65536, 'expression': '2 ** 16'}
2 to the power of 16 equals **65,536**.
Test 1: Large multiplication
Agent is using tool: calculate
Tool input: {'expression': '1234 * 5678'}
Tool result: {'success': True, 'result': 7006652, 'expression': '1234 * 5678'}
1,234 times 5,678 equals 7,006,652.

Test 2: Multiple operations
Agent is using tool: calculate
Tool input: {'expression': '100 - 23.50 - 15.75'}
Tool result: {'success': True, 'result': 60.75, 'expression': '100 - 23.50 - 15.75'}
You would have $60.75 left.

Test 3: Complex calculation
Agent is using tool: calculate
Tool input: {'expression': '2 ** 16'}
Tool result: {'success': True, 'result': 65536, 'expression': '2 ** 16'}
2 to the power of 16 equals 65,536.

Look at what happened. The agent:

  • Understood each question
  • Extracted the mathematical expression
  • Called the calculator tool
  • Received the exact result
  • Formulated a natural response

For Test 2, notice how the agent translated "spend \23.50onlunchandon lunch and$15.75$ on coffee" into the expression 100 - 23.50 - 15.75. It understood the word problem and converted it into the appropriate calculation.