Skip to main content

Async Python wrapper for the Claude Code CLI with session management

Project description

albeorla-claude-cli-bridge

Async Python wrapper for the Claude Code CLI with automatic session management.

Install

pip install albeorla-claude-cli-bridge

Requires the claude CLI to be installed and authenticated.

Usage

import asyncio
from claude_cli_bridge import ClaudeBridge

async def main():
    bridge = ClaudeBridge(work_dir="~/my-project")

    # First call creates a new session
    resp = await bridge.query(key="chat-1", message="explain this codebase")
    print(resp.text)

    # Follow-up resumes the same session automatically
    resp = await bridge.query(key="chat-1", message="now refactor the main module")
    print(resp.text)

    # One-shot (no session tracking)
    resp = await bridge.ask("what is 2 + 2?")
    print(resp.text)

asyncio.run(main())

API

ClaudeBridge

ClaudeBridge(
    claude_binary="claude",       # path to claude CLI
    work_dir=".",                 # working directory for subprocess
    system_prompt=None,           # appended on first turn
    permission_mode="bypassPermissions",
    model=None,                   # e.g., "haiku", "sonnet"
    allowed_tools=None,           # e.g., ["Read", "WebSearch"]
)

Methods

Method Description
await bridge.query(key, message) Send a message to a session. Creates one if needed, resumes on follow-up.
await bridge.ask(message) One-shot query, no session reuse.
bridge.get_session(key) Get session by key, or None.
bridge.create_session(key) Explicitly create a session.
bridge.remove_session(key) Remove a session.
bridge.active_sessions Dict of all active sessions.

Response

Field Type Description
text str The assistant's reply
session_id str Claude session ID
cost_usd float Cost of the query
duration_ms int Wall-clock time
num_turns int Number of agent turns
is_error bool Whether the query failed
raw dict Full JSON response from the CLI

Session

Field Type Description
session_id str UUID for the Claude CLI session
key Any Application-defined identifier
turn_count int Messages sent in this session
busy bool Whether a query is in progress

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

albeorla_claude_cli_bridge-0.2.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

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

albeorla_claude_cli_bridge-0.2.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file albeorla_claude_cli_bridge-0.2.0.tar.gz.

File metadata

File hashes

Hashes for albeorla_claude_cli_bridge-0.2.0.tar.gz
Algorithm Hash digest
SHA256 86b4e5b91dcedc0b72b63139e28e74e0184c15cf5cc9eea569f2feea19fa3463
MD5 0f4a1153999a079ce9a7f160bf1432c4
BLAKE2b-256 448b437d1d9313ab3a32cb0de4233bf45337bd04d5f5e39a05c318e531b950ab

See more details on using hashes here.

File details

Details for the file albeorla_claude_cli_bridge-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for albeorla_claude_cli_bridge-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 70b4efcc73a69e9d164b32773f48a225a16df56fb49644ef030073744f810193
MD5 493178ba014373e44775a540a616864f
BLAKE2b-256 a4e19f67a201c0c323efd79b6cdaa3a09fccd34be36b5e79ff1ba53c825247aa

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