Skip to main content

Claude -p compatible wrapper backed by interactive Claude Code subscription sessions

Project description

claude-p

claude-p is a claude -p compatible Python CLI and SDK backed by the interactive Claude Code TUI.

Use it when claude -p is unavailable in an environment, but interactive claude works with the local Claude Code subscription login state.

Install

pip install claude-p

CLI

Default output matches claude -p: plain text.

claude-p.py "Respond exactly: hello"

Structured outputs:

claude-p.py "Respond exactly: hello" --output-format json
claude-p.py "Respond exactly: hello" --output-format stream-json --include-partial-messages

The CLI accepts a broad subset of claude -p flags, including:

  • -p, --print
  • --model
  • --tools
  • --permission-mode
  • --output-format text|json|stream-json
  • --include-partial-messages
  • --session-id
  • --cwd
  • common Claude Code context/config flags such as --system-prompt, --append-system-prompt, --mcp-config, --settings, --plugin-dir, --allowedTools, --disallowedTools, --resume, and --continue

Known limits:

  • Token usage, cost, and exact rate-limit fields are best-effort placeholders.
  • Hook lifecycle events from claude -p --include-hook-events are not replayed yet.
  • --input-format stream-json is accepted but not implemented.
  • --bare conflicts with the subscription-login goal because Claude bare mode bypasses OAuth/keychain auth.

Python SDK

The API is intentionally shaped like the official Claude Agent SDK:

import asyncio
from claude_p import ClaudePOptions, query


async def main():
    options = ClaudePOptions(
        model="sonnet",
        tools="default",
        permission_mode="default",
    )

    async for message in query("这个目录里有多少个文件", options=options):
        print(message)


asyncio.run(main())

For a single final result:

import asyncio
from claude_p import ClaudePClient, ClaudePOptions


async def main():
    async with ClaudePClient(ClaudePOptions(model="sonnet")) as client:
        result = await client.run("Respond exactly: SDK_OK")
        print(result.result)


asyncio.run(main())

How it works

The wrapper does not call claude -p.

It:

  1. starts interactive claude in a pseudo-TTY;
  2. passes a deterministic --session-id;
  3. waits for the TUI response to complete;
  4. reads Claude Code's canonical session JSONL from ~/.claude/projects/**/<session-id>.jsonl;
  5. emits text/json/stream-json output compatible with claude -p.

The session JSONL is required because terminal rendering is lossy and can drop characters during redraws.

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_p-0.1.0.tar.gz (11.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_p-0.1.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file claude_p-0.1.0.tar.gz.

File metadata

  • Download URL: claude_p-0.1.0.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for claude_p-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3fd92089b1063d09e6275f311b4939c243ef42b312204212516d2c66bb22b7ad
MD5 66001130186d2248af32e3645a5a4fb6
BLAKE2b-256 c530174073d833c460b45681397810ea716c90ce130d314dcaa50f76a4f7f76e

See more details on using hashes here.

Provenance

The following attestation bundles were made for claude_p-0.1.0.tar.gz:

Publisher: publish.yml on Equality-Machine/claude-p

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file claude_p-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: claude_p-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for claude_p-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 569c33a2c329b52c53d863a78c5b569a9fa465167bacf3d64f6f85baba1a052a
MD5 497445b845b3785d226f077e64b8450d
BLAKE2b-256 fc2873ab3a9ad91294a2962cbcf7bf0fc8b8fa04f46db0e648662165f1e35038

See more details on using hashes here.

Provenance

The following attestation bundles were made for claude_p-0.1.0-py3-none-any.whl:

Publisher: publish.yml on Equality-Machine/claude-p

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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