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,
  })

Properties

PropertyTypeDescription
apiKeyString
modelString
systemPromptString
promptCacheEnabledbool
idToolCallId
nameString
bufferStringBuffer

Methods

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

)`

Parse Anthropic SSE event payloads into [LlmChunk]s.

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

Released under the MIT License.