Skip to content
2 min read · 418 words

Class: AnthropicMessagesAdapter

Defined in: src/batteries/llm/anthropic_messages/adapter.ts:469

Opinionated cross-environment LLM adapter for the Anthropic Messages wire shape.

Remarks

Construction validates options eagerly via validateOptions and throws @nhtio/adk/batteries/llm/anthropic_messages!E_INVALID_ANTHROPIC_MESSAGES_OPTIONS on failure. The returned instance is reusable: call AnthropicMessagesAdapter.executor once per DispatchRunner configuration to obtain a @nhtio/adk!DispatchExecutorFn bound to the baseline plus optional executor-scope overrides. Per-iteration overrides live on ctx.stash.anthropicMessages and take highest precedence; headers, helpers, and retry merge key-by-key across all three layers, every other field is replaced wholesale at the highest layer that sets it. Node-first: browser is explicitly not a target — an Anthropic API key in a browser bundle is unacceptably exposed.

Constructors

Constructor

ts
new AnthropicMessagesAdapter(options: unknown): AnthropicMessagesAdapter;

Defined in: src/batteries/llm/anthropic_messages/adapter.ts:480

Parameters

ParameterTypeDescription
optionsunknownConstructor-baseline options. Re-validated on every iteration after per-dispatch and per-iteration overrides are layered in.

Returns

AnthropicMessagesAdapter

Throws

@nhtio/adk/batteries/llm/anthropic_messages!E_INVALID_ANTHROPIC_MESSAGES_OPTIONS when options does not satisfy anthropicMessagesOptionsSchema.

Properties

PropertyModifierTypeDescriptionDefined in
STASH_KEYreadonly"anthropicMessages"Customary key for per-iteration overrides on ctx.stash.src/batteries/llm/anthropic_messages/adapter.ts:471

Methods

countTokens()

ts
countTokens(input:
  | AnthropicMessagesCountTokensRequestInput
  | AnthropicMessagesCountTokensInput, overrides?: Partial<AnthropicMessagesAdapterOptions>): Promise<{
  inputTokens: number;
  raw: unknown;
}>;

Defined in: src/batteries/llm/anthropic_messages/adapter.ts:1536

Counts the input tokens a request would consume, without generating.

Uses Anthropic's dedicated count-tokens surface (a distinct parameter shape from a create request, and one that takes no maxTokens), and returns a named field plus the raw upstream body rather than leaking the vendor type through this battery's public API.

input accepts either a dispatch context — whose prompt is assembled with the same helpers the executor uses — or a pre-built Anthropic-shaped request. Option precedence, revalidation, timeout ownership, and error translation all match AnthropicMessagesAdapter.executor.

Parameters

ParameterType
input| AnthropicMessagesCountTokensRequestInput | AnthropicMessagesCountTokensInput
overrides?Partial<AnthropicMessagesAdapterOptions>

Returns

Promise<{ inputTokens: number; raw: unknown; }>


executor()

ts
executor(overrides?: Partial<AnthropicMessagesAdapterOptions>): DispatchExecutorFn;

Defined in: src/batteries/llm/anthropic_messages/adapter.ts:491

Returns a @nhtio/adk!DispatchExecutorFn bound to this adapter's baseline plus optional executor-scope overrides.

Parameters

ParameterTypeDescription
overrides?Partial<AnthropicMessagesAdapterOptions>Optional executor-scope overrides. Higher precedence than the baseline, lower precedence than ctx.stash[STASH_KEY].

Returns

DispatchExecutorFn


isAnthropicMessagesAdapter()

ts
static isAnthropicMessagesAdapter(value: unknown): value is AnthropicMessagesAdapter;

Defined in: src/batteries/llm/anthropic_messages/adapter.ts:1546

Returns true when value is an AnthropicMessagesAdapter instance.

Parameters

ParameterType
valueunknown

Returns

value is AnthropicMessagesAdapter