Build custom AI agents with the Claude Code SDK
Install the SDK
@anthropic-ai/claude-code
from NPM:Set your API key
ANTHROPIC_API_KEY
environment variable:Create your first agent
Run the agent
@anthropic-ai/claude-code
from NPMclaude
command. Use the --print
(or -p
) flag to run in non-interactive mode and print the final result:Flag | Description | Example |
---|---|---|
--print , -p | Run in non-interactive mode | claude -p "query" |
--output-format | Specify output format (text , json , stream-json ) | claude -p --output-format json |
--resume , -r | Resume a conversation by session ID | claude --resume abc123 |
--continue , -c | Continue the most recent conversation | claude --continue |
--verbose | Enable verbose logging | claude --verbose |
--append-system-prompt | Append to system prompt (only with --print ) | claude --append-system-prompt "Custom instruction" |
--allowedTools | Space-separated list of allowed tools, or string of comma-separated list of allowed tools | claude --allowedTools mcp__slack mcp__filesystem claude --allowedTools "Bash(npm install),mcp__filesystem" |
--disallowedTools | Space-separated list of denied tools, or string of comma-separated list of denied tools | claude --disallowedTools mcp__splunk mcp__github claude --disallowedTools "Bash(git commit),mcp__github" |
--mcp-config | Load MCP servers from a JSON file | claude --mcp-config servers.json |
--permission-prompt-tool | MCP tool for handling permission prompts (only with --print ) | claude --permission-prompt-tool mcp__auth__prompt |
ANTHROPIC_API_KEY
environment variable, as demonstrated in the Quick start.
CLAUDE_CODE_USE_BEDROCK=1
environment variable and configure AWS credentialsCLAUDE_CODE_USE_VERTEX=1
environment variable and configure Google Cloud credentials--allowedTools
flag. MCP tool names follow the pattern mcp__<serverName>__<toolName>
where:serverName
is the key from your MCP configuration filetoolName
is the specific tool provided by that servermcp__<serverName>
), all tools from that server will be allowed.Glob patterns (e.g., mcp__go*
) are not supported.--permission-prompt-tool
to pass in an MCP tool that we will use to check whether or not the user grants the model permissions to invoke a given tool. When the model invokes a tool the following happens:
--allowedTools
and --disallowedTools
passed into the SDK; if one of these allows or denies the tool call, we proceed with the tool call--permission-prompt-tool
--permission-prompt-tool
MCP tool is passed the tool name and input, and must return a JSON-stringified payload with the result. The payload must be one of:
updatedInput
to tell the model that the permission prompt mutated its input; otherwise, set updatedInput
to the original input, as in the example above. For example, if the tool shows a file edit diff to the user and lets them edit the diff manually, the permission prompt tool should return that updated edit.init
system message, followed by a list of user and assistant messages, followed by a final result
system message with stats. Each message is emitted as a separate JSON object.
Message
and MessageParam
types are available in Anthropic SDKs. For example, see the Anthropic TypeScript and Python SDKs.
stdin
where each message represents a user turn. This allows multiple turns of a conversation without re-launching the claude
binary and allows providing guidance to the model while it is processing a request.
Each message is a JSON ‘User message’ object, following the same format as the output message schema. Messages are formatted using the jsonl format where each line of input is a complete JSON object. Streaming JSON input requires -p
and --output-format stream-json
.
Currently this is limited to text-only user messages.