← Claude Docs

Run Claude Code programmatically - Claude Code Docs

Claude Docs · April 8, 2026
The Agent SDK gives you the same tools, agent loop, and context management that power Claude Code. It’s available as a CLI for scripts and CI/CD, or as Python and TypeScript packages for full programmatic control. To run Claude Code programmatically from the

Detailed Analysis

Anthropic's Claude Code Agent SDK represents a significant expansion of the Claude Code platform beyond its interactive terminal interface, enabling developers to integrate Claude's full agentic capabilities directly into scripts, CI/CD pipelines, and production software systems. The SDK exposes the same underlying tools, agent loop, and context management that power the interactive Claude Code experience through three distinct interfaces: a CLI flag (`-p` or `--print`) for shell-based automation, and Python and TypeScript packages for native programmatic control. The CLI headless mode supports the full range of Claude Code's tool suite — including Bash execution, file reading and editing, glob searches, grep, directory listing, and sub-agent task delegation — while offering structured output formats including plain text, JSON with session metadata, and streaming newline-delimited JSON for real-time token processing.

A particularly notable architectural decision in the SDK is the introduction of "bare mode," activated via the `--bare` flag, which strips away all ambient context discovery that would otherwise occur in an interactive session. Without bare mode, `claude -p` loads hooks, skills, plugins, MCP servers, auto memory, and `CLAUDE.md` files from both the working directory and the user's home configuration — meaning a script run on one developer's machine could behave differently than the same script run in CI, depending on what tools and configurations happen to be present locally. Bare mode enforces hermetic execution: only explicitly passed flags take effect, and OAuth and keychain reads are skipped entirely, requiring authentication to come from environment variables or an inline settings object. This design directly addresses one of the most persistent challenges in agentic AI integration — reproducibility — by giving platform engineers a clear mechanism to guarantee consistent behavior across heterogeneous environments.

The SDK's output-format system reveals Anthropic's intent to position Claude Code as a composable infrastructure component rather than a standalone developer tool. The `--json-schema` flag allows callers to specify exact output structures conforming to JSON Schema definitions, transforming Claude from a generative text producer into a structured data extraction engine that can slot into data pipelines and API workflows. The streaming JSON format, combined with the `api_retry` event system — which surfaces attempt count, retry delay, HTTP status codes, and error categories — gives operators observability into Claude's network behavior at a granular level, enabling custom backoff logic and retry instrumentation. The permission mode system (`dontAsk`, `acceptEdits`) further refines the trust model, allowing operators to define graduated levels of autonomy appropriate to different deployment contexts, from fully locked-down read-only analysis to file-writing automation.

These capabilities connect to a broader industry movement toward what is increasingly called "agentic infrastructure" — the engineering discipline of deploying AI models not as query-response endpoints but as autonomous agents embedded in software systems with persistent state, tool access, and multi-step reasoning loops. Anthropic's approach with the Claude Code SDK reflects a deliberate strategy of exposing the same primitives that power its flagship developer product to the programmatic layer, rather than building a separate, reduced-capability API surface. This mirrors patterns seen across the AI tooling landscape, where companies like OpenAI with its Responses API and Google with its Gemini function-calling infrastructure are competing to become the foundational agent runtime layer for enterprise software. The inclusion of sub-agent orchestration via the Task tool, combined with MCP server configurability and plugin directory support, suggests Anthropic is positioning Claude Code's Agent SDK as a full agent orchestration framework capable of powering multi-agent pipelines — not merely a convenience wrapper for scripting single-turn interactions.

Read original article →