Practice: tool calling¶
Three exercises that build on the labs, then one open build task. Do them in your own copy (a fork) of this repo, commit as you go, and keep short notes.
Each exercise changes working code, so read the labs first: tools.py, agent.py, 04_error_handling.py, 06_human_approval.py.
Where your work goes:
- Code changes: in the files named below (
catalog.py,test_tools.py,06_human_approval.py). - Your own files:
my-work/in this folder. - Notes:
my-work/llm-api.md. For each exercise, a few lines on what you changed, why, and what you would do differently in production.
Some rules below say Decide. Make the choice yourself and write it in your notes.
If your team gave you an ASSIGNMENTS.md, it makes those choices for you: follow it.
ex1: Idempotency key for place_order¶
Why: a network retry or a model that repeats a tool call can place the same order twice. An idempotency key makes a repeat safe.
- Add an optional last parameter to
place_order:idempotency_key: str | None = None. - Same key and the same order (customer, SKU, quantity): return the first order. Stock and credit change only once.
- No key: works exactly as today.
- A failed order (no stock, no credit, bad SKU) does not use up its key.
reset()forgets every key.- Decide: the same key with a different order. Return an error, or return the first order with a warning?
- Decide: are keys per customer, or global?
- Add model-free tests to
test_tools.pyfor each rule.
Done when pytest -q passes, including your new tests.
ex2: Cancel an order¶
Why: every write action needs its undo, and the undo is a write action too.
- Add
cancel_order(order_id: str, reason: str = "") -> dicttocatalog.py, with a docstring andArgs:like the other tools. - It returns the order with
statusset to"cancelled", arefundfield in rupees and the reason ascancel_reason. - It puts the stock back and lowers the customer's
outstandingby the refund. - Unknown order, or an order already cancelled: return
{"error": ...}and change nothing. - It is a write action: add it to
ALL_TOOLS(notREAD_TOOLS) and toWRITE_TOOLSin06_human_approval.py, so it needs approval. - Decide: is there a cancellation fee? If so, it stays on the customer's outstanding.
- Decide: is a reason required?
- Add model-free tests to
test_tools.py.
Done when pytest -q passes, including your new tests.
ex3: Measure tool choice¶
Why: the docstring is the prompt. You only know a docstring change helped if you measure it.
- Create
my-work/tool_choice_eval.pywith a listCASESof at least 10 buyer questions:{"q": "Price of 60 M8 bolts?", "expect": "get_price"}.expectis a tool name,Nonewhen the model should answer without a tool, or a list of tool names in order when one question needs two tools. - Include some questions that need no tool and some that need two tools.
- For each case, call qwen3:8b once with
ALL_TOOLSand compare the tool calls it asks for withexpect. Print the accuracy. Put the run underif __name__ == "__main__":so importing the file does not call the model. - Pick one failure, change one docstring in
catalog.pyto fix it, and run again. - In your notes: accuracy before and after (as percentages), two failures, and what you changed.
Done when the script runs and your notes have both numbers.
Names these exercises build on¶
Please keep these names in catalog.py as they are: place_order, cancel_order, reset, ORDERS, STOCK, CUSTOMERS, READ_TOOLS, ALL_TOOLS. Checks and later topics rely on them.
Build it yourself: delivery slot booking¶
No starter code and no steps this time: a short brief, like one you'd get at work. Plan for half a day.
Your code goes in my-work/build/; reuse the loop in agent.py and the patterns in catalog.py rather than copying them. Notes go in a "Build" section of my-work/llm-api.md.
The brief. Buyers ask "can you deliver 200 bags of cement to Pune on Friday?" or "move my delivery to Monday". Build an assistant that answers with tools and books or moves a delivery slot.
It must:
- Have its own fake data: delivery slots per pincode and date, each truck with a capacity in kg, and a weight per product.
- Offer tools with clear docstrings: find slots for a pincode and date, estimate an order's weight, book a slot, reschedule a booking.
- Do all weight and capacity math in code. The model never adds up kilograms.
- Treat booking and rescheduling as write actions: they need approval and take an idempotency key.
- Return business errors as data the model can explain ("slot full: 1,200 kg left").
Decide: an order heavier than one truck: split it across slots or refuse? How close to the delivery date can a buyer reschedule?
Done when:
pytest -q my-work/buildpasses with model-free tests for every tool rule.- A demo runs three conversations on qwen3:8b: a booking that fits, an order that is too heavy, and a reschedule.
- A tool-choice eval like ex3 (at least 10 questions) runs, and your notes have its accuracy and one docstring change you measured.
Or bring your own problem. Build the same pattern on a problem from your own work instead. Write a five-line brief first (who uses it, what goes in, what comes out, what must never go wrong, how you'll measure it), then meet the same "Done when" list. Use synthetic data only, never real customer data.