Skip to content

How-ToGPU

How to Use Tool Calling in Ollama

How to use tool calling (function calling) in Ollama: describe functions in a tools list on /api/chat, read the model's tool_calls, run them and send results back as tool messages, plus parallel calls, agent loops and streaming, based on Ollama's documentation.

By , Systems & Technical Editor

3 min read

How to Use Tool Calling in Ollama

Tool calling, also known as function calling, lets a model running in Ollama ask your own code to do something it can't do on its own, such as look up a value, run a calculation or query a database, and then use the result in its reply. The model never runs your function itself: it returns a structured request, your script runs the function, and you send the result back. This guide is based on Ollama's tool calling documentation. It hasn't been tested by us on specific hardware, and you'll need a model that supports tool calling, so check the model's page in the Ollama library before you start.

What you need

  • Ollama installed and running, listening on http://localhost:11434 by default.

  • A model that supports tool calling. Ollama's own examples use qwen3.

  • Optionally, the Ollama Python library (pip install ollama -U) or the JavaScript library (npm i ollama). See our guide to using Ollama with Python if you haven't set that up.

Step 1: Describe your tool in the request

Tool calling uses the /api/chat endpoint. Alongside your messages, you send a tools list. Each tool has a type of function, a name, a plain-English description and a JSON schema for its parameters. In Ollama's example, a get_temperature tool takes one required string parameter, city.

The description matters: it's how the model decides whether your tool is relevant to the question.

Step 2: Read the tool call in the reply

If the model decides to use a tool, its reply message contains a tool_calls field instead of (or as well as) normal text. Each call names the function and gives the arguments as an object, for example get_temperature with {"city": "New York"}.

Step 3: Run the function and send the result back

Your code runs the real function with those arguments. You then make a second /api/chat request containing the whole conversation so far: the original user message, the assistant message with its tool_calls, and a new message with the role tool, a tool_name and the result as content (in Ollama's example, "22°C"). The model then writes its final answer using that result.

In the Python library you can pass ordinary Python functions straight into the tools list, or describe them as a JSON schema. Ollama's examples give each function a clear docstring describing what it does and its arguments, which is a good habit to copy.

Parallel calls and agent loops

  • Parallel tool calling: a model can request several tool calls in one reply, for example the weather in New York and in London. Run each one and send all the tool results back before asking for the final answer.

  • Multi-turn (agent loop): keep calling /api/chat in a loop, running any tools the model asks for, until it replies without a tool call. Ollama's docs note that it can help to tell the model that it is in a loop and can make multiple tool calls.

  • Streaming: if you stream responses, gather every chunk of thinking, content and tool_calls, then return those fields together with your tool results in the follow-up request.

Tips

  • Start with one simple tool and a clear description before building a bigger agent.

  • Validate the arguments the model sends before you act on them, especially for anything that writes files or changes data.

  • If you only need a reply in a fixed shape rather than an action, structured outputs may be simpler.

Our verdict

Tool calling in Ollama is a simple loop: describe your functions in a tools list on /api/chat, run whatever the model asks for, and send the results back as tool messages. Pick a model that supports tools, keep descriptions clear and check arguments before acting on them, and you can build useful local assistants without sending data to the cloud.

The bottom line

Tool calling in Ollama is a simple loop: describe your functions in a `tools` list on /api/chat, run whatever the model asks for, and send the results back as `tool` messages. Pick a model that supports tools, keep descriptions clear and check arguments before acting on them, and you can build useful local assistants without sending data to the cloud.

Sources

  1. 01Ollama Docs, Tool calling
  2. 02Ollama Docs, Chat API reference

About the author

Marcus Thorne

Systems & Technical Editor

Marcus writes Forge PC's step-by-step build, troubleshooting and system optimisation guides. His background spans Windows kernel and power-state diagnostics, UEFI/BIOS configuration, NVMe storage arrays, ATX 3.1 power delivery and running local AI and LLM workloads on consumer graphics cards.

More from Marcus Thorne

Read next

Temperature vs Top-p vs Top-k in Ollama: Which Setting to Change?

Versus

Temperature vs Top-p vs Top-k in Ollama: Which Setting to Change?

What Ollama's sampling settings do: temperature controls creativity, top_k and top_p limit which tokens are considered, min_p is a relative alternative to top_p, and seed makes output repeatable, with Ollama's documented defaults and how to set them.

Callum Reid3 min

Ollama /api/generate vs /api/chat: Which Endpoint Should You Use?

Versus

Ollama /api/generate vs /api/chat: Which Endpoint Should You Use?

Ollama's /api/generate takes a single prompt and suits one-off tasks, with extras such as suffix for fill-in-the-middle and raw mode; /api/chat takes a list of role-based messages and supports tool calling, so it's the one for chatbots and agents.

Callum Reid3 min

How to Fix Slow Token Generation in llama.cpp

How-To

How to Fix Slow Token Generation in llama.cpp

How to fix slow llama.cpp text generation by confirming GPU offload with -ngl and setting -t to your physical core count or lower, with the llama.cpp developers' own example showing the effect of each setting.

Marcus Thorne2 min