Skip to Content
ProvidersUse Anthropic with Modem

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 { ServiceProvider } from '@preternatural-js/client-sdk'

Non-streaming completion

const result = await client.chat.complete({ scope: { provider: ServiceProvider.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: ServiceProvider.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

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 { ServiceProvider } from '@preternatural-js/client-sdk' const model = 'claude-sonnet-4-5-20250929' const gateway = await client.gateway.providerConfiguration({ provider: ServiceProvider.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, }) 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.