Run Claude Code headless (claude -p) from Python on your subscription, with no API key.
It builds the flags, isolates the call, sorts the errors and parses the answer. Calls go through the OmniRoute gateway, which picks the route. Six of my projects had their own copy of this code, and this library replaced them.
Standard library only, Python 3.12+.
Install
uv add claude-subprocess # or: pip install claude-subprocess
The PyPI name is claude-subprocess, the import name is claude_cli.
At runtime you need omniroute and claude on PATH, with Claude Code logged in.
Usage
from claude_cli import run_claude, extract_json
res = run_claude('Reply with {"ok": true}', model="haiku", effort="low", timeout=60)
res.text # the model's answer
res.cost_usd # cost the envelope reported (may be None)
res.duration_ms
res.raw # the full envelope dict
extract_json(res.text) # {'ok': True}
extract_json tolerates code fences and prose around the object:
>>> extract_json('Sure! Here it is:\n```json\n{"ok": true}\n```\nAnything else?')
{'ok': True}
Async, streaming, cache, batch, project mode and pre-flight checks: docs/usage.md.
How a call is routed
Tool-less calls go over HTTP to the gateway. Calls that need tools, a project cwd or stream-json go through the CLI.
If HTTP fails, the call falls back to the CLI and HTTP is paused for 600 s.
Rules, switches and env vars: docs/routing.md. Step by step: docs/logic-map.md.
How it differs
- Claude Agent SDK (
claude-agent-sdk) is the official way to drive Claude Code from code, with the full agent loop. Use it to build an agent or a product. Its docs ask third-party products to use API keys, not a claude.ai login. - This library is for your own scripts on your own subscription: one prompt in, text or JSON out, no dependencies.
anthropicis the Messages API, billed per token on anANTHROPIC_API_KEY. Use it when you have a key and want every API feature.subprocess.run(["claude", "-p", ...])is what this library replaced. The hard parts are the isolation flags, killing the process tree on timeout, telling a usage limit from a login problem, and parsing the envelope.
Invariants
These are the reason the library exists.
--bareis never passed. It forces API-key-only mode and breaks subscription auth. A test checks it.- Every Claude Code subprocess is launched via
omniroute run claude, and the HTTP route talks to the same gateway. - The surface is isolated: an empty temp MCP config with
--strict-mcp-config,--setting-sources user,--disable-slash-commands,--no-session-persistence. - Every call gets a throwaway cwd, so no project
CLAUDE.mdgets loaded. --tools ""is the default. A non-emptytoolsvalue goes to both--toolsand--allowed-tools, because headless-pdenies anything not allow-listed.--output-format jsonandstream-jsonboth end up as the sameClaudeResult.
Errors
All errors derive from ClaudeCliError(RuntimeError).
NotLoggedInError and UsageLimitError fail fast, so a batch stops at the first one.
Causes and gateway diagnosis: docs/routing.md.
Tests
uv run --extra dev pytest -q
The suite fakes the subprocess calls and never runs the real claude binary.
License
MIT. Contributions: CONTRIBUTING.md.
Metadata
Release files for claude-subprocess 0.1.13
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| claude_subprocess-0.1.13.tar.gz | 51.4 kB | With provenance | Details for claude_subprocess-0.1.13.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| claude_subprocess-0.1.13-py3-none-any.whl | Python 3 | none | any | Details for claude_subprocess-0.1.13-py3-none-any.whl |
Total release size: 88.1 kB
Release files / claude_subprocess-0.1.13.tar.gz
| Download URL | claude_subprocess-0.1.13.tar.gz |
|---|---|
| Size | 51.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cc15df77c0bf80b6d9c3dc89f2ea24e6fe3b25a67387f8765b1b32aca61004c6
|
|
BLAKE2b-256 checksum How to use checksums |
fe47ca402fb64090f6e72fb0620c77202465eaa86899f2ab1c4c96af9e09bc10
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.
Transparency logRelease files / claude_subprocess-0.1.13-py3-none-any.whl
| Download URL | claude_subprocess-0.1.13-py3-none-any.whl |
|---|---|
| Size | 36.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
198eb17051916b5ecbfaffd2f1b1c2d190139c920125dde7334a4d8b56043bfd
|
|
BLAKE2b-256 checksum How to use checksums |
67bb1cf46867dfb0d1b945dc97c699a29a23d33e98cec3038059a300ed528b2b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.
Transparency logSecurity for claude-subprocess 0.1.13
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for claude_subprocess-0.1.13.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.