Fine-Tuning On-Device AI Agents: From Reliable Function Calling to Deployment

    Focused fine-tuning can help lightweight language models handle the structured tasks required by on-device AI agents. This article outlines a narrow, task-specific training approach, describes a reported experiment that improved function-calling success from 46% to 90%, and covers data design, training settings, quantization, and fallback mechanisms for deployment.

    1. Why General-Purpose Models Fall Short as AI Agents

    Developers building on-device AI agents often choose smaller language models to balance inference speed, memory consumption, and privacy. However, general-purpose pretrained models can struggle with core agent tasks: selecting external functions, extracting arguments, and maintaining context across multiple turns.

    Three issues account for much of this gap:

    • A mismatch between pretraining and task objectives. General-purpose models learn to predict the next token, rather than explicitly optimize for selecting the correct function and supplying valid arguments.
    • Inconsistent interpretation of contextual constraints. Complex instructions may contain implicit requirements, default values, and type-conversion rules that a general-purpose model does not reliably capture.
    • Unreliable output formatting. Function calls require strict JSON structures or other defined syntax. Models may drift from these requirements.

    Using an off-the-shelf model without task-specific adaptation can therefore produce low function-calling success rates. The source account reports results frequently below 50%, creating interruptions in the user experience. Targeted fine-tuning offers a way to address this bottleneck.

    2. Choosing a Fine-Tuning Strategy: Broad Coverage or Narrow Depth?

    A common starting point is to collect large volumes of general conversational data in the hope of making the model more capable overall. For resource-constrained on-device agents, however, this approach may deliver limited gains and can weaken existing capabilities.

    An alternative is a narrow-and-deep strategy: focus on one core, frequent, high-value skill, such as function calling, and fine-tune on a compact set of high-quality, information-dense examples. The dataset may contain only a few hundred to a few thousand samples.

    “Narrow” means keeping the task domain tightly scoped. “Deep” means designing examples that cover boundary conditions, combinations of constraints, and error-recovery patterns.

    The rationale is that lightweight models have limited capacity. Task-specific fine-tuning directs that capacity toward the behaviors that matter most. Concentrating the training budget on a critical skill can produce a clearer behavioral change than spreading it across loosely related tasks.

    3. A Reported Experiment: From 46% to 90% Function-Calling Success

    An experiment involving an on-device agent skills framework used an unnamed open-source model with approximately two billion parameters. The team applied the narrow-and-deep approach and reported a substantial improvement.

    3.1 Baseline Evaluation

    Before fine-tuning, the model achieved a 46% success rate on a standard function-calling benchmark.

    Typical failures included:

    • Confusing fields with similar labels, such as interpreting a date as the order date rather than the shipping date.
    • Inferring argument types incorrectly, such as converting a numeric string into an integer when the string type should have been preserved.
    • Omitting conditions in compound instructions, such as “Remind me tomorrow at 10 a.m., but move it to the afternoon if it rains.”

    3.2 Building the Training Dataset

    Instead of using general multi-turn conversations, the team created a highly structured function-calling dataset containing approximately 3,000 examples.

    Each example included:

    • A natural-language user instruction, including ambiguous phrasing, synonyms, or nested conditions.
    • The target function signature, including its name, argument list, and type declarations.
    • The expected output as normalized JSON or a code snippet.
    • Optional human-annotated intermediate reasoning steps intended to improve interpretability.

    Examples increased progressively in difficulty, moving from single-function, single-argument requests to parallel function calls, argument dependencies, and error recovery.

    The dataset also emphasized negative examples: requests that appeared valid but were actually incorrect or insufficiently specified. These examples were designed to teach the model when to decline a request or ask for clarification.

    3.3 Fine-Tuning Settings

    The training approach used the following settings and strategies:

    • Learning rate: A low learning rate, such as 2e-5, to reduce the risk of catastrophic forgetting.
    • Training duration: Three to five epochs, with validation-based early stopping to limit overfitting.
    • Context length: Inputs truncated to 70% of the model’s maximum context length while preserving important conversation turns.
    • Mixed training data: Basic instruction-following examples mixed with function-calling examples at a 1:10 ratio to help retain general conversational ability.

    These settings describe the reported experiment rather than a universal configuration for every model or dataset.

    3.4 Results

    After fine-tuning, function-calling success on the same benchmark increased from 46% to 90%.

    The experiment also reported:

    • 15% lower average response latency, attributed to fewer regeneration attempts caused by formatting errors.
    • A 30% improvement in generalization to previously unseen functions, when only their signature descriptions were provided.
    • Success rates above 85% under minor instruction perturbations, including colloquial phrasing and irrelevant words.

    These results illustrate the potential of small models with focused fine-tuning to compete with, and potentially outperform, larger models on a narrowly defined skill while retaining the advantages of on-device deployment. The reported figures do not, by themselves, establish a general advantage over larger models across tasks.

    4. Additional Considerations for On-Device Deployment

    Fine-tuning is only part of deploying a model within an on-device agent framework. Memory optimization and failure handling remain important engineering concerns.

    4.1 Quantization and Inference Optimization

    Model weights can be quantized from FP16 to INT8 or INT4. Efficient attention implementations, such as lightweight variants of FlashAttention, can also help optimize inference.

    The source describes memory reductions of more than 60% without a substantial loss in function-calling accuracy. Actual results need to be checked against the fine-tuned model’s validation set.

    If quantization degrades performance, a small amount of quantization-aware training may be necessary.

    4.2 Error Recovery and Fallbacks

    A 90% success rate still leaves failures that require a fallback path. The proposed approach uses two layers:

    • Layer 1: Model self-checking. Use an output-probability threshold as a confidence signal and request user confirmation when confidence is low.
    • Layer 2: Rule-based recovery. Repair common formatting problems, such as missing brackets or incorrectly escaped quotation marks.

    These mechanisms complement fine-tuning by addressing uncertainty and routine output errors at runtime.

    5. Limitations and Further Optimization

    Narrow-and-deep fine-tuning is not a universal solution. Its effectiveness depends on three conditions:

    1. Clearly defined task boundaries. Function calling is a tightly constrained, directly testable agent skill, making it a suitable target for focused optimization.
    2. Investment in high-quality data. Constructing useful negative examples requires domain expertise and careful review.
    3. Continuous evaluation and iteration. Teams need a process for sampling real production requests, evaluating performance, and adding examples for newly encountered scenarios.

    A possible next step is to combine focused fine-tuning with a small set of related training objectives. For example, function calling, intent classification, and entity extraction could share underlying representations.

    The source recommends limiting this combination to no more than three objectives to avoid returning to an overly broad training strategy. This is best treated as a scope-control guideline rather than a universal technical limit.

    6. Conclusion

    For on-device AI agents, improving a specific operational skill can be more valuable than pursuing broad capability gains. A narrow-and-deep fine-tuning strategy concentrates limited model capacity and training resources on the behaviors that matter most.

    The reported improvement from 46% to 90% function-calling success demonstrates the potential of carefully designed task data. Combined with quantization, validation, and runtime fallbacks, focused fine-tuning provides a practical path toward more dependable on-device agents.

    A useful starting point is a single core skill: define its boundaries, build examples that expose its failure modes, and evaluate whether targeted training produces a measurable improvement.