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 | CanUseTool | 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 |
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" ) |
permissionPromptToolName | string | Name of MCP tool for permission prompts | undefined |
resume | string | Session ID to resume | undefined |
stderr | (data: string) => void | Callback for stderr output | undefined |
strictMcpConfig | boolean | Enforce strict MCP configuration validation | undefined |
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:
Custom tools via MCP
The Model Context Protocol (MCP) lets you give your agents custom tools and capabilities:
Custom tools using MCPs
You can implement custom tools using MCPs, for example here is how you can create a custom permission handling tool.
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