How to Give a Local LLM Tools with Ollama in 30 Minutes
Why tool calling turns a chatbot into an agent
A local model that only chats is limited to what it memorized during training. It cannot check today's weather, read your database, or verify a calculation. Tool calling — also called function calling — changes that relationship. You describe your functions as structured schemas, the model replies with a structured request naming a function and its arguments, and your code decides whether to run it. The model never executes anything itself: it proposes, your code disposes.
This guide builds that loop with Ollama in about 30 minutes. If you have not run a model locally yet, start with our guide to running an open model locally with Ollama in 20 minutes and come back here.
Prerequisites
- Ollama installed and running on your machine.
- Python 3.10 or later, with pip.
- About 6 GB of free disk space for the example model, and 16 GB of RAM as a sensible starting point.
Step 1: Pick a model that actually supports tools
Not every Ollama model supports tool calling — send a tools list to one that does not and you will get prose instead of a structured call. Check the model's Ollama library page for a tools badge first. A reliable default is Qwen3 8B, which supports tool calling and fits on a laptop:
ollama pull qwen3:8b
ollama list
If ollama list shows the model, you are ready. If you swap in
another model later, re-check its library page rather than assuming tool
support carries over — small models vary widely in how reliably they format
tool arguments.
Step 2: Define one tool and make a single call
Install the official Ollama Python SDK, which turns a plain Python function into a tool schema from its signature and docstring. For this first run the function returns a fixed demo value — the point is the loop, not the weather service. Real data comes in Step 4.
pip install ollama
from ollama import chat
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
return f"The weather in {city} is sunny and 21 degrees Celsius."
messages = [{"role": "user", "content": "What is the weather in Seoul?"}]
response = chat(model="qwen3:8b", messages=messages, tools=[get_weather])
print(response.message.tool_calls)
Instead of a final answer, the model should return a tool call naming
get_weather with an argument like {"city": "Seoul"}.
That is success: it asked your code for a forecast instead of inventing one.
If it answers in prose, you are probably using a model without tool support,
or one too small to format calls reliably — move up a size before debugging
anything else.
Step 3: Close the loop — the agent pattern
One tool call is only half the pattern. A working agent loop appends the
model's message, executes each requested tool, appends each result as a
tool-role message, and calls the model again for the final
answer:
from ollama import chat
# Reuse get_weather from Step 2.
available_tools = {"get_weather": get_weather}
messages = [{"role": "user", "content": "What is the weather in Seoul?"}]
for _turn in range(5):
response = chat(model="qwen3:8b", messages=messages, tools=[get_weather])
messages.append(response.message)
tool_calls = response.message.tool_calls or []
if not tool_calls:
break
for call in tool_calls:
result = available_tools[call.function.name](**call.function.arguments)
messages.append({"role": "tool", "content": str(result)})
print(response.message.content)
Notice the turn limit of five. Without it, a confused model can bounce between tools forever. The loop also stops naturally: when the model returns no tool calls, the last message is its final answer.
Step 4: Swap the demo for a real tool
Replace the fixed string with a real data source, keeping the same function shape. A public weather endpoint that needs no API key keeps the example runnable, and the model code does not change at all — that separation is the point of the schema:
import requests
def get_weather(city: str) -> str:
"""Get the current weather for a city."""
lat, lon = {"Seoul": (37.57, 126.98)}.get(city, (37.57, 126.98))
data = requests.get("https://api.open-meteo.com/v1/forecast",
params={"latitude": lat, "longitude": lon, "current": "temperature_2m"},
timeout=10).json()
return f"The current temperature in {city} is {data['current']['temperature_2m']} degrees Celsius."
The same pattern works for reading a file, querying a database, or calling an internal API. Keep each tool narrow — one job, clear inputs, a short text result — because vague tools produce vague arguments, and long results eat your context window. When you are ready for a fuller agent with memory and branching workflows, our LangGraph agent guide builds on exactly this loop.
Growing beyond two tools
Real agents use several tools, and the same loop scales: pass them all in the tools list and let the model choose. Two rules keep it reliable. Keep the list short — three to five focused tools beat fifteen overlapping ones, because every extra tool is another chance for a small model to pick the wrong one. And spend your effort on tool descriptions: the description is what the model reads when deciding which tool fits, so write what the tool is for, when to use it, and what its arguments mean.
Troubleshooting
- Prose instead of a tool call: check the model's library page for tool support, and try a larger tool-capable model.
- Arguments fail validation: tighten the schema — required fields, simple types, plain-word descriptions. Avoid nested objects for small models.
- Loop never finishes: your turn limit is missing or too high, or a tool keeps returning an error the model retries. Log every call and result to see the cycle.
- Slow or garbled long loops: tool results are piling up in context. Truncate each result, or drop older tool messages when safe.
- Suspicious tool call: treat it as a bug in your code. Validate the name against an allowlist, and never pass model text to a shell or destructive operation without confirmation.
Safety checks before you wire tools to anything real
Tool calling is safe only when your code enforces the boundary. Validate every function name against your allowlist, and argument types and ranges too. Never give an agent a raw shell tool or a delete operation unless you have thought through what a mistaken argument could destroy. Keep tools read-only while learning, log every call, and add human confirmation before any action that spends money, sends a message, or changes data. For hardware sizing, see our guide to how much VRAM you need to run an LLM locally.
Closing
You now have the core pattern behind most local agents: a model that proposes tool calls, code that executes them, and a loop that turns results into answers. Start with one read-only tool, keep the list short, and add safety checks before you add power. From here, the work is better tools: clearer descriptions, smaller results, and stricter validation.


Comments
Post a Comment