Skip to Content

Anthropic

Send Anthropic requests with the Client SDK’s chat methods, which use the OpenAI-compatible API, or with the Anthropic SDK, which uses Anthropic’s Messages API.

Using the Client SDK

import { ExternalServiceProvider } from '@preternatural-js/client-sdk'

Non-streaming completion

const result = await client.chat.complete({ scope: { provider: ExternalServiceProvider.anthropic, modelIdentifier: 'claude-sonnet-4-5-20250929', productId: 'your-product-id', }, messages: [ { role: 'user', content: 'Explain quantum computing in one paragraph.', }, ], maxTokens: 500, temperature: 0.7, }) const message = result.choices[0]?.message.content

client.chat.complete() returns an OpenAI-compatible ChatCompletion. maxTokens defaults to 1000.

Streaming completion

const text = await client.chat.stream( { scope: { provider: ExternalServiceProvider.anthropic, modelIdentifier: 'claude-sonnet-4-5-20250929', productId: 'your-product-id', }, messages: [ { role: 'user', content: 'Write a short story about a lunar gardener.', }, ], maxTokens: 500, temperature: 0.7, }, (chunk) => { console.log(chunk) }, )

client.chat.stream() passes each piece of text to the callback and resolves to the whole text.

More info

Using the Anthropic SDK

Install the provider SDK with bun add @anthropic-ai/sdk. Get the gateway configuration from the Client SDK and pass it to the Anthropic client. Its Anthropic baseURL has no /v1, because the Anthropic SDK adds /v1/messages.

import Anthropic from '@anthropic-ai/sdk' import { ExternalServiceProvider } from '@preternatural-js/client-sdk' const model = 'claude-sonnet-4-5-20250929' const gateway = await client.gateway.providerConfiguration({ provider: ExternalServiceProvider.anthropic, modelIdentifier: model, productId: 'your-product-id', }) const anthropic = new Anthropic({ apiKey: null, authToken: 'PRETERNATURAL_GATEWAY_AUTHENTICATES_REQUESTS', baseURL: gateway.baseURL, defaultHeaders: gateway.headers, fetch: gateway.fetchImpl, maxRetries: 0, dangerouslyAllowBrowser: true, // Only needed when running in a browser. }) const message = await anthropic.messages.create({ model, max_tokens: 500, messages: [ { role: 'user', content: 'Explain quantum computing in one paragraph.', }, ], }) const text = message.content .filter((block) => block.type === 'text') .map((block) => block.text) .join('')

The Client SDK’s headers authenticate the request. authToken only satisfies the Anthropic SDK and is not an Anthropic API key. maxRetries: 0 matches the Client SDK, because a retried request can be billed twice.

API keys and Claude subscriptions

This page describes an Anthropic API-key account. A Claude subscription uses a native relay and its generated setup, not an API key pasted into this client. Do not infer subscription quotas, signed-thinking portability or credit permission from the API-key example.

The Client SDK chat helper exposes basic text completion fields. Use the native Messages client for provider-specific content/tool fields, and verify the selected model supports them. A protocol adapter does not guarantee every provider feature is portable.

The official Anthropic SDK  requires explicit browser opt-in. Here the request carries customer-scoped Modem headers and a placeholder auth token; never substitute a secret Anthropic API key in browser code. Omit the browser option in server-only code.