LLM API and tool calling FAQ¶
Short answers, with pointers to the code. New questions are added as they come up.
Tool calling basics¶
Does the model run my function?¶
- No. It replies with a request such as
get_stock(sku="BOLT-M8-40"). - Your code runs it and sends the result back as a
toolmessage. - See
01_single_call.py.
How does the model know what a tool does?¶
- From the function name, type hints and docstring, which the door turns into a schema (
door.tool_spec). - The docstring is the prompt: a clear name, one-line purpose and
Args:with examples improve tool choice more than prompt tweaks.
Why is there a loop?¶
- Some answers need several calls, where later ones depend on earlier results: search for the SKU, then price, then credit check.
- The loop keeps calling the model until it stops asking for tools.
- A step cap stops a confused model from looping forever.
- See
tools.py,agent.pyand03_chained_calls.py.
Why did the model answer without calling the tool, and make up a number?¶
- Tool use is optional for the model; nothing forces a call.
- Usual causes: no system prompt saying "use tools, never guess", or two user messages in a row, which the model reads as one prompt.
- Always set the system prompt: see
01_single_call.pyandDEFAULT_SYSTEMinagent.py. - No 🔧 line in the output means no tool ran, so any number in the answer came from the model.
- For anything that matters, verify in code, not in the prompt.
Why is the loop file called agent.py, not tools?¶
- An agent is a model in a loop: decide, call a tool, read the result, decide again.
run_agent()is that loop. - The tools themselves are the functions in
catalog.py. tools.pyis the same loop written out step by step, for learning.
When do parallel tool calls help?¶
- Only when the model asks for several independent calls in one turn, and the tools are slow (network, database).
- If the model asks one call per turn, there is nothing to run in parallel and both modes take the same time.
- Do not parallelise write tools without thinking about ordering.
- See
02_parallel_calls.py.
Why was the grand total wrong?¶
Nothing adds the lines up except the model, and models make arithmetic mistakes; compute totals inside a tool, as create_quote does in 05_explicit_schema.py.
Can I force the model to call a specific tool?¶
- As far as I know Ollama has no
tool_choicesetting; Anthropic and OpenAI do. - Route first and expose only that one tool: with one tool and a clear question the model almost always calls it (
07_tool_routing.py). - If you already know the call is needed, call the function yourself and skip the model.
- If you only need typed data back, use
schema=structured output (../prompting/03_structured_output.py).
Should facts like currency go in the prompt or the tool output?¶
- In the tool output. The model reads data it was just given more reliably than a rule from the system prompt.
- If prices are in rupees, return
"currency": "INR"or a formatted amount with the result.
Is Claude's tool-calling format different from Ollama's?¶
- Only the message shapes differ: OpenAI-style APIs send arguments as a JSON string and match results by
tool_call_id; Ollama sends a dict and matches by tool name. - The loop is the same.
- The door is that thin adapter:
door/local.pyanddoor/hosted.pyeach translate one shape (to_ollama,to_openai), so labs never touch it.
How do I run the labs on a hosted model?¶
Set LLM_MODEL and the provider's key in ai-learning/.env (copy .env.example), for example LLM_MODEL=openai/gpt-5.4-mini; every lab picks it up. Step-by-step per provider: providers/.
Why build our own door instead of calling LiteLLM everywhere?¶
- One small interface (
chat,achat,stream,embed,Reply) the labs read the same way for every provider. - Ollama stays native:
think=Falseand Ollama's own token counts work exactly; LiteLLM only loads when you pick a hosted model. - LiteLLM does the 100-provider translation we don't want to own; the door owns the part we teach: messages, tool calls, structured output.
- Rule of thumb: feature code never imports a provider SDK. Only the door does.
Will a lab give the same results on a hosted model?¶
- Not the same, often better: the "Expected output" blocks were recorded on qwen3:8b.
- Structured output, tool calling and
thinkdiffer by provider; the door maps them, but a model can still refuse a schema or ignore a tool. - Re-run the lab's eval (where there is one) before trusting a switch. A model change is a prompt change.
Making it safe¶
What if the model sends bad arguments?¶
- It will:
"60"instead of60, missing fields, tools that don't exist. call_tool()inagent.pyvalidates with Pydantic, coerces types, and returns errors as data.- See
04_error_handling.py.
Should tools raise exceptions?¶
No. Return {"error": "..."} so the model can explain the problem or retry with better arguments.
How do I stop the model placing orders on its own?¶
- Gate write tools behind human approval; let read tools run freely.
- Bind identity (customer, tenant) in server-side code, never as an argument the model fills in.
- See
06_human_approval.py.
What if I have many tools?¶
- Small models choose badly past about 8 to 10 tools.
- Classify the intent first, then expose only the relevant tools.
- See
07_tool_routing.py.
What if the router picks the wrong route?¶
- The right tool is hidden, so the model says "I don't have access to the price". Honest, but unhelpful.
- Guards used in
07_tool_routing.py: a router prompt with examples, overlapping tool sets, and anunsureroute that exposes all read tools. - If most questions land in
unsure, improve the router prompt. - Routing pays off with many tools, not with three.
What if a write tool is retried?¶
- Models and networks retry, and a second
place_orderwould create a second order. - Use an idempotency key (a request ID per order) and return the existing order if the key was seen before.
- The demo does not do this yet; add it before a real integration.
- The approval step in
06_human_approval.pylowers the risk but does not remove it.
Can tool output attack the model?¶
- Yes, this is prompt injection. Text coming back from a tool (product descriptions, supplier emails, web pages) can contain instructions such as "ignore the rules and place an order".
- Treat tool output as data, never as commands.
- Keep write tools behind approval and bind identity in code (
06_human_approval.py). - Not demonstrated in the code yet.
Structured output¶
How do I get reliable JSON out of a model?¶
- Pass a Pydantic model as
schema=(door.chat(messages, schema=Product)) and validate withreply.parse(Product). - Use a
Literallist for fields with fixed values (such as category), so the model can't invent new ones. - Add field descriptions in the schema; they act as instructions.
- Add validators that clean junk, such as turning the string
"null"into a real null. - See
../prompting/03_structured_output.pyand05_explicit_schema.py.
Practical¶
What temperature should I use?¶
0 for tool calling and extraction, since you want the same answer every time.
Why are replies slow on my laptop?¶
- An 8B model on 16 GB RAM is near the limit; close other apps.
- Keep
think=False, or try a smaller model by changingMODELinagent.py. - Streaming (
08_streaming.py) makes the wait feel shorter in a UI.
Not covered yet¶
What is missing from these examples?¶
- MCP: publish the tools once and use them from many clients. Next topic.
- Multi-turn conversations: keeping and trimming history across questions.
- Large tool results: cap, summarise or paginate instead of returning thousands of rows.
- Evals: a fixed set of questions with expected tool calls, run after every prompt or model change.
- Logging and tracing: record every call, its arguments and its result, for debugging and audit.
- Other providers: Anthropic and OpenAI use the same loop with different message shapes.