Skip to content

AnthropicClient ​

Category: LLM Providers

Source: anthropic_client.dart

Classes ​

AnthropicClient ​

LLM client for the Anthropic Messages API with streaming.

Prompt caching: when [promptCacheEnabled] is true (default), the request body includes a top-level cache_control: {type: "ephemeral"} directive. Anthropic's auto-caching advances the cache breakpoint to the last cacheable block on each turn, so a growing conversation accumulates cache hits without the client tracking breakpoint placement explicitly. Caching is GA on Claude 4.x — no beta header needed. Older models silently ignore the directive.

Constructor ​

dart
AnthropicClient({
    required this.apiKey,
    required this.model,
    required this.systemPrompt,
    String baseUrl = _defaultBaseUrl,
    http.Client Function()? requestClientFactory,
    this.promptCacheEnabled = true,
    this.reasoning = const ReasoningConfig(),
    this.maxOutputTokens,
    this.extraHeaders = const {},
  })

Properties ​

PropertyTypeDescription
apiKeyString
modelString
systemPromptString
promptCacheEnabledbool
reasoningReasoningConfig
maxOutputTokensint?Output cap for a turn. Comes from the catalog's max_output_tokens; the 8192 fallback is only for models the catalog doesn't describe.
extraHeadersMap<String, String>Extra request headers declared by the provider (request_headers: in the catalog) — proxies and gateways fronting Anthropic need these.

Methods ​

Stream<LlmChunk> stream(List<Message> messages, {List<Tool>? tools}) ​
`static Stream<LlmChunk> parseStreamEvents( ​
Stream&lt;Map&lt;String, dynamic&gt;&gt; events, {
bool showThoughts = true,

})`

Parse Anthropic SSE event payloads into [LlmChunk]s.

Exposed as static for testability (can feed synthetic events).

Released under the MIT License.