Registry / llm-agents / %40anthropic-ai/sdk

%40anthropic-ai/sdk

JSON →
library0.90.0jsnpmunverified

The official TypeScript and Node.js SDK for the Anthropic API, providing seamless access to the Claude model family, including Claude 3.5 Sonnet, Haiku, and the Claude Opus 4.6 series. The SDK features a unified architecture, native streaming, structured data handling, and full support for advanced features like Prompt Caching, Tool Use (Function Calling), Computer Use, and the Model Context Protocol (MCP). It is optimized for server-side JavaScript runtimes (Node.js, Deno, Bun, Cloudflare Workers). It acts as the standard method for interacting with Anthropic's APIs, abstracting raw HTTP calls while providing robust type definitions and automatic retries.

llm-agentsai-mlhttp-networking
npm install @anthropic-ai/sdk
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Anthropic
import Anthropic from '@anthropic-ai/sdk'; const anthropic = new Anthropic();
import { Anthropic } from '@anthropic-ai/sdk'; const anthropic = new AnthropicApi();
The SDK exports the Anthropic client as a default export. Destructuring or using non-existent named exports like AnthropicApi will fail in ESM environments.
AWS Bedrock / Google Vertex AI
import { AnthropicBedrock } from '@anthropic-ai/bedrock-sdk'; import { AnthropicVertex } from '@anthropic-ai/vertex-sdk';
import { AnthropicBedrock } from '@anthropic-ai/sdk';
If you are using Anthropic models through AWS Bedrock or Google Cloud Vertex AI, you must install and import from their specific companion packages (@anthropic-ai/bedrock-sdk or @anthropic-ai/vertex-sdk), not the standard @anthropic-ai/sdk package.
MessageParam
import Anthropic from '@anthropic-ai/sdk'; const msg: Anthropic.MessageParam = { role: 'user', content: '...' };
import { MessageParam } from '@anthropic-ai/sdk';
Anthropic strictly namespaces its types under the default export object. It is highly recommended to use the namespace pattern (Anthropic.MessageParam) rather than attempting to destructure type definitions directly from the root.

A basic Messages API request using the claude-opus-4-6 model, illustrating the required max_tokens parameter and the correct placement of the system prompt.

import Anthropic from '@anthropic-ai/sdk'; const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY, }); async function main() { const message = await anthropic.messages.create({ model: 'claude-opus-4-6', max_tokens: 1024, system: 'You are a helpful and concise assistant.', messages: [{ role: 'user', content: 'Explain quantum computing in one sentence.' }], }); console.log(message.content[0].type === 'text' ? message.content[0].text : 'Non-text response'); } main();
Debug
Known issues
breakingVersion 0.89.0 marks standard Sonnet 4 and Opus 4 strings as deprecated in favor of more specific point-releases like `claude-opus-4-6` and `claude-opus-4-7`. Ensure your model strings match the currently supported availability list.
fix
Update model strings in your code to explicitly point to supported versioned model strings (e.g., `claude-opus-4-6`).
affects: >=0.89.0
gotchaThe `system` prompt is a top-level parameter in the Messages API, not a message role. If migrating from OpenAI, do not include `{ role: 'system', content: '...' }` inside the messages array, as this will trigger a validation error.
fix
Extract the system instructions and assign them directly to the `system` property at the root of the `create()` method configuration object.
affects: All
gotchaAttempting to use the SDK in a browser environment will intentionally throw an error by default to prevent leaking API keys to the client side.
fix
Pass `dangerouslyAllowBrowser: true` to the constructor ONLY if you are absolutely sure about the security implications or are securely proxying requests.
affects: All
Errors
Common errors & fixes
400 Bad Request: max_tokens: required field
Unlike OpenAI, which often defaults to infinity or a specific model limit, the Anthropic Messages API strictly requires the developer to explicitly define the maximum number of tokens to generate for every request.
fix
Add the `max_tokens` parameter to your request body, ensuring it is an integer up to the model's maximum output limit (e.g., `max_tokens: 4096`).
400 Bad Request: roles must alternate between 'user' and 'assistant'
Anthropic's Messages API enforces strict conversational turn-taking. You cannot send two 'user' messages sequentially, start a conversation with an 'assistant' message, or leave trailing 'assistant' messages without specific pre-fill techniques.
fix
Combine sequential 'user' messages into a single message object, or ensure your messages array strictly alternates the sequence: `user -> assistant -> user`.
Error: It looks like you're running in a browser-like environment. This is disabled by default
The SDK detected a browser environment (like React, Vue, or Next.js client-components). Anthropic blocks this to prevent developers from accidentally exposing their secret `ANTHROPIC_API_KEY` in client-side JavaScript bundles.
fix
Move the API call to a backend Node.js server or a serverless API route. If you must run it in the client for testing, instantiate the client with `new Anthropic({ dangerouslyAllowBrowser: true })`.
Upgrade
Version history
0.90.0latest on PyPI
Audit
Dependencies

No dependency data recorded yet.

Agent activity
0 hits · last 30 days

No traffic data recorded yet.

Resources