Everything you need to know about using Claude Code programmatically
Community-built projects that wrap Claude Code CLI for programmatic use:
| Project | Description | Tag |
|---|---|---|
| using-claude-code-cli-agent-skill | Claude Code skill for programmatically invoking Claude Code CLI from Python orchestrators, shell scripts, and automation pipelines. https://github.com/SpillwaveSolutions/using-claude-code-cli-agent-skill |
Best for servers |
| openclaw-claude-code | Turns Claude Code CLI into a programmable headless engine with subprocess management, agent teams, and cost tracking. https://github.com/Enderfga/openclaw-claude-code |
Multi-agent |
| ralph-claude-code | Autonomous loop that continuously runs Claude Code with automatic session management across iterations. https://github.com/frankbria/ralph-claude-code |
Autonomous loop |
| learn-claude-code | Minimal Python agent harness built from scratch. Great for understanding how Q&A loops work under the hood. https://github.com/shareAI-lab/learn-claude-code |
Educational |
| CheetahClaws | Python-native Claude Code reimplementation — lightweight, readable, supports multiple models. https://github.com/SafeRL-Lab/nano-claude-code |
Python-native |
| claude-agent-sdk-python | Official Anthropic Agent SDK for Python. Runs in-process — no subprocess management needed. https://github.com/anthropics/claude-agent-sdk-python |
Official SDK |
| Method | Use Case | Requires |
|---|---|---|
| Claude Agent SDK | Build apps with agentic Claude | Anthropic API key |
| Anthropic API | Raw model access, full control | Anthropic API key |
| CLI Subprocess | Script / automate Claude Code | Claude Code + Pro or API key |
| MCP Server | Give Claude tools from your app | Claude Code installed |
Claude Code supports non-interactive scripting mode. Use -p to run a single query and exit, then carry the session_id forward to maintain conversation context.
| Flag | Description |
|---|---|
-p / --print | Non-interactive mode — run and exit |
--output-format json | Structured JSON output |
--output-format stream-json | Streaming newline-delimited JSON |
--resume <session_id> | Resume a previous session |
--allowedTools | Pre-approve tools, skip prompts |
--cwd <path> | Set working directory / project folder |
The session_id is returned automatically in the first JSON response — you do not create it manually.
claude -p "Hello" --output-format json
# Response:
{
"result": "Hello! How can I help you?",
"session_id": "55ac75f5-3c3e-4e24-9e39-bf6401e4456c",
...
}
import subprocess, json
session_id = None
print("Claude Q&A Loop (type 'exit' to quit)\n")
while True:
question = input("You: ").strip()
if question.lower() == "exit":
break
cmd = ["claude", "-p", question, "--output-format", "json"]
if session_id:
cmd += ["--resume", session_id]
result = subprocess.run(cmd, capture_output=True, text=True)
data = json.loads(result.stdout)
session_id = data.get("session_id")
print(f"Claude: {data['result']}\n")
# Option 1 — cd into the folder
cd /path/to/project
claude -p "Summarize this project" --output-format json
# Option 2 — use --cwd flag
claude -p "Summarize this project" --cwd /path/to/project --output-format json
# Option 3 — from Python subprocess
subprocess.run(
["claude", "-p", "Summarize this project", "--output-format", "json"],
cwd="/path/to/project",
capture_output=True, text=True
)
Gives you Claude Code's agentic capabilities as a library — no subprocess management needed.
# Python
pip install claude-agent-sdk
# TypeScript / Node.js
npm install @anthropic-ai/claude-agent-sdk
import Anthropic from "@anthropic-ai/sdk";
import express from "express";
const client = new Anthropic();
const app = express();
app.use(express.json());
app.post("/ask", async (req, res) => {
const session = await client.beta.sessions.create({});
const response = await client.beta.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 1024,
session_id: session.id,
messages: [{ role: "user", content: req.body.question }],
});
await client.beta.sessions.delete(session.id);
res.json({ answer: response.content[0].text });
});
app.listen(3000);
| CLI Subprocess | Agent SDK | |
|---|---|---|
| Claude Pro support | Yes (via claude login) | No (API credits only) |
| Speed | Slower (new process each turn) | Fast (in-process) |
| Multi-turn loop | Yes, via --resume | Yes, natively |
| Best for | Scripting, low frequency | Production servers |
MCP lets your program expose tools that Claude Code can call. Your program runs as an MCP server; Claude connects and calls your functions automatically.
Configure MCP servers in .claude/settings.json.
claude login
export ANTHROPIC_API_KEY="sk-ant-..."
Get an API key at console.anthropic.com