Claude Code Integration Guide

Everything you need to know about using Claude Code programmatically

Open Source Projects

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

Access Options Overview

MethodUse CaseRequires
Claude Agent SDKBuild apps with agentic ClaudeAnthropic API key
Anthropic APIRaw model access, full controlAnthropic API key
CLI SubprocessScript / automate Claude CodeClaude Code + Pro or API key
MCP ServerGive Claude tools from your appClaude Code installed
Claude Pro subscription ($20/month on claude.ai) does not grant API access. To use the Agent SDK you need a separate account at console.anthropic.com. However, the CLI subprocess approach can use your Pro subscription via claude login.

CLI Subprocess — Ask / Answer Loop

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.

Key Flags

FlagDescription
-p / --printNon-interactive mode — run and exit
--output-format jsonStructured JSON output
--output-format stream-jsonStreaming newline-delimited JSON
--resume <session_id>Resume a previous session
--allowedToolsPre-approve tools, skip prompts
--cwd <path>Set working directory / project folder

Getting a session_id

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",
  ...
}
Note: uuid in the response is the message/request ID — not the session. Always use session_id for --resume.

Python Q&A Loop

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")

Point Claude at a project folder

# 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
)

Claude Agent SDK

Gives you Claude Code's agentic capabilities as a library — no subprocess management needed.

Install

# Python
pip install claude-agent-sdk

# TypeScript / Node.js
npm install @anthropic-ai/claude-agent-sdk

Concurrent Q&A Server (TypeScript)

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 vs Agent SDK

CLI SubprocessAgent SDK
Claude Pro supportYes (via claude login)No (API credits only)
SpeedSlower (new process each turn)Fast (in-process)
Multi-turn loopYes, via --resumeYes, natively
Best forScripting, low frequencyProduction servers

MCP — Model Context Protocol

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.

Use MCP when you want Claude to call APIs, databases, or functions in your app.
Use the Agent SDK when you want your app to drive Claude as an agent.

Configure MCP servers in .claude/settings.json.

Which Approach Should I Use?

Your program wants to... │ ├── Give Claude tools (files, APIs, DBs) │ └── Build an MCP Server │ ├── Control Claude as an agent from code │ └── Use the Agent SDK │ ├── Simple scripting / CI / use Pro subscription │ └── Use CLI Subprocess (-p flag) │ └── Full low-level control over every token └── Use the Anthropic API directly

Authentication

CLI — use your Pro subscription

claude login

Agent SDK / Anthropic API — use an API key

export ANTHROPIC_API_KEY="sk-ant-..."

Get an API key at console.anthropic.com