AnthropicClient.ts
AnthropicClient.ts overview
Section titled “AnthropicClient.ts overview”The AnthropicClient module defines the low-level Effect service for
Anthropic’s Messages API. It builds a generated Anthropic HTTP client with
authentication headers, API version headers, response decoding, and error
mapping, then exposes helpers for regular and streaming message requests.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”constructors
Section titled “constructors”Creates an Anthropic client service with the given options.
When to use
Use when you have explicit configuration values and need an Effect that
constructs the Anthropic client service, rather than providing it as a Layer.
Details
The client handles API key authentication via the x-api-key header, API versioning via the anthropic-version
header, error mapping to the unified AiError type, and request/response transformations via AnthropicConfig. It
requires an HttpClient in the context.
See
layerfor providing the client as aLayerfrom explicit optionslayerConfigfor providing the client as aLayerwithConfig-based settings
Signature
declare const make: (options: Options) => Effect.Effect<Service, never, HttpClient.HttpClient>Since v4.0.0
layers
Section titled “layers”Creates a layer for the Anthropic client with the given options.
When to use
Use when you already have explicit Options values, such as an API key or
custom API URL, and want to provide AnthropicClient as a Layer.
See
makefor constructing the client service effectfullylayerConfigfor loading client settings fromConfig
Signature
declare const layer: (options: Options) => Layer.Layer<AnthropicClient, never, HttpClient.HttpClient>Since v4.0.0
layerConfig
Section titled “layerConfig”Creates a layer for the Anthropic client, loading the requisite configuration
via Effect’s Config module.
When to use
Use when you want to provide the Anthropic client as a Layer with
configuration loaded from Effect’s Config module, such as from environment
variables or a secrets provider.
See
layerfor providing the client from explicit options instead ofConfigmakefor constructing the client service effectfully
Signature
declare const layerConfig: (options?: { readonly apiKey?: Config.Config<Redacted.Redacted<string> | undefined> | undefined readonly apiUrl?: Config.Config<string> | undefined readonly apiVersion?: Config.Config<string> | undefined readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined}) => Layer.Layer<AnthropicClient, Config.ConfigError, HttpClient.HttpClient>Since v4.0.0
models
Section titled “models”MessageStreamEvent (type alias)
Section titled “MessageStreamEvent (type alias)”Represents an event received from the Anthropic Messages API during a streaming request.
Details
Events include:
message_start: Initial event containing message metadatamessage_delta: Incremental updates to the message (e.g., stop reason)message_stop: Final event indicating the message is completecontent_block_start: Start of a content blockcontent_block_delta: Incremental content updates (text, tool use, etc.)content_block_stop: End of a content blockerror: Error events with type and message
Signature
type MessageStreamEvent = | typeof Generated.BetaMessageStartEvent.Type | typeof Generated.BetaMessageDeltaEvent.Type | typeof Generated.BetaMessageStopEvent.Type | typeof Generated.BetaContentBlockStartEvent.Type | typeof Generated.BetaContentBlockDeltaEvent.Type | typeof Generated.BetaContentBlockStopEvent.Type | typeof Generated.BetaErrorResponse.TypeSince v4.0.0
Service (interface)
Section titled “Service (interface)”Represents the Anthropic client service with methods for the Messages API, including regular and streaming message creation.
Signature
export interface Service { /** * The underlying generated Anthropic client that exposes all API endpoints. */ readonly client: Generated.AnthropicClient
/** * Executes a low-level streaming HTTP request and decodes the Server-Sent Events response using the provided schema. */ readonly streamRequest: <S extends Sse.EventCodec>( schema: S ) => ( request: HttpClientRequest.HttpClientRequest ) => Stream.Stream<S["Type"], HttpClientError.HttpClientError | Schema.SchemaError | Sse.Retry, S["DecodingServices"]>
/** * Creates a message using the Anthropic Messages API and maps all errors to the unified `AiError` type. */ readonly createMessage: (options: { readonly payload: typeof Generated.BetaCreateMessageParams.Encoded readonly params?: typeof Generated.BetaMessagesPostParams.Encoded | undefined }) => Effect.Effect< [body: typeof Generated.BetaMessage.Type, response: HttpClientResponse.HttpClientResponse], AiError.AiError >
/** * Creates a streaming message using the Anthropic Messages API and maps all errors to the unified `AiError` type. * * **Details** * * The returned Effect yields the HTTP response and a stream of events as the model generates its response. The stream * automatically terminates when a `message_stop` event is received. */ readonly createMessageStream: (options: { readonly payload: Omit<typeof Generated.BetaCreateMessageParams.Encoded, "stream"> readonly params?: typeof Generated.BetaMessagesPostParams.Encoded | undefined }) => Effect.Effect< [response: HttpClientResponse.HttpClientResponse, stream: Stream.Stream<MessageStreamEvent, AiError.AiError>], AiError.AiError >}Since v4.0.0
options
Section titled “options”Options (type alias)
Section titled “Options (type alias)”Configuration for creating an Anthropic client.
When to use
Use when the Anthropic client settings are already available as values and
should be passed directly to make or layer.
Details
These options configure the base Anthropic URL, the x-api-key
authentication header, the anthropic-version header, and an optional
transformation of the underlying HttpClient.
See
makefor constructing an Anthropic client from explicit optionslayerfor providing an Anthropic client from explicit optionslayerConfigfor loading Anthropic client settings fromConfig
Signature
type Options = { /** * The Anthropic API key for authentication. Requests are made without authentication when this is omitted, which is * useful for proxied setups or testing. */ readonly apiKey?: Redacted.Redacted<string> | undefined
/** * The base URL for the Anthropic API. Override this to use a proxy or a different API-compatible endpoint. * * @default "https://api.anthropic.com" */ readonly apiUrl?: string | undefined
/** * The Anthropic API version header value. This controls which version of the API to use. * * @default "2023-06-01" */ readonly apiVersion?: string | undefined
/** * Optional transformer for the underlying HTTP client, such as middleware, logging, or custom request/response * handling. */ readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined}Since v4.0.0
services
Section titled “services”AnthropicClient (class)
Section titled “AnthropicClient (class)”Service tag for the Anthropic client.
When to use
Use when accessing or providing the Anthropic client service through Effect’s context.
See
makefor constructing an Anthropic client effectfullylayerfor providing a client from explicit optionslayerConfigfor providing a client fromConfig
Signature
declare class AnthropicClientSince v4.0.0