What you will build
- 3.1 Tool calling with the plain OpenAI library: you write the JSON "tool menu" and the message passing yourself
- 3.2 The same thing with LangChain, so you see what a framework saves you
- 3.3 A warehouse assistant where the LLM must choose between four tools
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.
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.
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"]}
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 calltool_map[name](**args)- Append
{"role": "tool", "tool_call_id": tool_call.id, "content": str(result)}
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
- 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
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
@tooldecorator above the function - The docstring becomes the tool description the LLM reads, so keep it clear
Need a hint?
- Each tool call is a dict with keys
"name", "args" and "id" tool_map[tc["name"]].invoke(tc["args"])runs the toolmessages.append(ToolMessage(content=str(result), tool_call_id=tc["id"]))
- 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.
Need a hint?
- Decorate with
@tool andwrite a clear docstring(the LLM reads it!) - Loop over
INVENTORY.items() andkeep items where stock<= reorder_point - Return a string,
e.g. "Items needing reorder (2 total): SKU-002 (...); SKU-003 (...)"
Need a hint?
tool_map = {t.name: t for t in tools}ChatOpenAI(model="gpt-6-luna", reasoning_effort="none").bind_tools(tools)
| Question | Expected 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
- In 3.3, how did the LLM know that "my supplier takes 10 days" should become
lead_time_days=10? - What would happen if two tools had the same description, for example "Get info about a SKU"?
- Why do we send
tool_call_idback with every tool result? - 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?
- Is it safe to let an LLM call any function? Give one tool you would never expose without a human check.
Stretch challenges
- 3.1: Add a second tool
power(base, exponent). Ask "What is 2 to the power 10, and its square root?" - 3.3: Ask "Show me everything about SKU-005". How many tools does the LLM call at once?
- 3.3: Ask about SKU-999 (does not exist). Does the final answer handle it politely?