Function calling (also called tool calling) is what turns a local LLM into an agent. Instead of only writing text, the model replies with a structured request such as “call get_weather with location=Paris“. Your code runs the function, sends the result back, and the model writes the final answer.
llama.cpp’s llama-server supports OpenAI-style function calling for practically any model, so tools and SDKs built for the OpenAI API work against your own GPU.
This guide shows how to start the server with tool support, which models and templates work best, how to send tools with curl and Python, how to build a full tool loop, and how to fix models that refuse to call tools.
It follows the llama.cpp docs/function-calling.md page as of November 2026.
- Start:
llama-server --jinja -fa on -hf bartowski/Qwen2.5-7B-Instruct-GGUF:Q4_K_M - Key flag:
--jinjauses the model’s chat template, which is what enables tool calls. - Send tools: the standard OpenAI
toolsarray to/v1/chat/completions. - Best results: models with a native format: Qwen 2.5, Llama 3.1-3.3, Mistral Nemo, Hermes 2/3, Functionary, Command R7B, GPT-OSS.
- Other models: work through a “Generic” handler that costs a few more tokens.
- Parallel calls: off by default; send
"parallel_tool_calls": true. - Avoid: extreme KV cache quantization like
-ctk q4_0, which hurts tool calling.
How Function Calling Works
- You send a chat request with a list of tools: each has a name, a description and a JSON Schema for its parameters.
- The model decides whether a tool would help. If yes, it returns a
tool_callsentry with the function name and JSON arguments instead of normal text. - Your code runs that function (for example, calls a weather API or reads a file).
- You send the result back as a message with
role: "tool". - The model uses the result to write its final answer, or asks for another tool.
The model never runs anything itself. It only asks; your program decides what actually happens, which is why you stay in control.
Already using Ollama? The concept is the same as in our Ollama tool calling guide, but llama-server gives you more control over templates and flags.
Native vs Generic Tool Formats
Every model family was trained to write tool calls in its own format. llama.cpp’s chat layer (common/chat.h) translates between those formats and the OpenAI API.
| Handler | Models | Notes |
|---|---|---|
| Llama 3.x | Llama 3.1, 3.2, 3.3 | Also supports built-in tools: wolfram_alpha, web_search/brave_search, code_interpreter |
| Hermes 2 Pro | Hermes 2/3, Qwen 2.5 and Qwen 2.5 Coder, many Qwen fine-tunes | Very common format |
| Mistral Nemo | Mistral Nemo, Mistral 7B v0.3, Mistral Large 2407 | |
| Functionary | Functionary v3.1 and v3.2 | Needs the matching template file |
| Firefunction v2 | Firefunction v2 | |
| Command R7B | Cohere Command R7B | Extracts reasoning |
| DeepSeek R1 | R1 and its distills | Marked work in progress; may be reluctant to call tools |
| Harmony | GPT-OSS, LLM-jp-4.1 | |
| Generic | Anything not recognised | Works, but may use more tokens and be less efficient |
The server log tells you which handler it picked. If you see Chat format: Generic, the model’s template was not recognised as a native tool format.
Start llama-server With Tool Support
- Get a recent llama.cpp build (release download or your own build).
- Start the server with
--jinja, which makes it use the model’s Jinja chat template:llama-server --jinja -fa on -hf bartowski/Qwen2.5-7B-Instruct-GGUF:Q4_K_M - Add
-ngl 99to keep the model on your GPU and-c 16384for more context, since tool schemas and results eat tokens. - Check the template supports tools: open
http://localhost:8080/propsand look at thechat_templateorchat_template_tool_usefields. - Point any OpenAI-compatible client at
http://localhost:8080/v1.
Other models the docs list as working with native support out of the box:
llama-server --jinja -fa on -hf bartowski/Mistral-Nemo-Instruct-2407-GGUF:Q6_K_L
llama-server --jinja -fa on -hf bartowski/Llama-3.3-70B-Instruct-GGUF:Q4_K_M
llama-server --jinja -fa on -hf ibm-granite/granite-4.1-3b-GGUF:Q4_K_M
The docs write the flash attention flag as a bare -fa; current builds expect a value, so this guide uses -fa on. For the full list of server options, see our llama-server guide. The docs’ own examples use port 9931; the default port is 8080 unless you pass --port.
When You Need a Template Override
Some GGUF files ship with a template that does not include tool support, or with a buggy one. Pass the right template with --chat-template-file. The templates live in the llama.cpp repo under models/templates/.
# DeepSeek R1 distills work best with llama.cpp's own template
llama-server --jinja -fa on -hf bartowski/DeepSeek-R1-Distill-Qwen-7B-GGUF:Q6_K_L \
--chat-template-file models/templates/llama-cpp-deepseek-r1.jinja
# Hermes 3 needs its tool_use template
llama-server --jinja -fa on -hf bartowski/Hermes-3-Llama-3.1-8B-GGUF:Q4_K_M \
--chat-template-file models/templates/NousResearch-Hermes-3-Llama-3.1-8B-tool_use.jinja
# Functionary v3.2
llama-server --jinja -fa on -hf bartowski/functionary-small-v3.2-GGUF:Q4_K_M \
--chat-template-file models/templates/meetkai-functionary-medium-v3.2.jinja
If a model has no official tool template at all, the docs suggest trying --chat-template chatml, which works with many models, or writing your own. To fetch an official template from a Hugging Face repo, use scripts/get_chat_template.py.
Models that work through the Generic handler need no override, for example bartowski/phi-4-GGUF:Q4_0 or bartowski/gemma-2-2b-it-GGUF:Q8_0.
Send Your First Tool Call With curl
curl http://localhost:8080/v1/chat/completions -d '{
"messages": [
{"role": "system", "content": "You are a chatbot that uses tools. Do not overthink things."},
{"role": "user", "content": "What is the weather in Istanbul?"}
],
"tools": [{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City and country, e.g. Paris, France"}
},
"required": ["location"]
}
}
}]
}'
Instead of text, a working setup returns a message whose content is null and whose tool_calls list contains get_current_weather with arguments like {"location": "Istanbul, Turkey"}.
Full Tool Loop in Python
This example uses the official openai package (pip install openai) and a fake weather function. Swap in real code for your own tools.
import json
from openai import OpenAI
client = OpenAI(base_url="http://localhost:8080/v1", api_key="not-needed")
def get_current_weather(location):
# Replace with a real API call
return {"location": location, "temp_c": 18, "sky": "clear"}
tools = [{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {"location": {"type": "string"}},
"required": ["location"],
},
},
}]
messages = [{"role": "user", "content": "Should I take an umbrella in Istanbul today?"}]
while True:
resp = client.chat.completions.create(model="local", messages=messages, tools=tools)
msg = resp.choices[0].message
if not msg.tool_calls:
print(msg.content)
break
messages.append(msg)
for call in msg.tool_calls:
args = json.loads(call.function.arguments)
result = get_current_weather(**args)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result),
})
- The first request returns a tool call instead of text.
- The loop runs the function and appends a
toolmessage with the matchingtool_call_id. - The next request lets the model read the result and answer.
- The loop stops when the model replies without tool calls.
Add a maximum number of rounds in real code so a confused model cannot loop forever, and validate arguments before running anything that changes files or sends data.
Parallel Tool Calls
Some models can request several tools at once, for example the weather in three cities. llama.cpp supports this on some models but keeps it off by default. Turn it on per request:
{
"messages": [...],
"tools": [...],
"parallel_tool_calls": true
}
With the OpenAI SDK, pass parallel_tool_calls=True to chat.completions.create. The loop above already handles several calls because it iterates over msg.tool_calls.
Which Local Models Are Best at Tools?
| Need | Good starting point |
|---|---|
| Small and fast (8 GB VRAM) | Qwen2.5-7B-Instruct Q4_K_M, IBM Granite 4.1 3B |
| Coding agents | Qwen2.5-Coder 7B or 32B |
| Strong general use (24 GB+) | Mistral Nemo 12B, larger Qwen models, GPT-OSS 20B |
| Big rigs | Llama 3.3 70B Q4_K_M |
Newer families appear constantly, and many use formats llama.cpp already understands. Test any new model with the curl example and check the log line for the handler. Quant choice matters too; very low quants are more likely to break JSON. Our GGUF quantization guide explains the trade-offs.
Tips for Reliable Tool Calls
- Write clear descriptions. The model picks tools based on the description text. Say exactly what the tool does and when to use it.
- Keep schemas simple. Flat objects with a few required fields work better than deep nesting on small models.
- Give fewer tools. Ten precise tools beat fifty vague ones. Only send the tools relevant to the task.
- Use a short system prompt such as the docs’ “You are a chatbot that uses tools/functions. Don’t overthink things.”
- Keep the KV cache at f16 or q8_0. The docs warn that extreme quantization like
-ctk q4_0can substantially degrade tool calling. - Leave room in the context for tool schemas and results, which add up fast.
- Use a GPU build for responsive agents; see our Vulkan guide for AMD and Intel cards.
Troubleshooting
| Problem | Fix |
|---|---|
| Model answers in text and never calls tools | Start with --jinja; check /props shows a tool-capable template; try a --chat-template-file override |
Log shows Chat format: Generic |
Template not recognised. It still works, but a native template may be more reliable |
| Tool call JSON is broken | Use a better quant, raise the KV cache type to f16/q8_0, simplify the schema |
| DeepSeek R1 will not call tools | Use llama-cpp-deepseek-r1.jinja; native R1 support is still marked work in progress |
| Only one call when you expected several | Send "parallel_tool_calls": true; not every model supports it |
| Model calls the same tool forever | Return clear results, cap the loop rounds, and make sure each tool message has the right tool_call_id |
| Context full after a few rounds | Raise -c, trim long tool outputs, or summarise old turns |
| Template file not found | Pass the full path to the file from the llama.cpp repo’s models/templates/ folder |
FAQ
Does llama.cpp support function calling?
Yes. llama-server supports OpenAI-style function calling when started with --jinja, for practically any model.
Is tool calling the same as function calling?
Yes. OpenAI renamed “functions” to “tools”, and the terms are used interchangeably.
Can I use LangChain, LlamaIndex or other agent frameworks?
Any framework that talks to an OpenAI-compatible endpoint can use llama-server. Set the base URL to http://localhost:8080/v1.
Does it work with llama-cli?
The function calling docs describe it for llama-server started with --jinja. Use the server for tool-calling apps.
Do I need an API key?
Not by default. If you start the server with --api-key, send that key as a Bearer token.
Can the model run code on my PC?
Only if your own program runs the code it asks for. The model only returns a request; be careful with tools that execute code or delete files.
What is the difference from structured JSON output?
JSON output forces a reply format. Tool calling lets the model choose whether to call a tool, which one, and with what arguments, then continue after seeing the result.



