Skip to main content

claude-subprocess: call claude -p from Python on your Claude Code subscription, with the risky flags locked down. Beside the title, the real argv build_args(model='haiku') produces: omniroute run claude -- -p --model haiku --effort medium --output-format json --no-session-persistence --tools '' --disable-slash-commands --setting-sources user --strict-mcp-config --mcp-config mcp-empty.json. --bare is never sent because it breaks OAuth.

CI PyPI Python 3.12+ License: MIT

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+.

A real recorded run in the Python REPL: run_claude('Tag "CI is red" as JSON {"type": str, "urgent": bool}', model="haiku") returns in about 3 seconds, and extract_json(res.text) gives {'type': 'ci_failure', 'urgent': True}.

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

run_claude, arun_claude and stream_claude send tool-less calls to OmniRoute's HTTP API at /v1/chat/completions. Calls with tools, a project cwd or stream-json go through omniroute run claude -- -p with a throwaway cwd and an empty MCP config. An HTTP error falls back to the CLI and parks HTTP for 600 seconds. Both paths return a ClaudeResult. NotLoggedInError and UsageLimitError fail fast and derive from ClaudeCliError(RuntimeError).

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.
  • anthropic is the Messages API, billed per token on an ANTHROPIC_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.

  • --bare is 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.md gets loaded.
  • --tools "" is the default. A non-empty tools value goes to both --tools and --allowed-tools, because headless -p denies anything not allow-listed.
  • --output-format json and stream-json both end up as the same ClaudeResult.

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)

Source distribution for claude-subprocess 0.1.13
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)

Table of built distributions (wheels) for claude-subprocess 0.1.13
File Interpreter ABI Platform Actions
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 log

Release 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 log

Security 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.

Select from 2 files

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 log

A 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.

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