Chapter 03 / 10 min read
The Calculator as Our Engineering Laboratory
The three real calculator functions, structured results, test cases and the validation problem.
The calculator as our engineering laboratory
The calculator became the consistent example for the rest of the implementation. This was a deliberate choice: we wanted to understand agent mechanics without mixing in web search, report generation and external services.
The actual problem
Suppose a user asks:
Multiply 250 by 40, then divide the result by 5, and finally add 100.
The arithmetic is simple:
250 × 40 = 10,000
10,000 ÷ 5 = 2,000
2,000 + 100 = 2,100
But the application has several engineering responsibilities:
- Select the correct operation for each part.
- Supply the correct argument names and values.
- Execute only functions that the application has explicitly registered.
- Validate the arguments before invocation.
- Capture the result of each operation.
- Feed tool results back to the model.
- Handle a failure such as division by zero.
- Keep an execution history that can be inspected.
This is why a calculator is useful: the expected mathematical result is easy to verify, so errors in orchestration become visible.
1. The calculator module
The source project kept the three functions together in tools/calculator.py. There was no need to create a separate Python module for every operation.
def calculate_addition(a: float, b: float) -> dict:
"""Add two numbers and return the result."""
result = a + b
return {
"success": True,
"operation": "addition",
"inputs": {"a": a, "b": b},
"result": result,
}
def multiply_numbers(a: float, b: float) -> dict:
"""Multiply two numbers and return the result."""
result = a * b
return {
"success": True,
"operation": "multiplication",
"inputs": {"a": a, "b": b},
"result": result,
}
def divide_numbers(a: float, b: float) -> dict:
"""Divide two numbers and return the result."""
try:
result = a / b
except ZeroDivisionError:
return {
"success": False,
"operation": "division",
"inputs": {"a": a, "b": b},
"error": "Division by zero is not allowed.",
}
return {
"success": True,
"operation": "division",
"inputs": {"a": a, "b": b},
"result": result,
}
if __name__ == "__main__":
print(calculate_addition(100, 250))
print(multiply_numbers(10, 20))
print(multiply_numbers(-5, 4))
print(divide_numbers(100, 5))
print(divide_numbers(100, 0))
print(divide_numbers(-100, 5))
Why return dictionaries?
A bare number would tell us the answer, but not whether the operation succeeded or which inputs were used. The dictionary gives the caller a stable shape:
| Field | Purpose |
|---|---|
success | Indicates whether the operation succeeded |
operation | Identifies the operation |
inputs | Records the supplied operands |
result | Contains the result on success |
error | Contains an error explanation on failure |
Success and failure results are intentionally not identical: successful results have result; failed results have error.
2. The observed output
The original PowerShell run produced the following values:
addition: 100 + 250 = 350
multiplication: 10 * 20 = 200
multiplication: -5 * 4 = -20
division: 100 / 5 = 20.0
division: 100 / 0 -> Division by zero is not allowed.
division: -100 / 5 = -20.0
The negative-number tests are useful. They show that the functions are not merely hard-coded for positive examples.
3. A function is reusable
The same function can be called by different parts of the application with different values:
first = multiply_numbers(10, 20)
second = multiply_numbers(250, 40)
third = multiply_numbers(-5, 4)
The function does not need to know who requested the calculation. Reuse is one reason we separate a tool's implementation from the agent's orchestration logic.
4. Validation is not automatically enforced by a type hint
The function signature says a: float and b: float, but Python type hints do not automatically reject every invalid runtime call. A key concern is that someone could call a function directly and bypass a separate Pydantic validation step.
That observation leads to an important rule: validation must be placed at the execution boundary, not merely performed somewhere else in the application. The tool executor will become that boundary later in the handbook.
Next
We now have reliable, testable functions. The next task is to map model-returned tool names to those functions without allowing the model to execute arbitrary code.