Wiring Android's MediaPipe LLM Inference API to a Real-Time Audio Transcription Pipeline A developer published a hands-on guide for building a fully on-device, real-time audio transcription pipeline on Android by wiring MediaPipe's LLM Inference API (Gemma 2B, 4-bit quantized) to a WebRTC VAD engine. The writeup shows that reusing the KV-cache across segments cuts prefill latency by roughly 75% (180ms to 45ms) and that combining KV-cache reuse with the GPU delegate brings end-to-end latency to about 125ms on a Pixel 9, well under the 400ms budget. It also details 1.5-second chunking with 200ms overlap to avoid word-boundary cuts and keep latency deterministic. --- title: "Real-Time Android Transcription with MediaPipe LLM: VAD, KV-Cache, and the 400ms Latency Budget" published: true description: "Wire MediaPipe's LLM Inference API to a live VAD pipeline on Android. Covers chunk sizing, overlap windows, KV-cache reuse, and NNAPI vs GPU delegate to hit sub-400ms latency on Pixel 9." tags: android, kotlin, mobile, architecture canonical url: https://mvpfactory.co/blog/android-mediapipe-llm-realtime-transcription --- A real-time, on-device audio transcription pipeline on Android — no cloud round-trips, no network dependency, no PII leaving the device. We will wire MediaPipe's LLM Inference API Gemma 2B, 4-bit quantized to a WebRTC VAD engine, tune the chunk buffer for deterministic latency, reuse the KV-cache across segments, and pick the right delegate to stay under 400ms end-to-end on a Pixel 9. Here is the pipeline we are targeting: Mic → AudioRecord → VAD Engine → Chunk Buffer → MediaPipe LLM Inference → Text Output ↑ ↑ WebRTC VAD KV-Cache rolling window mediapipe LLM Inference dependency added to your Gradle build .task file on-device download via MediaPipe Model Maker or the official model card webrtc-android VAD port available as a dependency RECORD AUDIO permission granted at runtime Let me show you a pattern I use in every project: treat chunk sizing as a first-class latency control, not an afterthought. Most teams get this wrong by feeding irregular-length audio into the model. The LLM Inference API is sensitive to input length variance — longer chunks inflate the prefill phase and blow the latency budget unpredictably. Target: 1.5-second chunks, 16kHz, mono, 16-bit PCM. WebRTC VAD operates on 10/20/30ms frames. Accumulate frames until you hit either the 1.5s ceiling or a speech-end event, then flush. The 200ms trailing overlap on each chunk is what prevents word-boundary cuts where a syllable lands in the wrong segment. class VadChunkBuffer private val sampleRate: Int = 16 000, private val chunkMs: Int = 1500, private val overlapMs: Int = 200 { private val chunkSamples = sampleRate chunkMs / 1000 private val overlapSamples = sampleRate overlapMs / 1000 private val buffer = ShortArray chunkSamples + overlapSamples private var writePos = 0 fun feed frame: ShortArray : ShortArray? { frame.copyInto buffer, writePos writePos += frame.size if writePos = chunkSamples { val chunk = buffer.copyOf writePos // Retain overlap for next chunk buffer.copyInto buffer, 0, writePos - overlapSamples, writePos writePos = overlapSamples return chunk } return null } } Here is the gotcha that will save you hours: your system prompt and task instructions are static across the entire transcription session. If you are rebuilding the prompt context on every segment, you are paying the prefill cost repeatedly for no reason. MediaPipe's LlmInference session supports prefix caching. Cache the KV state once at session init and reuse it for every segment. The numbers speak for themselves on Pixel 9 with Gemma 2B Q4: | Strategy | Avg Prefill | Avg Decode | End-to-End | |---|---|---|---| | No cache, full prompt each call | 180ms | 140ms | 320ms+ | | KV-cache reused across segments | 45ms | 140ms | ~185ms | | KV-cache + GPU delegate | 30ms | 95ms | ~125ms | Reusing the KV-cache alone cuts prefill by 75%. That is your highest-ROI optimization in this pipeline. Here is the minimal setup to get GPU delegate running: val options = LlmInference.LlmInferenceOptions.builder .setModelPath "/data/local/tmp/gemma2b q4.task" .setMaxTokens 256 .setPreferredBackend LlmInference.Backend.GPU .build val llmInference = LlmInference.createFromOptions context, options The docs do not mention this clearly, but NNAPI is the wrong choice for sustained live transcription. It offers lower peak power consumption but exhibits latency spikes during thermal throttling — which hits within 3–5 minutes of sustained inference on most devices. GPU delegate maintains consistent frame-to-frame latency at slightly higher sustained wattage. For a user actively speaking, consistency beats efficiency every time. If you are targeting sub-2019 SoCs without a capable GPU, fall back to CPU with XNNPACK — NNAPI inconsistency is measurably worse than CPU predictability in practice. Variable chunk length kills your latency budget. Lock at 1.5s. Any deviation adds unpredictable prefill variance that compounds at scale. Missing the overlap window causes phantom word drops. The 200ms trailing overlap is not optional — without it, final syllables of words at chunk boundaries get cut, and you need a separate re-stitching pass to recover them. Re-creating the LlmInference session per segment resets the KV-cache. Session init is expensive. Create once, reuse across the entire transcription session. NNAPI thermal behavior is device-dependent but consistently bad. Even on flagship hardware, 3–5 minutes of sustained inference triggers throttling. Run your benchmarks beyond the first 60 seconds or your measurements are misleading. GPU delegate requires the model to be in .task format. Raw GGUF or GGML weights will not work directly with MediaPipe — convert via MediaPipe Model Maker before deployment. Three takeaways to wire this up correctly: Lock chunk size at 1.5s with 200ms overlap. Variable-length inputs are the single largest source of latency unpredictability. The overlap window eliminates word-boundary cuts without a separate re-stitching pass. Initialize KV-cache once per session, not per segment. Static prompt prefill is wasted compute on every call. Cache it at session start — it is the highest-ROI optimization in the pipeline. Default to GPU delegate for sustained real-time workloads. NNAPI looks attractive on paper but thermal behavior makes it unreliable for sessions longer than a few minutes. GPU delegate keeps latency consistent where it matters most. Done right, end-to-end latency lands under 400ms on a Pixel 9 — with headroom left for post-processing and UI rendering. On-device, deterministic, and no PII on the wire. Further reading: MediaPipe LLM Inference API docs https://ai.google.dev/edge/mediapipe/solutions/genai/llm inference/android · WebRTC VAD Android port https://github.com/dpirch/libfvad