Skip to main content

Function Calling

llama.cpp supports OpenAI-style function calling for ~any model through native and generic handlers.

Overview

Function calling allows models to:
  • Call external tools and APIs
  • Execute code and retrieve results
  • Access real-time data (web search, calculators, databases)
  • Perform structured actions based on user requests
Function calling is implemented in common/chat.h and used by llama-server when started with the --jinja flag.

Supported Models

Native Format Support

These models have optimized native function calling handlers:
  • Llama 3.1 / 3.2 / 3.3 — Including builtin tools (wolfram_alpha, brave_search, code_interpreter)
  • Functionary v3.1 / v3.2 — Dedicated function calling models
  • Hermes 2/3 — Strong tool use capabilities
  • Qwen 2.5 / Qwen 2.5 Coder — Native tool calling support
  • Mistral Nemo — Function calling enabled
  • Firefunction v2 — Specialized for function calls
  • Command R7B — With reasoning extraction
  • DeepSeek R1 — Experimental support

Generic Format Support

When a model’s chat template isn’t recognized, llama.cpp falls back to generic function calling support. You’ll see Chat format: Generic in the logs.
Generic support works with any model but:
  • May consume more tokens than native format
  • May be less efficient
  • Can be overridden with --chat-template-file

Basic Usage

Server Setup

Start llama-server with function calling enabled:
Or with a custom chat template:

Define Functions

Define available functions in your API request:

Handle Tool Calls

The model will respond with a tool call:

Return Results

Execute the function and return results:
The model will generate a natural language response using the tool results.

Parallel Tool Calling

Some models support calling multiple functions simultaneously:
Parallel tool calling is disabled by default. Enable with "parallel_tool_calls": true in your request.

Complete Examples

Built-in Tools (Llama 3.x)

Llama 3.1+ models support built-in tool names:
  • wolfram_alpha — Mathematical and factual queries
  • brave_search / web_search — Web searching
  • code_interpreter — Code execution
These don’t require parameter definitions but still need tool result handling.

Custom Chat Templates

Override the default chat template for better function calling:
Or specify in the API request:

Best Practices

  • Write clear, concise descriptions
  • Include parameter constraints and units
  • Specify required vs optional parameters
  • Use JSON Schema for parameter validation
  • Return structured error messages in tool results
  • Include error types and codes
  • Handle rate limits and timeouts
  • Provide fallback behavior
  • Use models with native function calling support when possible
  • Test generic support with your specific model
  • Consider token efficiency for high-volume applications
  • Benchmark accuracy with your tool definitions

Troubleshooting

  • Ensure --jinja flag is set on server
  • Check tool descriptions are clear and specific
  • Verify model supports function calling
  • Try with --chat-template-file override
  • Add parameter constraints to JSON Schema
  • Include examples in descriptions
  • Use enums for limited choices
  • Validate and sanitize arguments before execution
  • Switch to a model with native support
  • Simplify tool descriptions
  • Reduce number of available tools
  • Use custom chat template optimized for your model

Next Steps

REST API

Learn about the chat completions endpoint

Server Configuration

Configure llama-server for function calling