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.1.1.tar.gz (4.5 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.1.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for albeorla_claude_cli_bridge-0.1.1.tar.gz
Algorithm Hash digest
SHA256 938dd93e0921c54df7cd0472aa8be7419e4bf7956bf9cf63d6089a5c89bf7e45
MD5 d6f5fefdb9e3d03ab059c42edb0073c2
BLAKE2b-256 f5b04f3f46789133103afb61632e58930021062c07bb32430f4dd6411b0d5d83

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for albeorla_claude_cli_bridge-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b9de24080f7bd88a7c0ebe79b12ae75e1fa4eb236e6231be34be6f50206b23c8
MD5 72428039fd13c8833a3feaa61894ecd3
BLAKE2b-256 36a7beeeff9dffc21818ee50d279fb2bea4205d92401de8398ad35bd48e01f61

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