The Claude Code SDK
Learn to use the Claude Code SDK to run Claude programmatically from scripts, CI/CD pipelines, and custom tools. Covers TypeScript and Python usage, permissions, and practical applications.
The Claude Code SDK lets you run Claude Code programmatically from within your own applications, scripts, and automation pipelines. Available for TypeScript, Python, and the CLI, it gives you the same powerful AI coding capabilities you use in the terminal — but integrated into any workflow you can imagine.
What the SDK Provides
The SDK runs the exact same Claude Code you use interactively. It has access to all the same tools, inherits settings from any Claude Code configuration in the current directory, and can read, search, and (with permission) modify your codebase.
- Same functionality as the terminal version of Claude Code
- Inherits all settings from Claude Code instances in the same directory
- Read-only permissions by default for safety
- Available in TypeScript, Python, and as a CLI tool
- Streams results message-by-message for real-time processing
Basic TypeScript Usage
Here is a simple example that asks Claude Code to analyze a directory for duplicate code:
1import { query } from "@anthropic-ai/claude-code";23const prompt = "Look for duplicate queries in the ./src/queries directory";45for await (const message of query({ prompt })) {6 console.log(JSON.stringify(message, null, 2));7}When you run this script, you see the raw conversation between your local Claude Code and the Claude model, streamed message by message. The final message contains Claude's complete analysis.
Configuring Permissions
By default, the SDK only has read-only permissions — it can read files, search directories, and run grep, but cannot write, edit, or create files. To enable write access, explicitly grant tools:
1for await (const message of query({2 prompt: "Refactor the utils module to use named exports",3 options: {4 allowedTools: ["Edit", "Write"]5 }6})) {7 console.log(JSON.stringify(message, null, 2));8}Be explicit about permissions
The read-only default is intentional. When running Claude Code programmatically — especially in automated pipelines — you want tight control over what it can modify. Only grant the specific tools each script needs.
Practical Applications
The SDK unlocks powerful automation scenarios that go far beyond interactive coding:
- Git hooks — automatically review staged changes before each commit
- Build scripts — analyze and optimize code during the build process
- CI/CD pipelines — run code quality checks with AI-powered analysis
- Documentation generation — automatically create or update docs from code changes
- Code maintenance — scheduled scripts that find and report technical debt
- Custom development tools — build team-specific CLI tools powered by Claude
The SDK essentially lets you add AI-powered intelligence to any part of your development process. Combined with hooks, MCP servers, and GitHub integration, it forms the foundation for a fully automated, AI-augmented development workflow.
Key Takeaways
- 01The Claude Code SDK runs Claude programmatically from TypeScript, Python, or the CLI.
- 02It provides the same capabilities as interactive Claude Code, including all tools and settings.
- 03Read-only by default — explicitly grant write permissions when needed.
- 04Ideal for git hooks, CI/CD pipelines, automated code review, and custom development tools.
- 05Combined with hooks and MCP servers, the SDK enables fully automated AI-augmented workflows.