Hands-on exercises · Give to learners

Module 3 — Tool Calling Basics: Exercises

Hands-on exercise sheet. Try every TODO yourself before you see the solution.

For learnersAbout 45 minutesOpenAI API key needed

What you will build

Time: about 45 minutes  ·  API key needed: Yes (OpenAI)

The one idea behind this module

An LLM cannot run code, read your database, or do exact maths reliably. With tool calling, the LLM can ask your program to run a function, then use the result in its answer.

1. Askquestion + tool menu
→
2. LLM decidesanswer, or "call tool X with Y"
→
3. Your coderuns the function
→
4. Send resultrole = tool
→
5. LLM answersusing the result
Important

The LLM never runs your function. It only asks. Your Python code runs it and reports back.

Setup: 11-rag-setup/Setup_Guide_for_Learners.html. Open the folder 1_GIVE_FIRST_Starter_Code.

Exercise 3.1 — Tool calling from scratch

File: 1_GIVE_FIRST_Starter_Code/1_tool_calling_from_scratch.py

You get a working Python function square_root(number). Teach the LLM that it exists, and handle its requests.

TODO 1Describe the square_root tool in OpenAI's JSON tool format

The LLM only sees this description, never your Python code. So the name, the description and the parameter types must be clear.

Need a hint?
  • A list with one dict: {"type": "function", "function": {"name": ..., "description": ..., "parameters": {...}}}
  • "parameters" is a JSON Schema: {"type": "object", "properties": {"number": {"type": "number", ...}}, "required": ["number"]}
TODO 2Run the requested tool and add its result to messages
Need a hint?
  • The tool name is in tool_call.function.name
  • The arguments are a JSON string in tool_call.function.arguments
  • json.loads(...) turns the arguments into a dict, then call tool_map[name](**args)
  • Append {"role": "tool", "tool_call_id": tool_call.id, "content": str(result)}
TODO 3Call the LLM a second time with the updated messages and return its text
Need a hint?
  • Same client.chat.completions.create(...) call as in Step 1, with the updated messages

Run it: python 1_tool_calling_from_scratch.py

You are done when
  • For 144 and 2025 you see a line starting with [tool] square_root(...), then a final answer of 12 and 45
  • For "capital of France" there is no [tool] line: the LLM answered directly

Exercise 3.2 — The same, with LangChain

File: 1_GIVE_FIRST_Starter_Code/2_tool_calling_with_langchain.py

TODO 1Turn square_root into a LangChain tool

When you run the file, the first three printed lines show the tool menu that @tool built for you. Compare it with the JSON you wrote by hand in 3.1.

Need a hint?
  • Put the @tool decorator above the function
  • The docstring becomes the tool description the LLM reads, so keep it clear
TODO 2Run every requested tool and add a ToolMessage with its result
Need a hint?
  • Each tool call is a dict with keys "name", "args" and "id"
  • tool_map[tc["name"]].invoke(tc["args"]) runs the tool
  • messages.append(ToolMessage(content=str(result), tool_call_id=tc["id"]))
You are done when
  • The trap question "Is 15241383937 the square of 123456?" calls the tool and the answer says no (it is off by one)
  • "Capital of France" is answered without a tool

Exercise 3.3 — Choosing between four tools

File: 1_GIVE_FIRST_Starter_Code/3_inventory_tools_demo.py

Story: A small factory warehouse keeps 5 items. Three tools are ready (get_stock_level, calculate_reorder_quantity, days_until_stockout). You write the fourth tool and connect everything to the LLM.

TODO 1Write a 4th tool that lists every SKU at or below its reorder point
Need a hint?
  • Decorate with @tool and write a clear docstring (the LLM reads it!)
  • Loop over INVENTORY.items() and keep items where stock <= reorder_point
  • Return a string, e.g. "Items needing reorder (2 total): SKU-002 (...); SKU-003 (...)"
TODO 2Put all four tools in a list, build tool_map, and bind the tools to the LLM
Need a hint?
  • tool_map = {t.name: t for t in tools}
  • ChatOpenAI(model="gpt-6-luna", reasoning_effort="none").bind_tools(tools)
You are done when each of the 4 questions prints a different tool name:
QuestionExpected tool
What is the current stock level for SKU-003?get_stock_level
Which items need to be reordered right now?list_low_stock_items (yours!)
How many days until SKU-002 runs out of stock?days_until_stockout
What quantity of SKU-003 should I order if my supplier takes 10 days?calculate_reorder_quantity with lead_time_days=10

Think about it

  1. In 3.3, how did the LLM know that "my supplier takes 10 days" should become lead_time_days=10?
  2. What would happen if two tools had the same description, for example "Get info about a SKU"?
  3. Why do we send tool_call_id back with every tool result?
  4. Our 3.1–3.3 code calls the LLM at most twice. Think of a question that would need two tools one after the other. What would break?
  5. Is it safe to let an LLM call any function? Give one tool you would never expose without a human check.

Stretch challenges