Query Claude Code for one-shot or unidirectional streaming interactions. This function is ideal for simple, stateless queries where you don't need bidirectional communication or conversation management. For interactive, stateful conversations, use ClaudeSDKClient instead. Key
(
*,
prompt: str | AsyncIterable[dict[str, Any]],
options: ClaudeAgentOptions | None = None,
transport: Transport | None = None,
)
| 9 | |
| 10 | |
| 11 | async def query( |
| 12 | *, |
| 13 | prompt: str | AsyncIterable[dict[str, Any]], |
| 14 | options: ClaudeAgentOptions | None = None, |
| 15 | transport: Transport | None = None, |
| 16 | ) -> AsyncIterator[Message]: |
| 17 | """ |
| 18 | Query Claude Code for one-shot or unidirectional streaming interactions. |
| 19 | |
| 20 | This function is ideal for simple, stateless queries where you don't need |
| 21 | bidirectional communication or conversation management. For interactive, |
| 22 | stateful conversations, use ClaudeSDKClient instead. |
| 23 | |
| 24 | Key differences from ClaudeSDKClient: |
| 25 | - **Unidirectional**: Send all messages upfront, receive all responses |
| 26 | - **Stateless**: Each query is independent, no conversation state |
| 27 | - **Simple**: Fire-and-forget style, no connection management |
| 28 | - **No interrupts**: Cannot interrupt or send follow-up messages |
| 29 | |
| 30 | When to use query(): |
| 31 | - Simple one-off questions ("What is 2+2?") |
| 32 | - Batch processing of independent prompts |
| 33 | - Code generation or analysis tasks |
| 34 | - Automated scripts and CI/CD pipelines |
| 35 | - When you know all inputs upfront |
| 36 | |
| 37 | When to use ClaudeSDKClient: |
| 38 | - Interactive conversations with follow-ups |
| 39 | - Chat applications or REPL-like interfaces |
| 40 | - When you need to send messages based on responses |
| 41 | - When you need interrupt capabilities |
| 42 | - Long-running sessions with state |
| 43 | |
| 44 | Args: |
| 45 | prompt: The prompt to send to Claude. Can be a string for single-shot queries |
| 46 | or an AsyncIterable[dict] for streaming mode with continuous interaction. |
| 47 | In streaming mode, each dict should have the structure: |
| 48 | { |
| 49 | "type": "user", |
| 50 | "message": {"role": "user", "content": "..."}, |
| 51 | "parent_tool_use_id": None, |
| 52 | "session_id": "..." |
| 53 | } |
| 54 | options: Optional configuration (defaults to ClaudeAgentOptions() if None). |
| 55 | Set options.permission_mode to control tool execution: |
| 56 | - 'default': CLI prompts for dangerous tools |
| 57 | - 'acceptEdits': Auto-accept file edits |
| 58 | - 'plan': Plan-only mode (no tool execution) |
| 59 | - 'bypassPermissions': Allow all tools (use with caution) |
| 60 | - 'dontAsk': Deny anything not pre-approved by allow rules |
| 61 | - 'auto': A model classifier approves or denies each tool call |
| 62 | Set options.cwd for working directory. |
| 63 | transport: Optional transport implementation. If provided, this will be used |
| 64 | instead of the default transport selection based on options. |
| 65 | The transport will be automatically configured with the prompt and options. |
| 66 | |
| 67 | Yields: |
| 68 | Messages from the conversation |