Cloud LLM calls cost money per token, add a network round trip to every prompt, and send user text to someone else's server. Running a small model on the phone fixes all three. But a 2-billion-parameter model sounds far too big for a phone. The reason it fits is quantization, and the numbers are simple.
The memory math
Neural network weights are normally stored as 32-bit floats (FP32). That is 4 bytes per parameter. For a 2-billion-parameter model such as Gemma 2B, the raw weights need about 8 GB of RAM. A mobile OS will kill an app that tries to hold that.
Quantization lowers the precision of each weight:
| Format | Size change vs FP32 |
|---|---|
| INT8 (8-bit integers) | 75% smaller |
| INT4 (4-bit integers) | 87.5% smaller |
A 4-bit Gemma 2B ends up at roughly 1 GB to 1.5 GB. That fits the memory of modern iOS and Android devices. You give up a small amount of precision and get faster inference, lower battery use, and a model that doesn't crash the host app.
It also helps speed. LLM inference is memory-bandwidth bound: phones spend more time pulling weights from RAM than multiplying them. Smaller weights mean less to pull.
Don't bundle the model
A 1.5 GB file will not fit in an app store release; the bundle limits are typically around 150 MB. So the model is downloaded on first launch:
- A background service downloads it from a storage bucket (S3, Firebase Storage) to the device's local files.
- Hash the file against a known checksum before you load it. A corrupt model gives you confusing inference failures.
Keep inference off the UI thread
Dart runs on a single-threaded event loop. A heavy native forward pass on the main thread blocks the UI, drops frames and can trigger an ANR on Android. Run the engine in its own isolate and talk to it with ports:
Future<void> startEngine(String modelPath) async {
final fromEngine = ReceivePort();
await Isolate.spawn(_engineMain, [fromEngine.sendPort, modelPath]);
final toEngine = await fromEngine.first as SendPort;
// Later: send a prompt, listen for tokens.
final tokens = ReceivePort();
toEngine.send([prompt, tokens.sendPort]);
tokens.listen((t) => appendToUi(t as String));
}
The isolate loads the model from the file path, receives prompts through its ReceivePort, and sends each generated token back through the SendPort the UI gave it. The UI paints tokens as they arrive.
The native bridge
At the time the source guide was written, MediaPipe had no all-encompassing pure Dart package for LLMs, so you bridge to the native iOS (Swift) and Android (Kotlin) libraries with platform channels or Dart FFI. The setup is three steps: point the engine at the model file path, set maxTokens and temperature, and stream output back to Dart.
For temperature, a low value such as 0.1 gives deterministic, factual output; a high one such as 0.8 gives more creative text.
Check the current API before you build. On 9 November 2026, Google's MediaPipe LLM Inference docs redirect to its LiteRT-LM pages, which Google describes as the production-ready layer for running LLMs on device, and newer Gemma generations have followed Gemma 2B. The architecture above (background isolate, downloaded quantized model, native bindings) still applies, but the API and model list may differ.
Limits you should design for
- Thermal throttling. Sustained inference heats the phone. After about 20 minutes of continuous prompts the OS may underclock the CPU or NPU and tokens per second drop. Add cool-down periods or queue work in the background.
- Older hardware. Devices with under 4 GB of total RAM will likely fail to allocate memory for the inference graph, even at 4-bit. Check at launch and fall back to a cloud API for those devices.
- Context window. Mobile models have strict limits. Count tokens before you submit a prompt; a huge document can crash the engine or degrade the output.
The takeaway
Quantization is what turns an 8 GB model into a 1 to 1.5 GB download. Add a checksum, an isolate, a RAM check and a cloud fallback, and on-device inference stops being a demo and becomes a real feature that works in airplane mode.
Further reading: Engineering cost-effective, private AI with Flutter and on-device LLMs
Would you ship an on-device model in your next app, or keep the cloud call? What would push you either way?
Top comments (0)