Crux
API ReferenceAdapters

@crux/anthropic

Anthropic SDK adapter — factory pattern with native options.

Peer dependency: @anthropic-ai/sdk

import { createAnthropic, anthropicProviderRuntime, anthropicTranscript, toMessages, fromMessages } from '@crux/anthropic'
import { createGenerateObjectFn, createGenerateTextFn } from '@crux/anthropic'

For usage examples and provider comparison, see the Execution guide.

createAnthropic(client)

Create an adapter bound to an Anthropic client.

createAnthropic() is anthropicProviderRuntime.create. The package-owned anthropicTranscript owns tool_use / tool_result conversion plus assistant text/tool-call extraction. Anthropic-specific request params, cache-control system blocks, and response metadata normalization stay owned by @crux/anthropic; Crux still owns prompt resolution, tool loops, safety, validation retry, memory capture, and observability.

FieldTypeDescription
clientAnthropicThe Anthropic client instance

Returns:

MethodDescription
.generate(prompt, options)Execute a prompt via messages.parse (structured) or .create (text)
.stream(prompt, options)Stream a prompt execution
import { createAnthropic } from '@crux/anthropic'
import Anthropic from '@anthropic-ai/sdk'

const adapter = createAnthropic(new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY }))

const result = await adapter.generate(editDraft, {
  model: 'claude-sonnet-4-5-20250929',
  input: { instruction: 'Fix the intro' },
})
result.text
result._meta.usage

adapter.generate(prompt, options)

FieldTypeDescription
promptPromptThe prompt to execute
options.modelstring | FallbackModel<string>Model name or fallback() wrapper
options.inputobjectInput values
options.toolsRecord<string, unknown>?Additional Crux tools to merge at call time
options.toolMiddlewareToolMiddleware | readonly ToolMiddleware[]?Tool execution hooks, including Crux resumable approvals through result.messages.
options.extra.toolsToolUnion[]?Anthropic-native tools that bypass Crux tool conversion
options.extra.tool_choiceToolChoice?Anthropic-native tool selection

Returns: normalized Crux generate result with:

  • result.text — extracted assistant text
  • result.raw — raw Anthropic SDK response
  • result._meta — normalized usage, finish reason, tool calls, and model metadata

When the prompt has an output schema, Anthropic's parsed value remains available on result.raw.parsed_output.

Anthropic requires max_tokens. If not set in your prompt's settings, the adapter defaults to 4096.

adapter.stream(prompt, options)

Stream a prompt execution.

FieldTypeDescription
promptPromptThe prompt to execute
options.modelstring | FallbackModel<string>Model name or fallback() wrapper
options.inputobjectInput values
options.toolsRecord<string, unknown>?Additional Crux tools to merge at call time
options.toolMiddlewareToolMiddleware | readonly ToolMiddleware[]?Tool execution hooks, including Crux resumable approvals through result.messages.
options.extra.toolsToolUnion[]?Anthropic-native tools that bypass Crux tool conversion
options.extra.tool_choiceToolChoice?Anthropic-native tool selection

Returns: Anthropic MessageStream (async iterable of MessageStreamEvent).

toMessages(sdkMessages)

Convert Anthropic MessageParam[] to canonical Message[].

FieldTypeDescription
sdkMessagesMessageParam[]Anthropic SDK messages

Returns: Message[]

fromMessages(messages)

Convert canonical Message[] to Anthropic MessageParam[].

FieldTypeDescription
messagesMessage[]Canonical messages

Returns: MessageParam[]

Anthropic has no tool role. Tool results are converted to user messages with tool_result content blocks.

anthropicTranscript is the lower-level NativeTranscriptCodec used by createAnthropic(). toMessages() and fromMessages() are provider-history converters, not a generic cross-provider transcript API; they delegate to the same Anthropic-owned codec that request assembly, assistant tool_use extraction, and second-call tool-loop payloads use.

Assistant tool calls become ordered tool_use content blocks. Tool-result content keeps native Anthropic image and PDF blocks where supported, falling back to deterministic text references for unsupported media.

createGenerateObjectFn(client, model)

Create a GenerateObjectFn bound to a client and model.

FieldTypeDescription
clientAnthropicAnthropic client instance
modelstringModel name

Returns: GenerateObjectFn

This is a provider-native helper generated from the same native chat profile as createAnthropic(). It uses Anthropic's structured parse surface and returns the parsed { object }, preserving provider errors. It does not run Crux prompt resolution, validation retry, safety, cassettes, tools, memory capture, or instrumentation. Use createGenerateObjectFnFromGenerate(generate) from @crux/core/compaction when a GenerateObjectFn needs full adapter runtime behavior.

createGenerateTextFn(client, model)

Create a GenerateTextFn bound to a client and model.

FieldTypeDescription
clientAnthropicAnthropic client instance
modelstringModel name

Returns: GenerateTextFn

This helper is also generated from the native chat profile, so text helper calls and adapter calls share request construction and response extraction.

Embeddings and rerankers

@crux/anthropic is a generation adapter. It does not export a provider-native embedding() helper because Anthropic's direct SDK surface here does not provide the dense embedding API that Crux needs for embedding().

When you want Anthropic for generation plus retrieval/indexing around it, use one of these patterns:

  • pair createAnthropic() with embedding() from @crux/ai
  • pair it with embedding() from @crux/openai or @crux/google for the write/query vector side
  • keep Anthropic as the chat model while another provider handles embeddings or reranking

For model-based reranking, use reranker() from @crux/ai.

Types

import type { AnthropicExtra, AnthropicRequest } from '@crux/anthropic'

On this page