createAnthropicProvider
import { createAnthropicProvider } from '@rudra-js/anthropic';Call createAnthropicProvider to connect the generator to Claude through the Anthropic API.
createAnthropicProvider(options: AnthropicProviderOptions) => ComponentProviderReference
createAnthropicProvider(options)
Parameters
options: Your API key, and settings for the calls.apiKey: Your Anthropic API key. No rudra-js package reads the environment, so pass it from wherever you keep secrets.modeloptional: The Claude model to call. Defaults to'claude-sonnet-5'.maxTokensoptional: The most tokens the model may write in one answer. Defaults to8192.baseUrloptional: Where the calls go. Defaults tohttps://api.anthropic.com. Set it to send them through your own proxy, a gateway or a regional endpoint. A gateway someone else runs sees every request and yourapiKey.workspaceIdoptional: The Anthropic workspace the calls act in, sent as theanthropic-workspace-idheader. An identity-linked API key needs it.thinkingoptional: Whether the model reasons before it answers. Defaults to{ type: 'disabled' }. Pass{ type: 'adaptive' }to let the model decide. Passnullto send nothinkingfield, for a model that rejects the default.fetchoptional: Thefetchthe calls go through. Defaults to the globalfetch. Pass your own to test without the network.
Returns
A ComponentProvider to pass to createComponentGenerator as provider. Its name is 'anthropic'. Its model is the model it calls.
Caveats
- When you pick another model or turn
thinkingon, raisemodelTimeoutMsto match. A model that reasons before it answers won't finish inside the default. Each request that runs out of time gets the block core builds without a model. - Read Running it in a regulated shop before you send real shopper traffic. It lists what each call carries.
- When the API answers with an error, the thrown error holds only the status and the error type, such as
anthropic responded 400 (invalid_request_error). It leaves out the API's own message, which can quote a shopper's searches.
Usage
Picking another Claude model
Pass model, and give the generator a modelTimeoutMs that fits it.
import { createAnthropicProvider } from '@rudra-js/anthropic';
import { createComponentGenerator } from '@rudra-js/core';
export const generator = createComponentGenerator({
provider: createAnthropicProvider({
apiKey: process.env.ANTHROPIC_API_KEY!,
model: 'claude-opus-5',
}),
modelTimeoutMs: 5_000,
});With Claude Opus 5.5, Sonnet 5.5, Fable 5.1 or Mythos 5.1, also pass thinking: null.
Time a few calls to the model you picked, then set modelTimeoutMs above them.
Log event.source to check that the model answered.
Troubleshooting
400 … anthropic-workspace-id is required
The API answers this way when an identity-linked key doesn't say which workspace a call is for. In event.error it shows as anthropic responded 400 (invalid_request_error).
A key you made against your identity, rather than inside a workspace, can act in several workspaces. Pass workspaceId to pick one, or create the key inside a workspace.
import { createAnthropicProvider } from '@rudra-js/anthropic';
import { createComponentGenerator } from '@rudra-js/core';
export const generator = createComponentGenerator({
provider: createAnthropicProvider({
apiKey: process.env.ANTHROPIC_API_KEY!,
workspaceId: process.env.ANTHROPIC_WORKSPACE_ID!,
}),
});source is always fallback
Log event.degradedReason and event.error, then start with the generator's troubleshooting.
A model that rejects thinking: { type: 'disabled' } fails every call. Claude Opus 5.5, Sonnet 5.5, Fable 5.1 and Mythos 5.1 do. Pass thinking: null with them. They reason before they answer, so raise modelTimeoutMs too.
anthropic returned no emit_component_spec tool use means the model answered in plain text instead of calling the tool. That request gets the block core builds without a model.
anthropic stopped at the max_tokens budget means the answer ran past maxTokens. Raise it.