Skip to main content

claude-runner

Python wrapper around the Claude Agent SDK with automatic retry, timeout management, and critical bug workarounds.

Install

pip install claude-runner

Requires claude-agent-sdk>=0.1.39 and Python 3.11+.

Usage

from claude_runner import run, run_sync

# Async — direct LLM call (default: tools=[], max_turns=1)
result = await run("Explain quicksort in one sentence")
print(result.text)

# Sync wrapper
result = run_sync("Explain quicksort in one sentence")

# Agentic with retry
result = await run(
    prompt="Refactor auth module",
    tools=None,           # SDK defaults (all tools)
    max_turns=None,       # SDK defaults (no limit)
    retries=5,
    retry_base_delay=10.0,
    retry_max_delay=60.0,
    retry_jitter=True,
    timeout_minutes=30,
)

if result.is_error:
    print(f"Failed: {result.error}")
else:
    print(result.text)
    print(f"Cost: ${result.cost_usd:.4f}")

API

run(prompt, **kwargs) -> RunResult

Async. Streams a Claude Agent SDK session. Never raises for session failures — check result.is_error.

Parameter Default Description
model None Model identifier (e.g. "claude-sonnet-4-6")
system_prompt None System prompt
max_turns 1 Max turns (None for SDK default)
tools [] Tool names (None for SDK defaults)
retries 0 Additional attempts after first
timeout_minutes 30 Per-attempt timeout
retry_base_delay 10.0 Base delay for exponential backoff
retry_max_delay None Cap on computed delay
retry_jitter False Randomize delay by +/-25%
on_text None Callback for each text block
on_stderr None Callback for CLI stderr
options None Full ClaudeAgentOptions escape hatch

run_sync(prompt, **kwargs) -> RunResult

Synchronous wrapper. Calls asyncio.run(run(...)). Cannot be called from a running event loop.

RunResult

Dataclass with: text, cost_usd, usage, duration_ms, num_turns, session_id, is_error, error, result_message, messages.

clean_claude_env()

Strips CLAUDE* env vars (except ANTHROPIC_API_KEY) to prevent subprocess contamination when running from within Claude Code.

Bug workarounds

Automatically applied at import time:

  1. Rate limit event kills generator — SDK's parse_message() raises on unknown message types. Patched to return SystemMessage instead.
  2. Env var contamination — CLAUDECODE=1 inherited from Claude Code breaks the CLI subprocess. clean_claude_env() strips it.
  3. Silent stderr discard — SDK sends stderr to /dev/null. The on_stderr parameter wires through automatically.

License

MIT

Metadata

Release files for claude-runner 0.1.1

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-runner 0.1.1
File Size Uploaded
claude_runner-0.1.1.tar.gz 48.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for claude-runner 0.1.1
File Interpreter ABI Platform
claude_runner-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 58.0 kB

Release files / claude_runner-0.1.1.tar.gz

Download URL claude_runner-0.1.1.tar.gz
Size 48.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ddf7b3560f84cd3f7554b5360eab8c6a2e862fddbd89a58eb9cc082185571b8e
BLAKE2b-256 checksum
How to use checksums
41c020b4628c2115fd092a448d9029a0960841e544fffeb3fed70d67b5cf7859
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release files / claude_runner-0.1.1-py3-none-any.whl

Download URL claude_runner-0.1.1-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
02abff407115abdc064a844536acae517da0b20c53aff5def641054c8fb2b5d4
BLAKE2b-256 checksum
How to use checksums
e795e180deeeb47138ad5fef5720cc48f11e2866031e5f6e2621602f58b87ce5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

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