flutter_ai_provider_anthropic

Anthropic (Claude) provider for flutter_ai — streams the Messages API with extended thinking and tool use, mapped to AiStreamEvents so the rest of the family works against Claude unchanged.

A streamed answer with reasoning, a tool call, and the final answer

flutter_ai_provider_anthropic on pub.dev pub points License: BSD-3-Clause

Family: flutter_ai · core · client · elements · openai · gemini
Recipes · Migrating from the Vercel AI SDK


An Anthropic (Claude) LlmProvider for the flutter_ai family. It streams the Anthropic Messages API and maps each event to AiStreamEvents, so the controllers and UI in flutter_ai_client / flutter_ai_elements work against Claude unchanged.

  • Streams text, extended thinking, tool use, and finish reasons.
  • Maps flutter_ai conversations to Anthropic's wire format (system folded into the top-level system field; assistant tool calls → tool_use; tool results → tool_result).
  • Injectable http.Client for testing and custom transport.

Usage

import 'package:flutter_ai_provider_anthropic/flutter_ai_provider_anthropic.dart';

final provider = AnthropicProvider(
  apiKey: const String.fromEnvironment('ANTHROPIC_API_KEY'),
  // defaultModel: 'claude-opus-4-8',  // override per request via AiRequestOptions
);

await for (final event in provider.send(conversation, tools: tools)) {
  // feed into a MessageProcessor / UseChatController
}

Wire it into a controller:

final controller = UseChatController(
  provider: AnthropicProvider(apiKey: myKey),
  options: const AiRequestOptions(model: 'claude-opus-4-8'),
);

Notes

  • max_tokens is required by the API. Set it via AiRequestOptions.maxOutputTokens, or rely on AnthropicProvider's defaultMaxTokens (4096).
  • Sampling parameters (temperature) are forwarded only when set. Newer Claude models reject them — leave it unset for those.
  • Extended thinking: pass it through AiRequestOptions.extra, e.g. extra: {'thinking': {'type': 'adaptive'}}. Thinking text streams as ReasoningDelta events (AiReasoning in the UI).
  • Images: user-message image attachments (FilePart with an image/* media type) are sent as base64 or URL image blocks. Other document types are not yet sent.
  • Retry: transient failures (429/5xx, network) are retried with backoff honoring Retry-After (maxRetries, default 2).

Status

The request/response mapping is unit-tested against recorded SSE events. Supply an API key to use it against the live API.

☕ Support this project

Buy me a coffee on Ko-fi

If flutter_ai saves you time, buy me a coffee ☕ — it keeps the whole family maintained.

Libraries

flutter_ai_provider_anthropic
Anthropic (Claude) provider for the flutter_ai family.