Topic 3 of 5 · 4 min read
Tool calling reliability with local models
Goal: Read a local model's tool-call log and name the failure and the fix.
A local model does not run tools; it writes a request to call one, and your code and the runner's tool parser must read it, check it and run it safely.
Check yourself · 1 of 3
A local model's reply has an empty tool_calls field, but its content is {"name":"lookup_order","arguments":{"id":4417}}. What is the likely problem?
Why it matters
Agents and assistants need tools: search, a database lookup, a file read. Hosted models are trained heavily for this. Many local models support it, but less reliably, and the quality depends on three things you control: the model, the chat template and the parser.
How tool calling works
- You send a list of tool definitions: name, description and a JSON schema for arguments.
- The runner's chat template shows those definitions to the model in the format it was trained on.
- The model replies with a call, for example
get_weatherwith{"city": "Oslo"}. - The runner's parser turns that text into a structured
tool_callsfield. - Your code validates the call, runs it, and sends the result back as a
toolmessage.
If the template or parser does not match the model, step 2 or 4 breaks. The call then arrives as plain text in content, or not at all.
Typical small-model failures
- Invented tool: the model calls
search_webwhen onlylookup_orderexists. - Broken arguments: a string where a number belongs, a missing required field, or JSON that does not parse.
- Wrong choice: it answers from memory when it should call a tool, or calls one when no tool is needed.
- Loops: it calls the same tool again and again with the same arguments.
How to make it dependable
- Use a model the runner lists as supporting tools, and keep the runner updated.
- Keep tool names distinct and descriptions short. Offer only the tools needed for the step.
- Validate every call against its schema before running. Reject unknown names and send back a clear error such as "unknown tool; choose one of: lookup_order".
- Cap the number of rounds and stop on repeated identical calls.
- Treat arguments as untrusted input, especially for anything that writes, deletes or spends.
const call = msg.tool_calls?.[0];
if (!call || !tools[call.function.name]) {
return retryWith("Unknown tool. Choose one of: " + Object.keys(tools).join(", "));
}
const args = schemas[call.function.name].safeParse(JSON.parse(call.function.arguments));
if (!args.success) return retryWith("Invalid arguments: " + args.error.message);
Before and after
Before: The app runs whatever name and arguments come back, and a made-up tool name crashes the loop.
After: The app checks the name and arguments, returns a short error to the model, allows two retries and then falls back to a plain answer.
What goes wrong
A model that emitted a call in text form, such as a JSON blob inside content, looks like a normal reply. The user sees raw JSON. Log both content and tool_calls for every turn so you can tell a template problem from a model problem.
Examples
Reading a log
{"role":"assistant","content":"","tool_calls":[{"function":{"name":"search_web","arguments":"{\"q\":\"order 4417\"}"}}]}
The tool list contains only lookup_order. This is an invented tool, not a parsing fault: the call parsed correctly. The fix is validation plus an error reply that lists the real tools.
A call that arrived as text
The reply's content holds {"name":"lookup_order","arguments":{"id":4417}} and tool_calls is empty. The model wrote a call, but the parser did not recognise the template format. Check that the model and runner version support tools and that the right chat template is used.
Key points
- The model only writes a call; your code and the runner's parser read it and your code runs the tool.
- A mismatched chat template or parser makes a call appear as plain text or vanish.
- Small models invent tools, produce bad arguments, skip needed calls or loop.
- Validate every call's name and arguments before running it and send clear errors back.
- Cap rounds, offer few tools, and treat arguments as untrusted input.
Common mistakes
- Running the tool name and arguments without checking them first.
- Offering ten tools to a small model when two would do.
- Not logging
contentnext totool_calls, so template faults look like model faults. - Letting the loop run without a round limit.
Check yourself · 2 of 3
The model calls search_web, but your app defines only lookup_order. What should your code do?
Check yourself · 3 of 3
Which setup makes a small local model more dependable at choosing tools?
Practice items, not counted towards your mastery.
Start here
- Ollama: OpenAI compatibility (opens in a new tab)DocsFree
Lists the endpoints and request fields Ollama accepts through its OpenAI-style API, and what is not supported.
- llama.cpp server README (opens in a new tab)DocsFree
Flags and endpoints of llama-server, including its OpenAI-compatible routes and JSON schema options.
- vLLM: structured outputs (opens in a new tab)DocsFree
How vLLM constrains output to a JSON schema, regex or grammar.
More for this topic
- Ollama: tool calling (opens in a new tab)DocsFree
How tools are declared and returned by local models, with examples.