AnthropicChatModel class

A ChatModel backed by Claude.

final claude = AnthropicChatModel(apiKey: Platform.environment['ANTHROPIC_API_KEY']!);
final reply = await claude.complete([ChatMessage.user('Hello, Claude')]);
print(reply.text);

Defaults follow Anthropic's current guidance: claude-opus-5, adaptive thinking, automatic prompt caching and — on models that support it — server-side refusal fallbacks (fallbacks: "default"), which re-run a request declined by a safety classifier on Anthropic's recommended fallback model instead of failing it. A request that is still declined throws LlmRefusalException.

Implemented types

Constructors

AnthropicChatModel({required String apiKey, String model = 'claude-opus-5', int maxTokens = 16000, int streamMaxTokens = 64000, bool adaptiveThinking = true, ClaudeEffort? effort, bool? serverSideFallbacks, bool promptCaching = true, Uri? baseUrl, Map<String, String> headers = const {}, Duration timeout = const Duration(minutes: 10), int maxRetries = 2, HttpClient? httpClient})
Creates a client.
AnthropicChatModel.fromEnvironment({String model = 'claude-opus-5'})
Creates a client from the ANTHROPIC_API_KEY environment variable.
factory

Properties

adaptiveThinking → bool
Send thinking: {type: "adaptive"}. Turn off for models without adaptive thinking (e.g. Claude Haiku 4.5).
final
apiKey → String
API key (x-api-key).
final
baseUrl → Uri
API root, for proxies and gateways.
final
effort → ClaudeEffort?
Thinking effort; null uses the API default (high).
final
hashCode → int
The hash code for this object.
no setterinherited
headers → Map<String, String>
Extra request headers (e.g. additional anthropic-beta flags). Header names are matched case-insensitively, as HTTP requires.
final
maxTokens → int
Output limit for complete.
final
model → String
Model identifier sent to the provider.
final
promptCaching → bool
Ask the API to cache the longest reusable prompt prefix (top-level cache_control).
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
serverSideFallbacks → bool?
Server-side refusal fallbacks; null enables them for the models that support the "default" mode (Claude Opus 5 and Claude Fable 5.1).
final
streamMaxTokens → int
Output limit for stream (streaming is not bound by HTTP timeouts, so it gets more room).
final

Methods

close() → void
Releases network resources.
override
complete(List<ChatMessage> messages, {String? system, int? maxTokens}) → Future<ChatResponse>
Generates a complete response to messages.
override
completeWithTools(List<ChatMessage> messages, {String? system, int? maxTokens, List<Tool> tools = const []}) → Future<ChatResponse>
Like ChatModel.complete, but the model may answer with ChatResponse.toolCalls instead of (or as well as) text.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
stream(List<ChatMessage> messages, {String? system, int? maxTokens}) → Stream<String>
Streams the response to messages as text deltas.
override
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited