
Function Calling: Training Models to Use External Tools
The Agent's Hands. Learn how to train your model to output precise function-call syntax (JSON) that can trigger external APIs, databases, or search engines.
Function Calling: Training Models to Use External Tools
To build an AI Agent, your model needs to be more than just a talker. it needs "Hands." These hands are Functions (or Tools).
Function calling is the ability of a model to recognize when a user's request requires an external action (like "Check the weather" or "Update a row in SQL") and output a machine-readable JSON object instead of a conversational sentence.
While base models like GPT-4o are excellent at this, smaller open-source models often struggle. They might hallucinate function names or use the wrong data types (e.g., providing a string when the function needs an integer). Fine-tuning for Function Calling changes the model's behavior from "Conversationalist" to "API-first Controller."
1. The Anatomy of a Function Call Prompt
When training for function calling, you must show the model three things:
- The System Prompt: A description of the available tools.
- The User Query: The request that triggers the tool.
- The Target Output: The specific JSON call.
Example Training Row:
- Input: "You have a tool
get_stock_price(ticker: str). User: What is Nvidia worth right now?" - Output:
{"function": "get_stock_price", "parameters": {"ticker": "NVDA"}}
2. Training for Multi-Step and Parallel Tools
Advanced agents often need to call multiple tools at once.
- User: "Check the price of Bitcoin and Ethereum."
- Fine-Tuned Output: A list of two JSON objects.
By fine-tuning on batches of parallel calls, you ensure the model doesn't get "lazy" and only do one task when two were requested.
Visualizing the Function Call Loop
graph TD
A["User Request"] --> B["Fine-Tuned Controller LLM"]
B --> C{"Decision: Tool Needed?"}
C -- "No" --> D["Return Natural Language"]
C -- "Yes" --> E["Generate JSON Tool Call"]
E --> F["External API / DB"]
F --> G["Tool Result (JSON)"]
G --> B
B --> H["Final Unified Answer"]
subgraph "The 'Controller' Training"
B
E
end
3. Dealing with Hallucinated Functions
One of the biggest issues in Agent development is when the model "invents" a function that doesn't exist.
- User: "Fly me to the moon."
- Model:
{"function": "launch_rocket", "parameters": {"destination": "moon"}}(But you don't have alaunch_rockettool!).
The SFT Fix: Include "Negative Tool Samples" in your training data. If the user asks for something impossible, the model should be trained to respond with: "I'm sorry, I don't have a tool for that." rather than guessing.
4. The "Zero-Shot" Transfer
If you fine-tune your model on $10$ specific tools, it often gets much better at calling Any tool, even ones it hasn't seen before. This is because it has learned the "Syntax Logic" of function calling—how to map an English intent to a JSON structure.
Summary and Key Takeaways
- Function Calling turns your model into a controller for software.
- Schema Strictness: Fine-tuning ensures the model adheres to your specific API schemas (strings, ints, lists).
- Parallel Power: Train the model to handle multiple intents in a single turn.
- Negative Samples: Use training to prevent the model from inventing non-existent tools.
In the next lesson, we will look at how to connect this to the most popular AI orchestrators: LangChain Integration: Using your Custom LLM Class.
Reflection Exercise
- Why is it better to have a model output raw JSON for a tool call instead of writing a sentence like "I am now going to call the weather function for you"?
- If you change your API's variable name from
zip_codetopostal_code, do you need to retrain your model, or can you just change the system prompt? (Hint: See 'The Alignment Tax' and 'In-Context Learning').
SEO Metadata & Keywords
Focus Keywords: fine-tuning for function calling, training llm to use tools, function call json syntax AI, agentic fine-tuning tutorial, parallel function calling llm. Meta Description: Give your AI hands. Learn how to fine-tune your models for high-accuracy function and tool calling, ensuring they generate perfect JSON for your APIs and databases.