Narrow-Domain Fine-Tuning for Function Calling in On-Device AI Agents
Function calling enables on-device AI agents to convert natural language intent into executable actions, but limited computing resources make small models the practical choice—models that often underperform without targeted training. This article presents a narrow-domain fine-tuning practice for improving function-calling reliability on edge devices, covering training data construction, parameter-efficient fine-tuning with LoRA, evaluation metrics and workflow, real-world results showing success rates rising from roughly 46% to 90%, and practical deployment recommendations including fallback mechanisms and human confirmation for high-risk operations.
1. Why On-Device Agents Need Function Calling
On-device hardware is constrained by compute, memory, and power, making it impractical to deploy general-purpose large models directly. The core capability of an agent is converting natural language intent into executable actions. Function calling lets the model output a structured function name and arguments, which an external program then executes—querying a calendar, controlling a device, or reading a local file, for example.
Function-calling failures generally fall into three categories: selecting the wrong function, missing parameters or incorrect parameter types, and parameter values that don’t match user intent. In on-device scenarios, a single failure can break the entire task chain, and the user immediately perceives the response as useless. Call success rate is therefore the key metric for whether an on-device agent is usable.
2. Narrow and Deep: The Fine-Tuning Path for Small Models
General-purpose large models cover a broad range of tasks but aren’t necessarily optimal for any single one. Small models have limited parameter capacity; training them on large volumes of heterogeneous data tends to cause interference between tasks. A more reliable approach is “narrow and deep”: restrict the task scope, concentrate data and training resources, and train the model deeply on a small set of core capabilities.
To put this into practice, follow these principles:
- Define clear task boundaries. List the functions to be supported, and document the purpose, parameters, and return value of each. The fewer the functions, the fewer options the model must distinguish between, and the more stable the results.
- Keep data close to the real distribution. Training samples should include synonymous expressions, colloquial phrasing, omitted references, and ambiguous requests, so the model adapts to how users actually phrase inputs.
- Include negative samples. When a request falls outside the supported scope, the model should decline to call a function or proactively ask for clarification. Models trained without negative samples tend to force a function call for any input.
- Standardize the output format. The training target should be a strict, structured format that programs can parse reliably.
3. Building the Training Data
Training data typically comes from three sources: high-quality manually annotated samples, template-based augmented samples, and samples filtered from real usage logs after desensitization. Manual samples guarantee quality, template samples broaden coverage, and log samples reflect the real distribution.
A sample generally contains the user input, the available function descriptions, and the expected call result. For example:
{"messages": [{"role": "user", "content": "Remind me to submit my weekly report at 10 a.m. tomorrow"},{"role": "assistant", "tool_call": {"name": "create_reminder","arguments": {"time": "2026-11-25T10:00:00", "text": "Submit weekly report"}}}],"tools": ["create_reminder", "query_weather"]}
Two additional points matter in data construction. First, the sample count for each function should be roughly balanced; otherwise the model will be biased toward high-frequency functions. Second, deduplicate. Repeated samples cause the model to overfit fixed sentence patterns, which hurts performance on real inputs.
4. Training and Parameter Selection
Full-parameter fine-tuning of small models is expensive. Parameter-efficient fine-tuning methods such as Low-Rank Adaptation (LoRA) train only a small set of additional parameters, consume little GPU memory, and allow separate adapter weights to be saved for different capabilities—making switching and rollback easy.
During training, keep the following in mind:
- The learning rate should not be too large, or it may damage the model’s existing language understanding.
- The validation set must be separate from the training set and include expressions not seen during training; otherwise validation results will be overly optimistic.
- Continuously monitor the format error rate. Format errors usually surface earlier than semantic errors and are an important signal of whether training is proceeding normally.
5. Evaluation Metrics and Workflow
Once the evaluation set is built, results can be measured along four dimensions:
- Function selection accuracy: Did the model choose the correct function?
- Required parameter completeness: Are all required parameters present?
- Parameter value accuracy: Do the parameter values match user intent?
- Format validity: Can the output be parsed correctly by the parsing program?
The end-to-end call success rate is the proportion of samples that pass all four checks. The following pseudocode illustrates the evaluation workflow:
success = 0for sample in eval_set:output = model.generate(sample.prompt, tools=sample.tools)call = parse_tool_call(output)if call is None:continue # format errorif call.name != sample.expected_name:continue # wrong function selectedif not has_required(call.arguments, sample.required):continue # required parameters missingif normalize(call.arguments) != normalize(sample.expected_arguments):continue # parameter values mismatch; normalize before comparingsuccess += 1print(success / len(eval_set))
Normalization is critical. For example, timestamp formats, string casing, and whitespace should be standardized before comparison; otherwise equivalent correct results will be misjudged as errors.
6. A Set of Performance Results
A technical talk presented the following experimental results: on a task with a restricted function set, targeted fine-tuning raised the function-calling success rate from roughly 46% to about 90%. These numbers demonstrate that narrow-domain fine-tuning delivers clear gains for small models.
Three caveats apply when interpreting these results. First, how closely the training and test distributions match significantly affects the outcome. Second, the more functions a task involves, the harder it becomes. Third, the strictness of the evaluation criteria matters—for example, whether exact parameter value matches are required. Results from a single case should not be generalized to all scenarios; always re-measure on your own task.
7. Deployment Recommendations
- Measure resources before discussing effectiveness. Before launch, measure inference latency, memory usage, and power consumption on the target device. Model quality and resource cost must be weighed together—success rate alone is not enough.
- Design fallback mechanisms. When parsing fails or model confidence is low, fall back to rule-based logic or ask the user to confirm before executing.
- Continuously collect failure samples. Real-world failure cases are the most valuable source of training data. Organize, annotate, and feed them into the next training round on a regular basis.
- Keep human confirmation for high-risk operations. Deleting data, sending messages, and executing payments should require user confirmation even when the model’s call is correct—high success rates are no reason to skip verification.
