TypeScript
Build custom AI agents with the Claude Code TypeScript SDK
Prerequisites
- Node.js 18+
Installation
Install @anthropic-ai/claude-code
from NPM:
To view the TypeScript SDK source code, visit the @anthropic-ai/claude-code
page on NPM.
Basic usage
The primary interface via the TypeScript SDK is the query
function, which returns an async iterator that streams messages as they arrive:
Configuration options
Argument | Type | Description | Default |
---|---|---|---|
abortController | AbortController | Abort controller for cancelling operations | new AbortController() |
additionalDirectories | string[] | Additional directories to include in the session | undefined |
allowedTools | string[] | List of tools that Claude is allowed to use | All tools enabled by default |
appendSystemPrompt | string | Text to append to the default system prompt | undefined |
canUseTool | (toolName: string, input: any) => Promise<ToolPermissionResult> | Custom permission function for tool usage | undefined |
continue | boolean | Continue the most recent session | false |
customSystemPrompt | string | Replace the default system prompt entirely | undefined |
cwd | string | Current working directory | process.cwd() |
disallowedTools | string[] | List of tools that Claude is not allowed to use | undefined |
env | Dict<string> | Environment variables to set | undefined |
executable | 'bun' | 'deno' | 'node' | Which JavaScript runtime to use | node when running with Node.js, bun when running with Bun |
executableArgs | string[] | Arguments to pass to the executable | [] |
fallbackModel | string | Model to use if primary model fails | undefined |
hooks | Partial<Record<HookEvent, HookCallbackMatcher[]>> | Lifecycle hooks for customization | undefined |
includePartialMessages | boolean | Include partial streaming events in the message stream | false |
maxThinkingTokens | number | Maximum tokens for Claude’s thinking process | undefined |
maxTurns | number | Maximum number of conversation turns | undefined |
mcpServers | Record<string, McpServerConfig> | MCP server configurations | undefined |
model | string | Claude model to use | Uses default from CLI configuration |
pathToClaudeCodeExecutable | string | Path to the Claude Code executable | Executable that ships with @anthropic-ai/claude-code |
permissionMode | PermissionMode | Permission mode for the session | "default" (options: "default" , "acceptEdits" , "bypassPermissions" , "plan" ) |
resume | string | Session ID to resume | undefined |
stderr | (data: string) => void | Callback for stderr output | undefined |
strictMcpConfig | boolean | Enforce strict MCP configuration validation | undefined |
Partial message streaming
When includePartialMessages
is enabled, the SDK will emit stream_event
messages that contain raw streaming events from the Claude API. This allows you to access partial content as it’s being generated, useful for implementing real-time UI updates or progress indicators.
Each stream_event
message includes:
event
: The raw streaming event from the APIsession_id
: The current session identifierparent_tool_use_id
: The ID of the tool being executed (if applicable)uuid
: A unique identifier for this event
Partial message streaming is primarily useful for advanced use cases where you need granular control over the streaming response. For most applications, the default behavior (waiting for complete messages) is sufficient.
Multi-turn conversations
For multi-turn conversations, you have two options.
You can generate responses and resume them, or you can use streaming input mode which accepts an async/generator for an array of messages. For now, streaming input mode is the only way to attach images via messages.
Resume with session management
Streaming input mode
Streaming input mode allows you to provide messages as an async iterable instead of a single string. This enables multi-turn conversations, image attachments, and dynamic message generation:
Streaming input with images
Streaming input mode is the only way to attach images via messages:
Custom system prompts
System prompts define your agent’s role, expertise, and behavior:
MCP Server Integration
The Model Context Protocol (MCP) lets you give your agents custom tools and capabilities:
Custom tools with in-process MCP servers
SDK MCP servers allow you to create custom tools that run directly in your application process, providing type-safe tool execution without the overhead of separate processes or network communication.
Creating custom tools
Use the createSdkMcpServer
and tool
helper functions to define type-safe custom tools:
Type safety with Zod
The tool
helper provides full TypeScript type inference from your Zod schemas:
Hooks
Hooks allow you to customize and extend Claude Code’s behavior by running custom callbacks at various points in the agent’s lifecycle. Unlike CLI hooks that execute bash commands, SDK hooks are JavaScript/TypeScript functions that run in-process.
Defining hooks
Hooks are organized by event type, with optional matchers to filter when they run:
Available hook events
- PreToolUse: Runs before tool execution. Can block tools or provide feedback.
- PostToolUse: Runs after successful tool execution.
- UserPromptSubmit: Runs when user submits a prompt.
- SessionStart: Runs when a session starts.
- SessionEnd: Runs when a session ends.
- Stop: Runs when Claude is about to stop responding.
- SubagentStop: Runs when a subagent is about to stop.
- PreCompact: Runs before conversation compaction.
- Notification: Runs when notifications are sent.
Hook input types
Each hook receives typed input based on the event:
Hook output
Hooks return output that controls execution flow:
Practical examples
Logging and monitoring
File operation validation
Auto-formatting code
Prompt enhancement
Custom compaction instructions
Hook execution behavior
- Parallelization: All matching hooks run in parallel
- Timeout: Hooks respect the abort signal from options
- Error handling: Hook errors are logged but don’t stop execution
- Matchers: Support regex patterns (e.g.,
"Write|Edit"
)
Combining hooks with canUseTool
While canUseTool
provides permission control, hooks offer broader lifecycle integration:
Permission control with canUseTool
The canUseTool
callback provides fine-grained control over tool execution. It’s called before each tool use and can allow, deny, or modify tool inputs:
Use cases for canUseTool
- Permission management: Check user permissions before allowing tool execution
- Input validation: Validate or sanitize tool inputs before execution
- Rate limiting: Implement rate limits for expensive operations
- Audit logging: Log tool usage for compliance or debugging
- Dynamic permissions: Enable/disable tools based on runtime conditions
Output formats
Text output (default)
JSON output
Input formats
Agent integration examples
SRE incident response agent
Automated security review
Multi-turn legal assistant
Message schema
Messages returned from the JSON API are strictly typed according to the following schema:
Additional supporting types:
Message
, MessageParam
, and Usage
types are available in the Anthropic TypeScript SDK.
Related resources
- CLI usage and controls - Complete CLI documentation
- GitHub Actions integration - Automate your GitHub workflow with Claude
- Common workflows - Step-by-step guides for common use cases