1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//! One-shot query function for simple interactions.
//!
//! This module provides the [`query`] function for simple, one-shot queries
//! to Claude. For more complex interactions requiring bidirectional
//! communication, use [`ClaudeClient`](crate::ClaudeClient).
use Pin;
use Stream;
use crateInternalClient;
use crateTransport;
use crateResult;
use crate;
/// Execute a one-shot query to Claude.
///
/// This is the simplest way to interact with Claude. It sends a prompt
/// and returns a stream of messages. The stream completes when Claude
/// finishes responding.
///
/// # Arguments
///
/// * `prompt` - The prompt to send to Claude
/// * `options` - Optional configuration for the query
/// * `transport` - Optional custom transport (uses subprocess by default)
///
/// # Returns
///
/// An async stream of [`Message`]s from Claude.
///
/// # Examples
///
/// ```rust,no_run
/// use claude_agents_sdk::{query, ClaudeAgentOptions, Message};
/// use tokio_stream::StreamExt;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let options = ClaudeAgentOptions::new()
/// .with_model("claude-3-sonnet")
/// .with_max_turns(3);
///
/// let mut stream = query("Hello, Claude!", Some(options), None).await?;
///
/// while let Some(message) = stream.next().await {
/// match message? {
/// Message::Assistant(msg) => {
/// println!("Claude: {}", msg.text());
/// }
/// Message::Result(result) => {
/// println!("Completed in {}ms", result.duration_ms);
/// }
/// _ => {}
/// }
/// }
///
/// Ok(())
/// }
/// ```
///
/// # Notes
///
/// - For queries requiring tool permission callbacks or hooks, the SDK
/// automatically uses streaming mode internally.
/// - The returned stream must be fully consumed to ensure proper cleanup.
pub async
/// Execute a query and collect all messages.
///
/// This is a convenience function that collects all messages from a query
/// into a vector. Useful for simple cases where you want to process all
/// results at once.
///
/// # Arguments
///
/// * `prompt` - The prompt to send to Claude
/// * `options` - Optional configuration for the query
///
/// # Returns
///
/// A vector of all [`Message`]s from the query.
///
/// # Examples
///
/// ```rust,no_run
/// use claude_agents_sdk::{query_all, ClaudeAgentOptions, Message};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let messages = query_all("What is 2 + 2?", None).await?;
///
/// for msg in messages {
/// if let Message::Assistant(asst) = msg {
/// println!("{}", asst.text());
/// }
/// }
///
/// Ok(())
/// }
/// ```
pub async
/// Execute a query with a prompt built from chunks.
///
/// This is useful when you want to build a prompt from multiple parts,
/// such as reading from a file or combining multiple sources. This is
/// the Rust equivalent of Python's `AsyncIterable[str]` prompt support.
///
/// For true streaming input patterns where you need to send multiple
/// messages over time, use [`ClaudeClient`](crate::ClaudeClient) instead.
///
/// # Arguments
///
/// * `chunks` - An iterator of string chunks that will be joined to form the prompt
/// * `options` - Optional configuration for the query
///
/// # Returns
///
/// An async stream of [`Message`]s from Claude.
///
/// # Examples
///
/// ```rust,no_run
/// use claude_agents_sdk::{query_chunks, Message};
/// use tokio_stream::StreamExt;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let prompt_parts = vec![
/// "Please analyze this code:\n",
/// "```rust\n",
/// "fn main() { println!(\"Hello\"); }\n",
/// "```\n",
/// ];
///
/// let mut stream = query_chunks(prompt_parts, None).await?;
///
/// while let Some(message) = stream.next().await {
/// if let Ok(Message::Assistant(msg)) = message {
/// println!("{}", msg.text());
/// }
/// }
///
/// Ok(())
/// }
/// ```
pub async
/// Get the final result from a query.
///
/// This is a convenience function that runs a query and returns only the
/// final result message, which contains cost and usage information.
///
/// # Arguments
///
/// * `prompt` - The prompt to send to Claude
/// * `options` - Optional configuration for the query
///
/// # Returns
///
/// The final text response and the result message with metadata.
///
/// # Examples
///
/// ```rust,no_run
/// use claude_agents_sdk::query_result;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let (response, result) = query_result("What is the capital of France?", None).await?;
///
/// println!("Response: {}", response);
/// if let Some(cost) = result.total_cost_usd {
/// println!("Cost: ${:.4}", cost);
/// }
///
/// Ok(())
/// }
/// ```
pub async