# Anthropic SDK (/docs/guides/sdks/anthropic)

Your `@anthropic-ai/sdk` client keeps its methods, its stream helpers and its error classes. It takes the options of your `AiConnect` client, and every call then answers through the visitor's wallet, on the account the visitor picked for your site. That account can be a Claude plan, a ChatGPT plan, a DeepSeek key or any other account the wallet holds. The [AI SDK guide](/docs/guides/existing-sdks) explains how this works for every SDK.

## Install [#install]

```bash
npm install @anthropic-ai/sdk @tensorpass/aiconnect
```

Until `@tensorpass/aiconnect` is published on npm, the [JavaScript quickstart](/docs/quickstart/javascript#install-the-package) shows how to point your project at a checkout of the repository.

## The change [#the-change]

```ts
// Before
const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });

// After
const client = new Anthropic(aiconnect.sdkOptions);
```

`aiconnect` is your `AiConnect` client, and `sdkOptions` is `{ apiKey: 'aiwallet', fetch: aiconnect.fetch, dangerouslyAllowBrowser: true }`. The SDK requires a key, so the placeholder `aiwallet` fills it, and the wallet never reads it. `dangerouslyAllowBrowser` lifts the SDK's guard against real keys in a page, and no key reaches the page here.

The call runs in the page, where the wallet answers. Show your connect entry point first, as the [React quickstart](/docs/quickstart/react) does: a request from a site the visitor has not connected fails with `Anthropic.AuthenticationError`.

## Messages with tools [#messages-with-tools]

The bookshop demo in `apps/demo` of the AI Connect repository runs this loop in its SDK mode. `search_books` stands for your own tools, `runTool` runs one and returns its result as text, and `showText` writes the answer as it streams.

```ts
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic(aiconnect.sdkOptions);
const messages: Anthropic.MessageParam[] = [{ role: 'user', content: question }];
for (let step = 0; step < 4; step += 1) {
  const stream = client.messages.stream(
    { model: 'claude-sonnet-5-5', max_tokens: 2048, system, messages, tools },
    { signal },
  );
  stream.on('text', (delta) => showText(delta));
  const message = await stream.finalMessage();
  messages.push({ role: 'assistant', content: message.content });
  const uses = message.content.filter((block) => block.type === 'tool_use');
  if (!uses.length) {
    break;
  }
  const results: Anthropic.ToolResultBlockParam[] = [];
  for (const use of uses) {
    results.push({ type: 'tool_result', tool_use_id: use.id, content: await runTool(use.name, use.input) });
  }
  messages.push({ role: 'user', content: results });
}
```

`tools` holds your tools in the Anthropic format, `{ name, description, input_schema }`. When they live on your MCP server, the [MCP guide](/docs/guides/mcp-tools#with-an-ai-sdk) shows how to read them in the page. `max_tokens` reaches every account that takes a limit, and `tool_choice` reaches the account as you set it. Claude Opus 5.5, Claude Sonnet 5.5 and Claude Fable 5.1 refuse a forced choice (`any` or a named tool), and their error comes back to the SDK.

## Images and structured output [#images-and-structured-output]

An image block with a `base64` source reaches every account. A `url` source reaches the accounts whose provider fetches images, and the Gemini plan refuses it. A tool result carries text only, so an image inside a `tool_result` is refused with a 400.

A JSON schema in `output_config.format` reaches the account in its own provider's format:

```ts
const message = await client.messages.create({
  model: 'claude-sonnet-5-5',
  max_tokens: 1024,
  messages: [{ role: 'user', content: question }],
  output_config: { format: { type: 'json_schema', schema: pickSchema } },
});
const block = message.content.find((part) => part.type === 'text');
const pick = JSON.parse(block?.text ?? '{}');
```

The thinking settings, `thinking` and `output_config.effort`, are not passed on: the visitor sets the thinking effort in the wallet. Thinking blocks you send back in an assistant turn are left out.

## The model that answers [#the-model-that-answers]

The `model` you pass is ignored, since the visitor chooses the model in the wallet. `message_start` carries the model of the account that answers, so `message.model` names it. The [model names section](/docs/guides/existing-sdks#model-names) tells which event carries which name.

## Errors and retries [#errors-and-retries]

A failure before any text throws the SDK's own error class for the wallet's status: `Anthropic.AuthenticationError` when the site is not connected, `Anthropic.PermissionDeniedError` when the visitor declined or left the wallet locked, `Anthropic.RateLimitError` when the account's plan is spent. A failure after the text started arrives as the stream's error event. The [errors section](/docs/guides/existing-sdks#errors) lists every status.

The SDK retries a 429 and the 5xx statuses twice. On Auto the wallet already moves to the visitor's next account, so you can answer sooner:

```ts
const client = new Anthropic({ ...aiconnect.sdkOptions, maxRetries: 0 });
```

Aborting the `signal`, or calling `stream.abort()`, stops the answer in the wallet.

## What a request can carry [#what-a-request-can-carry]

Documents, files and Anthropic's own server tools, such as its web search or code execution, are refused with a 400. The [AI SDK guide](/docs/guides/existing-sdks#what-the-wallet-takes-from-a-request) lists what the wallet passes on to each account.
