What it does
The Claude Code SDK lets you run Claude models in your application via the Anthropic API, with streaming, structured outputs, and vision support built in. It's a JavaScript/TypeScript wrapper around the Anthropic API that handles authentication, request formatting, message streaming, and response parsing. You can call Claude models directly from Node.js servers, edge functions, or browser environments—perfect for building AI features like code analysis, chat, content generation, or agent loops into your product.
When to use it
Use the Claude Code SDK when you're building a Node.js or browser-based app and want to query Claude models programmatically. It's a lightweight alternative to calling the REST API directly—the SDK handles marshalling requests, streaming responses, and error handling so you don't have to. Compare this to the Anthropic SDK (the official, bare-metal Python/Node client): the Claude Code SDK is purpose-built for frontend and edge deployments, with better defaults for streaming and type safety in TypeScript. If you're shipping a web app, bot, or edge function that calls Claude, reach for the Claude Code SDK. If you're building backend infrastructure in Python or need maximum control over every API parameter, the Anthropic SDK is the right choice.
Try it yourself
Install from npm and set your API key as an environment variable. Then create a simple client instance, pass a message, and log the response—you can try streaming mode by iterating over the stream chunks instead of waiting for the full response. The SDK's TypeScript types will guide you toward valid model IDs and message formats.
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
const message = await client.messages.create({
model: "claude-opus-4-8",
max_tokens: 1024,
messages: [
{ role: "user", content: "Explain quantum entanglement in one sentence." },
],
});
console.log(message.content[0].type === "text" && message.content[0].text);
For streaming, use client.messages.stream() instead and iterate over events:
const stream = await client.messages.stream({
model: "claude-haiku-4-5-20251001",
max_tokens: 256,
messages: [{ role: "user", content: "Count to 10." }],
});
for await (const chunk of stream) {
if (
chunk.type === "content_block_delta" &&
chunk.delta.type === "text_delta"
) {
process.stdout.write(chunk.delta.text);
}
}
Gotchas
API key exposure: the SDK needs your ANTHROPIC_API_KEY to authenticate. Never hardcode it or commit it to git—always load it from environment variables or a secrets manager. In browser code, you cannot use the API key directly (it would be visible to users); instead, proxy Claude calls through your backend via a route handler that holds the key server-side.
Streaming doesn't return a full message object: when you use .stream(), you get an event stream, not the final message object with token counts and stop reason. If you need those metadata fields, either use the non-streaming create() call, or listen for the message_stop event in the stream and extract the stop reason from there.
Model names change: this guide was written with current models like claude-opus-4-8 and claude-haiku-4-5-20251001. Always check the docs for the latest available models—copy the exact model ID from the reference to avoid "not found" errors.
Costs add up fast: each API call costs money based on input and output tokens. Streaming doesn't reduce cost (you pay for tokens either way), but it does reduce latency and lets you start showing results to users while Claude is still thinking. For high-volume apps, always rate-limit and validate user input before sending to Claude.
Try it yourself
Type the command in the fake terminal. Nothing leaves your browser.