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.- 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: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:Parallel Tool Calling
Some models support calling multiple functions simultaneously:Complete Examples
- Python
- JavaScript
- cURL
Built-in Tools (Llama 3.x)
Llama 3.1+ models support built-in tool names:wolfram_alpha— Mathematical and factual queriesbrave_search/web_search— Web searchingcode_interpreter— Code execution
Custom Chat Templates
Override the default chat template for better function calling:Best Practices
Function Descriptions
Function Descriptions
- Write clear, concise descriptions
- Include parameter constraints and units
- Specify required vs optional parameters
- Use JSON Schema for parameter validation
Error Handling
Error Handling
- Return structured error messages in tool results
- Include error types and codes
- Handle rate limits and timeouts
- Provide fallback behavior
Model Selection
Model Selection
- 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
Model not calling functions
Model not calling functions
- Ensure
--jinjaflag is set on server - Check tool descriptions are clear and specific
- Verify model supports function calling
- Try with
--chat-template-fileoverride
Invalid arguments generated
Invalid arguments generated
- Add parameter constraints to JSON Schema
- Include examples in descriptions
- Use enums for limited choices
- Validate and sanitize arguments before execution
Generic format using too many tokens
Generic format using too many tokens
- 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

