NodeLLM

Turning an Android Phone into a Shared, Local LLM Node

Abstract

Local LLMs on phones usually exist as single-user chat apps. NodeLLM treats a rootless Android phone as a small shared AI node on the local network instead. The phone runs GGUF models on its CPU with llama.cpp and serves both an on-device chat and HTTPS APIs to other devices on the LAN, with no cloud and no data leaving the network.

The node has an owner and guests. The app on the phone is the master: only it loads models and changes settings. Other devices connect as API clients with scoped keys and never gain master privileges. The project is mostly an exercise in making a constrained device behave like a dependable server: fitting models into a few GB of RAM, scheduling a single inference engine between several users, and keeping the network boundary tight.

Architecture

flowchart LR
    A[Local chat<br/>WebView] --> G
    B[LAN clients<br/>browser / API] --> G
    G[HTTPS gateway<br/>pinned self-signed cert] --> Z[Auth & validation<br/>API key scopes, rate limits]
    Z --> Q[Job registry +<br/>global priority queue]
    Q --> W[Single inference worker<br/>llama.cpp CPU, JNI]
    W --> T[Tool broker]
    T --> S[Sandbox service<br/>QuickJS · MicroPython · Lua · SQLite]
    L[Model library +<br/>Hugging Face downloader] --> E[RAM / KV estimator<br/>automatic limits]
    E --> W
    W -->|SSE / polling| A
    W -->|SSE / polling| B

The local UI and the HTTP API share the same application services, so there is no implicit privileged path. Administrative routes are not reachable over HTTP at all and are served only through the native channel of the master app.

Engineering Highlights

Fitting models into phone RAM

Before downloading or loading a model, NodeLLM estimates whether it fits. The KV cache size is computed from the GGUF metadata, covering dense, sliding-window, MLA and hybrid attention layouts, and including the block overhead of quantized formats. The app then picks the largest context (up to 16K tokens) whose KV cache, weights and a 512 MiB safety margin fit in the memory budget. Layouts it does not recognize get an explicit unknown verdict instead of a guess.

Models larger than the available RAM can still run by streaming the weights from storage (memory-mapped, without locking them in RAM). This came from a real failure: a 3.67 GiB model loaded with about 835 MiB free got the app killed by Android’s low-memory killer.

Quantized KV cache

The KV cache precision (F16, Q8_0 or Q4_0) is a per-model setting. Measured on a Galaxy A52s with MiniCPM5 (Q4_K_M, context 512):

KV precision Allocated KV Max abs. logit error vs F16 Mean abs. error
F16 21.0 MiB 0 0
Q8_0 11.16 MiB 1.24 0.17
Q4_0 5.91 MiB 5.78 0.60

Incompatible combinations are rejected at load time instead of silently falling back to another configuration.

Prompt-prefix cache

On a phone, prefill is the bottleneck: a long system prompt of about 1,300 tokens takes around 26 s at roughly 50 tokens/s. NodeLLM snapshots the KV state of common prefixes and restores it only when the tokens match exactly, with a RAM budget plus an on-disk cache written atomically. A background warm-up prepares the cache right after a model is loaded. Requests from different users never share KV state.

Scheduling and API contract

  • One inference worker, one global queue. The master’s requests always run first. Client priorities are set per key and per endpoint, server-side only.
  • OpenAI-style APIs: /v1/chat/completions (JSON and SSE), /v1/responses and the Messages-style /v1/messages, with structured output via JSON schema and extended sampling parameters.
  • Strict validation: parameters out of range are rejected, never silently clamped. Per-key token-bucket rate limits and connection caps protect the single engine.
  • Deadlines and recovery: every job has a deadline. An expired queued job never reaches the engine and its reservation is refunded, while a running job is cancelled cooperatively and keeps its partial output. A client can attach its own request ID and later ask whether an uncertain request was admitted, so lost replies are never blindly retried.

Network boundary

The server uses a self-signed certificate whose fingerprint is fetched through the master channel and pinned before any API key is sent. Requests with a wrong key, a forged role header, an unexpected Host or a cross-origin Origin are rejected, and the web UI runs under a strict Content Security Policy.

Sandboxed code tools

The model can run code in JavaScript (QuickJS), Python (MicroPython), Lua and SQL (SQLite). Each run happens in a fresh VM inside an Android service with an isolated UID, with a 16 MiB heap, a 2 s interpreter deadline, a 7 s hard timeout and capped source and output sizes. The sandbox has no file, network or process access, and tool use is deny-by-default with single-use approvals.

Reproducible build and CI

All third-party sources and toolchains are pinned and verified by SHA-256. CI runs a host test suite (Kotlin, JavaScript, native code, HTTP and MCP), then builds, signs and checks the APK, and publishes it with its checksum on every release tag. The repository also contains on-device smoke tests.

Current Limitations

Inference is CPU-only (no GPU or NPU). There is no image input, no RAG or embeddings, and the MCP client is only partly implemented. Speed depends heavily on the phone, long contexts are limited by RAM, and 2B-class models are unreliable on multi-step tasks.

Resources