Mobile LLM Application Architecture: Dual-Mode Routing, RAG, and On-Device Design

    This article explains how to architect large language model applications for mobile devices under limited screen, compute, and network conditions. It covers a layered device-cloud design, a dual-mode system that separates a fast mode for short queries from a deep reasoning mode for complex tasks, interpretable routing that combines deterministic rules with a lightweight classifier, retrieval-augmented generation with source attribution, a document understanding pipeline for OCR and file uploads, and practical guidance on latency, cost, reliability, security, and observability. The goal is a configurable and testable engineering system that balances user experience, cost, and compliance.

    1. Technical Positioning of a Mobile AI Assistant

    A mobile AI assistant must deliver large language model capabilities to users in a low-friction, predictable way, despite limited screen space, compute, and network bandwidth. The requirements usually follow two tracks. The first is instant question answering, which demands low time-to-first-token latency and smooth interaction. The second is complex problem analysis, which demands stronger reasoning and better context integration. These two tracks have completely different resource profiles, so modern mobile applications generally adopt a dual-mode design: a fast mode for high-frequency short queries, and a deep thinking mode for low-frequency, high-value tasks.

    The key point is that dual mode is not simply a switch between model sizes. It is a parameterized configuration set that also covers the context window, retrieval strategy, reasoning budget, timeout thresholds, and streaming output policy. In engineering terms, these differences should be abstracted into configurable policy sets rather than scattered across business logic as conditional branches.

    2. Overall Architecture: A Layered Device-Cloud Design

    A robust layered structure typically looks like this:

    • Access layer: handles authentication, rate limiting, canary release, and protocol adaptation, consolidating traffic at a single entry point.
    • Orchestration layer: intent recognition, mode routing, prompt assembly, and tool-call orchestration.
    • Capability layer: atomic capabilities such as model inference, web retrieval, document parsing, and image text recognition.
    • Data layer: session storage, caching, vector indexes, and audit logs.
    • Observability layer: metrics, distributed tracing, alerting, and cost accounting.

    The device side keeps only lightweight capabilities, such as local caching, draft saving, image preprocessing, and result rendering. Heavy workloads run in the cloud and stream incremental content back over a streaming protocol, which balances user experience against cost.

    3. Dual-Mode Routing: Replacing a Black Box with Explainable Decisions

    Mode selection should follow a “rules first, model as fallback” strategy: deterministic rules handle obvious cases first, and a lightweight classification model handles ambiguous requests.

    1. def route_mode(query: str, has_file: bool, ctx_len: int) -> str:
    2. # Rule layer: directly match high-certainty scenarios
    3. if has_file or ctx_len > 8000:
    4. return "deep"
    5. if len(query) < 40 and not any(k in query for k in ("why", "compare", "plan")):
    6. return "fast"
    7. # Fallback layer: score with a lightweight classification model
    8. score = classifier.predict(query)
    9. return "deep" if score > 0.6 else "fast"

    The routing result should be written into the trace context so that latency and satisfaction can later be attributed per mode. Users should also keep a manual switch, so the final decision stays with them.

    4. Retrieval Augmentation with Web Search: Freshness and Traceability

    Web search adds value by filling the freshness gaps in model parameters, but concatenating raw page content introduces noise and length bloat. The recommended flow is query rewriting, recall, reranking, snippet trimming, and citation annotation. Reranking can combine three signals: text similarity, source credibility, and freshness. Trimming should split on semantic boundaries to avoid cutting off key conclusions. The final prompt should explicitly retain source identifiers so that answers remain verifiable.

    5. Multimodal Document and Image Text Parsing

    Capabilities such as solving problems from a photo, uploading files, and recognizing scanned documents can be abstracted into a unified document understanding pipeline: preprocessing (denoising, deskewing, compression), layout analysis, text recognition, structured normalization, semantic chunking, and inference. Three engineering points matter. First, upload size and page count should be validated up front with clear feedback. Second, regions with low recognition confidence should trigger a second confirmation rather than a silent guess. Third, intermediate parsing artifacts should be cacheable and reusable to avoid repeated billing and repeated latency.

    6. Performance, Cost, and Reliability

    • Latency management: measure time to first token and total duration separately; pair streaming output with skeleton screens; degrade to segmented responses on weak networks.
    • Cost control: set context and output limits per mode, reuse cache hits directly, and summarize long documents before question answering.
    • Reliability: design separate fallback paths for timeouts, rate limiting, and content safety blocks, so that “having a response” beats “having no response.”
    • Security and compliance: encrypt data in transit and at rest, mask sensitive fields, and retain only the minimum necessary audit logs.

    7. Observability and Continuous Iteration

    Build a per-mode dashboard with core metrics such as time-to-first-token percentiles, task completion rate, follow-up question rate, parsing failure rate, and cost per task. During canary release, ramp traffic along two dimensions, mode and scenario, and roll back quickly by routing key if anomalies appear. The iteration loop depends on real badcase feedback: failed samples are structurally labeled and added to the evaluation set, forming a cycle of discovery, attribution, fix, and regression.

    8. Conclusion

    The competitiveness of a mobile LLM application does not depend on stacking more models. It depends on whether routing is explainable, retrieval is traceable, parsing is reusable, and degradation is predictable. Turning these pieces into a configurable, observable, and regression-testable engineering system is what keeps experience, cost, and compliance in stable balance.