Deploying and Optimizing Multi-Agent Applications with On-Device Large Language Models

    This article examines the engineering challenges of running fully offline large language models on mobile and edge devices. It covers the limitations of cross-platform inference plugins, the transition to a native bridge with a singleton inference engine, and the use of global resource locking to safely serialize multi-agent workloads under constrained CPU, GPU, and memory conditions. It also discusses the future role of quantization, mobile NPUs, and cloud-device collaboration.

    As AI continues to evolve, large language models are moving beyond the cloud and into edge devices and mobile applications. For privacy-focused personal life-logging and knowledge-management apps, running a fully offline model without relying on cloud services represents a new technical starting point.

    However, mobile devices have limited computing power and memory. Running a large model reliably and efficiently while supporting multiple agents introduces significant engineering challenges. This article summarizes practical lessons from deploying on-device large language models, including common pitfalls and the architectural changes required to achieve stable multi-agent execution.

    1. Early On-Device Inference and the Risks of Cross-Platform Plugins

    At the beginning of an on-device AI project, developers often choose a cross-platform mobile framework and its associated third-party packages to improve development efficiency. These plugins typically wrap lightweight inference engines and expose simple APIs.

    In our initial implementation, we used a popular cross-platform model inference plugin to load an on-device large language model, including lightweight E2B/E4B parameter-scale variants. In practice, the application experienced severe stability problems. During inference, the app could crash, and in more serious cases, the entire test phone would display a black screen and reboot.

    Further investigation showed that the same device and model file worked normally in the vendor’s official sample application. The key difference was the technology stack. The official sample directly called the underlying inference framework through native code, while our implementation added an additional cross-platform bridge layer through the plugin.

    When loading multi-gigabyte model files and performing intensive GPU memory operations, this extra layer may introduce conflicts in low-level memory management or cause thread contention. These problems can eventually trigger operating-system-level protection mechanisms, including a full device restart.

    Given this level of stability risk, abandoning the third-party plugin and calling the native inference APIs directly became the necessary choice.

    2. Architectural Refactoring: A Native Bridge and Singleton Inference Engine

    To avoid the unpredictable memory and thread management introduced by the cross-platform wrapper, we switched to the native programming language and called the official on-device inference framework directly. The resulting architecture became:

    Cross-platform business logic → Platform channel → Native inference plugin → On-device inference engine

    On the native side, we built a dedicated inference plugin based on two principles: singleton engine initialization and on-demand session creation.

    1. Singleton engine

      On-device large language model files are substantial, often exceeding 2 GB. Loading a model from disk into GPU memory is both time-consuming and resource-intensive. Therefore, the engine should be initialized only once during the application lifecycle.

      The configuration specifies the model path, uses the GPU as the compute backend, sets the maximum token limit, and assigns a cache directory. After initialization, the model remains resident in memory and provides the foundation for subsequent inference tasks.

    2. Session lifecycle management

      Compared with the inference engine, a conversation session is lightweight. Whenever the application receives an inference request, it creates a new session on demand, sends the message asynchronously, and closes the session immediately after inference completes.

      This design prevents context contamination between requests while limiting the growth of memory usage.

    After the architectural transition, inference speed returned to normal, and the device no longer experienced black-screen restarts. At the cross-platform business-logic layer, we also implemented a local client with the same interface as the cloud-based large language model service client.

    As a result, the upper-level Agent logic does not need to know whether it is calling a cloud API or a local on-device model. Switching to offline on-device inference requires changing only a configuration item, which significantly improves code reuse and extensibility.

    3. Multi-Agent Concurrency and Global Resource Locking

    Once basic on-device inference was working reliably, the application faced more complex multi-agent requirements. A mature local knowledge-management application may include several agents running in parallel, such as:

    • Card Agent, which processes card information
    • PKM Agent, which builds and maintains the personal knowledge base
    • Asset Analysis Agent, which analyzes multimodal data such as images and audio

    In the cloud, sending concurrent requests from multiple agents is a natural approach. Cloud clusters can scale horizontally and provide substantial computing resources. On a mobile device, however, CPU, GPU, and memory resources are highly constrained.

    If multiple agents are allowed to submit inference requests to the on-device engine at the same time, resource contention can sharply reduce inference speed. It can also trigger out-of-memory errors and cause the application to terminate unexpectedly.

    To address this issue, we introduced a global lock on the native side. The lock ensures that only one agent can execute an inference task inside the engine at any given time.

    When multiple agents submit requests simultaneously, their tasks are placed in a serialized queue. After the current inference task completes and releases the lock, the next queued task is awakened and executed.

    Although this scheduling strategy forces concurrent requests to run sequentially, it protects the inference engine from unsafe memory pressure. It also allows the available device resources to focus on a single task at a time, producing relatively stable inference throughput on constrained mobile hardware.

    4. Conclusion and Outlook

    Lightweight open-source large language models running on edge devices are creating new possibilities for offline AI applications. However, deployment is far more complex than simply calling an API.

    The process may involve system-level crashes caused by cross-platform plugins, architectural refactoring through a native bridge, and global resource-lock scheduling for multi-agent workloads. Each stage requires a detailed understanding of memory management, threading, and hardware resource constraints.

    Looking ahead, improvements in model quantization and the computing capabilities of mobile NPUs should make on-device large language models increasingly practical. At the same time, cloud-device collaboration will become an important direction for complex AI applications.

    Cloud platforms such as Baidu AI Cloud provide strong capabilities for cloud-based model inference, large-scale knowledge storage, and collaborative computing. In the future, smooth cooperation between local on-device models and high-capacity cloud models can help build AI architectures that are more intelligent, efficient, and privacy-aware.

    As developers continue optimizing on-device execution, they should also pay close attention to unified cloud-device interface design. This will make it easier to support increasingly complex AI application requirements without tightly coupling agent logic to a specific inference environment.