1> ## Documentation Index
2> Fetch the complete documentation index at: https://code.claude.com/docs/llms.txt
3> Use this file to discover all available pages before exploring further.
4
5# Agent SDK reference - Python
6
7> Complete API reference for the Python Agent SDK, including all functions, types, and classes.
8
9## Installation
10
11```bash theme={null}
12pip install claude-agent-sdk
13```
14
15## Choosing between `query()` and `ClaudeSDKClient`
16
17The Python SDK provides two ways to interact with Claude Code:
18
19### Quick comparison
20
21| Feature | `query()` | `ClaudeSDKClient` |
22| :------------------ | :---------------------------- | :--------------------------------- |
23| **Session** | Creates new session each time | Reuses same session |
24| **Conversation** | Single exchange | Multiple exchanges in same context |
25| **Connection** | Managed automatically | Manual control |
26| **Streaming Input** | ✅ Supported | ✅ Supported |
27| **Interrupts** | ❌ Not supported | ✅ Supported |
28| **Hooks** | ✅ Supported | ✅ Supported |
29| **Custom Tools** | ✅ Supported | ✅ Supported |
30| **Continue Chat** | ❌ New session each time | ✅ Maintains conversation |
31| **Use Case** | One-off tasks | Continuous conversations |
32
33### When to use `query()` (new session each time)
34
35**Best for:**
36
37* One-off questions where you don't need conversation history
38* Independent tasks that don't require context from previous exchanges
39* Simple automation scripts
40* When you want a fresh start each time
41
42### When to use `ClaudeSDKClient` (continuous conversation)
43
44**Best for:**
45
46* **Continuing conversations** - When you need Claude to remember context
47* **Follow-up questions** - Building on previous responses
48* **Interactive applications** - Chat interfaces, REPLs
49* **Response-driven logic** - When next action depends on Claude's response
50* **Session control** - Managing conversation lifecycle explicitly
51
52## Functions
53
54### `query()`
55
56Creates a new session for each interaction with Claude Code. Returns an async iterator that yields messages as they arrive. Each call to `query()` starts fresh with no memory of previous interactions.
57
58```python theme={null}
59async def query(
60 *,
61 prompt: str | AsyncIterable[dict[str, Any]],
62 options: ClaudeAgentOptions | None = None,
63 transport: Transport | None = None
64) -> AsyncIterator[Message]
65```
66
67#### Parameters
68
69| Parameter | Type | Description |
70| :---------- | :--------------------------- | :------------------------------------------------------------------------- |
71| `prompt` | `str \| AsyncIterable[dict]` | The input prompt as a string or async iterable for streaming mode |
72| `options` | `ClaudeAgentOptions \| None` | Optional configuration object (defaults to `ClaudeAgentOptions()` if None) |
73| `transport` | `Transport \| None` | Optional custom transport for communicating with the CLI process |
74
75#### Returns
76
77Returns an `AsyncIterator[Message]` that yields messages from the conversation.
78
79#### Example - With options
80
81```python theme={null}
82import asyncio
83from claude_agent_sdk import query, ClaudeAgentOptions
84
85
86async def main():
87 options = ClaudeAgentOptions(
88 system_prompt="You are an expert Python developer",
89 permission_mode="acceptEdits",
90 cwd="/home/user/project",
91 )
92
93 async for message in query(prompt="Create a Python web server", options=options):
94 print(message)
95
96
97asyncio.run(main())
98```
99
100### `tool()`
101
102Decorator for defining MCP tools with type safety.
103
104```python theme={null}
105def tool(
106 name: str,
107 description: str,
108 input_schema: type | dict[str, Any],
109 annotations: ToolAnnotations | None = None
110) -> Callable[[Callable[[Any], Awaitable[dict[str, Any]]]], SdkMcpTool[Any]]
111```
112
113#### Parameters
114
115| Parameter | Type | Description |
116| :------------- | :----------------------------------------------- | :------------------------------------------------------------------ |
117| `name` | `str` | Unique identifier for the tool |
118| `description` | `str` | Human-readable description of what the tool does |
119| `input_schema` | `type \| dict[str, Any]` | Schema defining the tool's input parameters (see below) |
120| `annotations` | [`ToolAnnotations`](#tool-annotations)` \| None` | Optional MCP tool annotations providing behavioral hints to clients |
121
122#### Input schema options
123
1241. **Simple type mapping** (recommended):
125
126 ```python theme={null}
127 {"text": str, "count": int, "enabled": bool}
128 ```
129
1302. **JSON Schema format** (for complex validation):
131 ```python theme={null}
132 {
133 "type": "object",
134 "properties": {
135 "text": {"type": "string"},
136 "count": {"type": "integer", "minimum": 0},
137 },
138 "required": ["text"],
139 }
140 ```
141
142#### Returns
143
144A decorator function that wraps the tool implementation and returns an `SdkMcpTool` instance.
145
146#### Example
147
148```python theme={null}
149from claude_agent_sdk import tool
150from typing import Any
151
152
153@tool("greet", "Greet a user", {"name": str})
154async def greet(args: dict[str, Any]) -> dict[str, Any]:
155 return {"content": [{"type": "text", "text": f"Hello, {args['name']}!"}]}
156```
157
158#### `ToolAnnotations`
159
160Re-exported from `mcp.types` (also available as `from claude_agent_sdk import ToolAnnotations`). All fields are optional hints; clients should not rely on them for security decisions.
161
162| Field | Type | Default | Description |
163| :---------------- | :------------- | :------ | :--------------------------------------------------------------------------------------------------------------------------------------------------- |
164| `title` | `str \| None` | `None` | Human-readable title for the tool |
165| `readOnlyHint` | `bool \| None` | `False` | If `True`, the tool does not modify its environment |
166| `destructiveHint` | `bool \| None` | `True` | If `True`, the tool may perform destructive updates (only meaningful when `readOnlyHint` is `False`) |
167| `idempotentHint` | `bool \| None` | `False` | If `True`, repeated calls with the same arguments have no additional effect (only meaningful when `readOnlyHint` is `False`) |
168| `openWorldHint` | `bool \| None` | `True` | If `True`, the tool interacts with external entities (for example, web search). If `False`, the tool's domain is closed (for example, a memory tool) |
169
170```python theme={null}
171from claude_agent_sdk import tool, ToolAnnotations
172from typing import Any
173
174
175@tool(
176 "search",
177 "Search the web",
178 {"query": str},
179 annotations=ToolAnnotations(readOnlyHint=True, openWorldHint=True),
180)
181async def search(args: dict[str, Any]) -> dict[str, Any]:
182 return {"content": [{"type": "text", "text": f"Results for: {args['query']}"}]}
183```
184
185### `create_sdk_mcp_server()`
186
187Create an in-process MCP server that runs within your Python application.
188
189```python theme={null}
190def create_sdk_mcp_server(
191 name: str,
192 version: str = "1.0.0",
193 tools: list[SdkMcpTool[Any]] | None = None
194) -> McpSdkServerConfig
195```
196
197#### Parameters
198
199| Parameter | Type | Default | Description |
200| :-------- | :------------------------------ | :-------- | :---------------------------------------------------- |
201| `name` | `str` | - | Unique identifier for the server |
202| `version` | `str` | `"1.0.0"` | Server version string |
203| `tools` | `list[SdkMcpTool[Any]] \| None` | `None` | List of tool functions created with `@tool` decorator |
204
205#### Returns
206
207Returns an `McpSdkServerConfig` object that can be passed to `ClaudeAgentOptions.mcp_servers`.
208
209#### Example
210
211```python theme={null}
212from claude_agent_sdk import tool, create_sdk_mcp_server
213
214
215@tool("add", "Add two numbers", {"a": float, "b": float})
216async def add(args):
217 return {"content": [{"type": "text", "text": f"Sum: {args['a'] + args['b']}"}]}
218
219
220@tool("multiply", "Multiply two numbers", {"a": float, "b": float})
221async def multiply(args):
222 return {"content": [{"type": "text", "text": f"Product: {args['a'] * args['b']}"}]}
223
224
225calculator = create_sdk_mcp_server(
226 name="calculator",
227 version="2.0.0",
228 tools=[add, multiply], # Pass decorated functions
229)
230
231# Use with Claude
232options = ClaudeAgentOptions(
233 mcp_servers={"calc": calculator},
234 allowed_tools=["mcp__calc__add", "mcp__calc__multiply"],
235)
236```
237
238### `list_sessions()`
239
240Lists past sessions with metadata. Filter by project directory or list sessions across all projects. Synchronous; returns immediately.
241
242```python theme={null}
243def list_sessions(
244 directory: str | None = None,
245 limit: int | None = None,
246 include_worktrees: bool = True
247) -> list[SDKSessionInfo]
248```
249
250#### Parameters
251
252| Parameter | Type | Default | Description |
253| :------------------ | :------------ | :------ | :------------------------------------------------------------------------------------ |
254| `directory` | `str \| None` | `None` | Directory to list sessions for. When omitted, returns sessions across all projects |
255| `limit` | `int \| None` | `None` | Maximum number of sessions to return |
256| `include_worktrees` | `bool` | `True` | When `directory` is inside a git repository, include sessions from all worktree paths |
257
258#### Return type: `SDKSessionInfo`
259
260| Property | Type | Description |
261| :-------------- | :------------ | :------------------------------------------------------------------- |
262| `session_id` | `str` | Unique session identifier |
263| `summary` | `str` | Display title: custom title, auto-generated summary, or first prompt |
264| `last_modified` | `int` | Last modified time in milliseconds since epoch |
265| `file_size` | `int \| None` | Session file size in bytes (`None` for remote storage backends) |
266| `custom_title` | `str \| None` | User-set session title |
267| `first_prompt` | `str \| None` | First meaningful user prompt in the session |
268| `git_branch` | `str \| None` | Git branch at the end of the session |
269| `cwd` | `str \| None` | Working directory for the session |
270| `tag` | `str \| None` | User-set session tag (see [`tag_session()`](#tag-session)) |
271| `created_at` | `int \| None` | Session creation time in milliseconds since epoch |
272
273#### Example
274
275Print the 10 most recent sessions for a project. Results are sorted by `last_modified` descending, so the first item is the newest. Omit `directory` to search across all projects.
276
277```python theme={null}
278from claude_agent_sdk import list_sessions
279
280for session in list_sessions(directory="/path/to/project", limit=10):
281 print(f"{session.summary} ({session.session_id})")
282```
283
284### `get_session_messages()`
285
286Retrieves messages from a past session. Synchronous; returns immediately.
287
288```python theme={null}
289def get_session_messages(
290 session_id: str,
291 directory: str | None = None,
292 limit: int | None = None,
293 offset: int = 0
294) -> list[SessionMessage]
295```
296
297#### Parameters
298
299| Parameter | Type | Default | Description |
300| :----------- | :------------ | :------- | :---------------------------------------------------------------- |
301| `session_id` | `str` | required | The session ID to retrieve messages for |
302| `directory` | `str \| None` | `None` | Project directory to look in. When omitted, searches all projects |
303| `limit` | `int \| None` | `None` | Maximum number of messages to return |
304| `offset` | `int` | `0` | Number of messages to skip from the start |
305
306#### Return type: `SessionMessage`
307
308| Property | Type | Description |
309| :------------------- | :----------------------------- | :------------------------ |
310| `type` | `Literal["user", "assistant"]` | Message role |
311| `uuid` | `str` | Unique message identifier |
312| `session_id` | `str` | Session identifier |
313| `message` | `Any` | Raw message content |
314| `parent_tool_use_id` | `None` | Reserved for future use |
315
316#### Example
317
318```python theme={null}
319from claude_agent_sdk import list_sessions, get_session_messages
320
321sessions = list_sessions(limit=1)
322if sessions:
323 messages = get_session_messages(sessions[0].session_id)
324 for msg in messages:
325 print(f"[{msg.type}] {msg.uuid}")
326```
327
328### `get_session_info()`
329
330Reads metadata for a single session by ID without scanning the full project directory. Synchronous; returns immediately.
331
332```python theme={null}
333def get_session_info(
334 session_id: str,
335 directory: str | None = None,
336) -> SDKSessionInfo | None
337```
338
339#### Parameters
340
341| Parameter | Type | Default | Description |
342| :----------- | :------------ | :------- | :--------------------------------------------------------------------- |
343| `session_id` | `str` | required | UUID of the session to look up |
344| `directory` | `str \| None` | `None` | Project directory path. When omitted, searches all project directories |
345
346Returns [`SDKSessionInfo`](#return-type-sdk-session-info), or `None` if the session is not found.
347
348#### Example
349
350Look up a single session's metadata without scanning the project directory. Useful when you already have a session ID from a previous run.
351
352```python theme={null}
353from claude_agent_sdk import get_session_info
354
355info = get_session_info("550e8400-e29b-41d4-a716-446655440000")
356if info:
357 print(f"{info.summary} (branch: {info.git_branch}, tag: {info.tag})")
358```
359
360### `rename_session()`
361
362Renames a session by appending a custom-title entry. Repeated calls are safe; the most recent title wins. Synchronous.
363
364```python theme={null}
365def rename_session(
366 session_id: str,
367 title: str,
368 directory: str | None = None,
369) -> None
370```
371
372#### Parameters
373
374| Parameter | Type | Default | Description |
375| :----------- | :------------ | :------- | :--------------------------------------------------------------------- |
376| `session_id` | `str` | required | UUID of the session to rename |
377| `title` | `str` | required | New title. Must be non-empty after stripping whitespace |
378| `directory` | `str \| None` | `None` | Project directory path. When omitted, searches all project directories |
379
380Raises `ValueError` if `session_id` is not a valid UUID or `title` is empty; `FileNotFoundError` if the session cannot be found.
381
382#### Example
383
384Rename the most recent session so it's easier to find later. The new title appears in [`SDKSessionInfo.custom_title`](#return-type-sdk-session-info) on subsequent reads.
385
386```python theme={null}
387from claude_agent_sdk import list_sessions, rename_session
388
389sessions = list_sessions(directory="/path/to/project", limit=1)
390if sessions:
391 rename_session(sessions[0].session_id, "Refactor auth module")
392```
393
394### `tag_session()`
395
396Tags a session. Pass `None` to clear the tag. Repeated calls are safe; the most recent tag wins. Synchronous.
397
398```python theme={null}
399def tag_session(
400 session_id: str,
401 tag: str | None,
402 directory: str | None = None,
403) -> None
404```
405
406#### Parameters
407
408| Parameter | Type | Default | Description |
409| :----------- | :------------ | :------- | :--------------------------------------------------------------------- |
410| `session_id` | `str` | required | UUID of the session to tag |
411| `tag` | `str \| None` | required | Tag string, or `None` to clear. Unicode-sanitized before storing |
412| `directory` | `str \| None` | `None` | Project directory path. When omitted, searches all project directories |
413
414Raises `ValueError` if `session_id` is not a valid UUID or `tag` is empty after sanitization; `FileNotFoundError` if the session cannot be found.
415
416#### Example
417
418Tag a session, then filter by that tag on a later read. Pass `None` to clear an existing tag.
419
420```python theme={null}
421from claude_agent_sdk import list_sessions, tag_session
422
423# Tag a session
424tag_session("550e8400-e29b-41d4-a716-446655440000", "needs-review")
425
426# Later: find all sessions with that tag
427for session in list_sessions(directory="/path/to/project"):
428 if session.tag == "needs-review":
429 print(session.summary)
430```
431
432## Classes
433
434### `ClaudeSDKClient`
435
436**Maintains a conversation session across multiple exchanges.** This is the Python equivalent of how the TypeScript SDK's `query()` function works internally - it creates a client object that can continue conversations.
437
438#### Key Features
439
440* **Session continuity**: Maintains conversation context across multiple `query()` calls
441* **Same conversation**: The session retains previous messages
442* **Interrupt support**: Can stop execution mid-task
443* **Explicit lifecycle**: You control when the session starts and ends
444* **Response-driven flow**: Can react to responses and send follow-ups
445* **Custom tools and hooks**: Supports custom tools (created with `@tool` decorator) and hooks
446
447```python theme={null}
448class ClaudeSDKClient:
449 def __init__(self, options: ClaudeAgentOptions | None = None, transport: Transport | None = None)
450 async def connect(self, prompt: str | AsyncIterable[dict] | None = None) -> None
451 async def query(self, prompt: str | AsyncIterable[dict], session_id: str = "default") -> None
452 async def receive_messages(self) -> AsyncIterator[Message]
453 async def receive_response(self) -> AsyncIterator[Message]
454 async def interrupt(self) -> None
455 async def set_permission_mode(self, mode: str) -> None
456 async def set_model(self, model: str | None = None) -> None
457 async def rewind_files(self, user_message_id: str) -> None
458 async def get_mcp_status(self) -> McpStatusResponse
459 async def reconnect_mcp_server(self, server_name: str) -> None
460 async def toggle_mcp_server(self, server_name: str, enabled: bool) -> None
461 async def stop_task(self, task_id: str) -> None
462 async def get_server_info(self) -> dict[str, Any] | None
463 async def disconnect(self) -> None
464```
465
466#### Methods
467
468| Method | Description |
469| :---------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
470| `__init__(options)` | Initialize the client with optional configuration |
471| `connect(prompt)` | Connect to Claude with an optional initial prompt or message stream |
472| `query(prompt, session_id)` | Send a new request in streaming mode |
473| `receive_messages()` | Receive all messages from Claude as an async iterator |
474| `receive_response()` | Receive messages until and including a ResultMessage |
475| `interrupt()` | Send interrupt signal (only works in streaming mode) |
476| `set_permission_mode(mode)` | Change the permission mode for the current session |
477| `set_model(model)` | Change the model for the current session. Pass `None` to reset to default |
478| `rewind_files(user_message_id)` | Restore files to their state at the specified user message. Requires `enable_file_checkpointing=True`. See [File checkpointing](/en/agent-sdk/file-checkpointing) |
479| `get_mcp_status()` | Get the status of all configured MCP servers. Returns [`McpStatusResponse`](#mcp-status-response) |
480| `reconnect_mcp_server(server_name)` | Retry connecting to an MCP server that failed or was disconnected |
481| `toggle_mcp_server(server_name, enabled)` | Enable or disable an MCP server mid-session. Disabling removes its tools |
482| `stop_task(task_id)` | Stop a running background task. A [`TaskNotificationMessage`](#task-notification-message) with status `"stopped"` follows in the message stream |
483| `get_server_info()` | Get server information including session ID and capabilities |
484| `disconnect()` | Disconnect from Claude |
485
486#### Context Manager Support
487
488The client can be used as an async context manager for automatic connection management:
489
490```python theme={null}
491async with ClaudeSDKClient() as client:
492 await client.query("Hello Claude")
493 async for message in client.receive_response():
494 print(message)
495```
496
497> **Important:** When iterating over messages, avoid using `break` to exit early as this can cause asyncio cleanup issues. Instead, let the iteration complete naturally or use flags to track when you've found what you need.
498
499#### Example - Continuing a conversation
500
501```python theme={null}
502import asyncio
503from claude_agent_sdk import ClaudeSDKClient, AssistantMessage, TextBlock, ResultMessage
504
505
506async def main():
507 async with ClaudeSDKClient() as client:
508 # First question
509 await client.query("What's the capital of France?")
510
511 # Process response
512 async for message in client.receive_response():
513 if isinstance(message, AssistantMessage):
514 for block in message.content:
515 if isinstance(block, TextBlock):
516 print(f"Claude: {block.text}")
517
518 # Follow-up question - the session retains the previous context
519 await client.query("What's the population of that city?")
520
521 async for message in client.receive_response():
522 if isinstance(message, AssistantMessage):
523 for block in message.content:
524 if isinstance(block, TextBlock):
525 print(f"Claude: {block.text}")
526
527 # Another follow-up - still in the same conversation
528 await client.query("What are some famous landmarks there?")
529
530 async for message in client.receive_response():
531 if isinstance(message, AssistantMessage):
532 for block in message.content:
533 if isinstance(block, TextBlock):
534 print(f"Claude: {block.text}")
535
536
537asyncio.run(main())
538```
539
540#### Example - Streaming input with ClaudeSDKClient
541
542```python theme={null}
543import asyncio
544from claude_agent_sdk import ClaudeSDKClient
545
546
547async def message_stream():
548 """Generate messages dynamically."""
549 yield {
550 "type": "user",
551 "message": {"role": "user", "content": "Analyze the following data:"},
552 }
553 await asyncio.sleep(0.5)
554 yield {
555 "type": "user",
556 "message": {"role": "user", "content": "Temperature: 25°C, Humidity: 60%"},
557 }
558 await asyncio.sleep(0.5)
559 yield {
560 "type": "user",
561 "message": {"role": "user", "content": "What patterns do you see?"},
562 }
563
564
565async def main():
566 async with ClaudeSDKClient() as client:
567 # Stream input to Claude
568 await client.query(message_stream())
569
570 # Process response
571 async for message in client.receive_response():
572 print(message)
573
574 # Follow-up in same session
575 await client.query("Should we be concerned about these readings?")
576
577 async for message in client.receive_response():
578 print(message)
579
580
581asyncio.run(main())
582```
583
584#### Example - Using interrupts
585
586```python theme={null}
587import asyncio
588from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions, ResultMessage
589
590
591async def interruptible_task():
592 options = ClaudeAgentOptions(allowed_tools=["Bash"], permission_mode="acceptEdits")
593
594 async with ClaudeSDKClient(options=options) as client:
595 # Start a long-running task
596 await client.query("Count from 1 to 100 slowly, using the bash sleep command")
597
598 # Let it run for a bit
599 await asyncio.sleep(2)
600
601 # Interrupt the task
602 await client.interrupt()
603 print("Task interrupted!")
604
605 # Drain the interrupted task's messages (including its ResultMessage)
606 async for message in client.receive_response():
607 if isinstance(message, ResultMessage):
608 print(f"Interrupted task finished with subtype={message.subtype!r}")
609 # subtype is "error_during_execution" for interrupted tasks
610
611 # Send a new command
612 await client.query("Just say hello instead")
613
614 # Now receive the new response
615 async for message in client.receive_response():
616 if isinstance(message, ResultMessage) and message.subtype == "success":
617 print(f"New result: {message.result}")
618
619
620asyncio.run(interruptible_task())
621```
622
623<Note>
624 **Buffer behavior after interrupt:** `interrupt()` sends a stop signal but does not clear the message buffer. Messages already produced by the interrupted task, including its `ResultMessage` (with `subtype="error_during_execution"`), remain in the stream. You must drain them with `receive_response()` before reading the response to a new query. If you send a new query immediately after `interrupt()` and call `receive_response()` only once, you'll receive the interrupted task's messages, not the new query's response.
625</Note>
626
627#### Example - Advanced permission control
628
629```python theme={null}
630from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions
631from claude_agent_sdk.types import (
632 PermissionResultAllow,
633 PermissionResultDeny,
634 ToolPermissionContext,
635)
636
637
638async def custom_permission_handler(
639 tool_name: str, input_data: dict, context: ToolPermissionContext
640) -> PermissionResultAllow | PermissionResultDeny:
641 """Custom logic for tool permissions."""
642
643 # Block writes to system directories
644 if tool_name == "Write" and input_data.get("file_path", "").startswith("/system/"):
645 return PermissionResultDeny(
646 message="System directory write not allowed", interrupt=True
647 )
648
649 # Redirect sensitive file operations
650 if tool_name in ["Write", "Edit"] and "config" in input_data.get("file_path", ""):
651 safe_path = f"./sandbox/{input_data['file_path']}"
652 return PermissionResultAllow(
653 updated_input={**input_data, "file_path": safe_path}
654 )
655
656 # Allow everything else
657 return PermissionResultAllow(updated_input=input_data)
658
659
660async def main():
661 options = ClaudeAgentOptions(
662 can_use_tool=custom_permission_handler, allowed_tools=["Read", "Write", "Edit"]
663 )
664
665 async with ClaudeSDKClient(options=options) as client:
666 await client.query("Update the system config file")
667
668 async for message in client.receive_response():
669 # Will use sandbox path instead
670 print(message)
671
672
673asyncio.run(main())
674```
675
676## Types
677
678<Note>
679 **`@dataclass` vs `TypedDict`:** This SDK uses two kinds of types. Classes decorated with `@dataclass` (such as `ResultMessage`, `AgentDefinition`, `TextBlock`) are object instances at runtime and support attribute access: `msg.result`. Classes defined with `TypedDict` (such as `ThinkingConfigEnabled`, `McpStdioServerConfig`, `SyncHookJSONOutput`) are **plain dicts at runtime** and require key access: `config["budget_tokens"]`, not `config.budget_tokens`. The `ClassName(field=value)` call syntax works for both, but only dataclasses produce objects with attributes.
680</Note>
681
682### `SdkMcpTool`
683
684Definition for an SDK MCP tool created with the `@tool` decorator.
685
686```python theme={null}
687@dataclass
688class SdkMcpTool(Generic[T]):
689 name: str
690 description: str
691 input_schema: type[T] | dict[str, Any]
692 handler: Callable[[T], Awaitable[dict[str, Any]]]
693 annotations: ToolAnnotations | None = None
694```
695
696| Property | Type | Description |
697| :------------- | :----------------------------------------- | :--------------------------------------------------------------------------------------------------------- |
698| `name` | `str` | Unique identifier for the tool |
699| `description` | `str` | Human-readable description |
700| `input_schema` | `type[T] \| dict[str, Any]` | Schema for input validation |
701| `handler` | `Callable[[T], Awaitable[dict[str, Any]]]` | Async function that handles tool execution |
702| `annotations` | `ToolAnnotations \| None` | Optional MCP tool annotations (e.g., `readOnlyHint`, `destructiveHint`, `openWorldHint`). From `mcp.types` |
703
704### `Transport`
705
706Abstract base class for custom transport implementations. Use this to communicate with the Claude process over a custom channel (for example, a remote connection instead of a local subprocess).
707
708<Warning>
709 This is a low-level internal API. The interface may change in future releases. Custom implementations must be updated to match any interface changes.
710</Warning>
711
712```python theme={null}
713from abc import ABC, abstractmethod
714from collections.abc import AsyncIterator
715from typing import Any
716
717
718class Transport(ABC):
719 @abstractmethod
720 async def connect(self) -> None: ...
721
722 @abstractmethod
723 async def write(self, data: str) -> None: ...
724
725 @abstractmethod
726 def read_messages(self) -> AsyncIterator[dict[str, Any]]: ...
727
728 @abstractmethod
729 async def close(self) -> None: ...
730
731 @abstractmethod
732 def is_ready(self) -> bool: ...
733
734 @abstractmethod
735 async def end_input(self) -> None: ...
736```
737
738| Method | Description |
739| :---------------- | :-------------------------------------------------------------------------- |
740| `connect()` | Connect the transport and prepare for communication |
741| `write(data)` | Write raw data (JSON + newline) to the transport |
742| `read_messages()` | Async iterator that yields parsed JSON messages |
743| `close()` | Close the connection and clean up resources |
744| `is_ready()` | Returns `True` if the transport can send and receive |
745| `end_input()` | Close the input stream (for example, close stdin for subprocess transports) |
746
747Import: `from claude_agent_sdk import Transport`
748
749### `ClaudeAgentOptions`
750
751Configuration dataclass for Claude Code queries.
752
753```python theme={null}
754@dataclass
755class ClaudeAgentOptions:
756 tools: list[str] | ToolsPreset | None = None
757 allowed_tools: list[str] = field(default_factory=list)
758 system_prompt: str | SystemPromptPreset | None = None
759 mcp_servers: dict[str, McpServerConfig] | str | Path = field(default_factory=dict)
760 permission_mode: PermissionMode | None = None
761 continue_conversation: bool = False
762 resume: str | None = None
763 max_turns: int | None = None
764 max_budget_usd: float | None = None
765 disallowed_tools: list[str] = field(default_factory=list)
766 model: str | None = None
767 fallback_model: str | None = None
768 betas: list[SdkBeta] = field(default_factory=list)
769 output_format: dict[str, Any] | None = None
770 permission_prompt_tool_name: str | None = None
771 cwd: str | Path | None = None
772 cli_path: str | Path | None = None
773 settings: str | None = None
774 add_dirs: list[str | Path] = field(default_factory=list)
775 env: dict[str, str] = field(default_factory=dict)
776 extra_args: dict[str, str | None] = field(default_factory=dict)
777 max_buffer_size: int | None = None
778 debug_stderr: Any = sys.stderr # Deprecated
779 stderr: Callable[[str], None] | None = None
780 can_use_tool: CanUseTool | None = None
781 hooks: dict[HookEvent, list[HookMatcher]] | None = None
782 user: str | None = None
783 include_partial_messages: bool = False
784 fork_session: bool = False
785 agents: dict[str, AgentDefinition] | None = None
786 setting_sources: list[SettingSource] | None = None
787 sandbox: SandboxSettings | None = None
788 plugins: list[SdkPluginConfig] = field(default_factory=list)
789 max_thinking_tokens: int | None = None # Deprecated: use thinking instead
790 thinking: ThinkingConfig | None = None
791 effort: Literal["low", "medium", "high", "max"] | None = None
792 enable_file_checkpointing: bool = False
793 session_store: SessionStore | None = None
794```
795
796| Property | Type | Default | Description |
797| :---------------------------- | :------------------------------------------------------------------------------------- | :--------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
798| `tools` | `list[str] \| ToolsPreset \| None` | `None` | Tools configuration. Use `{"type": "preset", "preset": "claude_code"}` for Claude Code's default tools |
799| `allowed_tools` | `list[str]` | `[]` | Tools to auto-approve without prompting. This does not restrict Claude to only these tools; unlisted tools fall through to `permission_mode` and `can_use_tool`. Use `disallowed_tools` to block tools. See [Permissions](/en/agent-sdk/permissions#allow-and-deny-rules) |
800| `system_prompt` | `str \| SystemPromptPreset \| None` | `None` | System prompt configuration. Pass a string for custom prompt, or use `{"type": "preset", "preset": "claude_code"}` for Claude Code's system prompt. Add `"append"` to extend the preset |
801| `mcp_servers` | `dict[str, McpServerConfig] \| str \| Path` | `{}` | MCP server configurations or path to config file |
802| `permission_mode` | `PermissionMode \| None` | `None` | Permission mode for tool usage |
803| `continue_conversation` | `bool` | `False` | Continue the most recent conversation |
804| `resume` | `str \| None` | `None` | Session ID to resume |
805| `max_turns` | `int \| None` | `None` | Maximum agentic turns (tool-use round trips) |
806| `max_budget_usd` | `float \| None` | `None` | Stop the query when the client-side cost estimate reaches this USD value. Compared against the same estimate as `total_cost_usd`; see [Track cost and usage](/en/agent-sdk/cost-tracking) for accuracy caveats |
807| `disallowed_tools` | `list[str]` | `[]` | Tools to always deny. Deny rules are checked first and override `allowed_tools` and `permission_mode` (including `bypassPermissions`) |
808| `enable_file_checkpointing` | `bool` | `False` | Enable file change tracking for rewinding. See [File checkpointing](/en/agent-sdk/file-checkpointing) |
809| `model` | `str \| None` | `None` | Claude model to use |
810| `fallback_model` | `str \| None` | `None` | Fallback model to use if the primary model fails |
811| `betas` | `list[SdkBeta]` | `[]` | Beta features to enable. See [`SdkBeta`](#sdk-beta) for available options |
812| `output_format` | `dict[str, Any] \| None` | `None` | Output format for structured responses (e.g., `{"type": "json_schema", "schema": {...}}`). See [Structured outputs](/en/agent-sdk/structured-outputs) for details |
813| `permission_prompt_tool_name` | `str \| None` | `None` | MCP tool name for permission prompts |
814| `cwd` | `str \| Path \| None` | `None` | Current working directory |
815| `cli_path` | `str \| Path \| None` | `None` | Custom path to the Claude Code CLI executable |
816| `settings` | `str \| None` | `None` | Path to settings file |
817| `add_dirs` | `list[str \| Path]` | `[]` | Additional directories Claude can access |
818| `env` | `dict[str, str]` | `{}` | Environment variables merged on top of the inherited process environment. See [Environment variables](/en/env-vars) for variables the underlying CLI reads |
819| `extra_args` | `dict[str, str \| None]` | `{}` | Additional CLI arguments to pass directly to the CLI |
820| `max_buffer_size` | `int \| None` | `None` | Maximum bytes when buffering CLI stdout |
821| `debug_stderr` | `Any` | `sys.stderr` | *Deprecated* - File-like object for debug output. Use `stderr` callback instead |
822| `stderr` | `Callable[[str], None] \| None` | `None` | Callback function for stderr output from CLI |
823| `can_use_tool` | [`CanUseTool`](#can-use-tool) ` \| None` | `None` | Tool permission callback function. See [Permission types](#can-use-tool) for details |
824| `hooks` | `dict[HookEvent, list[HookMatcher]] \| None` | `None` | Hook configurations for intercepting events |
825| `user` | `str \| None` | `None` | User identifier |
826| `include_partial_messages` | `bool` | `False` | Include partial message streaming events. When enabled, [`StreamEvent`](#stream-event) messages are yielded |
827| `fork_session` | `bool` | `False` | When resuming with `resume`, fork to a new session ID instead of continuing the original session |
828| `agents` | `dict[str, AgentDefinition] \| None` | `None` | Programmatically defined subagents |
829| `plugins` | `list[SdkPluginConfig]` | `[]` | Load custom plugins from local paths. See [Plugins](/en/agent-sdk/plugins) for details |
830| `sandbox` | [`SandboxSettings`](#sandbox-settings) ` \| None` | `None` | Configure sandbox behavior programmatically. See [Sandbox settings](#sandbox-settings) for details |
831| `setting_sources` | `list[SettingSource] \| None` | `None` (CLI defaults: all sources) | Control which filesystem settings to load. Pass `[]` to disable user, project, and local settings. Managed policy settings load regardless. See [Use Claude Code features](/en/agent-sdk/claude-code-features#what-settingsources-does-not-control) |
832| `max_thinking_tokens` | `int \| None` | `None` | *Deprecated* - Maximum tokens for thinking blocks. Use `thinking` instead |
833| `thinking` | [`ThinkingConfig`](#thinking-config) ` \| None` | `None` | Controls extended thinking behavior. Takes precedence over `max_thinking_tokens` |
834| `effort` | `Literal["low", "medium", "high", "max"] \| None` | `None` | Effort level for thinking depth |
835| `session_store` | [`SessionStore`](/en/agent-sdk/session-storage#the-session-store-interface) ` \| None` | `None` | Mirror session transcripts to an external backend so any host can resume them. See [Persist sessions to external storage](/en/agent-sdk/session-storage) |
836
837### `OutputFormat`
838
839Configuration for structured output validation. Pass this as a `dict` to the `output_format` field on `ClaudeAgentOptions`:
840
841```python theme={null}
842# Expected dict shape for output_format
843{
844 "type": "json_schema",
845 "schema": {...}, # Your JSON Schema definition
846}
847```
848
849| Field | Required | Description |
850| :------- | :------- | :------------------------------------------------- |
851| `type` | Yes | Must be `"json_schema"` for JSON Schema validation |
852| `schema` | Yes | JSON Schema definition for output validation |
853
854### `SystemPromptPreset`
855
856Configuration for using Claude Code's preset system prompt with optional additions.
857
858```python theme={null}
859class SystemPromptPreset(TypedDict):
860 type: Literal["preset"]
861 preset: Literal["claude_code"]
862 append: NotRequired[str]
863 exclude_dynamic_sections: NotRequired[bool]
864```
865
866| Field | Required | Description |
867| :------------------------- | :------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
868| `type` | Yes | Must be `"preset"` to use a preset system prompt |
869| `preset` | Yes | Must be `"claude_code"` to use Claude Code's system prompt |
870| `append` | No | Additional instructions to append to the preset system prompt |
871| `exclude_dynamic_sections` | No | Move per-session context such as working directory, git status, and memory paths from the system prompt into the first user message. Improves prompt-cache reuse across users and machines. See [Modify system prompts](/en/agent-sdk/modifying-system-prompts#improve-prompt-caching-across-users-and-machines) |
872
873### `SettingSource`
874
875Controls which filesystem-based configuration sources the SDK loads settings from.
876
877```python theme={null}
878SettingSource = Literal["user", "project", "local"]
879```
880
881| Value | Description | Location |
882| :---------- | :------------------------------------------- | :---------------------------- |
883| `"user"` | Global user settings | `~/.claude/settings.json` |
884| `"project"` | Shared project settings (version controlled) | `.claude/settings.json` |
885| `"local"` | Local project settings (gitignored) | `.claude/settings.local.json` |
886
887#### Default behavior
888
889When `setting_sources` is omitted or `None`, `query()` loads the same filesystem settings as the Claude Code CLI: user, project, and local. Managed policy settings are loaded in all cases. See [What settingSources does not control](/en/agent-sdk/claude-code-features#what-settingsources-does-not-control) for inputs that are read regardless of this option, and how to disable them.
890
891#### Why use setting\_sources
892
893**Disable filesystem settings:**
894
895```python theme={null}
896# Do not load user, project, or local settings from disk
897from claude_agent_sdk import query, ClaudeAgentOptions
898
899async for message in query(
900 prompt="Analyze this code",
901 options=ClaudeAgentOptions(
902 setting_sources=[]
903 ),
904):
905 print(message)
906```
907
908<Note>
909 In Python SDK 0.1.59 and earlier, an empty list was treated the same as omitting the option, so `setting_sources=[]` did not disable filesystem settings. Upgrade to a newer release if you need an empty list to take effect. The TypeScript SDK is not affected.
910</Note>
911
912**Load all filesystem settings explicitly:**
913
914```python theme={null}
915from claude_agent_sdk import query, ClaudeAgentOptions
916
917async for message in query(
918 prompt="Analyze this code",
919 options=ClaudeAgentOptions(
920 setting_sources=["user", "project", "local"]
921 ),
922):
923 print(message)
924```
925
926**Load only specific setting sources:**
927
928```python theme={null}
929# Load only project settings, ignore user and local
930async for message in query(
931 prompt="Run CI checks",
932 options=ClaudeAgentOptions(
933 setting_sources=["project"] # Only .claude/settings.json
934 ),
935):
936 print(message)
937```
938
939**Testing and CI environments:**
940
941```python theme={null}
942# Ensure consistent behavior in CI by excluding local settings
943async for message in query(
944 prompt="Run tests",
945 options=ClaudeAgentOptions(
946 setting_sources=["project"], # Only team-shared settings
947 permission_mode="bypassPermissions",
948 ),
949):
950 print(message)
951```
952
953**SDK-only applications:**
954
955```python theme={null}
956# Define everything programmatically.
957# Pass [] to opt out of filesystem setting sources.
958async for message in query(
959 prompt="Review this PR",
960 options=ClaudeAgentOptions(
961 setting_sources=[],
962 agents={...},
963 mcp_servers={...},
964 allowed_tools=["Read", "Grep", "Glob"],
965 ),
966):
967 print(message)
968```
969
970**Loading CLAUDE.md project instructions:**
971
972```python theme={null}
973# Load project settings to include CLAUDE.md files
974async for message in query(
975 prompt="Add a new feature following project conventions",
976 options=ClaudeAgentOptions(
977 system_prompt={
978 "type": "preset",
979 "preset": "claude_code", # Use Claude Code's system prompt
980 },
981 setting_sources=["project"], # Loads CLAUDE.md from project
982 allowed_tools=["Read", "Write", "Edit"],
983 ),
984):
985 print(message)
986```
987
988#### Settings precedence
989
990When multiple sources are loaded, settings are merged with this precedence (highest to lowest):
991
9921. Local settings (`.claude/settings.local.json`)
9932. Project settings (`.claude/settings.json`)
9943. User settings (`~/.claude/settings.json`)
995
996Programmatic options such as `agents` and `allowed_tools` override user, project, and local filesystem settings. Managed policy settings take precedence over programmatic options.
997
998### `AgentDefinition`
999
1000Configuration for a subagent defined programmatically.
1001
1002```python theme={null}
1003@dataclass
1004class AgentDefinition:
1005 description: str
1006 prompt: str
1007 tools: list[str] | None = None
1008 disallowedTools: list[str] | None = None
1009 model: str | None = None
1010 skills: list[str] | None = None
1011 memory: Literal["user", "project", "local"] | None = None
1012 mcpServers: list[str | dict[str, Any]] | None = None
1013 initialPrompt: str | None = None
1014 maxTurns: int | None = None
1015 background: bool | None = None
1016 effort: Literal["low", "medium", "high", "max"] | int | None = None
1017 permissionMode: PermissionMode | None = None
1018```
1019
1020| Field | Required | Description |
1021| :---------------- | :------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------- |
1022| `description` | Yes | Natural language description of when to use this agent |
1023| `prompt` | Yes | The agent's system prompt |
1024| `tools` | No | Array of allowed tool names. If omitted, inherits all tools |
1025| `disallowedTools` | No | Array of tool names to remove from the agent's tool set |
1026| `model` | No | Model override for this agent. Accepts an alias such as `"sonnet"`, `"opus"`, `"haiku"`, or `"inherit"`, or a full model ID. If omitted, uses the main model |
1027| `skills` | No | List of skill names available to this agent |
1028| `memory` | No | Memory source for this agent: `"user"`, `"project"`, or `"local"` |
1029| `mcpServers` | No | MCP servers available to this agent. Each entry is a server name or an inline `{name: config}` dict |
1030| `initialPrompt` | No | Auto-submitted as the first user turn when this agent runs as the main thread agent |
1031| `maxTurns` | No | Maximum number of agentic turns before the agent stops |
1032| `background` | No | Run this agent as a non-blocking background task when invoked |
1033| `effort` | No | Reasoning effort level for this agent. Accepts a named level or an integer |
1034| `permissionMode` | No | Permission mode for tool execution within this agent. See [`PermissionMode`](#permission-mode) |
1035
1036<Note>
1037 `AgentDefinition` field names use camelCase, such as `disallowedTools`, `permissionMode`, and `maxTurns`. These names map directly to the wire format shared with the TypeScript SDK. This differs from `ClaudeAgentOptions`, which uses Python snake\_case for the equivalent top-level fields such as `disallowed_tools` and `permission_mode`. Because `AgentDefinition` is a dataclass, passing a snake\_case keyword raises a `TypeError` at construction time.
1038</Note>
1039
1040### `PermissionMode`
1041
1042Permission modes for controlling tool execution.
1043
1044```python theme={null}
1045PermissionMode = Literal[
1046 "default", # Standard permission behavior
1047 "acceptEdits", # Auto-accept file edits
1048 "plan", # Planning mode - no execution
1049 "dontAsk", # Deny anything not pre-approved instead of prompting
1050 "bypassPermissions", # Bypass all permission checks (use with caution)
1051]
1052```
1053
1054### `CanUseTool`
1055
1056Type alias for tool permission callback functions.
1057
1058```python theme={null}
1059CanUseTool = Callable[
1060 [str, dict[str, Any], ToolPermissionContext], Awaitable[PermissionResult]
1061]
1062```
1063
1064The callback receives:
1065
1066* `tool_name`: Name of the tool being called
1067* `input_data`: The tool's input parameters
1068* `context`: A `ToolPermissionContext` with additional information
1069
1070Returns a `PermissionResult` (either `PermissionResultAllow` or `PermissionResultDeny`).
1071
1072### `ToolPermissionContext`
1073
1074Context information passed to tool permission callbacks.
1075
1076```python theme={null}
1077@dataclass
1078class ToolPermissionContext:
1079 signal: Any | None = None # Future: abort signal support
1080 suggestions: list[PermissionUpdate] = field(default_factory=list)
1081```
1082
1083| Field | Type | Description |
1084| :------------ | :----------------------- | :----------------------------------------- |
1085| `signal` | `Any \| None` | Reserved for future abort signal support |
1086| `suggestions` | `list[PermissionUpdate]` | Permission update suggestions from the CLI |
1087
1088### `PermissionResult`
1089
1090Union type for permission callback results.
1091
1092```python theme={null}
1093PermissionResult = PermissionResultAllow | PermissionResultDeny
1094```
1095
1096### `PermissionResultAllow`
1097
1098Result indicating the tool call should be allowed.
1099
1100```python theme={null}
1101@dataclass
1102class PermissionResultAllow:
1103 behavior: Literal["allow"] = "allow"
1104 updated_input: dict[str, Any] | None = None
1105 updated_permissions: list[PermissionUpdate] | None = None
1106```
1107
1108| Field | Type | Default | Description |
1109| :-------------------- | :------------------------------- | :-------- | :---------------------------------------- |
1110| `behavior` | `Literal["allow"]` | `"allow"` | Must be "allow" |
1111| `updated_input` | `dict[str, Any] \| None` | `None` | Modified input to use instead of original |
1112| `updated_permissions` | `list[PermissionUpdate] \| None` | `None` | Permission updates to apply |
1113
1114### `PermissionResultDeny`
1115
1116Result indicating the tool call should be denied.
1117
1118```python theme={null}
1119@dataclass
1120class PermissionResultDeny:
1121 behavior: Literal["deny"] = "deny"
1122 message: str = ""
1123 interrupt: bool = False
1124```
1125
1126| Field | Type | Default | Description |
1127| :---------- | :---------------- | :------- | :----------------------------------------- |
1128| `behavior` | `Literal["deny"]` | `"deny"` | Must be "deny" |
1129| `message` | `str` | `""` | Message explaining why the tool was denied |
1130| `interrupt` | `bool` | `False` | Whether to interrupt the current execution |
1131
1132### `PermissionUpdate`
1133
1134Configuration for updating permissions programmatically.
1135
1136```python theme={null}
1137@dataclass
1138class PermissionUpdate:
1139 type: Literal[
1140 "addRules",
1141 "replaceRules",
1142 "removeRules",
1143 "setMode",
1144 "addDirectories",
1145 "removeDirectories",
1146 ]
1147 rules: list[PermissionRuleValue] | None = None
1148 behavior: Literal["allow", "deny", "ask"] | None = None
1149 mode: PermissionMode | None = None
1150 directories: list[str] | None = None
1151 destination: (
1152 Literal["userSettings", "projectSettings", "localSettings", "session"] | None
1153 ) = None
1154```
1155
1156| Field | Type | Description |
1157| :------------ | :---------------------------------------- | :---------------------------------------------- |
1158| `type` | `Literal[...]` | The type of permission update operation |
1159| `rules` | `list[PermissionRuleValue] \| None` | Rules for add/replace/remove operations |
1160| `behavior` | `Literal["allow", "deny", "ask"] \| None` | Behavior for rule-based operations |
1161| `mode` | `PermissionMode \| None` | Mode for setMode operation |
1162| `directories` | `list[str] \| None` | Directories for add/remove directory operations |
1163| `destination` | `Literal[...] \| None` | Where to apply the permission update |
1164
1165### `PermissionRuleValue`
1166
1167A rule to add, replace, or remove in a permission update.
1168
1169```python theme={null}
1170@dataclass
1171class PermissionRuleValue:
1172 tool_name: str
1173 rule_content: str | None = None
1174```
1175
1176### `ToolsPreset`
1177
1178Preset tools configuration for using Claude Code's default tool set.
1179
1180```python theme={null}
1181class ToolsPreset(TypedDict):
1182 type: Literal["preset"]
1183 preset: Literal["claude_code"]
1184```
1185
1186### `ThinkingConfig`
1187
1188Controls extended thinking behavior. A union of three configurations:
1189
1190```python theme={null}
1191class ThinkingConfigAdaptive(TypedDict):
1192 type: Literal["adaptive"]
1193
1194
1195class ThinkingConfigEnabled(TypedDict):
1196 type: Literal["enabled"]
1197 budget_tokens: int
1198
1199
1200class ThinkingConfigDisabled(TypedDict):
1201 type: Literal["disabled"]
1202
1203
1204ThinkingConfig = ThinkingConfigAdaptive | ThinkingConfigEnabled | ThinkingConfigDisabled
1205```
1206
1207| Variant | Fields | Description |
1208| :--------- | :---------------------- | :------------------------------------------- |
1209| `adaptive` | `type` | Claude adaptively decides when to think |
1210| `enabled` | `type`, `budget_tokens` | Enable thinking with a specific token budget |
1211| `disabled` | `type` | Disable thinking |
1212
1213Because these are `TypedDict` classes, they're plain dicts at runtime. Either construct them as dict literals or call the class like a constructor; both produce a `dict`. Access fields with `config["budget_tokens"]`, not `config.budget_tokens`:
1214
1215```python theme={null}
1216from claude_agent_sdk import ClaudeAgentOptions, ThinkingConfigEnabled
1217
1218# Option 1: dict literal (recommended, no import needed)
1219options = ClaudeAgentOptions(thinking={"type": "enabled", "budget_tokens": 20000})
1220
1221# Option 2: constructor-style (returns a plain dict)
1222config = ThinkingConfigEnabled(type="enabled", budget_tokens=20000)
1223print(config["budget_tokens"]) # 20000
1224# config.budget_tokens would raise AttributeError
1225```
1226
1227### `SdkBeta`
1228
1229Literal type for SDK beta features.
1230
1231```python theme={null}
1232SdkBeta = Literal["context-1m-2025-08-07"]
1233```
1234
1235Use with the `betas` field in `ClaudeAgentOptions` to enable beta features.
1236
1237<Warning>
1238 The `context-1m-2025-08-07` beta is retired as of April 30, 2026. Passing this header with Claude Sonnet 4.5 or Sonnet 4 has no effect, and requests that exceed the standard 200k-token context window return an error. To use a 1M-token context window, migrate to [Claude Sonnet 4.6, Claude Opus 4.6, or Claude Opus 4.7](https://platform.claude.com/docs/en/about-claude/models/overview), which include 1M context at standard pricing with no beta header required.
1239</Warning>
1240
1241### `McpSdkServerConfig`
1242
1243Configuration for SDK MCP servers created with `create_sdk_mcp_server()`.
1244
1245```python theme={null}
1246class McpSdkServerConfig(TypedDict):
1247 type: Literal["sdk"]
1248 name: str
1249 instance: Any # MCP Server instance
1250```
1251
1252### `McpServerConfig`
1253
1254Union type for MCP server configurations.
1255
1256```python theme={null}
1257McpServerConfig = (
1258 McpStdioServerConfig | McpSSEServerConfig | McpHttpServerConfig | McpSdkServerConfig
1259)
1260```
1261
1262#### `McpStdioServerConfig`
1263
1264```python theme={null}
1265class McpStdioServerConfig(TypedDict):
1266 type: NotRequired[Literal["stdio"]] # Optional for backwards compatibility
1267 command: str
1268 args: NotRequired[list[str]]
1269 env: NotRequired[dict[str, str]]
1270```
1271
1272#### `McpSSEServerConfig`
1273
1274```python theme={null}
1275class McpSSEServerConfig(TypedDict):
1276 type: Literal["sse"]
1277 url: str
1278 headers: NotRequired[dict[str, str]]
1279```
1280
1281#### `McpHttpServerConfig`
1282
1283```python theme={null}
1284class McpHttpServerConfig(TypedDict):
1285 type: Literal["http"]
1286 url: str
1287 headers: NotRequired[dict[str, str]]
1288```
1289
1290### `McpServerStatusConfig`
1291
1292The configuration of an MCP server as reported by [`get_mcp_status()`](#methods). This is the union of all [`McpServerConfig`](#mcp-server-config) transport variants plus an output-only `claudeai-proxy` variant for servers proxied through claude.ai.
1293
1294```python theme={null}
1295McpServerStatusConfig = (
1296 McpStdioServerConfig
1297 | McpSSEServerConfig
1298 | McpHttpServerConfig
1299 | McpSdkServerConfigStatus
1300 | McpClaudeAIProxyServerConfig
1301)
1302```
1303
1304`McpSdkServerConfigStatus` is the serializable form of [`McpSdkServerConfig`](#mcp-sdk-server-config) with only `type` (`"sdk"`) and `name` (`str`) fields; the in-process `instance` is omitted. `McpClaudeAIProxyServerConfig` has `type` (`"claudeai-proxy"`), `url` (`str`), and `id` (`str`) fields.
1305
1306### `McpStatusResponse`
1307
1308Response from [`ClaudeSDKClient.get_mcp_status()`](#methods). Wraps the list of server statuses under the `mcpServers` key.
1309
1310```python theme={null}
1311class McpStatusResponse(TypedDict):
1312 mcpServers: list[McpServerStatus]
1313```
1314
1315### `McpServerStatus`
1316
1317Status of a connected MCP server, contained in [`McpStatusResponse`](#mcp-status-response).
1318
1319```python theme={null}
1320class McpServerStatus(TypedDict):
1321 name: str
1322 status: McpServerConnectionStatus # "connected" | "failed" | "needs-auth" | "pending" | "disabled"
1323 serverInfo: NotRequired[McpServerInfo]
1324 error: NotRequired[str]
1325 config: NotRequired[McpServerStatusConfig]
1326 scope: NotRequired[str]
1327 tools: NotRequired[list[McpToolInfo]]
1328```
1329
1330| Field | Type | Description |
1331| :----------- | :-------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
1332| `name` | `str` | Server name |
1333| `status` | `str` | One of `"connected"`, `"failed"`, `"needs-auth"`, `"pending"`, or `"disabled"` |
1334| `serverInfo` | `dict` (optional) | Server name and version (`{"name": str, "version": str}`) |
1335| `error` | `str` (optional) | Error message if the server failed to connect |
1336| `config` | [`McpServerStatusConfig`](#mcp-server-status-config) (optional) | Server configuration. Same shape as [`McpServerConfig`](#mcp-server-config) (stdio, SSE, HTTP, or SDK), plus a `claudeai-proxy` variant for servers connected through claude.ai |
1337| `scope` | `str` (optional) | Configuration scope |
1338| `tools` | `list` (optional) | Tools provided by this server, each with `name`, `description`, and `annotations` fields |
1339
1340### `SdkPluginConfig`
1341
1342Configuration for loading plugins in the SDK.
1343
1344```python theme={null}
1345class SdkPluginConfig(TypedDict):
1346 type: Literal["local"]
1347 path: str
1348```
1349
1350| Field | Type | Description |
1351| :----- | :----------------- | :--------------------------------------------------------- |
1352| `type` | `Literal["local"]` | Must be `"local"` (only local plugins currently supported) |
1353| `path` | `str` | Absolute or relative path to the plugin directory |
1354
1355**Example:**
1356
1357```python theme={null}
1358plugins = [
1359 {"type": "local", "path": "./my-plugin"},
1360 {"type": "local", "path": "/absolute/path/to/plugin"},
1361]
1362```
1363
1364For complete information on creating and using plugins, see [Plugins](/en/agent-sdk/plugins).
1365
1366## Message Types
1367
1368### `Message`
1369
1370Union type of all possible messages.
1371
1372```python theme={null}
1373Message = (
1374 UserMessage
1375 | AssistantMessage
1376 | SystemMessage
1377 | ResultMessage
1378 | StreamEvent
1379 | RateLimitEvent
1380)
1381```
1382
1383### `UserMessage`
1384
1385User input message.
1386
1387```python theme={null}
1388@dataclass
1389class UserMessage:
1390 content: str | list[ContentBlock]
1391 uuid: str | None = None
1392 parent_tool_use_id: str | None = None
1393 tool_use_result: dict[str, Any] | None = None
1394```
1395
1396| Field | Type | Description |
1397| :------------------- | :-------------------------- | :---------------------------------------------------- |
1398| `content` | `str \| list[ContentBlock]` | Message content as text or content blocks |
1399| `uuid` | `str \| None` | Unique message identifier |
1400| `parent_tool_use_id` | `str \| None` | Tool use ID if this message is a tool result response |
1401| `tool_use_result` | `dict[str, Any] \| None` | Tool result data if applicable |
1402
1403### `AssistantMessage`
1404
1405Assistant response message with content blocks.
1406
1407```python theme={null}
1408@dataclass
1409class AssistantMessage:
1410 content: list[ContentBlock]
1411 model: str
1412 parent_tool_use_id: str | None = None
1413 error: AssistantMessageError | None = None
1414 usage: dict[str, Any] | None = None
1415 message_id: str | None = None
1416```
1417
1418| Field | Type | Description |
1419| :------------------- | :------------------------------------------------------------- | :------------------------------------------------------------------------------ |
1420| `content` | `list[ContentBlock]` | List of content blocks in the response |
1421| `model` | `str` | Model that generated the response |
1422| `parent_tool_use_id` | `str \| None` | Tool use ID if this is a nested response |
1423| `error` | [`AssistantMessageError`](#assistant-message-error) ` \| None` | Error type if the response encountered an error |
1424| `usage` | `dict[str, Any] \| None` | Per-message token usage (same keys as [`ResultMessage.usage`](#result-message)) |
1425| `message_id` | `str \| None` | API message ID. Multiple messages from one turn share the same ID |
1426
1427### `AssistantMessageError`
1428
1429Possible error types for assistant messages.
1430
1431```python theme={null}
1432AssistantMessageError = Literal[
1433 "authentication_failed",
1434 "billing_error",
1435 "rate_limit",
1436 "invalid_request",
1437 "server_error",
1438 "max_output_tokens",
1439 "unknown",
1440]
1441```
1442
1443### `SystemMessage`
1444
1445System message with metadata.
1446
1447```python theme={null}
1448@dataclass
1449class SystemMessage:
1450 subtype: str
1451 data: dict[str, Any]
1452```
1453
1454### `ResultMessage`
1455
1456Final result message with cost and usage information.
1457
1458```python theme={null}
1459@dataclass
1460class ResultMessage:
1461 subtype: str
1462 duration_ms: int
1463 duration_api_ms: int
1464 is_error: bool
1465 num_turns: int
1466 session_id: str
1467 total_cost_usd: float | None = None
1468 usage: dict[str, Any] | None = None
1469 result: str | None = None
1470 stop_reason: str | None = None
1471 structured_output: Any = None
1472 model_usage: dict[str, Any] | None = None
1473```
1474
1475The `usage` dict contains the following keys when present:
1476
1477| Key | Type | Description |
1478| ----------------------------- | ----- | ---------------------------------------- |
1479| `input_tokens` | `int` | Total input tokens consumed. |
1480| `output_tokens` | `int` | Total output tokens generated. |
1481| `cache_creation_input_tokens` | `int` | Tokens used to create new cache entries. |
1482| `cache_read_input_tokens` | `int` | Tokens read from existing cache entries. |
1483
1484The `model_usage` dict maps model names to per-model usage. The inner dict keys use camelCase because the value is passed through unmodified from the underlying CLI process, matching the TypeScript [`ModelUsage`](/en/agent-sdk/typescript#model-usage) type:
1485
1486| Key | Type | Description |
1487| -------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
1488| `inputTokens` | `int` | Input tokens for this model. |
1489| `outputTokens` | `int` | Output tokens for this model. |
1490| `cacheReadInputTokens` | `int` | Cache read tokens for this model. |
1491| `cacheCreationInputTokens` | `int` | Cache creation tokens for this model. |
1492| `webSearchRequests` | `int` | Web search requests made by this model. |
1493| `costUSD` | `float` | Estimated cost in USD for this model, computed client-side. See [Track cost and usage](/en/agent-sdk/cost-tracking) for billing caveats. |
1494| `contextWindow` | `int` | Context window size for this model. |
1495| `maxOutputTokens` | `int` | Maximum output token limit for this model. |
1496
1497### `StreamEvent`
1498
1499Stream event for partial message updates during streaming. Only received when `include_partial_messages=True` in `ClaudeAgentOptions`. Import via `from claude_agent_sdk.types import StreamEvent`.
1500
1501```python theme={null}
1502@dataclass
1503class StreamEvent:
1504 uuid: str
1505 session_id: str
1506 event: dict[str, Any] # The raw Claude API stream event
1507 parent_tool_use_id: str | None = None
1508```
1509
1510| Field | Type | Description |
1511| :------------------- | :--------------- | :-------------------------------------------------- |
1512| `uuid` | `str` | Unique identifier for this event |
1513| `session_id` | `str` | Session identifier |
1514| `event` | `dict[str, Any]` | The raw Claude API stream event data |
1515| `parent_tool_use_id` | `str \| None` | Parent tool use ID if this event is from a subagent |
1516
1517### `RateLimitEvent`
1518
1519Emitted when rate limit status changes (for example, from `"allowed"` to `"allowed_warning"`). Use this to warn users before they hit a hard limit, or to back off when status is `"rejected"`.
1520
1521```python theme={null}
1522@dataclass
1523class RateLimitEvent:
1524 rate_limit_info: RateLimitInfo
1525 uuid: str
1526 session_id: str
1527```
1528
1529| Field | Type | Description |
1530| :---------------- | :---------------------------------- | :----------------------- |
1531| `rate_limit_info` | [`RateLimitInfo`](#rate-limit-info) | Current rate limit state |
1532| `uuid` | `str` | Unique event identifier |
1533| `session_id` | `str` | Session identifier |
1534
1535### `RateLimitInfo`
1536
1537Rate limit state carried by [`RateLimitEvent`](#rate-limit-event).
1538
1539```python theme={null}
1540RateLimitStatus = Literal["allowed", "allowed_warning", "rejected"]
1541RateLimitType = Literal[
1542 "five_hour", "seven_day", "seven_day_opus", "seven_day_sonnet", "overage"
1543]
1544
1545
1546@dataclass
1547class RateLimitInfo:
1548 status: RateLimitStatus
1549 resets_at: int | None = None
1550 rate_limit_type: RateLimitType | None = None
1551 utilization: float | None = None
1552 overage_status: RateLimitStatus | None = None
1553 overage_resets_at: int | None = None
1554 overage_disabled_reason: str | None = None
1555 raw: dict[str, Any] = field(default_factory=dict)
1556```
1557
1558| Field | Type | Description |
1559| :------------------------ | :------------------------ | :---------------------------------------------------------------------------------------------------- |
1560| `status` | `RateLimitStatus` | Current status. `"allowed_warning"` means approaching the limit; `"rejected"` means the limit was hit |
1561| `resets_at` | `int \| None` | Unix timestamp when the rate limit window resets |
1562| `rate_limit_type` | `RateLimitType \| None` | Which rate limit window applies |
1563| `utilization` | `float \| None` | Fraction of the rate limit consumed (0.0 to 1.0) |
1564| `overage_status` | `RateLimitStatus \| None` | Status of pay-as-you-go overage usage, if applicable |
1565| `overage_resets_at` | `int \| None` | Unix timestamp when the overage window resets |
1566| `overage_disabled_reason` | `str \| None` | Why overage is unavailable, if status is `"rejected"` |
1567| `raw` | `dict[str, Any]` | Full raw dict from the CLI, including fields not modeled above |
1568
1569### `TaskStartedMessage`
1570
1571Emitted when a background task starts. A background task is anything tracked outside the main turn: a backgrounded Bash command, a [Monitor](#monitor) watch, a subagent spawned via the Agent tool, or a remote agent. The `task_type` field tells you which. This naming is unrelated to the `Task`-to-`Agent` tool rename.
1572
1573```python theme={null}
1574@dataclass
1575class TaskStartedMessage(SystemMessage):
1576 task_id: str
1577 description: str
1578 uuid: str
1579 session_id: str
1580 tool_use_id: str | None = None
1581 task_type: str | None = None
1582```
1583
1584| Field | Type | Description |
1585| :------------ | :------------ | :-------------------------------------------------------------------------------------------------------------------------- |
1586| `task_id` | `str` | Unique identifier for the task |
1587| `description` | `str` | Description of the task |
1588| `uuid` | `str` | Unique message identifier |
1589| `session_id` | `str` | Session identifier |
1590| `tool_use_id` | `str \| None` | Associated tool use ID |
1591| `task_type` | `str \| None` | Which kind of background task: `"local_bash"` for background Bash and Monitor watches, `"local_agent"`, or `"remote_agent"` |
1592
1593### `TaskUsage`
1594
1595Token and timing data for a background task.
1596
1597```python theme={null}
1598class TaskUsage(TypedDict):
1599 total_tokens: int
1600 tool_uses: int
1601 duration_ms: int
1602```
1603
1604### `TaskProgressMessage`
1605
1606Emitted periodically with progress updates for a running background task.
1607
1608```python theme={null}
1609@dataclass
1610class TaskProgressMessage(SystemMessage):
1611 task_id: str
1612 description: str
1613 usage: TaskUsage
1614 uuid: str
1615 session_id: str
1616 tool_use_id: str | None = None
1617 last_tool_name: str | None = None
1618```
1619
1620| Field | Type | Description |
1621| :--------------- | :------------ | :---------------------------------- |
1622| `task_id` | `str` | Unique identifier for the task |
1623| `description` | `str` | Current status description |
1624| `usage` | `TaskUsage` | Token usage for this task so far |
1625| `uuid` | `str` | Unique message identifier |
1626| `session_id` | `str` | Session identifier |
1627| `tool_use_id` | `str \| None` | Associated tool use ID |
1628| `last_tool_name` | `str \| None` | Name of the last tool the task used |
1629
1630### `TaskNotificationMessage`
1631
1632Emitted when a background task completes, fails, or is stopped. Background tasks include `run_in_background` Bash commands, Monitor watches, and background subagents.
1633
1634```python theme={null}
1635@dataclass
1636class TaskNotificationMessage(SystemMessage):
1637 task_id: str
1638 status: TaskNotificationStatus # "completed" | "failed" | "stopped"
1639 output_file: str
1640 summary: str
1641 uuid: str
1642 session_id: str
1643 tool_use_id: str | None = None
1644 usage: TaskUsage | None = None
1645```
1646
1647| Field | Type | Description |
1648| :------------ | :----------------------- | :----------------------------------------------- |
1649| `task_id` | `str` | Unique identifier for the task |
1650| `status` | `TaskNotificationStatus` | One of `"completed"`, `"failed"`, or `"stopped"` |
1651| `output_file` | `str` | Path to the task output file |
1652| `summary` | `str` | Summary of the task result |
1653| `uuid` | `str` | Unique message identifier |
1654| `session_id` | `str` | Session identifier |
1655| `tool_use_id` | `str \| None` | Associated tool use ID |
1656| `usage` | `TaskUsage \| None` | Final token usage for the task |
1657
1658## Content Block Types
1659
1660### `ContentBlock`
1661
1662Union type of all content blocks.
1663
1664```python theme={null}
1665ContentBlock = TextBlock | ThinkingBlock | ToolUseBlock | ToolResultBlock
1666```
1667
1668### `TextBlock`
1669
1670Text content block.
1671
1672```python theme={null}
1673@dataclass
1674class TextBlock:
1675 text: str
1676```
1677
1678### `ThinkingBlock`
1679
1680Thinking content block (for models with thinking capability).
1681
1682```python theme={null}
1683@dataclass
1684class ThinkingBlock:
1685 thinking: str
1686 signature: str
1687```
1688
1689### `ToolUseBlock`
1690
1691Tool use request block.
1692
1693```python theme={null}
1694@dataclass
1695class ToolUseBlock:
1696 id: str
1697 name: str
1698 input: dict[str, Any]
1699```
1700
1701### `ToolResultBlock`
1702
1703Tool execution result block.
1704
1705```python theme={null}
1706@dataclass
1707class ToolResultBlock:
1708 tool_use_id: str
1709 content: str | list[dict[str, Any]] | None = None
1710 is_error: bool | None = None
1711```
1712
1713## Error Types
1714
1715### `ClaudeSDKError`
1716
1717Base exception class for all SDK errors.
1718
1719```python theme={null}
1720class ClaudeSDKError(Exception):
1721 """Base error for Claude SDK."""
1722```
1723
1724### `CLINotFoundError`
1725
1726Raised when Claude Code CLI is not installed or not found.
1727
1728```python theme={null}
1729class CLINotFoundError(CLIConnectionError):
1730 def __init__(
1731 self, message: str = "Claude Code not found", cli_path: str | None = None
1732 ):
1733 """
1734 Args:
1735 message: Error message (default: "Claude Code not found")
1736 cli_path: Optional path to the CLI that was not found
1737 """
1738```
1739
1740### `CLIConnectionError`
1741
1742Raised when connection to Claude Code fails.
1743
1744```python theme={null}
1745class CLIConnectionError(ClaudeSDKError):
1746 """Failed to connect to Claude Code."""
1747```
1748
1749### `ProcessError`
1750
1751Raised when the Claude Code process fails.
1752
1753```python theme={null}
1754class ProcessError(ClaudeSDKError):
1755 def __init__(
1756 self, message: str, exit_code: int | None = None, stderr: str | None = None
1757 ):
1758 self.exit_code = exit_code
1759 self.stderr = stderr
1760```
1761
1762### `CLIJSONDecodeError`
1763
1764Raised when JSON parsing fails.
1765
1766```python theme={null}
1767class CLIJSONDecodeError(ClaudeSDKError):
1768 def __init__(self, line: str, original_error: Exception):
1769 """
1770 Args:
1771 line: The line that failed to parse
1772 original_error: The original JSON decode exception
1773 """
1774 self.line = line
1775 self.original_error = original_error
1776```
1777
1778## Hook Types
1779
1780For a comprehensive guide on using hooks with examples and common patterns, see the [Hooks guide](/en/agent-sdk/hooks).
1781
1782### `HookEvent`
1783
1784Supported hook event types.
1785
1786```python theme={null}
1787HookEvent = Literal[
1788 "PreToolUse", # Called before tool execution
1789 "PostToolUse", # Called after tool execution
1790 "PostToolUseFailure", # Called when a tool execution fails
1791 "UserPromptSubmit", # Called when user submits a prompt
1792 "Stop", # Called when stopping execution
1793 "SubagentStop", # Called when a subagent stops
1794 "PreCompact", # Called before message compaction
1795 "Notification", # Called for notification events
1796 "SubagentStart", # Called when a subagent starts
1797 "PermissionRequest", # Called when a permission decision is needed
1798]
1799```
1800
1801<Note>
1802 The TypeScript SDK supports additional hook events not yet available in Python: `SessionStart`, `SessionEnd`, `Setup`, `TeammateIdle`, `TaskCompleted`, `ConfigChange`, `WorktreeCreate`, `WorktreeRemove`, and `PostToolBatch`.
1803</Note>
1804
1805### `HookCallback`
1806
1807Type definition for hook callback functions.
1808
1809```python theme={null}
1810HookCallback = Callable[[HookInput, str | None, HookContext], Awaitable[HookJSONOutput]]
1811```
1812
1813Parameters:
1814
1815* `input`: Strongly-typed hook input with discriminated unions based on `hook_event_name` (see [`HookInput`](#hook-input))
1816* `tool_use_id`: Optional tool use identifier (for tool-related hooks)
1817* `context`: Hook context with additional information
1818
1819Returns a [`HookJSONOutput`](#hook-json-output) that may contain:
1820
1821* `decision`: `"block"` to block the action
1822* `systemMessage`: System message to add to the transcript
1823* `hookSpecificOutput`: Hook-specific output data
1824
1825### `HookContext`
1826
1827Context information passed to hook callbacks.
1828
1829```python theme={null}
1830class HookContext(TypedDict):
1831 signal: Any | None # Future: abort signal support
1832```
1833
1834### `HookMatcher`
1835
1836Configuration for matching hooks to specific events or tools.
1837
1838```python theme={null}
1839@dataclass
1840class HookMatcher:
1841 matcher: str | None = (
1842 None # Tool name or pattern to match (e.g., "Bash", "Write|Edit")
1843 )
1844 hooks: list[HookCallback] = field(
1845 default_factory=list
1846 ) # List of callbacks to execute
1847 timeout: float | None = (
1848 None # Timeout in seconds for all hooks in this matcher (default: 60)
1849 )
1850```
1851
1852### `HookInput`
1853
1854Union type of all hook input types. The actual type depends on the `hook_event_name` field.
1855
1856```python theme={null}
1857HookInput = (
1858 PreToolUseHookInput
1859 | PostToolUseHookInput
1860 | PostToolUseFailureHookInput
1861 | UserPromptSubmitHookInput
1862 | StopHookInput
1863 | SubagentStopHookInput
1864 | PreCompactHookInput
1865 | NotificationHookInput
1866 | SubagentStartHookInput
1867 | PermissionRequestHookInput
1868)
1869```
1870
1871### `BaseHookInput`
1872
1873Base fields present in all hook input types.
1874
1875```python theme={null}
1876class BaseHookInput(TypedDict):
1877 session_id: str
1878 transcript_path: str
1879 cwd: str
1880 permission_mode: NotRequired[str]
1881```
1882
1883| Field | Type | Description |
1884| :---------------- | :--------------- | :---------------------------------- |
1885| `session_id` | `str` | Current session identifier |
1886| `transcript_path` | `str` | Path to the session transcript file |
1887| `cwd` | `str` | Current working directory |
1888| `permission_mode` | `str` (optional) | Current permission mode |
1889
1890### `PreToolUseHookInput`
1891
1892Input data for `PreToolUse` hook events.
1893
1894```python theme={null}
1895class PreToolUseHookInput(BaseHookInput):
1896 hook_event_name: Literal["PreToolUse"]
1897 tool_name: str
1898 tool_input: dict[str, Any]
1899 tool_use_id: str
1900 agent_id: NotRequired[str]
1901 agent_type: NotRequired[str]
1902```
1903
1904| Field | Type | Description |
1905| :---------------- | :---------------------- | :----------------------------------------------------------------- |
1906| `hook_event_name` | `Literal["PreToolUse"]` | Always "PreToolUse" |
1907| `tool_name` | `str` | Name of the tool about to be executed |
1908| `tool_input` | `dict[str, Any]` | Input parameters for the tool |
1909| `tool_use_id` | `str` | Unique identifier for this tool use |
1910| `agent_id` | `str` (optional) | Subagent identifier, present when the hook fires inside a subagent |
1911| `agent_type` | `str` (optional) | Subagent type, present when the hook fires inside a subagent |
1912
1913### `PostToolUseHookInput`
1914
1915Input data for `PostToolUse` hook events.
1916
1917```python theme={null}
1918class PostToolUseHookInput(BaseHookInput):
1919 hook_event_name: Literal["PostToolUse"]
1920 tool_name: str
1921 tool_input: dict[str, Any]
1922 tool_response: Any
1923 tool_use_id: str
1924 agent_id: NotRequired[str]
1925 agent_type: NotRequired[str]
1926```
1927
1928| Field | Type | Description |
1929| :---------------- | :----------------------- | :----------------------------------------------------------------- |
1930| `hook_event_name` | `Literal["PostToolUse"]` | Always "PostToolUse" |
1931| `tool_name` | `str` | Name of the tool that was executed |
1932| `tool_input` | `dict[str, Any]` | Input parameters that were used |
1933| `tool_response` | `Any` | Response from the tool execution |
1934| `tool_use_id` | `str` | Unique identifier for this tool use |
1935| `agent_id` | `str` (optional) | Subagent identifier, present when the hook fires inside a subagent |
1936| `agent_type` | `str` (optional) | Subagent type, present when the hook fires inside a subagent |
1937
1938### `PostToolUseFailureHookInput`
1939
1940Input data for `PostToolUseFailure` hook events. Called when a tool execution fails.
1941
1942```python theme={null}
1943class PostToolUseFailureHookInput(BaseHookInput):
1944 hook_event_name: Literal["PostToolUseFailure"]
1945 tool_name: str
1946 tool_input: dict[str, Any]
1947 tool_use_id: str
1948 error: str
1949 is_interrupt: NotRequired[bool]
1950 agent_id: NotRequired[str]
1951 agent_type: NotRequired[str]
1952```
1953
1954| Field | Type | Description |
1955| :---------------- | :------------------------------ | :----------------------------------------------------------------- |
1956| `hook_event_name` | `Literal["PostToolUseFailure"]` | Always "PostToolUseFailure" |
1957| `tool_name` | `str` | Name of the tool that failed |
1958| `tool_input` | `dict[str, Any]` | Input parameters that were used |
1959| `tool_use_id` | `str` | Unique identifier for this tool use |
1960| `error` | `str` | Error message from the failed execution |
1961| `is_interrupt` | `bool` (optional) | Whether the failure was caused by an interrupt |
1962| `agent_id` | `str` (optional) | Subagent identifier, present when the hook fires inside a subagent |
1963| `agent_type` | `str` (optional) | Subagent type, present when the hook fires inside a subagent |
1964
1965### `UserPromptSubmitHookInput`
1966
1967Input data for `UserPromptSubmit` hook events.
1968
1969```python theme={null}
1970class UserPromptSubmitHookInput(BaseHookInput):
1971 hook_event_name: Literal["UserPromptSubmit"]
1972 prompt: str
1973```
1974
1975| Field | Type | Description |
1976| :---------------- | :---------------------------- | :-------------------------- |
1977| `hook_event_name` | `Literal["UserPromptSubmit"]` | Always "UserPromptSubmit" |
1978| `prompt` | `str` | The user's submitted prompt |
1979
1980### `StopHookInput`
1981
1982Input data for `Stop` hook events.
1983
1984```python theme={null}
1985class StopHookInput(BaseHookInput):
1986 hook_event_name: Literal["Stop"]
1987 stop_hook_active: bool
1988```
1989
1990| Field | Type | Description |
1991| :----------------- | :---------------- | :------------------------------ |
1992| `hook_event_name` | `Literal["Stop"]` | Always "Stop" |
1993| `stop_hook_active` | `bool` | Whether the stop hook is active |
1994
1995### `SubagentStopHookInput`
1996
1997Input data for `SubagentStop` hook events.
1998
1999```python theme={null}
2000class SubagentStopHookInput(BaseHookInput):
2001 hook_event_name: Literal["SubagentStop"]
2002 stop_hook_active: bool
2003 agent_id: str
2004 agent_transcript_path: str
2005 agent_type: str
2006```
2007
2008| Field | Type | Description |
2009| :---------------------- | :------------------------ | :------------------------------------- |
2010| `hook_event_name` | `Literal["SubagentStop"]` | Always "SubagentStop" |
2011| `stop_hook_active` | `bool` | Whether the stop hook is active |
2012| `agent_id` | `str` | Unique identifier for the subagent |
2013| `agent_transcript_path` | `str` | Path to the subagent's transcript file |
2014| `agent_type` | `str` | Type of the subagent |
2015
2016### `PreCompactHookInput`
2017
2018Input data for `PreCompact` hook events.
2019
2020```python theme={null}
2021class PreCompactHookInput(BaseHookInput):
2022 hook_event_name: Literal["PreCompact"]
2023 trigger: Literal["manual", "auto"]
2024 custom_instructions: str | None
2025```
2026
2027| Field | Type | Description |
2028| :-------------------- | :-------------------------- | :--------------------------------- |
2029| `hook_event_name` | `Literal["PreCompact"]` | Always "PreCompact" |
2030| `trigger` | `Literal["manual", "auto"]` | What triggered the compaction |
2031| `custom_instructions` | `str \| None` | Custom instructions for compaction |
2032
2033### `NotificationHookInput`
2034
2035Input data for `Notification` hook events.
2036
2037```python theme={null}
2038class NotificationHookInput(BaseHookInput):
2039 hook_event_name: Literal["Notification"]
2040 message: str
2041 title: NotRequired[str]
2042 notification_type: str
2043```
2044
2045| Field | Type | Description |
2046| :------------------ | :------------------------ | :--------------------------- |
2047| `hook_event_name` | `Literal["Notification"]` | Always "Notification" |
2048| `message` | `str` | Notification message content |
2049| `title` | `str` (optional) | Notification title |
2050| `notification_type` | `str` | Type of notification |
2051
2052### `SubagentStartHookInput`
2053
2054Input data for `SubagentStart` hook events.
2055
2056```python theme={null}
2057class SubagentStartHookInput(BaseHookInput):
2058 hook_event_name: Literal["SubagentStart"]
2059 agent_id: str
2060 agent_type: str
2061```
2062
2063| Field | Type | Description |
2064| :---------------- | :------------------------- | :--------------------------------- |
2065| `hook_event_name` | `Literal["SubagentStart"]` | Always "SubagentStart" |
2066| `agent_id` | `str` | Unique identifier for the subagent |
2067| `agent_type` | `str` | Type of the subagent |
2068
2069### `PermissionRequestHookInput`
2070
2071Input data for `PermissionRequest` hook events. Allows hooks to handle permission decisions programmatically.
2072
2073```python theme={null}
2074class PermissionRequestHookInput(BaseHookInput):
2075 hook_event_name: Literal["PermissionRequest"]
2076 tool_name: str
2077 tool_input: dict[str, Any]
2078 permission_suggestions: NotRequired[list[Any]]
2079```
2080
2081| Field | Type | Description |
2082| :----------------------- | :----------------------------- | :---------------------------------------- |
2083| `hook_event_name` | `Literal["PermissionRequest"]` | Always "PermissionRequest" |
2084| `tool_name` | `str` | Name of the tool requesting permission |
2085| `tool_input` | `dict[str, Any]` | Input parameters for the tool |
2086| `permission_suggestions` | `list[Any]` (optional) | Suggested permission updates from the CLI |
2087
2088### `HookJSONOutput`
2089
2090Union type for hook callback return values.
2091
2092```python theme={null}
2093HookJSONOutput = AsyncHookJSONOutput | SyncHookJSONOutput
2094```
2095
2096#### `SyncHookJSONOutput`
2097
2098Synchronous hook output with control and decision fields.
2099
2100```python theme={null}
2101class SyncHookJSONOutput(TypedDict):
2102 # Control fields
2103 continue_: NotRequired[bool] # Whether to proceed (default: True)
2104 suppressOutput: NotRequired[bool] # Hide stdout from transcript
2105 stopReason: NotRequired[str] # Message when continue is False
2106
2107 # Decision fields
2108 decision: NotRequired[Literal["block"]]
2109 systemMessage: NotRequired[str] # Warning message for user
2110 reason: NotRequired[str] # Feedback for Claude
2111
2112 # Hook-specific output
2113 hookSpecificOutput: NotRequired[HookSpecificOutput]
2114```
2115
2116<Note>
2117 Use `continue_` (with underscore) in Python code. It is automatically converted to `continue` when sent to the CLI.
2118</Note>
2119
2120#### `HookSpecificOutput`
2121
2122A `TypedDict` containing the hook event name and event-specific fields. The shape depends on the `hookEventName` value. For full details on available fields per hook event, see [Control execution with hooks](/en/agent-sdk/hooks#outputs).
2123
2124A discriminated union of event-specific output types. The `hookEventName` field determines which fields are valid.
2125
2126```python theme={null}
2127class PreToolUseHookSpecificOutput(TypedDict):
2128 hookEventName: Literal["PreToolUse"]
2129 permissionDecision: NotRequired[Literal["allow", "deny", "ask"]]
2130 permissionDecisionReason: NotRequired[str]
2131 updatedInput: NotRequired[dict[str, Any]]
2132 additionalContext: NotRequired[str]
2133
2134
2135class PostToolUseHookSpecificOutput(TypedDict):
2136 hookEventName: Literal["PostToolUse"]
2137 additionalContext: NotRequired[str]
2138 updatedMCPToolOutput: NotRequired[Any]
2139
2140
2141class PostToolUseFailureHookSpecificOutput(TypedDict):
2142 hookEventName: Literal["PostToolUseFailure"]
2143 additionalContext: NotRequired[str]
2144
2145
2146class UserPromptSubmitHookSpecificOutput(TypedDict):
2147 hookEventName: Literal["UserPromptSubmit"]
2148 additionalContext: NotRequired[str]
2149
2150
2151class NotificationHookSpecificOutput(TypedDict):
2152 hookEventName: Literal["Notification"]
2153 additionalContext: NotRequired[str]
2154
2155
2156class SubagentStartHookSpecificOutput(TypedDict):
2157 hookEventName: Literal["SubagentStart"]
2158 additionalContext: NotRequired[str]
2159
2160
2161class PermissionRequestHookSpecificOutput(TypedDict):
2162 hookEventName: Literal["PermissionRequest"]
2163 decision: dict[str, Any]
2164
2165
2166HookSpecificOutput = (
2167 PreToolUseHookSpecificOutput
2168 | PostToolUseHookSpecificOutput
2169 | PostToolUseFailureHookSpecificOutput
2170 | UserPromptSubmitHookSpecificOutput
2171 | NotificationHookSpecificOutput
2172 | SubagentStartHookSpecificOutput
2173 | PermissionRequestHookSpecificOutput
2174)
2175```
2176
2177#### `AsyncHookJSONOutput`
2178
2179Async hook output that defers hook execution.
2180
2181```python theme={null}
2182class AsyncHookJSONOutput(TypedDict):
2183 async_: Literal[True] # Set to True to defer execution
2184 asyncTimeout: NotRequired[int] # Timeout in milliseconds
2185```
2186
2187<Note>
2188 Use `async_` (with underscore) in Python code. It is automatically converted to `async` when sent to the CLI.
2189</Note>
2190
2191### Hook Usage Example
2192
2193This example registers two hooks: one that blocks dangerous bash commands like `rm -rf /`, and another that logs all tool usage for auditing. The security hook only runs on Bash commands (via the `matcher`), while the logging hook runs on all tools.
2194
2195```python theme={null}
2196from claude_agent_sdk import query, ClaudeAgentOptions, HookMatcher, HookContext
2197from typing import Any
2198
2199
2200async def validate_bash_command(
2201 input_data: dict[str, Any], tool_use_id: str | None, context: HookContext
2202) -> dict[str, Any]:
2203 """Validate and potentially block dangerous bash commands."""
2204 if input_data["tool_name"] == "Bash":
2205 command = input_data["tool_input"].get("command", "")
2206 if "rm -rf /" in command:
2207 return {
2208 "hookSpecificOutput": {
2209 "hookEventName": "PreToolUse",
2210 "permissionDecision": "deny",
2211 "permissionDecisionReason": "Dangerous command blocked",
2212 }
2213 }
2214 return {}
2215
2216
2217async def log_tool_use(
2218 input_data: dict[str, Any], tool_use_id: str | None, context: HookContext
2219) -> dict[str, Any]:
2220 """Log all tool usage for auditing."""
2221 print(f"Tool used: {input_data.get('tool_name')}")
2222 return {}
2223
2224
2225options = ClaudeAgentOptions(
2226 hooks={
2227 "PreToolUse": [
2228 HookMatcher(
2229 matcher="Bash", hooks=[validate_bash_command], timeout=120
2230 ), # 2 min for validation
2231 HookMatcher(
2232 hooks=[log_tool_use]
2233 ), # Applies to all tools (default 60s timeout)
2234 ],
2235 "PostToolUse": [HookMatcher(hooks=[log_tool_use])],
2236 }
2237)
2238
2239async for message in query(prompt="Analyze this codebase", options=options):
2240 print(message)
2241```
2242
2243## Tool Input/Output Types
2244
2245Documentation of input/output schemas for all built-in Claude Code tools. While the Python SDK doesn't export these as types, they represent the structure of tool inputs and outputs in messages.
2246
2247### Agent
2248
2249**Tool name:** `Agent` (previously `Task`, which is still accepted as an alias)
2250
2251**Input:**
2252
2253```python theme={null}
2254{
2255 "description": str, # A short (3-5 word) description of the task
2256 "prompt": str, # The task for the agent to perform
2257 "subagent_type": str, # The type of specialized agent to use
2258}
2259```
2260
2261**Output:**
2262
2263```python theme={null}
2264{
2265 "result": str, # Final result from the subagent
2266 "usage": dict | None, # Token usage statistics
2267 "total_cost_usd": float | None, # Estimated total cost in USD
2268 "duration_ms": int | None, # Execution duration in milliseconds
2269}
2270```
2271
2272### AskUserQuestion
2273
2274**Tool name:** `AskUserQuestion`
2275
2276Asks the user clarifying questions during execution. See [Handle approvals and user input](/en/agent-sdk/user-input#handle-clarifying-questions) for usage details.
2277
2278**Input:**
2279
2280```python theme={null}
2281{
2282 "questions": [ # Questions to ask the user (1-4 questions)
2283 {
2284 "question": str, # The complete question to ask the user
2285 "header": str, # Very short label displayed as a chip/tag (max 12 chars)
2286 "options": [ # The available choices (2-4 options)
2287 {
2288 "label": str, # Display text for this option (1-5 words)
2289 "description": str, # Explanation of what this option means
2290 }
2291 ],
2292 "multiSelect": bool, # Set to true to allow multiple selections
2293 }
2294 ],
2295 "answers": dict | None, # User answers populated by the permission system
2296}
2297```
2298
2299**Output:**
2300
2301```python theme={null}
2302{
2303 "questions": [ # The questions that were asked
2304 {
2305 "question": str,
2306 "header": str,
2307 "options": [{"label": str, "description": str}],
2308 "multiSelect": bool,
2309 }
2310 ],
2311 "answers": dict[str, str], # Maps question text to answer string
2312 # Multi-select answers are comma-separated
2313}
2314```
2315
2316### Bash
2317
2318**Tool name:** `Bash`
2319
2320**Input:**
2321
2322```python theme={null}
2323{
2324 "command": str, # The command to execute
2325 "timeout": int | None, # Optional timeout in milliseconds (max 600000)
2326 "description": str | None, # Clear, concise description (5-10 words)
2327 "run_in_background": bool | None, # Set to true to run in background
2328}
2329```
2330
2331**Output:**
2332
2333```python theme={null}
2334{
2335 "output": str, # Combined stdout and stderr output
2336 "exitCode": int, # Exit code of the command
2337 "killed": bool | None, # Whether command was killed due to timeout
2338 "shellId": str | None, # Shell ID for background processes
2339}
2340```
2341
2342### Monitor
2343
2344**Tool name:** `Monitor`
2345
2346Runs a background script and delivers each stdout line to Claude as an event so it can react without polling. Monitor follows the same permission rules as Bash. See the [Monitor tool reference](/en/tools-reference#monitor-tool) for behavior and provider availability.
2347
2348**Input:**
2349
2350```python theme={null}
2351{
2352 "command": str, # Shell script; each stdout line is an event, exit ends the watch
2353 "description": str, # Short description shown in notifications
2354 "timeout_ms": int | None, # Kill after this deadline (default 300000, max 3600000)
2355 "persistent": bool | None, # Run for the lifetime of the session; stop with TaskStop
2356}
2357```
2358
2359**Output:**
2360
2361```python theme={null}
2362{
2363 "taskId": str, # ID of the background monitor task
2364 "timeoutMs": int, # Timeout deadline in milliseconds (0 when persistent)
2365 "persistent": bool | None, # True when running until TaskStop or session end
2366}
2367```
2368
2369### Edit
2370
2371**Tool name:** `Edit`
2372
2373**Input:**
2374
2375```python theme={null}
2376{
2377 "file_path": str, # The absolute path to the file to modify
2378 "old_string": str, # The text to replace
2379 "new_string": str, # The text to replace it with
2380 "replace_all": bool | None, # Replace all occurrences (default False)
2381}
2382```
2383
2384**Output:**
2385
2386```python theme={null}
2387{
2388 "message": str, # Confirmation message
2389 "replacements": int, # Number of replacements made
2390 "file_path": str, # File path that was edited
2391}
2392```
2393
2394### Read
2395
2396**Tool name:** `Read`
2397
2398**Input:**
2399
2400```python theme={null}
2401{
2402 "file_path": str, # The absolute path to the file to read
2403 "offset": int | None, # The line number to start reading from
2404 "limit": int | None, # The number of lines to read
2405}
2406```
2407
2408**Output (Text files):**
2409
2410```python theme={null}
2411{
2412 "content": str, # File contents with line numbers
2413 "total_lines": int, # Total number of lines in file
2414 "lines_returned": int, # Lines actually returned
2415}
2416```
2417
2418**Output (Images):**
2419
2420```python theme={null}
2421{
2422 "image": str, # Base64 encoded image data
2423 "mime_type": str, # Image MIME type
2424 "file_size": int, # File size in bytes
2425}
2426```
2427
2428### Write
2429
2430**Tool name:** `Write`
2431
2432**Input:**
2433
2434```python theme={null}
2435{
2436 "file_path": str, # The absolute path to the file to write
2437 "content": str, # The content to write to the file
2438}
2439```
2440
2441**Output:**
2442
2443```python theme={null}
2444{
2445 "message": str, # Success message
2446 "bytes_written": int, # Number of bytes written
2447 "file_path": str, # File path that was written
2448}
2449```
2450
2451### Glob
2452
2453**Tool name:** `Glob`
2454
2455**Input:**
2456
2457```python theme={null}
2458{
2459 "pattern": str, # The glob pattern to match files against
2460 "path": str | None, # The directory to search in (defaults to cwd)
2461}
2462```
2463
2464**Output:**
2465
2466```python theme={null}
2467{
2468 "matches": list[str], # Array of matching file paths
2469 "count": int, # Number of matches found
2470 "search_path": str, # Search directory used
2471}
2472```
2473
2474### Grep
2475
2476**Tool name:** `Grep`
2477
2478**Input:**
2479
2480```python theme={null}
2481{
2482 "pattern": str, # The regular expression pattern
2483 "path": str | None, # File or directory to search in
2484 "glob": str | None, # Glob pattern to filter files
2485 "type": str | None, # File type to search
2486 "output_mode": str | None, # "content", "files_with_matches", or "count"
2487 "-i": bool | None, # Case insensitive search
2488 "-n": bool | None, # Show line numbers
2489 "-B": int | None, # Lines to show before each match
2490 "-A": int | None, # Lines to show after each match
2491 "-C": int | None, # Lines to show before and after
2492 "head_limit": int | None, # Limit output to first N lines/entries
2493 "multiline": bool | None, # Enable multiline mode
2494}
2495```
2496
2497**Output (content mode):**
2498
2499```python theme={null}
2500{
2501 "matches": [
2502 {
2503 "file": str,
2504 "line_number": int | None,
2505 "line": str,
2506 "before_context": list[str] | None,
2507 "after_context": list[str] | None,
2508 }
2509 ],
2510 "total_matches": int,
2511}
2512```
2513
2514**Output (files\_with\_matches mode):**
2515
2516```python theme={null}
2517{
2518 "files": list[str], # Files containing matches
2519 "count": int, # Number of files with matches
2520}
2521```
2522
2523### NotebookEdit
2524
2525**Tool name:** `NotebookEdit`
2526
2527**Input:**
2528
2529```python theme={null}
2530{
2531 "notebook_path": str, # Absolute path to the Jupyter notebook
2532 "cell_id": str | None, # The ID of the cell to edit
2533 "new_source": str, # The new source for the cell
2534 "cell_type": "code" | "markdown" | None, # The type of the cell
2535 "edit_mode": "replace" | "insert" | "delete" | None, # Edit operation type
2536}
2537```
2538
2539**Output:**
2540
2541```python theme={null}
2542{
2543 "message": str, # Success message
2544 "edit_type": "replaced" | "inserted" | "deleted", # Type of edit performed
2545 "cell_id": str | None, # Cell ID that was affected
2546 "total_cells": int, # Total cells in notebook after edit
2547}
2548```
2549
2550### WebFetch
2551
2552**Tool name:** `WebFetch`
2553
2554**Input:**
2555
2556```python theme={null}
2557{
2558 "url": str, # The URL to fetch content from
2559 "prompt": str, # The prompt to run on the fetched content
2560}
2561```
2562
2563**Output:**
2564
2565```python theme={null}
2566{
2567 "response": str, # AI model's response to the prompt
2568 "url": str, # URL that was fetched
2569 "final_url": str | None, # Final URL after redirects
2570 "status_code": int | None, # HTTP status code
2571}
2572```
2573
2574### WebSearch
2575
2576**Tool name:** `WebSearch`
2577
2578**Input:**
2579
2580```python theme={null}
2581{
2582 "query": str, # The search query to use
2583 "allowed_domains": list[str] | None, # Only include results from these domains
2584 "blocked_domains": list[str] | None, # Never include results from these domains
2585}
2586```
2587
2588**Output:**
2589
2590```python theme={null}
2591{
2592 "results": [{"title": str, "url": str, "snippet": str, "metadata": dict | None}],
2593 "total_results": int,
2594 "query": str,
2595}
2596```
2597
2598### TodoWrite
2599
2600**Tool name:** `TodoWrite`
2601
2602**Input:**
2603
2604```python theme={null}
2605{
2606 "todos": [
2607 {
2608 "content": str, # The task description
2609 "status": "pending" | "in_progress" | "completed", # Task status
2610 "activeForm": str, # Active form of the description
2611 }
2612 ]
2613}
2614```
2615
2616**Output:**
2617
2618```python theme={null}
2619{
2620 "message": str, # Success message
2621 "stats": {"total": int, "pending": int, "in_progress": int, "completed": int},
2622}
2623```
2624
2625### BashOutput
2626
2627**Tool name:** `BashOutput`
2628
2629**Input:**
2630
2631```python theme={null}
2632{
2633 "bash_id": str, # The ID of the background shell
2634 "filter": str | None, # Optional regex to filter output lines
2635}
2636```
2637
2638**Output:**
2639
2640```python theme={null}
2641{
2642 "output": str, # New output since last check
2643 "status": "running" | "completed" | "failed", # Current shell status
2644 "exitCode": int | None, # Exit code when completed
2645}
2646```
2647
2648### KillBash
2649
2650**Tool name:** `KillBash`
2651
2652**Input:**
2653
2654```python theme={null}
2655{
2656 "shell_id": str # The ID of the background shell to kill
2657}
2658```
2659
2660**Output:**
2661
2662```python theme={null}
2663{
2664 "message": str, # Success message
2665 "shell_id": str, # ID of the killed shell
2666}
2667```
2668
2669### ExitPlanMode
2670
2671**Tool name:** `ExitPlanMode`
2672
2673**Input:**
2674
2675```python theme={null}
2676{
2677 "plan": str # The plan to run by the user for approval
2678}
2679```
2680
2681**Output:**
2682
2683```python theme={null}
2684{
2685 "message": str, # Confirmation message
2686 "approved": bool | None, # Whether user approved the plan
2687}
2688```
2689
2690### ListMcpResources
2691
2692**Tool name:** `ListMcpResources`
2693
2694**Input:**
2695
2696```python theme={null}
2697{
2698 "server": str | None # Optional server name to filter resources by
2699}
2700```
2701
2702**Output:**
2703
2704```python theme={null}
2705{
2706 "resources": [
2707 {
2708 "uri": str,
2709 "name": str,
2710 "description": str | None,
2711 "mimeType": str | None,
2712 "server": str,
2713 }
2714 ],
2715 "total": int,
2716}
2717```
2718
2719### ReadMcpResource
2720
2721**Tool name:** `ReadMcpResource`
2722
2723**Input:**
2724
2725```python theme={null}
2726{
2727 "server": str, # The MCP server name
2728 "uri": str, # The resource URI to read
2729}
2730```
2731
2732**Output:**
2733
2734```python theme={null}
2735{
2736 "contents": [
2737 {"uri": str, "mimeType": str | None, "text": str | None, "blob": str | None}
2738 ],
2739 "server": str,
2740}
2741```
2742
2743## Advanced Features with ClaudeSDKClient
2744
2745### Building a Continuous Conversation Interface
2746
2747```python theme={null}
2748from claude_agent_sdk import (
2749 ClaudeSDKClient,
2750 ClaudeAgentOptions,
2751 AssistantMessage,
2752 TextBlock,
2753)
2754import asyncio
2755
2756
2757class ConversationSession:
2758 """Maintains a single conversation session with Claude."""
2759
2760 def __init__(self, options: ClaudeAgentOptions | None = None):
2761 self.client = ClaudeSDKClient(options)
2762 self.turn_count = 0
2763
2764 async def start(self):
2765 await self.client.connect()
2766 print("Starting conversation session. Claude will remember context.")
2767 print(
2768 "Commands: 'exit' to quit, 'interrupt' to stop current task, 'new' for new session"
2769 )
2770
2771 while True:
2772 user_input = input(f"\n[Turn {self.turn_count + 1}] You: ")
2773
2774 if user_input.lower() == "exit":
2775 break
2776 elif user_input.lower() == "interrupt":
2777 await self.client.interrupt()
2778 print("Task interrupted!")
2779 continue
2780 elif user_input.lower() == "new":
2781 # Disconnect and reconnect for a fresh session
2782 await self.client.disconnect()
2783 await self.client.connect()
2784 self.turn_count = 0
2785 print("Started new conversation session (previous context cleared)")
2786 continue
2787
2788 # Send message - the session retains all previous messages
2789 await self.client.query(user_input)
2790 self.turn_count += 1
2791
2792 # Process response
2793 print(f"[Turn {self.turn_count}] Claude: ", end="")
2794 async for message in self.client.receive_response():
2795 if isinstance(message, AssistantMessage):
2796 for block in message.content:
2797 if isinstance(block, TextBlock):
2798 print(block.text, end="")
2799 print() # New line after response
2800
2801 await self.client.disconnect()
2802 print(f"Conversation ended after {self.turn_count} turns.")
2803
2804
2805async def main():
2806 options = ClaudeAgentOptions(
2807 allowed_tools=["Read", "Write", "Bash"], permission_mode="acceptEdits"
2808 )
2809 session = ConversationSession(options)
2810 await session.start()
2811
2812
2813# Example conversation:
2814# Turn 1 - You: "Create a file called hello.py"
2815# Turn 1 - Claude: "I'll create a hello.py file for you..."
2816# Turn 2 - You: "What's in that file?"
2817# Turn 2 - Claude: "The hello.py file I just created contains..." (remembers!)
2818# Turn 3 - You: "Add a main function to it"
2819# Turn 3 - Claude: "I'll add a main function to hello.py..." (knows which file!)
2820
2821asyncio.run(main())
2822```
2823
2824### Using Hooks for Behavior Modification
2825
2826```python theme={null}
2827from claude_agent_sdk import (
2828 ClaudeSDKClient,
2829 ClaudeAgentOptions,
2830 HookMatcher,
2831 HookContext,
2832)
2833import asyncio
2834from typing import Any
2835
2836
2837async def pre_tool_logger(
2838 input_data: dict[str, Any], tool_use_id: str | None, context: HookContext
2839) -> dict[str, Any]:
2840 """Log all tool usage before execution."""
2841 tool_name = input_data.get("tool_name", "unknown")
2842 print(f"[PRE-TOOL] About to use: {tool_name}")
2843
2844 # You can modify or block the tool execution here
2845 if tool_name == "Bash" and "rm -rf" in str(input_data.get("tool_input", {})):
2846 return {
2847 "hookSpecificOutput": {
2848 "hookEventName": "PreToolUse",
2849 "permissionDecision": "deny",
2850 "permissionDecisionReason": "Dangerous command blocked",
2851 }
2852 }
2853 return {}
2854
2855
2856async def post_tool_logger(
2857 input_data: dict[str, Any], tool_use_id: str | None, context: HookContext
2858) -> dict[str, Any]:
2859 """Log results after tool execution."""
2860 tool_name = input_data.get("tool_name", "unknown")
2861 print(f"[POST-TOOL] Completed: {tool_name}")
2862 return {}
2863
2864
2865async def user_prompt_modifier(
2866 input_data: dict[str, Any], tool_use_id: str | None, context: HookContext
2867) -> dict[str, Any]:
2868 """Add context to user prompts."""
2869 original_prompt = input_data.get("prompt", "")
2870
2871 # Add a timestamp as additional context for Claude to see
2872 from datetime import datetime
2873
2874 timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
2875
2876 return {
2877 "hookSpecificOutput": {
2878 "hookEventName": "UserPromptSubmit",
2879 "additionalContext": f"[Submitted at {timestamp}] Original prompt: {original_prompt}",
2880 }
2881 }
2882
2883
2884async def main():
2885 options = ClaudeAgentOptions(
2886 hooks={
2887 "PreToolUse": [
2888 HookMatcher(hooks=[pre_tool_logger]),
2889 HookMatcher(matcher="Bash", hooks=[pre_tool_logger]),
2890 ],
2891 "PostToolUse": [HookMatcher(hooks=[post_tool_logger])],
2892 "UserPromptSubmit": [HookMatcher(hooks=[user_prompt_modifier])],
2893 },
2894 allowed_tools=["Read", "Write", "Bash"],
2895 )
2896
2897 async with ClaudeSDKClient(options=options) as client:
2898 await client.query("List files in current directory")
2899
2900 async for message in client.receive_response():
2901 # Hooks will automatically log tool usage
2902 pass
2903
2904
2905asyncio.run(main())
2906```
2907
2908### Real-time Progress Monitoring
2909
2910```python theme={null}
2911from claude_agent_sdk import (
2912 ClaudeSDKClient,
2913 ClaudeAgentOptions,
2914 AssistantMessage,
2915 ToolUseBlock,
2916 ToolResultBlock,
2917 TextBlock,
2918)
2919import asyncio
2920
2921
2922async def monitor_progress():
2923 options = ClaudeAgentOptions(
2924 allowed_tools=["Write", "Bash"], permission_mode="acceptEdits"
2925 )
2926
2927 async with ClaudeSDKClient(options=options) as client:
2928 await client.query("Create 5 Python files with different sorting algorithms")
2929
2930 # Monitor progress in real-time
2931 async for message in client.receive_response():
2932 if isinstance(message, AssistantMessage):
2933 for block in message.content:
2934 if isinstance(block, ToolUseBlock):
2935 if block.name == "Write":
2936 file_path = block.input.get("file_path", "")
2937 print(f"Creating: {file_path}")
2938 elif isinstance(block, ToolResultBlock):
2939 print("Completed tool execution")
2940 elif isinstance(block, TextBlock):
2941 print(f"Claude says: {block.text[:100]}...")
2942
2943 print("Task completed!")
2944
2945
2946asyncio.run(monitor_progress())
2947```
2948
2949## Example Usage
2950
2951### Basic file operations (using query)
2952
2953```python theme={null}
2954from claude_agent_sdk import query, ClaudeAgentOptions, AssistantMessage, ToolUseBlock
2955import asyncio
2956
2957
2958async def create_project():
2959 options = ClaudeAgentOptions(
2960 allowed_tools=["Read", "Write", "Bash"],
2961 permission_mode="acceptEdits",
2962 cwd="/home/user/project",
2963 )
2964
2965 async for message in query(
2966 prompt="Create a Python project structure with setup.py", options=options
2967 ):
2968 if isinstance(message, AssistantMessage):
2969 for block in message.content:
2970 if isinstance(block, ToolUseBlock):
2971 print(f"Using tool: {block.name}")
2972
2973
2974asyncio.run(create_project())
2975```
2976
2977### Error handling
2978
2979```python theme={null}
2980from claude_agent_sdk import query, CLINotFoundError, ProcessError, CLIJSONDecodeError
2981
2982try:
2983 async for message in query(prompt="Hello"):
2984 print(message)
2985except CLINotFoundError:
2986 print(
2987 "Claude Code CLI not found. Try reinstalling: pip install --force-reinstall claude-agent-sdk"
2988 )
2989except ProcessError as e:
2990 print(f"Process failed with exit code: {e.exit_code}")
2991except CLIJSONDecodeError as e:
2992 print(f"Failed to parse response: {e}")
2993```
2994
2995### Streaming mode with client
2996
2997```python theme={null}
2998from claude_agent_sdk import ClaudeSDKClient
2999import asyncio
3000
3001
3002async def interactive_session():
3003 async with ClaudeSDKClient() as client:
3004 # Send initial message
3005 await client.query("What's the weather like?")
3006
3007 # Process responses
3008 async for msg in client.receive_response():
3009 print(msg)
3010
3011 # Send follow-up
3012 await client.query("Tell me more about that")
3013
3014 # Process follow-up response
3015 async for msg in client.receive_response():
3016 print(msg)
3017
3018
3019asyncio.run(interactive_session())
3020```
3021
3022### Using custom tools with ClaudeSDKClient
3023
3024```python theme={null}
3025from claude_agent_sdk import (
3026 ClaudeSDKClient,
3027 ClaudeAgentOptions,
3028 tool,
3029 create_sdk_mcp_server,
3030 AssistantMessage,
3031 TextBlock,
3032)
3033import asyncio
3034from typing import Any
3035
3036
3037# Define custom tools with @tool decorator
3038@tool("calculate", "Perform mathematical calculations", {"expression": str})
3039async def calculate(args: dict[str, Any]) -> dict[str, Any]:
3040 try:
3041 result = eval(args["expression"], {"__builtins__": {}})
3042 return {"content": [{"type": "text", "text": f"Result: {result}"}]}
3043 except Exception as e:
3044 return {
3045 "content": [{"type": "text", "text": f"Error: {str(e)}"}],
3046 "is_error": True,
3047 }
3048
3049
3050@tool("get_time", "Get current time", {})
3051async def get_time(args: dict[str, Any]) -> dict[str, Any]:
3052 from datetime import datetime
3053
3054 current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
3055 return {"content": [{"type": "text", "text": f"Current time: {current_time}"}]}
3056
3057
3058async def main():
3059 # Create SDK MCP server with custom tools
3060 my_server = create_sdk_mcp_server(
3061 name="utilities", version="1.0.0", tools=[calculate, get_time]
3062 )
3063
3064 # Configure options with the server
3065 options = ClaudeAgentOptions(
3066 mcp_servers={"utils": my_server},
3067 allowed_tools=["mcp__utils__calculate", "mcp__utils__get_time"],
3068 )
3069
3070 # Use ClaudeSDKClient for interactive tool usage
3071 async with ClaudeSDKClient(options=options) as client:
3072 await client.query("What's 123 * 456?")
3073
3074 # Process calculation response
3075 async for message in client.receive_response():
3076 if isinstance(message, AssistantMessage):
3077 for block in message.content:
3078 if isinstance(block, TextBlock):
3079 print(f"Calculation: {block.text}")
3080
3081 # Follow up with time query
3082 await client.query("What time is it now?")
3083
3084 async for message in client.receive_response():
3085 if isinstance(message, AssistantMessage):
3086 for block in message.content:
3087 if isinstance(block, TextBlock):
3088 print(f"Time: {block.text}")
3089
3090
3091asyncio.run(main())
3092```
3093
3094## Sandbox Configuration
3095
3096### `SandboxSettings`
3097
3098Configuration for sandbox behavior. Use this to enable command sandboxing and configure network restrictions programmatically.
3099
3100```python theme={null}
3101class SandboxSettings(TypedDict, total=False):
3102 enabled: bool
3103 autoAllowBashIfSandboxed: bool
3104 excludedCommands: list[str]
3105 allowUnsandboxedCommands: bool
3106 network: SandboxNetworkConfig
3107 ignoreViolations: SandboxIgnoreViolations
3108 enableWeakerNestedSandbox: bool
3109```
3110
3111| Property | Type | Default | Description |
3112| :-------------------------- | :------------------------------------------------------ | :------ | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
3113| `enabled` | `bool` | `False` | Enable sandbox mode for command execution |
3114| `autoAllowBashIfSandboxed` | `bool` | `True` | Auto-approve bash commands when sandbox is enabled |
3115| `excludedCommands` | `list[str]` | `[]` | Commands that always bypass sandbox restrictions (e.g., `["docker"]`). These run unsandboxed automatically without model involvement |
3116| `allowUnsandboxedCommands` | `bool` | `True` | Allow the model to request running commands outside the sandbox. When `True`, the model can set `dangerouslyDisableSandbox` in tool input, which falls back to the [permissions system](#permissions-fallback-for-unsandboxed-commands) |
3117| `network` | [`SandboxNetworkConfig`](#sandbox-network-config) | `None` | Network-specific sandbox configuration |
3118| `ignoreViolations` | [`SandboxIgnoreViolations`](#sandbox-ignore-violations) | `None` | Configure which sandbox violations to ignore |
3119| `enableWeakerNestedSandbox` | `bool` | `False` | Enable a weaker nested sandbox for compatibility |
3120
3121<Note>
3122 **Filesystem and network access restrictions** are NOT configured via sandbox settings. Instead, they are derived from [permission rules](/en/settings#permission-settings):
3123
3124 * **Filesystem read restrictions**: Read deny rules
3125 * **Filesystem write restrictions**: Edit allow/deny rules
3126 * **Network restrictions**: WebFetch allow/deny rules
3127
3128 Use sandbox settings for command execution sandboxing, and permission rules for filesystem and network access control.
3129</Note>
3130
3131#### Example usage
3132
3133```python theme={null}
3134from claude_agent_sdk import query, ClaudeAgentOptions, SandboxSettings
3135
3136sandbox_settings: SandboxSettings = {
3137 "enabled": True,
3138 "autoAllowBashIfSandboxed": True,
3139 "network": {"allowLocalBinding": True},
3140}
3141
3142async for message in query(
3143 prompt="Build and test my project",
3144 options=ClaudeAgentOptions(sandbox=sandbox_settings),
3145):
3146 print(message)
3147```
3148
3149<Warning>
3150 **Unix socket security**: The `allowUnixSockets` option can grant access to powerful system services. For example, allowing `/var/run/docker.sock` effectively grants full host system access through the Docker API, bypassing sandbox isolation. Only allow Unix sockets that are strictly necessary and understand the security implications of each.
3151</Warning>
3152
3153### `SandboxNetworkConfig`
3154
3155Network-specific configuration for sandbox mode.
3156
3157```python theme={null}
3158class SandboxNetworkConfig(TypedDict, total=False):
3159 allowLocalBinding: bool
3160 allowUnixSockets: list[str]
3161 allowAllUnixSockets: bool
3162 httpProxyPort: int
3163 socksProxyPort: int
3164```
3165
3166| Property | Type | Default | Description |
3167| :-------------------- | :---------- | :------ | :---------------------------------------------------------------- |
3168| `allowLocalBinding` | `bool` | `False` | Allow processes to bind to local ports (e.g., for dev servers) |
3169| `allowUnixSockets` | `list[str]` | `[]` | Unix socket paths that processes can access (e.g., Docker socket) |
3170| `allowAllUnixSockets` | `bool` | `False` | Allow access to all Unix sockets |
3171| `httpProxyPort` | `int` | `None` | HTTP proxy port for network requests |
3172| `socksProxyPort` | `int` | `None` | SOCKS proxy port for network requests |
3173
3174### `SandboxIgnoreViolations`
3175
3176Configuration for ignoring specific sandbox violations.
3177
3178```python theme={null}
3179class SandboxIgnoreViolations(TypedDict, total=False):
3180 file: list[str]
3181 network: list[str]
3182```
3183
3184| Property | Type | Default | Description |
3185| :-------- | :---------- | :------ | :------------------------------------------ |
3186| `file` | `list[str]` | `[]` | File path patterns to ignore violations for |
3187| `network` | `list[str]` | `[]` | Network patterns to ignore violations for |
3188
3189### Permissions Fallback for Unsandboxed Commands
3190
3191When `allowUnsandboxedCommands` is enabled, the model can request to run commands outside the sandbox by setting `dangerouslyDisableSandbox: True` in the tool input. These requests fall back to the existing permissions system, meaning your `can_use_tool` handler will be invoked, allowing you to implement custom authorization logic.
3192
3193<Note>
3194 **`excludedCommands` vs `allowUnsandboxedCommands`:**
3195
3196 * `excludedCommands`: A static list of commands that always bypass the sandbox automatically (e.g., `["docker"]`). The model has no control over this.
3197 * `allowUnsandboxedCommands`: Lets the model decide at runtime whether to request unsandboxed execution by setting `dangerouslyDisableSandbox: True` in the tool input.
3198</Note>
3199
3200```python theme={null}
3201from claude_agent_sdk import (
3202 query,
3203 ClaudeAgentOptions,
3204 HookMatcher,
3205 PermissionResultAllow,
3206 PermissionResultDeny,
3207 ToolPermissionContext,
3208)
3209
3210
3211async def can_use_tool(
3212 tool: str, input: dict, context: ToolPermissionContext
3213) -> PermissionResultAllow | PermissionResultDeny:
3214 # Check if the model is requesting to bypass the sandbox
3215 if tool == "Bash" and input.get("dangerouslyDisableSandbox"):
3216 # The model is requesting to run this command outside the sandbox
3217 print(f"Unsandboxed command requested: {input.get('command')}")
3218
3219 if is_command_authorized(input.get("command")):
3220 return PermissionResultAllow()
3221 return PermissionResultDeny(
3222 message="Command not authorized for unsandboxed execution"
3223 )
3224 return PermissionResultAllow()
3225
3226
3227# Required: dummy hook keeps the stream open for can_use_tool
3228async def dummy_hook(input_data, tool_use_id, context):
3229 return {"continue_": True}
3230
3231
3232async def prompt_stream():
3233 yield {
3234 "type": "user",
3235 "message": {"role": "user", "content": "Deploy my application"},
3236 }
3237
3238
3239async def main():
3240 async for message in query(
3241 prompt=prompt_stream(),
3242 options=ClaudeAgentOptions(
3243 sandbox={
3244 "enabled": True,
3245 "allowUnsandboxedCommands": True, # Model can request unsandboxed execution
3246 },
3247 permission_mode="default",
3248 can_use_tool=can_use_tool,
3249 hooks={"PreToolUse": [HookMatcher(matcher=None, hooks=[dummy_hook])]},
3250 ),
3251 ):
3252 print(message)
3253```
3254
3255This pattern enables you to:
3256
3257* **Audit model requests**: Log when the model requests unsandboxed execution
3258* **Implement allowlists**: Only permit specific commands to run unsandboxed
3259* **Add approval workflows**: Require explicit authorization for privileged operations
3260
3261<Warning>
3262 Commands running with `dangerouslyDisableSandbox: True` have full system access. Ensure your `can_use_tool` handler validates these requests carefully.
3263
3264 If `permission_mode` is set to `bypassPermissions` and `allow_unsandboxed_commands` is enabled, the model can autonomously execute commands outside the sandbox without any approval prompts. This combination effectively allows the model to escape sandbox isolation silently.
3265</Warning>
3266
3267## See also
3268
3269* [SDK overview](/en/agent-sdk/overview) - General SDK concepts
3270* [TypeScript SDK reference](/en/agent-sdk/typescript) - TypeScript SDK documentation
3271* [CLI reference](/en/cli-reference) - Command-line interface
3272* [Common workflows](/en/common-workflows) - Step-by-step guides