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.2.tar.gz (6.6 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.2-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: claude_code_headless-0.1.2.tar.gz
  • Upload date:
  • Size: 6.6 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.2.tar.gz
Algorithm Hash digest
SHA256 50530008204830633472422f1761ba0b5c584c9b4f95e979012a2e0992ddd690
MD5 5a5e7e7f3d9190e011cc6ecbf5558826
BLAKE2b-256 6e61d43538c98c548ffd5a4944ff34b9e48baa65696ceac8f91b1fe96f23262a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for claude_code_headless-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f0c8768b64316fe5cb165d1d53d62ea57379f3a25ad724340a4ff93d67674657
MD5 3607894c5b3abd55766b901c0110e1b1
BLAKE2b-256 f1d4286cf16a33e1f4aa0d4063bacd2ff80c68311856854d215e467a52a9e151

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