Skip to main content

Reusable Claude Agent SDK wrapper with bug workarounds, retry, and timeout

Project description

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 contaminationCLAUDECODE=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

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_runner-0.1.1.tar.gz (48.9 kB view details)

Uploaded Source

Built Distribution

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

claude_runner-0.1.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: claude_runner-0.1.1.tar.gz
  • Upload date:
  • Size: 48.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for claude_runner-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ddf7b3560f84cd3f7554b5360eab8c6a2e862fddbd89a58eb9cc082185571b8e
MD5 52b14820a0b91df7f7415bcdb308d016
BLAKE2b-256 41c020b4628c2115fd092a448d9029a0960841e544fffeb3fed70d67b5cf7859

See more details on using hashes here.

File details

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

File metadata

  • Download URL: claude_runner-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for claude_runner-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 02abff407115abdc064a844536acae517da0b20c53aff5def641054c8fb2b5d4
MD5 1d4b9111c0ce4407995bbebf8fb54896
BLAKE2b-256 e795e180deeeb47138ad5fef5720cc48f11e2866031e5f6e2621602f58b87ce5

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