Give an assistant a calculator and a lookup tool

You will build a small assistant that calls two tools you wrote and uses what they return in its answer.

If you ask a language model what S$289 is in ringgit, or what 17 percent of S$4,380 comes to, it usually gives you a number that looks right. Sometimes the number is right. As lesson 3.2 on AI fundamentals explains, a model writes by predicting the next token over and over, so to the model a calculation in the middle of a sentence is just more text. If your app relies on a calculation, the arithmetic should come from code instead of from the model's prediction.

You'll build a small assistant with 2 tools and test when the model uses each one. The calculator is there so numbers come from code, and the lookup is there so facts come from your own data. The exercise takes about 35 minutes, from building the tools to reading the results.

Building the calculator and the lookup tool

The calculator takes two numbers and one operation. The operation is an enum with four allowed values: add, subtract, multiply and divide. Keep the calculator that simple. Don't build a tool that evaluates any expression the model writes, because that means running model output as code, which is dangerous for the reasons given in lesson 8.2. The calculator code checks that both inputs are numbers, refuses division by zero with a clear error message, and returns the result.

The lookup tool searches a small table you create. Marcus, who sells coffee gear, uses his product list: 10 rows (an invented example) with columns for product code, name, price in Singapore dollars and units in stock. A class timetable, a price list or a list of office rooms would work just as well. The lookup takes a product name or code and returns the matching row, or a clear message if nothing matches.

Write each tool definition the way lesson 4.2 describes: a plain name, a description that says when to use the tool and when not to, typed parameters and required fields. The model reads these definitions when it decides which tool to call.

Getting a coding assistant to write the loop

Ask a coding assistant to write the script that connects the model to your tools. The script sends the user's question along with both tool definitions and checks whether the reply contains tool calls. If it does, the script validates the arguments, runs the matching function and sends the results back. It repeats this until the model gives a text answer.

The loop needs a round limit. The script must stop after five rounds no matter what, so a confused model cannot loop forever, and lesson 5.2 makes this limit more careful.

The script also writes a tool call log, with one line per tool call showing the tool name, the arguments, whether validation passed, and the result.

Read the generated code before you run it. Confirm that validation runs before each function runs, since checking afterwards is too late, and that the API key is read from the environment.

Writing eight test questions for four kinds of case

Write 8 test questions, two for each of four kinds of case: no tool, one tool, both tools and bad input. Marcus wrote these:

No tool: "What does a burr grinder do?" No tool: "Do you ship to Malaysia?" One tool, the lookup: "How many hand grinders do you have in stock?" One tool, the calculator: "What is 289 times 3?" Both tools: "How much would two of the gooseneck kettles cost?" Both tools: "If I buy the grinder and the kettle, what is the total?" Bad input: "Look up product code XJ-9999" Bad input: "What is 50 divided by 0?"

The Malaysia question is a trap if the answer isn't in any tool or in the prompt. A good assistant admits it doesn't know and does not make up something like a shipping policy. The kettle question needs a lookup for the price, then the calculator: in Marcus's table a gooseneck kettle costs S$68, so 2 kettles x S$68 = S$136. The grinder-and-kettle question needs two lookups and an addition. Product code XJ-9999 does not exist. Both bad-input questions should get a clear explanation instead of an error message or an invented answer.

Reading the tool call log before the answer

Run all eight questions. For each one, read the log before you read the answer, and check four things in order: which tools were called, or that none was called when none was needed; the arguments; what each tool returned; and whether the final answer used that result correctly.

Reading the log first matters because an answer can be right by luck, and you only spot that in the log. Marcus's first run went mostly as planned, with one surprise. On the kettle question, the model looked up the price and then did the multiplication itself instead of calling the calculator, so the answer was right only by luck.

When the log shows a bad tool choice, change the tool's description and run the question again, because adding one line to a description can change whether the model calls that tool. Marcus added "Use this for every calculation, including simple ones" to the calculator's description, and on the next run the model called the calculator.

When you finish, you should have a script with two working tools, argument validation and a round limit, plus eight test questions covering no tool, one tool, both tools and bad input. You should also have the tool call log from all eight questions, with a short note beside each saying whether the tool choice, the arguments and the final answer were right. The note on each result matters more than a perfect score, because a log that shows exactly where the model chose badly is the raw material for the agent you build in module 5.

Build the two-tool assistant and run eight test questions through it. Save the tool call log and write a note on each call.

Course

Junxiong-WFG Organisation is an authorised representative of AIA Financial Advisers Private Limited (Reg. No. 201715016G).