Skip to main content
The Claude Code SDK lets you drive Claude Code programmatically from TypeScript or JavaScript. Use it to build automation pipelines, embed Claude Code inside your own tools, or manage multi-turn sessions from code.
Functions prefixed with unstable_v2_ are marked @alpha and may change between releases without a major version bump.

Installation

Core functions

query() — one-shot query

The primary entry point for driving Claude Code. Returns an async iterable of SDK messages.

unstable_v2_prompt() — one-shot convenience function

A simpler wrapper around query() for single prompts. Returns the final SDKResultMessage directly.

unstable_v2_createSession() — persistent session

Create a persistent session for multi-turn conversations. The session object provides a send() method that returns an async iterable of messages for each turn.

unstable_v2_resumeSession() — resume by ID

Resume an existing session using its UUID. Conversation history is loaded from the transcript file.

Session management functions

Returns sessions with metadata. Provide dir to scope to a project directory, or omit it to list sessions across all projects. Supports pagination via limit and offset.

Custom tools

Use tool() to define custom tools that Claude can call during a session. Tools are typed using Zod schemas.

tool() signature

In-process MCP server

createSdkMcpServer() wraps one or more tool() definitions into an in-process MCP server that runs in the same Node.js process as your code. This avoids the overhead of spawning a separate MCP process.
If your in-process MCP tool calls will take longer than 60 seconds, set the CLAUDE_CODE_STREAM_CLOSE_TIMEOUT environment variable to a higher value (in milliseconds).

Session lifecycle

The result message is always the last message in each turn and contains:

Additional session operations