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.

21 HooksSnapshotsAgentSpec + Runner

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

See how these fit together in the architecture overview, or browse all eight primitives in depth.

build it

Ship this on your users' phones.

agentlib is open source, MIT, and on pub.dev. Add one pubspec line to get started.