ClaudeAgentSDK.Query (claude_agent_sdk v0.6.9)
View SourceHandles querying Claude Code and processing responses.
This module is responsible for building the appropriate command-line arguments for different types of Claude Code queries (new queries, continuations, and resumptions) and delegating to the Process module for execution.
All functions in this module return a Stream of ClaudeAgentSDK.Message structs.
SDK MCP Server Support
When SDK MCP servers are detected in options, the query automatically uses the Client GenServer (which supports bidirectional control protocol) instead of the simpler Process.stream approach. This is transparent to the caller - you still get the same Stream interface.
Summary
Functions
Continues the most recent conversation.
Resumes a specific conversation by session ID.
Runs a new query with the given prompt and options.
Functions
@spec continue(String.t() | nil, ClaudeAgentSDK.Options.t()) :: Enumerable.t(ClaudeAgentSDK.Message.t())
Continues the most recent conversation.
Parameters
prompt- Optional additional prompt to send (string or nil)options- Configuration options (seeClaudeAgentSDK.Options.t/0)
Returns
A stream of ClaudeAgentSDK.Message.t/0 structs.
Examples
ClaudeAgentSDK.Query.continue("Add error handling", %ClaudeAgentSDK.Options{})
@spec resume(String.t(), String.t() | nil, ClaudeAgentSDK.Options.t()) :: Enumerable.t(ClaudeAgentSDK.Message.t())
Resumes a specific conversation by session ID.
Parameters
session_id- The session ID to resume (string)prompt- Optional additional prompt to send (string or nil)options- Configuration options (seeClaudeAgentSDK.Options.t/0)
Returns
A stream of ClaudeAgentSDK.Message.t/0 structs.
Examples
ClaudeAgentSDK.Query.resume("session-123", "Add tests", %ClaudeAgentSDK.Options{})
@spec run(String.t(), ClaudeAgentSDK.Options.t()) :: Enumerable.t(ClaudeAgentSDK.Message.t())
Runs a new query with the given prompt and options.
Automatically detects if SDK MCP servers are present in options and routes to the appropriate backend:
- SDK MCP servers present → Uses Client GenServer (bidirectional control protocol)
- No SDK MCP servers → Uses Process.stream (simple unidirectional streaming)
Parameters
prompt- The prompt to send to Claude (string)options- Configuration options (seeClaudeAgentSDK.Options.t/0)
Returns
A stream of ClaudeAgentSDK.Message.t/0 structs.
Examples
# Simple query (no SDK MCP)
ClaudeAgentSDK.Query.run("Write a hello world function", %ClaudeAgentSDK.Options{})
# With SDK MCP servers (auto-uses Client)
server = ClaudeAgentSDK.create_sdk_mcp_server(name: "math", tools: [Add])
options = %Options{mcp_servers: %{"math" => server}}
ClaudeAgentSDK.Query.run("What is 2+2?", options)