@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.
| Field | Type | Description |
|---|---|---|
client | Anthropic | The Anthropic client instance |
Returns:
| Method | Description |
|---|---|
.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.usageadapter.generate(prompt, options)
| Field | Type | Description |
|---|---|---|
prompt | Prompt | The prompt to execute |
options.model | string | FallbackModel<string> | Model name or fallback() wrapper |
options.input | object | Input values |
options.tools | Record<string, unknown>? | Additional Crux tools to merge at call time |
options.toolMiddleware | ToolMiddleware | readonly ToolMiddleware[]? | Tool execution hooks, including Crux resumable approvals through result.messages. |
options.extra.tools | ToolUnion[]? | Anthropic-native tools that bypass Crux tool conversion |
options.extra.tool_choice | ToolChoice? | Anthropic-native tool selection |
Returns: normalized Crux generate result with:
result.text— extracted assistant textresult.raw— raw Anthropic SDK responseresult._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.
| Field | Type | Description |
|---|---|---|
prompt | Prompt | The prompt to execute |
options.model | string | FallbackModel<string> | Model name or fallback() wrapper |
options.input | object | Input values |
options.tools | Record<string, unknown>? | Additional Crux tools to merge at call time |
options.toolMiddleware | ToolMiddleware | readonly ToolMiddleware[]? | Tool execution hooks, including Crux resumable approvals through result.messages. |
options.extra.tools | ToolUnion[]? | Anthropic-native tools that bypass Crux tool conversion |
options.extra.tool_choice | ToolChoice? | Anthropic-native tool selection |
Returns: Anthropic MessageStream (async iterable of MessageStreamEvent).
toMessages(sdkMessages)
Convert Anthropic MessageParam[] to canonical Message[].
| Field | Type | Description |
|---|---|---|
sdkMessages | MessageParam[] | Anthropic SDK messages |
Returns: Message[]
fromMessages(messages)
Convert canonical Message[] to Anthropic MessageParam[].
| Field | Type | Description |
|---|---|---|
messages | Message[] | 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.
| Field | Type | Description |
|---|---|---|
client | Anthropic | Anthropic client instance |
model | string | Model 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.
| Field | Type | Description |
|---|---|---|
client | Anthropic | Anthropic client instance |
model | string | Model 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()withembedding()from@crux/ai - pair it with
embedding()from@crux/openaior@crux/googlefor 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'Related
- Guide: Execution
- Reference: Prompts
- Reference: @crux/ai (Vercel AI SDK)