{"info":{"author":null,"author_email":"Anthropic <support@anthropic.com>","bugtrack_url":null,"classifiers":["Development Status :: 3 - Alpha","Intended Audience :: Developers","License :: OSI Approved :: MIT License","Programming Language :: Python :: 3","Programming Language :: Python :: 3.10","Programming Language :: Python :: 3.11","Programming Language :: Python :: 3.12","Programming Language :: Python :: 3.13","Typing :: Typed"],"description":"# Claude Agent SDK for Python\n\nPython SDK for Claude Agent. See the [Claude Agent SDK documentation](https:\/\/platform.claude.com\/docs\/en\/agent-sdk\/python) for more information.\n\n## Installation\n\n```bash\npip install claude-agent-sdk\n```\n\n**Prerequisites:**\n\n- Python 3.10+\n\n**Note:** The Claude Code CLI is automatically bundled with the package - no separate installation required! The SDK will use the bundled CLI by default. If you prefer to use a system-wide installation or a specific version, you can:\n\n- Install Claude Code separately: `curl -fsSL https:\/\/claude.ai\/install.sh | bash`\n- Specify a custom path: `ClaudeAgentOptions(cli_path=\"\/path\/to\/claude\")`\n\n## Quick Start\n\n```python\nimport anyio\nfrom claude_agent_sdk import query\n\nasync def main():\n    async for message in query(prompt=\"What is 2 + 2?\"):\n        print(message)\n\nanyio.run(main)\n```\n\n## Basic Usage: query()\n\n`query()` is an async function for querying Claude Code. It returns an `AsyncIterator` of response messages. See [src\/claude_agent_sdk\/query.py](src\/claude_agent_sdk\/query.py).\n\n```python\nfrom claude_agent_sdk import query, ClaudeAgentOptions, AssistantMessage, TextBlock\n\n# Simple query\nasync for message in query(prompt=\"Hello Claude\"):\n    if isinstance(message, AssistantMessage):\n        for block in message.content:\n            if isinstance(block, TextBlock):\n                print(block.text)\n\n# With options\noptions = ClaudeAgentOptions(\n    system_prompt=\"You are a helpful assistant\",\n    max_turns=1\n)\n\nasync for message in query(prompt=\"Tell me a joke\", options=options):\n    print(message)\n```\n\n### Using Tools\n\nBy default, Claude has access to the full [Claude Code toolset](https:\/\/code.claude.com\/docs\/en\/settings#tools-available-to-claude) (Read, Write, Edit, Bash, and others). `allowed_tools` is a permission allowlist: listed tools are auto-approved, and unlisted tools fall through to `permission_mode` and `can_use_tool` for a decision. It does not remove tools from Claude's toolset. To block specific tools, use `disallowed_tools`. See the [permissions guide](https:\/\/platform.claude.com\/docs\/en\/agent-sdk\/permissions) for the full evaluation order.\n\n```python\noptions = ClaudeAgentOptions(\n    allowed_tools=[\"Read\", \"Write\", \"Bash\"],  # auto-approve these tools\n    permission_mode='acceptEdits'  # auto-accept file edits\n)\n\nasync for message in query(\n    prompt=\"Create a hello.py file\",\n    options=options\n):\n    # Process tool use and results\n    pass\n```\n\n### Working Directory\n\n```python\nfrom pathlib import Path\n\noptions = ClaudeAgentOptions(\n    cwd=\"\/path\/to\/project\"  # or Path(\"\/path\/to\/project\")\n)\n```\n\n### System Prompt\n\nBy default, Claude Code builds the system prompt on a session's first request, records it, and reuses it on every later request, including after you resume the session. A changed custom prompt, or changed `append` text on the `claude_code` preset, then has no effect until the session is compacted or you start a new session. To rebuild the prompt on every request instead, for example while you iterate on its wording, set `snapshot` to `False` in the `{\"type\": \"preset\", ...}` or `{\"type\": \"custom\", ...}` dict:\n\n```python\noptions = ClaudeAgentOptions(\n    system_prompt={\"type\": \"custom\", \"prompt\": \"You are a release bot.\", \"snapshot\": False}\n)\n```\n\nRequires Claude Code CLI 2.1.257 or later. Before 2.1.265, a session with an `append` or custom prompt recorded it only when `snapshot` was True. See [Modifying system prompts](https:\/\/code.claude.com\/docs\/en\/agent-sdk\/modifying-system-prompts#change-the-prompt-of-an-existing-session) for details.\n\n## ClaudeSDKClient\n\n`ClaudeSDKClient` supports bidirectional, interactive conversations with Claude\nCode. See [src\/claude_agent_sdk\/client.py](src\/claude_agent_sdk\/client.py).\n\nUnlike `query()`, `ClaudeSDKClient` additionally enables **custom tools** and **hooks**, both of which can be defined as Python functions.\n\n### Custom Tools (as In-Process SDK MCP Servers)\n\nA **custom tool** is a Python function that you can offer to Claude, for Claude to invoke as needed.\n\nCustom tools are implemented in-process MCP servers that run directly within your Python application, eliminating the need for separate processes that regular MCP servers require.\n\nFor an end-to-end example, see [MCP Calculator](examples\/mcp_calculator.py).\n\n#### Creating a Simple Tool\n\n```python\nfrom claude_agent_sdk import tool, create_sdk_mcp_server, ClaudeAgentOptions, ClaudeSDKClient\n\n# Define a tool using the @tool decorator\n@tool(\"greet\", \"Greet a user\", {\"name\": str})\nasync def greet_user(args):\n    return {\n        \"content\": [\n            {\"type\": \"text\", \"text\": f\"Hello, {args['name']}!\"}\n        ]\n    }\n\n# Create an SDK MCP server\nserver = create_sdk_mcp_server(\n    name=\"my-tools\",\n    version=\"1.0.0\",\n    tools=[greet_user]\n)\n\n# Use it with Claude. allowed_tools pre-approves the tool so it runs\n# without a permission prompt; it does not control tool availability.\noptions = ClaudeAgentOptions(\n    mcp_servers={\"tools\": server},\n    allowed_tools=[\"mcp__tools__greet\"]\n)\n\nasync with ClaudeSDKClient(options=options) as client:\n    await client.query(\"Greet Alice\")\n\n    # Extract and print response\n    async for msg in client.receive_response():\n        print(msg)\n```\n\n#### Benefits Over External MCP Servers\n\n- **No subprocess management** - Runs in the same process as your application\n- **Better performance** - No IPC overhead for tool calls\n- **Simpler deployment** - Single Python process instead of multiple\n- **Easier debugging** - All code runs in the same process\n- **Type safety** - Direct Python function calls with type hints\n\n#### Migration from External Servers\n\n```python\n# BEFORE: External MCP server (separate process)\noptions = ClaudeAgentOptions(\n    mcp_servers={\n        \"calculator\": {\n            \"type\": \"stdio\",\n            \"command\": \"python\",\n            \"args\": [\"-m\", \"calculator_server\"]\n        }\n    }\n)\n\n# AFTER: SDK MCP server (in-process)\nfrom my_tools import add, subtract  # Your tool functions\n\ncalculator = create_sdk_mcp_server(\n    name=\"calculator\",\n    tools=[add, subtract]\n)\n\noptions = ClaudeAgentOptions(\n    mcp_servers={\"calculator\": calculator}\n)\n```\n\n#### Mixed Server Support\n\nYou can use both SDK and external MCP servers together:\n\n```python\noptions = ClaudeAgentOptions(\n    mcp_servers={\n        \"internal\": sdk_server,      # In-process SDK server\n        \"external\": {                # External subprocess server\n            \"type\": \"stdio\",\n            \"command\": \"external-server\"\n        }\n    }\n)\n```\n\n### Hooks\n\nA **hook** is a Python function that the Claude Code _application_ (_not_ Claude) invokes at specific points of the Claude agent loop. Hooks can provide deterministic processing and automated feedback for Claude. Read more in [Intercept and control agent behavior with hooks](https:\/\/platform.claude.com\/docs\/en\/agent-sdk\/hooks).\n\nFor more examples, see examples\/hooks.py.\n\n#### Example\n\n```python\nfrom claude_agent_sdk import ClaudeAgentOptions, ClaudeSDKClient, HookMatcher\n\nasync def check_bash_command(input_data, tool_use_id, context):\n    tool_name = input_data[\"tool_name\"]\n    tool_input = input_data[\"tool_input\"]\n    if tool_name != \"Bash\":\n        return {}\n    command = tool_input.get(\"command\", \"\")\n    block_patterns = [\"foo.sh\"]\n    for pattern in block_patterns:\n        if pattern in command:\n            return {\n                \"hookSpecificOutput\": {\n                    \"hookEventName\": \"PreToolUse\",\n                    \"permissionDecision\": \"deny\",\n                    \"permissionDecisionReason\": f\"Command contains invalid pattern: {pattern}\",\n                }\n            }\n    return {}\n\noptions = ClaudeAgentOptions(\n    allowed_tools=[\"Bash\"],\n    hooks={\n        \"PreToolUse\": [\n            HookMatcher(matcher=\"Bash\", hooks=[check_bash_command]),\n        ],\n    }\n)\n\nasync with ClaudeSDKClient(options=options) as client:\n    # Test 1: Command with forbidden pattern (will be blocked)\n    await client.query(\"Run the bash command: .\/foo.sh --help\")\n    async for msg in client.receive_response():\n        print(msg)\n\n    print(\"\\n\" + \"=\" * 50 + \"\\n\")\n\n    # Test 2: Safe command that should work\n    await client.query(\"Run the bash command: echo 'Hello from hooks example!'\")\n    async for msg in client.receive_response():\n        print(msg)\n```\n\n## Types\n\nSee [src\/claude_agent_sdk\/types.py](src\/claude_agent_sdk\/types.py) for complete type definitions:\n\n- `ClaudeAgentOptions` - Configuration options\n- `AssistantMessage`, `UserMessage`, `SystemMessage`, `ResultMessage` - Message types\n- `TextBlock`, `ToolUseBlock`, `ToolResultBlock` - Content blocks\n\n## Error Handling\n\n```python\nfrom claude_agent_sdk import (\n    ClaudeSDKError,      # Base error\n    CLINotFoundError,    # Claude Code not installed\n    CLIConnectionError,  # Connection issues\n    ProcessError,        # Process failed\n    ResultError,         # Run ended with an error result (subclass of ProcessError)\n    CLIJSONDecodeError,  # JSON parsing issues\n)\n\ntry:\n    async for message in query(prompt=\"Hello\"):\n        pass\nexcept CLINotFoundError:\n    print(\"Please install Claude Code\")\nexcept ResultError as e:\n    # The CLI reported a terminal error result (also yielded as the final\n    # ResultMessage) and exited. Structured fields are on the exception.\n    print(f\"Run failed: {e.subtype=} {e.terminal_reason=} {e.api_error_status=}\")\n    print(e.result or e.errors)\nexcept ProcessError as e:\n    print(f\"Process failed with exit code: {e.exit_code}\")\nexcept CLIJSONDecodeError as e:\n    print(f\"Failed to parse response: {e}\")\n```\n\nSee [src\/claude_agent_sdk\/\\_errors.py](src\/claude_agent_sdk\/_errors.py) for all error types.\n\n## Available Tools\n\nSee the [Claude Code documentation](https:\/\/code.claude.com\/docs\/en\/settings#tools-available-to-claude) for a complete list of available tools.\n\n## Examples\n\nSee [examples\/quick_start.py](examples\/quick_start.py) for a complete working example.\n\nSee [examples\/streaming_mode.py](examples\/streaming_mode.py) for comprehensive examples involving `ClaudeSDKClient`. You can even run interactive examples in IPython from [examples\/streaming_mode_ipython.py](examples\/streaming_mode_ipython.py).\n\n## Migrating from Claude Code SDK\n\nIf you're upgrading from the Claude Code SDK (versions < 0.1.0), please see the [CHANGELOG.md](CHANGELOG.md#010) for details on breaking changes and new features, including:\n\n- `ClaudeCodeOptions` \u2192 `ClaudeAgentOptions` rename\n- Merged system prompt configuration\n- Settings isolation and explicit control\n- New programmatic subagents and session forking features\n\n## Development\n\nIf you're contributing to this project, run the initial setup script to install git hooks:\n\n```bash\n.\/scripts\/initial-setup.sh\n```\n\nThis installs a pre-push hook that runs lint checks before pushing, matching the CI workflow. To skip the hook temporarily, use `git push --no-verify`.\n\n### Building Wheels Locally\n\nTo build wheels with the bundled Claude Code CLI:\n\n```bash\n# Install build dependencies\npip install build twine\n\n# Build wheel with bundled CLI\npython scripts\/build_wheel.py\n\n# Build with specific version\npython scripts\/build_wheel.py --version 0.1.4\n\n# Build with specific CLI version\npython scripts\/build_wheel.py --cli-version 2.0.0\n\n# Clean bundled CLI after building\npython scripts\/build_wheel.py --clean\n\n# Skip CLI download (use existing)\npython scripts\/build_wheel.py --skip-download\n```\n\nThe build script:\n\n1. Downloads Claude Code CLI for your platform\n2. Bundles it in the wheel\n3. Builds both wheel and source distribution\n4. Checks the package with twine\n\nSee `python scripts\/build_wheel.py --help` for all options.\n\n### Release Workflow\n\nThe package is published to PyPI via the GitHub Actions workflow in `.github\/workflows\/publish.yml`. To create a new release:\n\n1. **Trigger the workflow** manually from the Actions tab with two inputs:\n   - `version`: The package version to publish (e.g., `0.1.5`)\n   - `claude_code_version`: The Claude Code CLI version to bundle (e.g., `2.0.0` or `latest`)\n\n2. **The workflow will**:\n   - Build platform-specific wheels for macOS, Linux, and Windows\n   - Bundle the specified Claude Code CLI version in each wheel\n   - Build a source distribution\n   - Publish all artifacts to PyPI\n   - Create a release branch with version updates\n   - Open a PR to main with:\n     - Updated `pyproject.toml` version\n     - Updated `src\/claude_agent_sdk\/_version.py`\n     - Updated `src\/claude_agent_sdk\/_cli_version.py` with bundled CLI version\n     - Auto-generated `CHANGELOG.md` entry\n\n3. **Review and merge** the release PR to update main with the new version information\n\nThe workflow tracks both the package version and the bundled CLI version separately, allowing you to release a new package version with an updated CLI without code changes.\n\n## License and terms\n\nUse of this SDK is governed by Anthropic's [Commercial Terms of Service](https:\/\/www.anthropic.com\/legal\/commercial-terms), including when you use it to power products and services that you make available to your own customers and end users, except to the extent a specific component or dependency is covered by a different license as indicated in that component's LICENSE file.\n","description_content_type":"text\/markdown","docs_url":null,"download_url":null,"downloads":{"last_day":-1,"last_month":-1,"last_week":-1},"dynamic":null,"home_page":null,"keywords":"ai, anthropic, claude, sdk","license":"MIT","license_expression":null,"license_files":["LICENSE"],"maintainer":null,"maintainer_email":null,"name":"claude-agent-sdk","package_url":"https:\/\/pypi.org\/project\/claude-agent-sdk\/","platform":null,"project_url":"https:\/\/pypi.org\/project\/claude-agent-sdk\/","project_urls":{"Documentation":"https:\/\/docs.anthropic.com\/en\/docs\/claude-code\/sdk","Homepage":"https:\/\/github.com\/anthropics\/claude-agent-sdk-python","Issues":"https:\/\/github.com\/anthropics\/claude-agent-sdk-python\/issues"},"provides_extra":["dev","examples","otel"],"release_url":"https:\/\/pypi.org\/project\/claude-agent-sdk\/0.2.167\/","requires_dist":["anyio>=4.0.0","jsonschema>=4.20.0","mcp<3.0.0,>=1.23.0","sniffio>=1.0.0","typing-extensions>=4.0.0; python_version < \"3.11\"","anyio[trio]>=4.0.0; extra == \"dev\"","mypy>=1.0.0; extra == \"dev\"","pytest-cov>=4.0.0; extra == \"dev\"","pytest>=7.0.0; extra == \"dev\"","ruff>=0.1.0; extra == \"dev\"","asyncpg>=0.27.0; extra == \"examples\"","boto3>=1.28.0; extra == \"examples\"","fakeredis>=2.20.0; extra == \"examples\"","moto[s3]>=5.0.0; extra == \"examples\"","redis>=4.2.0; extra == \"examples\"","opentelemetry-api>=1.20.0; extra == \"otel\""],"requires_python":">=3.10","summary":"Python SDK for Claude Code","version":"0.2.167","yanked":false,"yanked_reason":null},"last_serial":43861481,"ownership":{"organization":"Anthropic","roles":[{"role":"Owner","user":"dickson-ant"}]},"urls":[{"comment_text":null,"core-metadata":{"sha256":"463b790b438d3a660b296b9f074f6482f902ed26ef28352f3dff4d6817c51c07"},"digests":{"blake2b_256":"5009368d0a39a32e6396cede4943e45d6b9c58f4aa5e275bca28f5d31413b397","md5":"3ab0b19985b4a60fa053af4c0f10d117","sha256":"e9f699d6f00f5f710e696a00010d5efe5ab2de21b040b4e843cbd170f01d1008"},"downloads":-1,"filename":"claude_agent_sdk-0.2.167-py3-none-macosx_11_0_arm64.whl","has_sig":false,"md5_digest":"3ab0b19985b4a60fa053af4c0f10d117","packagetype":"bdist_wheel","python_version":"py3","requires_python":">=3.10","size":103974741,"upload_time":"2026-10-14T19:22:41","upload_time_iso_8601":"2026-10-14T19:22:41.737377Z","url":"https:\/\/files.pythonhosted.org\/packages\/50\/09\/368d0a39a32e6396cede4943e45d6b9c58f4aa5e275bca28f5d31413b397\/claude_agent_sdk-0.2.167-py3-none-macosx_11_0_arm64.whl","yanked":false,"yanked_reason":null},{"comment_text":null,"core-metadata":{"sha256":"463b790b438d3a660b296b9f074f6482f902ed26ef28352f3dff4d6817c51c07"},"digests":{"blake2b_256":"583c646f4be0524614ddf074384d5e38dcf1eecf29c66d36b4485d4aa8f4415c","md5":"eca0d6cf96586e8de1f789f9221d0a61","sha256":"2947d9fc4e5e172bcf1bb2f7582aec41c52a26f1f7cc88ac231790a80294b294"},"downloads":-1,"filename":"claude_agent_sdk-0.2.167-py3-none-macosx_11_0_x86_64.whl","has_sig":false,"md5_digest":"eca0d6cf96586e8de1f789f9221d0a61","packagetype":"bdist_wheel","python_version":"py3","requires_python":">=3.10","size":109064833,"upload_time":"2026-10-14T19:22:46","upload_time_iso_8601":"2026-10-14T19:22:46.555908Z","url":"https:\/\/files.pythonhosted.org\/packages\/58\/3c\/646f4be0524614ddf074384d5e38dcf1eecf29c66d36b4485d4aa8f4415c\/claude_agent_sdk-0.2.167-py3-none-macosx_11_0_x86_64.whl","yanked":false,"yanked_reason":null},{"comment_text":null,"core-metadata":{"sha256":"463b790b438d3a660b296b9f074f6482f902ed26ef28352f3dff4d6817c51c07"},"digests":{"blake2b_256":"ddea37967c0c53ba83a8041ae2a1fbdd054ec38ae80436de75d84d6b55a809e3","md5":"94701bc4254bf4310230cb3b9773c2b8","sha256":"5dc2e3f08d78913a05ab7dea3e7fe8f5614813a8311f7a098cab6434e0b3b4b4"},"downloads":-1,"filename":"claude_agent_sdk-0.2.167-py3-none-manylinux_2_17_aarch64.whl","has_sig":false,"md5_digest":"94701bc4254bf4310230cb3b9773c2b8","packagetype":"bdist_wheel","python_version":"py3","requires_python":">=3.10","size":113650816,"upload_time":"2026-10-14T19:22:51","upload_time_iso_8601":"2026-10-14T19:22:51.385367Z","url":"https:\/\/files.pythonhosted.org\/packages\/dd\/ea\/37967c0c53ba83a8041ae2a1fbdd054ec38ae80436de75d84d6b55a809e3\/claude_agent_sdk-0.2.167-py3-none-manylinux_2_17_aarch64.whl","yanked":false,"yanked_reason":null},{"comment_text":null,"core-metadata":{"sha256":"463b790b438d3a660b296b9f074f6482f902ed26ef28352f3dff4d6817c51c07"},"digests":{"blake2b_256":"0682e6188eb3705ecb5ec4a8035ee1e5f02d0142b8a9dc835fc6d54b981d1201","md5":"139e47fca7c6e289cf6ba883ed44d46e","sha256":"e3a6aaa40b36aea29fef6d4a96ad1bcfc1700b394896e308c4f261f52b805b7c"},"downloads":-1,"filename":"claude_agent_sdk-0.2.167-py3-none-manylinux_2_17_x86_64.whl","has_sig":false,"md5_digest":"139e47fca7c6e289cf6ba883ed44d46e","packagetype":"bdist_wheel","python_version":"py3","requires_python":">=3.10","size":114067091,"upload_time":"2026-10-14T19:22:57","upload_time_iso_8601":"2026-10-14T19:22:57.345473Z","url":"https:\/\/files.pythonhosted.org\/packages\/06\/82\/e6188eb3705ecb5ec4a8035ee1e5f02d0142b8a9dc835fc6d54b981d1201\/claude_agent_sdk-0.2.167-py3-none-manylinux_2_17_x86_64.whl","yanked":false,"yanked_reason":null},{"comment_text":null,"core-metadata":{"sha256":"463b790b438d3a660b296b9f074f6482f902ed26ef28352f3dff4d6817c51c07"},"digests":{"blake2b_256":"061def41f5a385ad12ebb93427c847b10b6d00510e2be9a94e52fc2269aab704","md5":"271e3bb4c2f38b15c7caa7e1737e734d","sha256":"139597261e6d7329c3a99fbadcbb6e7835f40bda77fa7d34db53888beef777b8"},"downloads":-1,"filename":"claude_agent_sdk-0.2.167-py3-none-win_amd64.whl","has_sig":false,"md5_digest":"271e3bb4c2f38b15c7caa7e1737e734d","packagetype":"bdist_wheel","python_version":"py3","requires_python":">=3.10","size":116912913,"upload_time":"2026-10-14T19:23:01","upload_time_iso_8601":"2026-10-14T19:23:01.927926Z","url":"https:\/\/files.pythonhosted.org\/packages\/06\/1d\/ef41f5a385ad12ebb93427c847b10b6d00510e2be9a94e52fc2269aab704\/claude_agent_sdk-0.2.167-py3-none-win_amd64.whl","yanked":false,"yanked_reason":null},{"comment_text":null,"core-metadata":false,"digests":{"blake2b_256":"bacd0dd391195a3d2c7aed32165ec23458248f541c0fd115b44b37e2a31b08e2","md5":"2e83b7af98a3ae6fa671a0f8945cc669","sha256":"06c9b8137f39c5dd8d0959321267c1de006f0458f5a27f645dfbffe479f8e207"},"downloads":-1,"filename":"claude_agent_sdk-0.2.167.tar.gz","has_sig":false,"md5_digest":"2e83b7af98a3ae6fa671a0f8945cc669","packagetype":"sdist","python_version":"source","requires_python":">=3.10","size":371584,"upload_time":"2026-10-14T19:22:37","upload_time_iso_8601":"2026-10-14T19:22:37.699151Z","url":"https:\/\/files.pythonhosted.org\/packages\/ba\/cd\/0dd391195a3d2c7aed32165ec23458248f541c0fd115b44b37e2a31b08e2\/claude_agent_sdk-0.2.167.tar.gz","yanked":false,"yanked_reason":null}],"vulnerabilities":[]}