Offline and on-device agents with cloud fallback

Some agents must keep working with no signal, or must never send certain data off the device. agentlib treats on-device models as first-class ModelProviders and uses ModelRoute to escalate to the cloud only when a request exceeds local capability — with no branching in your app code.

ModelRouteAgentSpec + Runner

Four on-device providers, one interface

Apple Foundation Models, Gemini Nano, llama.cpp (via Fllama), and MediaPipe each implement the same ModelProvider shape as AnthropicProvider. That means an on-device model and a cloud model are interchangeable in an AgentSpec. See the on-device deep dive for the full provider matrix.

Capability-aware routing

ModelRoute.preferOnDevice([...]) picks the first capable on-device provider and only reaches for a cloud provider when the local one can't satisfy the request (context length, tool use, vision). Privacy-sensitive turns stay local; heavy reasoning escalates — automatically.

Graceful degradation, not failure

Because routing is capability-aware, an offline device doesn't error — it runs the on-device path. When connectivity returns, the OnNetworkChange hook lets you re-enable cloud escalation. The agent's behaviour degrades gracefully instead of breaking.

lib/route.dart
final model = ModelRoute.preferOnDevice([
  FoundationModelsProvider(),   // iOS 19+
  GeminiNanoProvider(),         // Pixel 8+, S24+
  FllamaProvider(model: 'qwen2.5-1.5b'),
], fallback: AnthropicProvider(
  apiKey: const String.fromEnvironment('ANTHROPIC_API_KEY'),
));

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.