Long-running agents that survive backgrounding and app kills
Mobile agents don't run on a server that stays up — they run in an app the OS will background, suspend, and kill at will. agentlib makes every Runner state serialisable and adds lifecycle hooks so a task can outlive a single foreground session.
Serializable RunState
A run's RunState is serialisable, so you can persist it when the app is about to be suspended and rehydrate it later. The agent picks up exactly where it left off — same context window, same pending tool calls.
Lifecycle-aware hooks
OnSuspend, OnResume, OnLowBattery, and OnNetworkChange are among agentlib's 21 hooks. Use them to checkpoint before the OS suspends you, pause expensive cloud work on low battery, or re-route to on-device when the network drops.
Push-resume via APNs / FCM
Because the run is serialisable and lifecycle-aware, you can resume it from a push notification (APNs on iOS, FCM on Android). A background task that finishes while the app is closed can wake the app and hand back a completed result.
Related use cases
- An in-app AI assistant that ships inside your Flutter app — Build a Claude-Code-grade assistant that runs in your Flutter app on iOS and Android — no Node subprocess, no backend agent runtime.
- Offline and on-device agents with cloud fallback — Run agents on Apple Foundation Models, Gemini Nano, or llama.cpp — and fall back to Anthropic, OpenAI, or Google only when needed.
See how these fit together in the architecture overview, or browse all eight primitives in depth.