Joy ZhaoHire Me

Offline First Data Strategy

Session persistence, sliding-window context management, and local storage patterns for offline-capable Android apps.

AndroidOffline FirstRoomArchitecture

Why Offline First?

Users expect apps to work without connectivity. For an on-device LLM SDK, offline isn't optional — it's the core value proposition. Every design decision must assume intermittent or zero network.

Session Persistence with Room

The Offline LLM SDK stores chat sessions in SQLite via Room:

  • Session metadata — ID, title, creation time, model used
  • Message history — Role, content, timestamp, token count
  • Automatic cleanup — Old sessions pruned based on storage limits

Sliding-Window Context Management

LLM context windows are finite. The SDK implements automatic truncation:

  1. Tokenize each message using the native tokenize API
  2. Track cumulative token count per session
  3. When exceeding the model's context limit, evict earliest messages
  4. Preserve system prompt and most recent user/assistant pairs

This prevents OOM crashes while maintaining conversational coherence.

App Lifecycle Integration

EventAction
App backgroundedCache current session state, release KV cache
App foregroundedRestore session context, reload model if needed
Session switchClear KV cache, load new session history
Model switchUnload current model, load new GGUF file

Design Principles

  1. Local is the source of truth — Cloud sync is optional, never required
  2. Graceful degradation — Network features enhance but don't block core UX
  3. Predictable storage — Users can see and manage local data
  4. Background safety — State survives process death and app kills

These patterns apply beyond LLM apps — any Android app that needs reliable offline behavior benefits from the same architecture.