On-Device Agent Fine-Tuning: Reliable Function Calling with Small Models
Small models can support reliable on-device agents when fine-tuning focuses on a limited set of local functions. This article explains how to design a skill framework, build targeted training data, evaluate function-calling accuracy, and combine specialized models in an offline transcription application while managing resource constraints and error propagation.
On-Device Agent Fine-Tuning: Reliable Function Calling with Small Models
As large language models become more capable, AI agents are emerging as a new approach to application development. Most agent implementations, however, depend on large cloud-hosted models. This introduces network dependencies, privacy risks, and unpredictable latency.
For applications with strict responsiveness and data-security requirements, moving agent capabilities onto the device is an increasingly important option. The challenge is limited compute: on-device hardware typically supports only smaller models. Developers must therefore find ways to make these models reliable agents, particularly when generating function calls.
1. Designing a Skill Framework for On-Device Agents
The defining difference between on-device and cloud-based agents is their resource budget. A practical on-device skill framework typically has four layers:
- Task understanding: Accept natural-language input, identify the user’s intent, and extract relevant slots.
- Skill registration: Describe available local capabilities—such as recording, transcription, file operations, and system settings—in a structured format. Each skill includes its name, parameter definitions, and return-value format.
- Decision and dispatch: Use a small model to match the intent to an available skill and generate a function-call request in the required format.
- Execution and feedback: Parse the model output, execute the local function, and pass the result back to the model to generate a final response.
The decision and dispatch layer is the critical component. Large cloud models can draw on their capacity to generalize across tool descriptions. Small on-device models have more limited generalization and need targeted training to close that gap.
2. Fine-Tune for Narrow Scope and Deep Coverage
The central lesson is straightforward: do not try to turn a small model into a general-purpose assistant. Train it to be highly reliable within a clearly defined set of skills.
Why Broad, Shallow Training Falls Short
Small models—typically those with a few billion parameters or fewer—have limited capacity. If fine-tuning data spans too many domains and task types, the model may learn only superficial patterns for each task.
Common failures include:
- Generating the wrong function name.
- Producing incorrectly formatted arguments.
- Failing to call a function when one is needed.
- Calling a function when no call is appropriate.
On-device applications have little room for such errors. An incorrect function call can cause an application crash or damage user data.
What Narrow and Deep Means
- Narrow scope: Define exactly which skills the model must support. An offline transcription application, for example, might need only a dozen or so functions covering actions such as starting a recording, stopping it, transcribing audio, and saving files. Training data should focus on these functions and their combinations.
- Deep coverage: Cover each skill thoroughly. Include varied phrasing, such as conversational requests, direct commands, and abbreviated instructions; different argument combinations; reference resolution across multiple conversation turns; and negative examples that teach the model to politely decline unsupported requests rather than force a function call.
Building Fine-Tuning Data
A training example can use a consistent conversation structure:
{"messages": [{"role": "system","content": "Available functions: [{\"name\":\"start_recording\",\"params\":{\"quality\":\"high|normal\"}}, ...]"},{"role": "user","content": "Start recording in high-quality mode."},{"role": "assistant","content": "{\"function\":\"start_recording\",\"arguments\":{\"quality\":\"high\"}}"}]}
Key data-construction guidelines include:
- Prepare at least several hundred positive examples per function, covering different ways users might express the same request.
- Include 20%–30% negative examples to teach the model to reject requests outside its skill boundaries.
- Keep the output format strictly consistent so the execution layer can parse it deterministically.
- Reserve difficult examples from realistic scenarios for validation rather than relying entirely on clean, simplified data.
After fine-tuning, quantization—such as reducing weights to 4-bit or 8-bit precision—can help the model fit within on-device memory limits. Reevaluate function-calling accuracy after quantization. Aggressive quantization can significantly degrade structured-output reliability, making model size and accuracy an important trade-off.
3. Improving Function Calling from Unreliable to Usable
Without targeted fine-tuning, small models often have low function-calling success rates. In practical cases, a base model’s out-of-the-box success rate may be only 40%–50%, meaning roughly one in every two requests fails. That is unsuitable for a real product.
The narrow-and-deep approach—defining skill boundaries, building high-quality data, including negative examples, and standardizing output—can raise function-calling success rates to around 90%. In the cases described, this represents a move toward production usability. The improvement comes from aligning the training objective with the application, not from increasing model size.
Maintaining that improvement requires continuous evaluation:
- Offline evaluation set: Maintain a fixed set of test cases and run regression tests after every model update. Track function-calling success rate, argument accuracy, and refusal accuracy.
- Runtime fallback: Validate the model’s output format. If parsing fails, fall back to a clarifying question—such as “Do you want to start recording or view previous recordings?”—rather than failing silently.
- Data feedback loop: De-identify failures collected during real-world use and feed them back into the training set to reduce long-tail errors over time.
4. Practical Example: A Multi-Model Pipeline for Offline Transcription
On-device applications rarely solve every problem with a single model. A fully offline speech-transcription application can instead combine several specialized small models:
- Speech recognition model: Converts audio streams into text and focuses on the acoustic task.
- Small agent model: Interprets user instructions and dispatches local functions, such as starting or stopping recording, triggering transcription, and exporting text.
- Optional text post-processing model: Restores punctuation, divides text into paragraphs, and generates summaries.
Combining a specialized pipeline with agent-based dispatch addresses several core on-device requirements:
- Privacy: Audio and text remain on the device, supporting data-protection requirements in sensitive settings such as meeting recording and medical consultations.
- No network dependency: The application remains functional with limited or no connectivity.
- Controlled latency: Small models can achieve real-time performance for individual inference steps on suitable on-device chips.
- Clear responsibilities: Each model has a focused role and training objective, making failures easier to isolate and components easier to replace.
The main caveat is error propagation. A speech-recognition mistake can cause the agent to misinterpret a command. Validation between models, using mechanisms such as confidence thresholds and command-keyword allowlists, can help reduce this risk.
5. Conclusion
Reliable on-device agents depend on three engineering principles.
First, accept the limits of small models and use narrow, deeply covered fine-tuning to achieve reliability within a specific application domain.
Second, make function-calling success rate a core quality metric, supported by a complete feedback loop from training-data construction to runtime fallback and failure analysis.
Third, use multiple specialized models for complex applications so that each component handles the task it is best suited to perform.
This approach provides a practical alternative to cloud-dependent agents for applications where privacy and responsiveness are central requirements.
