Skip to main content

Claude Code Headless

PyPI version Python 3.10+ License: MIT

Call Claude programmatically using your Claude Max subscription via Claude Code CLI - no API key required.

Installation

pip install claude-code-headless

Prerequisites

  1. Claude Code CLI installed:
npm install -g @anthropic-ai/claude-code
  1. Login to Claude:
claude login
  1. WSL (Windows Subsystem for Linux) if running on Windows

Quick Start

from claude_code_headless import call_claude

response = call_claude("What is the capital of France?")
print(response)  # Paris

Usage

Basic Call

from claude_code_headless import call_claude

response = call_claude("Explain Python in one sentence.")
print(response)

With System Prompt

from claude_code_headless import call_claude_with_system

response = call_claude_with_system(
    prompt="Explain quantum computing",
    system="You are a teacher explaining to a 10-year-old."
)
print(response)

JSON Output (with metadata)

from claude_code_headless import call_claude_json

response = call_claude_json("What is 2 + 2?")
print(response["result"])  # 4
print(response["usage"])   # Token usage stats

Streaming Output

from claude_code_headless import call_claude_streaming

# Print as it streams
call_claude_streaming("Write a poem about coding")

# Or use a callback
chunks = []
call_claude_streaming(
    "Write a poem",
    callback=lambda chunk: chunks.append(chunk)
)

With Tools

from claude_code_headless import call_claude

# Allow Claude to read files
response = call_claude(
    "Summarize the contents of README.md",
    allowed_tools=["Read"]
)

Available tools: Read, Edit, Write, Bash, Glob, Grep, WebSearch, WebFetch

Class-based Interface

from claude_code_headless import ClaudeClient

client = ClaudeClient(
    default_tools=["Read"],
    default_system="Be concise and helpful."
)

response = client.ask("What files are in this directory?")
print(response)

# JSON response
data = client.ask_json("Count to 5")
print(data["result"])

API Reference

Functions

Function Description
call_claude(prompt, allowed_tools=None) Basic call, returns text
call_claude_with_system(prompt, system) Call with system prompt
call_claude_json(prompt, allowed_tools=None) Returns full JSON with metadata
call_claude_streaming(prompt, allowed_tools=None, callback=None) Streaming output

ClaudeClient Class

client = ClaudeClient(
    default_tools=None,    # Default tools for all calls
    default_system=None    # Default system prompt
)

client.ask(prompt, tools=None, system=None)        # Text response
client.ask_json(prompt, tools=None)                # JSON response
client.ask_streaming(prompt, tools=None, callback=None)  # Streaming

Platform Support

Platform Support
Windows (CMD, PowerShell, Git Bash) Via WSL
WSL Native
Linux Native
macOS Native

How It Works

This package wraps Claude Code's headless mode (-p flag), routing calls through the CLI:

Python → Claude Code CLI → Claude API (using Max subscription)

On Windows, commands are automatically routed through WSL.

Comparison: API vs Claude Code Headless

Feature Claude API Claude Code Headless
Cost Pay per token Included in Max subscription
Setup API key required Just login
Tools Manual implementation Built-in (Read, Edit, Bash, etc.)
Best for Production apps Scripts, automation, prototyping

Troubleshooting

"claude: command not found"

npm install -g @anthropic-ai/claude-code
claude --version

WSL errors on Windows

wsl --install
wsl --set-default Ubuntu

License

MIT

Metadata

Release files for claude-code-headless 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for claude-code-headless 0.1.2
File Size Uploaded
claude_code_headless-0.1.2.tar.gz 6.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for claude-code-headless 0.1.2
File Interpreter ABI Platform
claude_code_headless-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 14.3 kB

Release files / claude_code_headless-0.1.2.tar.gz

Download URL claude_code_headless-0.1.2.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
50530008204830633472422f1761ba0b5c584c9b4f95e979012a2e0992ddd690
BLAKE2b-256 checksum
How to use checksums
6e61d43538c98c548ffd5a4944ff34b9e48baa65696ceac8f91b1fe96f23262a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / claude_code_headless-0.1.2-py3-none-any.whl

Download URL claude_code_headless-0.1.2-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f0c8768b64316fe5cb165d1d53d62ea57379f3a25ad724340a4ff93d67674657
BLAKE2b-256 checksum
How to use checksums
f1d4286cf16a33e1f4aa0d4063bacd2ff80c68311856854d215e467a52a9e151
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page