LLM tool calling¶
Run it, don't just read it
Every lab runs on your laptop with Ollama. Set up once (INSTALL), then:
git clone https://github.com/JSWOne/ai-learning.git && cd ai-learning
cd llm-api && python 01_single_call.py
Lab files and data: llm-api/ on GitHub. In VS Code, open the repo folder and use Terminal > Run Task > Run this lab.
Small, runnable examples of tool (function) calling using a local model. Each file shows one pattern. New to tool calling? Read learn-llm-api.md first: what it is and why, the mental model in code and diagrams, the labs in three stages, key decisions and advanced topics. Before you start: prompting: roles, few-shot, structured output and prompt evals are taught there. After it, you can: let a model call your code (stock, prices, credit), safely. Done with the labs? Try the three exercises and the build-it-yourself task in PRACTICE.md.
The B2B catalog data is fake and lives in catalog.py; replace those function bodies with real DB or API calls.
Setup¶
Install steps for all topics are in ../INSTALL.md. Short version, from the ai-learning folder:
ollama pull qwen3:8b
uv venv && source .venv/bin/activate # the one venv at ai-learning/ (see ../INSTALL.md section 4)
uv pip install -r requirements.lock # all topics, the tested versions
Run any example from this folder, for example python 03_chained_calls.py.
Files¶
Track: Core labs are the Core path through the course. Before you ship: safety lessons to do before anything you build goes to real users. Go further: when your work needs that pattern (see Use it when).
| File | Track | Pattern | Use it when | Time |
|---|---|---|---|---|
tools.py |
The loop spelled out: ask, run tools, feed results back, repeat | Start here to understand the mechanics | ||
01_single_call.py |
Core | One tool call, no loop, raw request and response | You want to see exactly what the model sends | ~10 s |
02_parallel_calls.py |
Go further | Run independent calls in a thread pool | Tools are slow network calls | ~30 s |
03_chained_calls.py |
Core | Later calls depend on earlier results | Search, then price, then credit check | ~1 min |
04_error_handling.py |
Core | Errors as data, retries, step cap | Any real integration | ~1 min |
05_explicit_schema.py |
Go further | Hand-written JSON schema plus Pydantic validation | Nested arguments such as a list of line items | ~20 s |
06_human_approval.py |
Core | Approval gate for write actions, identity bound server-side | Anything that changes data | ~30 s |
07_tool_routing.py |
Go further | Classify intent first, expose only relevant tools | Many tools, small model | ~1 min |
08_streaming.py |
Go further | Stream text and handle tool calls mid-stream | Chat UIs | ~20 s |
Shared code: catalog.py (fake data and tools), agent.py (reusable loop: run_agent, call_tool, with_schema).
Exercises to practise with: PRACTICE.md (your own files go in my-work/).
Questions and answers on all of this are in FAQ.md. The learning guide is learn-llm-api.md (diagrams in diagrams/).
Rules of thumb¶
- The docstring is the prompt. Clear names, one-line purpose, and
Args:descriptions with examples improve tool choice more than prompt tweaking. - The model only asks for a call. Your code runs it, so validate every argument (
call_tooldoes this with Pydantic). - Return errors as
{"error": ...}instead of raising, so the model can explain or recover. - Always cap the loop (
max_steps). - Keep
temperatureat 0 for tool use. - Put identity (customer, tenant, user) in server-side code, never in a tool argument the model fills in.
- Gate write actions behind approval; let read tools run freely.
- Few tools per call. Past roughly 8 to 10, route first (example 07).
Tests¶
pytest -q runs model-free tests for the tools, argument validation and the loop (using a fake chat).
The model-driven behaviour (which tool qwen3:8b picks) can only be checked by running the examples against a real model.