Skip to content

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.

  1. Add an optional last parameter to place_order: idempotency_key: str | None = None.
  2. Same key and the same order (customer, SKU, quantity): return the first order. Stock and credit change only once.
  3. No key: works exactly as today.
  4. A failed order (no stock, no credit, bad SKU) does not use up its key.
  5. reset() forgets every key.
  6. Decide: the same key with a different order. Return an error, or return the first order with a warning?
  7. Decide: are keys per customer, or global?
  8. Add model-free tests to test_tools.py for 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.

  1. Add cancel_order(order_id: str, reason: str = "") -> dict to catalog.py, with a docstring and Args: like the other tools.
  2. It returns the order with status set to "cancelled", a refund field in rupees and the reason as cancel_reason.
  3. It puts the stock back and lowers the customer's outstanding by the refund.
  4. Unknown order, or an order already cancelled: return {"error": ...} and change nothing.
  5. It is a write action: add it to ALL_TOOLS (not READ_TOOLS) and to WRITE_TOOLS in 06_human_approval.py, so it needs approval.
  6. Decide: is there a cancellation fee? If so, it stays on the customer's outstanding.
  7. Decide: is a reason required?
  8. 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.

  1. Create my-work/tool_choice_eval.py with a list CASES of at least 10 buyer questions: {"q": "Price of 60 M8 bolts?", "expect": "get_price"}. expect is a tool name, None when the model should answer without a tool, or a list of tool names in order when one question needs two tools.
  2. Include some questions that need no tool and some that need two tools.
  3. For each case, call qwen3:8b once with ALL_TOOLS and compare the tool calls it asks for with expect. Print the accuracy. Put the run under if __name__ == "__main__": so importing the file does not call the model.
  4. Pick one failure, change one docstring in catalog.py to fix it, and run again.
  5. 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/build passes 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.