Anthropic
Chat completions using Anthropic models can be created with:
- the chat completion methods in
@preternatural-js/client-sdk - the official Anthropic SDK, configured with the Client SDK’s gateway settings
Using the Client SDK
import { ServiceProvider } from '@preternatural-js/client-sdk'Non-streaming completion
Set streamMode to false for a non-streaming chat completion.
const result = await client.streamChat({
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,
streamMode: false,
})
if (typeof result === 'string') {
throw new Error('Expected a non-streaming chat completion')
}
const message = result.choices[0]?.message.contentWhen streamMode is false, streamChat() returns an OpenAI-compatible ChatCompletion object.
Anthropic requires a maximum output-token value, so include maxTokens in the request.
Streaming completion
Set streamMode to true and provide a callback to process generated text as it arrives.
const text = await client.streamChat(
{
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,
streamMode: true,
},
(chunk) => {
console.log(chunk)
},
)The callback receives each generated text chunk. The returned promise resolves to the complete generated text after the stream finishes.
More info
Using the Anthropic SDK
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 gateway headers authenticate the request. The authToken value satisfies the Anthropic SDK’s authentication requirement; it is not an Anthropic API key.