Agent Fine-Tuning: Tool Calling & Function Loops
Learn how to fine-tune compact open-weights models (Llama 3.1 8B, Qwen 2.5 7B) for deterministic tool execution, complex JSON schema adherence, and multi-turn agent reasoning loops using Unsloth, LoRA, and QLoRA.
Sub-50ms Latency
General frontier models (GPT-4o, Claude 3.5 Sonnet) suffer from 400-800ms TTFT. Fine-tuning an 8B model cuts routing latency by 10x, enabling true real-time agent reflexes.
Zero Schema Hallucination
General models frequently add non-existent parameters or fail JSON punctuation. Fine-tuning encodes your private API schemas and tool schemas directly into model weights.
Self-Hosted Privacy
Deploy fine-tuned LoRA adapters directly onto air-gapped vLLM or Ollama clusters. Sensitive enterprise database queries and customer records never leave your VPC.
1. Tool-Calling Chat Template Format
Hermes / Llama 3 StyleTo train models on multi-turn function calling, training data must use consistent special tokens separating system instructions, tool definitions, thoughts, and execution observations:
2. High-Speed Fine-Tuning with Unsloth & QLoRA
Python 3.11+The script below trains a Qwen 2.5 7B or Llama 3.1 8B model using 4-bit QLoRA. Unsloth patches attention layers for 2x to 5x faster training while fitting in 16GB VRAM.
3. Evaluation on BFCL & Real-World Testing
Key Benchmark Metrics
- AST Match Score: Validates exact parameter types (numbers as integers, not stringified floats).
- Execution Correctness: Executes tool calls against live mock sandboxes to verify valid outputs.
- Irrelevance Rejection: Confirms the agent avoids invoking tools when the user's prompt requires plain text conversation.
Production vLLM Serving
vllm serve qwen-agent-vllm \
--tensor-parallel-size 1 \
--gpu-memory-utilization 0.90 \
--max-model-len 4096 \
--enable-auto-tool-choice
Serves 150+ requests per second with sub-40ms P50 latency on an NVIDIA L4 or RTX 4090.
Frequently Asked Questions
How many agent trajectories are required for high accuracy? →
For targeted tool domains (10 to 30 enterprise APIs), 500 to 2,000 diverse synthetic multi-turn trajectories are typically sufficient to achieve >99% function calling schema accuracy when starting from an instruction-tuned base model (Qwen 2.5 or Llama 3.1).
Should I fine-tune with LoRA or Full Fine-Tuning? →
LoRA (or QLoRA) is strongly recommended for tool calling. LoRA preserves the foundational conversational reasoning and knowledge of the base model while adapting only projection layers to follow your tool calling syntax, preventing catastrophic forgetting.