Skip to main content

Call Claude programmatically using your Claude Max subscription via Claude Code CLI

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

claude_code_headless-0.1.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

claude_code_headless-0.1.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file claude_code_headless-0.1.1.tar.gz.

File metadata

  • Download URL: claude_code_headless-0.1.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for claude_code_headless-0.1.1.tar.gz
Algorithm Hash digest
SHA256 28962c943451e9c3f4650d1df2e3e12f41d1689528e184ef00b0226f853e8cc7
MD5 6d4ac4b1401dac9e3807d569cdd06fae
BLAKE2b-256 0a92372abf488f775dee9fe13fdf4af5ce3dad3ba4633b9a1ac3c498eb715836

See more details on using hashes here.

File details

Details for the file claude_code_headless-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for claude_code_headless-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4dc9924ecabc850e6847873fc4adf459b603370f8875c68600089f10741a47cd
MD5 fe85ace0edbb48d5148e3c20010dfd90
BLAKE2b-256 6446ff1f8db8ee7797a7921323f508ee0a075d9e8fd15545ab40bc4a4c1b103c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page