Turning Edge AI into Real Features
Do LLMs on mobile make any sense?
Even if they're getting good, why should you still bother?
One model, shared by every app
ML Kit GenAI and the Prompt API
ML Kit Prompt API
implementation("com.google.mlkit:genai-prompt:1.0.0-beta4")val generativeModel = Generation.getClient()val status = generativeModel.checkStatus()generativeModel.generateContentStream(prompt).collect { chunk -> print(chunk.candidates[0].text) }
ML Kit Prompt API
The high-level route to Gemini Nano
ML Kit GenAI APIs
val articleToSummarize = "We are excited to announce a set of on-device..."val options = SummarizerOptions.builder(context).setInputType(InputType.ARTICLE).setOutputType(OutputType.ONE_BULLET).setLanguage(Language.ENGLISH).build()val summarizer = Summarization.getClient(options)
The DIY alternative
Adding it to your app
What are these models?
What's under the hood
What's under the hood
Gemma3_1b_fine_tune.ipynb, simplified
def generate(self, prompt, max_decode_steps):token_ids = self._tokenizer.apply_chat_template([{"role": "user", "content": prompt}],tokenize=True, add_generation_prompt=True)# Prefill: the whole prompt in one pass fills the KV cachekv_cache = self._run_prefill(token_ids[:-1])# Decode: one token per step, reusing the KV cachenext_token, next_pos = token_ids[-1], len(token_ids) - 1for _ in range(max_decode_steps):outputs = self._decode_runner(tokens=[[next_token]], input_pos=[next_pos], **kv_cache)logits = outputs.pop("logits")[0][0]next_token = int(np.argmax(logits)) # greedy samplerif next_token == self._tokenizer.eos_token_id:breakprint(self._tokenizer.decode(next_token), end="")kv_cache = outputsnext_pos += 1
What model do we use?
LiteRT-LM
implementation("com.google.ai.edge.litertlm:litertlm-android:0.17.1")
LiteRT-LM
# Push the model to the deviceMODEL_FILE="gemma-4-E2B-it.litertlm"TARGET_DIR="/storage/emulated/0/Android/data/com.myapp/files/"adb push "$MODEL_FILE" "$TARGET_DIR"
LiteRT-LM
val modelPath = File(context.getExternalFilesDir(null),modelName).absolutePathval engine = Engine(EngineConfig(modelPath = modelPath, backend = Backend.GPU()))engine.initialize() // slow: call it off the main thread
LiteRT-LM
val conversation = engine.createConversation(ConversationConfig(samplerConfig = SamplerConfig(topK = 50,topP = 0.95,temperature = 0.8)))
LiteRT-LM
conversation.sendMessageAsync(prompt).collect { message -> Log.d(message.toString()) }conversation.close()engine.close()
It depends 😌
Caveats
A real on-device AI feature in production
A Brazilian food delivery app
A high-attention moment after checkout.
What if the order is not for the user?
A floating AI button at the bottom of the waiting screen.
The feature was competing with the product.
One button with one job. A much quieter place in the product.
The loading state is actual local inference not a request waiting for our backend.
Success isn't generating text. It's helping the user send it.
A technically valid answer can still be completely wrong for the product.
We don't want creativity here. We want a faithful transformation of known facts.
The feature only exists when inference can run right now.
Before asking who clicked the feature, we need to know who could even see it.
At iFood scale, even limited device coverage means thousands of opportunities every day.
Local inference worked. But it wasn't instant.
Most of the hard problems weren't model problems.
On-device AI is a product decision, not an SDK demo.
This is a fad.
L.A. Noire
3 ideas
Remember Mainframes?
Mainframe, 1970s-80s
Remember Mainframes?
Terminal
Remember Mainframes?
Early personal computer
Mini models everywhere
Specialization and ubiquity
GBoard Writing Tools
Pixel Screenshots
google-ai-edge/gallery
AI Edge Gallery
flutteredge.ai
Flutter Edge AI demo
Putting the genie in the bottle