aquaregia 0.1.6

Provider-agnostic Rust toolkit for AI apps and agents.
Documentation
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
//! Provider-bound client types and retry behavior for Aquaregia.
//!
//! This module provides the core client abstractions for making LLM requests:
//!
//! - [`LlmClient`]: Entry point for creating provider-specific clients
//! - [`ClientBuilder<P>`]: Builder for configuring HTTP/runtime behavior
//! - [`BoundClient<P>`]: Reusable client for generate/stream/agent operations
//!
//! ## Architecture
//!
//! The client architecture uses a type-state pattern where:
//! 1. [`LlmClient`] constructors return a [`ClientBuilder<P>`] with provider type `P`
//! 2. [`ClientBuilder<P>`] configures settings and HTTP behavior
//! 3. [`ClientBuilder<P>::build()`] produces a [`BoundClient<P>`]
//! 4. [`BoundClient<P>`] is used for all subsequent operations
//!
//! ## Example
//!
//! ```rust,no_run
//! use aquaregia::{GenerateTextRequest, LlmClient};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Create and build client
//! let client = LlmClient::openai("api-key")
//!     .timeout(std::time::Duration::from_secs(60))
//!     .max_retries(3)
//!     .build()?;
//!
//! // Use client for generation
//! let response = client
//!     .generate(GenerateTextRequest::from_user_prompt("gpt-4o", "Hello!"))
//!     .await?;
//!
//! println!("{}", response.output_text);
//! # Ok(())
//! # }
//! ```

use std::marker::PhantomData;
use std::sync::Arc;
use std::time::{Duration, Instant};

use futures_util::future::join_all;
use tokio::time::sleep;

use crate::error::{Error, ErrorCode};
use crate::model_adapters::ModelAdapter;
use crate::model_adapters::anthropic::{AnthropicAdapter, AnthropicAdapterSettings};
use crate::model_adapters::google::{GoogleAdapter, GoogleAdapterSettings};
use crate::model_adapters::openai::{OpenAiAdapter, OpenAiAdapterSettings};
use crate::model_adapters::openai_compatible::{
    OpenAiCompatibleAdapter, OpenAiCompatibleAdapterSettings,
};
use crate::tool::{ToolExecError, ToolRegistry};
use crate::types::{
    AgentFinish, AgentPrepareStep, AgentPreparedStep, AgentResponse, AgentStart, AgentStep,
    AgentStepStart, AgentToolCallFinish, AgentToolCallStart, Anthropic, ContentPart,
    GenerateTextRequest, GenerateTextResponse, Google, Message, OpenAi, OpenAiCompatible,
    ProviderMarker, RunTools, TextStream, ToolCall, ToolErrorPolicy, ToolResult, Usage,
    validate_max_steps, validate_messages, validate_model_ref, validate_sampling,
};

/// Binds a provider marker type to its adapter settings and adapter implementation.
///
/// This trait is mainly an internal extension seam used by [`ClientBuilder`].
/// It associates each provider marker with its specific settings type and
/// provides the conversion logic to create a runtime adapter.
///
/// ## Implementors
///
/// This trait is implemented for the four provider markers:
/// - [`OpenAi`]
/// - [`Anthropic`]
/// - [`Google`]
/// - [`OpenAiCompatible`]
pub trait ProviderBinding: ProviderMarker {
    /// Provider-specific client configuration payload.
    ///
    /// Each provider has different configuration options:
    /// - OpenAI: `base_url`, `api_key`
    /// - Anthropic: `base_url`, `api_key`, `api_version`
    /// - Google: `base_url`, `api_key`
    /// - OpenAI-compatible: `base_url`, optional `api_key`, custom headers/query params
    type Settings;

    /// Converts provider settings into a runtime adapter.
    ///
    /// This method consumes the settings and creates a boxed adapter trait object
    /// that handles provider-specific request/response formatting.
    fn into_adapter(
        settings: Self::Settings,
        http: Arc<reqwest::Client>,
    ) -> Arc<dyn ModelAdapter<Self>>;
}

impl ProviderBinding for OpenAi {
    type Settings = OpenAiAdapterSettings;

    fn into_adapter(
        settings: Self::Settings,
        http: Arc<reqwest::Client>,
    ) -> Arc<dyn ModelAdapter<Self>> {
        Arc::new(OpenAiAdapter::from_settings(settings, http))
    }
}

impl ProviderBinding for Anthropic {
    type Settings = AnthropicAdapterSettings;

    fn into_adapter(
        settings: Self::Settings,
        http: Arc<reqwest::Client>,
    ) -> Arc<dyn ModelAdapter<Self>> {
        Arc::new(AnthropicAdapter::from_settings(settings, http))
    }
}

impl ProviderBinding for Google {
    type Settings = GoogleAdapterSettings;

    fn into_adapter(
        settings: Self::Settings,
        http: Arc<reqwest::Client>,
    ) -> Arc<dyn ModelAdapter<Self>> {
        Arc::new(GoogleAdapter::from_settings(settings, http))
    }
}

impl ProviderBinding for OpenAiCompatible {
    type Settings = OpenAiCompatibleAdapterSettings;

    fn into_adapter(
        settings: Self::Settings,
        http: Arc<reqwest::Client>,
    ) -> Arc<dyn ModelAdapter<Self>> {
        Arc::new(OpenAiCompatibleAdapter::from_settings(settings, http))
    }
}

/// Entry point for creating provider-bound clients.
///
/// `LlmClient` only provides constructors; call `.build()` on the returned
/// [`ClientBuilder`] to obtain a reusable [`BoundClient`].
pub struct LlmClient;

impl LlmClient {
    /// Creates an OpenAI client builder.
    pub fn openai(api_key: impl Into<String>) -> ClientBuilder<OpenAi> {
        ClientBuilder::new(OpenAiAdapterSettings::new(api_key))
    }

    /// Creates an Anthropic client builder.
    pub fn anthropic(api_key: impl Into<String>) -> ClientBuilder<Anthropic> {
        ClientBuilder::new(AnthropicAdapterSettings::new(api_key))
    }

    /// Creates a Google client builder.
    pub fn google(api_key: impl Into<String>) -> ClientBuilder<Google> {
        ClientBuilder::new(GoogleAdapterSettings::new(api_key))
    }

    /// Creates an OpenAI-compatible client builder.
    pub fn openai_compatible(base_url: impl Into<String>) -> ClientBuilder<OpenAiCompatible> {
        ClientBuilder::new(OpenAiCompatibleAdapterSettings::new(base_url))
    }
}

/// Configures HTTP/runtime behavior before building a [`BoundClient`].
pub struct ClientBuilder<P: ProviderBinding> {
    timeout: Duration,
    max_retries: u8,
    default_max_steps: u8,
    user_agent: String,
    settings: P::Settings,
}

impl<P: ProviderBinding> ClientBuilder<P> {
    fn new(settings: P::Settings) -> Self {
        Self {
            timeout: Duration::from_secs(30),
            max_retries: 3,
            default_max_steps: 8,
            user_agent: format!("aquaregia-ai-sdk/{}", env!("CARGO_PKG_VERSION")),
            settings,
        }
    }

    /// Sets request timeout for all requests sent by this client.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Sets the maximum number of retries for retryable errors.
    pub fn max_retries(mut self, retries: u8) -> Self {
        self.max_retries = retries;
        self
    }

    /// Sets the default max step count used by agent tool loops.
    pub fn default_max_steps(mut self, max_steps: u8) -> Self {
        self.default_max_steps = max_steps;
        self
    }

    /// Overrides the default SDK `User-Agent` header value.
    pub fn user_agent(mut self, ua: impl Into<String>) -> Self {
        self.user_agent = ua.into();
        self
    }

    /// Builds a provider-bound client with validated settings.
    pub fn build(self) -> Result<BoundClient<P>, Error> {
        validate_max_steps(self.default_max_steps)?;
        let http = Arc::new(
            reqwest::Client::builder()
                .timeout(self.timeout)
                .user_agent(self.user_agent)
                .build()
                .map_err(|e| Error::new(ErrorCode::Transport, e.to_string()))?,
        );

        Ok(BoundClient {
            max_retries: self.max_retries,
            default_max_steps: self.default_max_steps,
            adapter: P::into_adapter(self.settings, http),
            _marker: PhantomData,
        })
    }
}

impl ClientBuilder<OpenAi> {
    /// Overrides the OpenAI API base URL.
    pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
        self.settings.base_url = base_url.into();
        self
    }
}

impl ClientBuilder<Anthropic> {
    /// Overrides the Anthropic API base URL.
    pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
        self.settings.base_url = base_url.into();
        self
    }

    /// Overrides the Anthropic API version header.
    pub fn api_version(mut self, api_version: impl Into<String>) -> Self {
        self.settings.api_version = api_version.into();
        self
    }
}

impl ClientBuilder<Google> {
    /// Overrides the Google Generative Language API base URL.
    pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
        self.settings.base_url = base_url.into();
        self
    }
}

impl ClientBuilder<OpenAiCompatible> {
    /// Sets a bearer token for OpenAI-compatible requests.
    pub fn api_key(mut self, api_key: impl Into<String>) -> Self {
        self.settings.set_api_key(api_key);
        self
    }

    /// Sends requests without an `Authorization` bearer token.
    pub fn no_api_key(mut self) -> Self {
        self.settings.clear_api_key();
        self
    }

    /// Adds or replaces a custom HTTP header.
    pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.settings.insert_header(name, value);
        self
    }

    /// Adds or replaces a query parameter on the chat completions endpoint.
    pub fn query_param(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.settings.insert_query_param(name, value);
        self
    }

    /// Overrides the chat completions path (default: `/v1/chat/completions`).
    pub fn chat_completions_path(mut self, path: impl Into<String>) -> Self {
        self.settings.set_chat_completions_path(path);
        self
    }

    /// Enables or disables `<think>/<thinking>` parsing from `content`.
    ///
    /// Disabled by default.
    pub fn think_tag_parsing(mut self, enabled: bool) -> Self {
        self.settings.set_think_tag_parsing_enabled(enabled);
        self
    }

    /// Controls case sensitivity for think tag parsing.
    ///
    /// Enabled by default (`true`) and only used when think tag parsing is enabled.
    pub fn think_tag_case_insensitive(mut self, case_insensitive: bool) -> Self {
        self.settings
            .set_think_tag_case_insensitive(case_insensitive);
        self
    }
}

/// Reusable provider-bound client used for `generate`, `stream`, and agent loops.
pub struct BoundClient<P: ProviderMarker> {
    max_retries: u8,
    default_max_steps: u8,
    adapter: Arc<dyn ModelAdapter<P>>,
    _marker: PhantomData<P>,
}

impl<P: ProviderMarker> BoundClient<P> {
    #[cfg_attr(
        feature = "telemetry",
        tracing::instrument(
            skip_all,
            fields(model = %req.model.model(), provider = %P::KIND.as_slug())
        )
    )]
    /// Runs a non-streaming generation request.
    ///
    /// The request is validated locally and retried on retryable failures.
    pub async fn generate(
        &self,
        req: GenerateTextRequest<P>,
    ) -> Result<GenerateTextResponse, Error> {
        validate_model_ref(&req.model)?;
        validate_messages(&req.messages)?;
        validate_sampling(req.temperature, req.top_p)?;
        self.call_with_retry(|| async { self.adapter.generate_text(&req).await })
            .await
    }

    #[cfg_attr(
        feature = "telemetry",
        tracing::instrument(
            skip_all,
            fields(model = %req.model.model())
        )
    )]
    /// Runs a streaming generation request.
    ///
    /// The request is validated locally and retried on retryable failures.
    pub async fn stream(&self, req: GenerateTextRequest<P>) -> Result<TextStream, Error> {
        validate_model_ref(&req.model)?;
        validate_messages(&req.messages)?;
        validate_sampling(req.temperature, req.top_p)?;
        self.call_with_retry(|| async { self.adapter.stream_text(&req).await })
            .await
    }

    pub(crate) async fn run_tools(&self, req: RunTools<P>) -> Result<AgentResponse, Error> {
        let RunTools {
            model,
            messages,
            tools,
            max_steps,
            temperature,
            top_p,
            max_output_tokens,
            stop_sequences,
            prepare_step,
            on_start,
            on_step_start,
            on_tool_call_start,
            on_tool_call_finish,
            on_step_finish,
            on_finish,
            stop_when,
            tool_error_policy,
            cancellation_token,
        } = req;

        let resolved_max_steps = max_steps.unwrap_or(self.default_max_steps);

        let mut messages = messages;
        let mut usage_total = Usage::default();
        let mut step_results = Vec::new();

        if let Some(callback) = &on_start {
            callback(&AgentStart {
                model_id: model.id(),
                messages: messages.clone(),
                tool_count: tools.len(),
                max_steps: resolved_max_steps,
            });
        }

        for step in 1..=resolved_max_steps {
            if cancellation_token
                .as_ref()
                .map(|t| t.is_cancelled())
                .unwrap_or(false)
            {
                return Err(Error::new(ErrorCode::Cancelled, "agent cancelled"));
            }

            #[cfg(feature = "telemetry")]
            let _step_span = tracing::info_span!("agent_step", step = step).entered();

            let mut prepared_step = AgentPreparedStep {
                model: model.clone(),
                messages: messages.clone(),
                tools: tools.clone(),
                temperature,
                max_output_tokens,
                stop_sequences: stop_sequences.clone(),
            };
            if let Some(callback) = &prepare_step {
                prepared_step = callback(&AgentPrepareStep {
                    step,
                    model: model.clone(),
                    messages: messages.clone(),
                    tools: tools.clone(),
                    temperature,
                    max_output_tokens,
                    stop_sequences: stop_sequences.clone(),
                    previous_steps: step_results.clone(),
                });
            }

            validate_messages(&prepared_step.messages)?;

            if let Some(callback) = &on_step_start {
                callback(&AgentStepStart {
                    step,
                    messages: prepared_step.messages.clone(),
                });
            }

            let response = self
                .generate(GenerateTextRequest {
                    model: prepared_step.model.clone(),
                    messages: prepared_step.messages.clone(),
                    temperature: prepared_step.temperature,
                    top_p,
                    max_output_tokens: prepared_step.max_output_tokens,
                    stop_sequences: prepared_step.stop_sequences.clone(),
                    tools: if prepared_step.tools.is_empty() {
                        None
                    } else {
                        Some(
                            prepared_step
                                .tools
                                .iter()
                                .map(|tool| tool.descriptor.clone())
                                .collect(),
                        )
                    },
                    cancellation_token: cancellation_token.clone(),
                })
                .await?;
            usage_total += response.usage.clone();
            let mut next_messages = prepared_step.messages.clone();
            next_messages.push(assistant_message_from_response(&response));

            if response.tool_calls.is_empty() {
                let step_state = AgentStep {
                    step,
                    output_text: response.output_text.clone(),
                    reasoning_text: response.reasoning_text.clone(),
                    reasoning_parts: response.reasoning_parts.clone(),
                    finish_reason: response.finish_reason.clone(),
                    usage: response.usage.clone(),
                    tool_calls: Vec::new(),
                    tool_results: Vec::new(),
                };
                step_results.push(step_state.clone());
                if let Some(callback) = &on_step_finish {
                    callback(&step_state);
                }
                let final_response = AgentResponse {
                    output_text: response.output_text,
                    steps: step,
                    transcript: next_messages,
                    usage_total,
                    step_results: step_results.clone(),
                };
                emit_on_finish(
                    on_finish.as_ref(),
                    &final_response,
                    &step_state.finish_reason,
                    &step_results,
                );
                return Ok(final_response);
            }

            let tool_registry = ToolRegistry::from_tools(prepared_step.tools.clone())?;
            let executed_tool_calls = execute_tool_calls(
                &tool_registry,
                &response.tool_calls,
                step,
                tool_error_policy,
                on_tool_call_start.as_ref(),
                on_tool_call_finish.as_ref(),
            )
            .await?;
            let mut tool_messages = executed_tool_calls
                .iter()
                .map(|entry| Message::tool_result(entry.result.clone()))
                .collect::<Vec<_>>();
            let step_state = AgentStep {
                step,
                output_text: response.output_text.clone(),
                reasoning_text: response.reasoning_text.clone(),
                reasoning_parts: response.reasoning_parts.clone(),
                finish_reason: response.finish_reason.clone(),
                usage: response.usage.clone(),
                tool_calls: response.tool_calls.clone(),
                tool_results: executed_tool_calls
                    .iter()
                    .map(|entry| entry.result.clone())
                    .collect(),
            };
            step_results.push(step_state.clone());
            next_messages.append(&mut tool_messages);
            if let Some(callback) = &on_step_finish {
                callback(&step_state);
            }
            if stop_when
                .as_ref()
                .is_some_and(|predicate| predicate(&step_state))
            {
                let final_response = AgentResponse {
                    output_text: response.output_text,
                    steps: step,
                    transcript: next_messages,
                    usage_total,
                    step_results: step_results.clone(),
                };
                emit_on_finish(
                    on_finish.as_ref(),
                    &final_response,
                    &step_state.finish_reason,
                    &step_results,
                );
                return Ok(final_response);
            }

            messages = next_messages;
        }

        Err(Error::new(
            ErrorCode::MaxStepsExceeded,
            format!(
                "agent reached max_steps ({}) without final answer",
                resolved_max_steps
            ),
        ))
    }

    async fn call_with_retry<T, F, Fut>(&self, mut op: F) -> Result<T, Error>
    where
        F: FnMut() -> Fut,
        Fut: std::future::Future<Output = Result<T, Error>>,
    {
        let mut attempt = 0u8;
        loop {
            match op().await {
                Ok(v) => return Ok(v),
                Err(err) => {
                    if !err.retryable || attempt >= self.max_retries {
                        return Err(err);
                    }
                    attempt = attempt.saturating_add(1);
                    let delay = backoff_delay(attempt);
                    sleep(delay).await;
                }
            }
        }
    }
}

fn backoff_delay(attempt: u8) -> Duration {
    let base_ms = 200u64;
    let cap_ms = 2_000u64;
    let exp = 2u64.saturating_pow(attempt as u32);
    Duration::from_millis((base_ms.saturating_mul(exp)).min(cap_ms))
}

fn assistant_message_from_response(response: &GenerateTextResponse) -> Message {
    let mut parts = Vec::new();
    for reasoning in &response.reasoning_parts {
        parts.push(ContentPart::Reasoning(reasoning.clone()));
    }
    if !response.output_text.is_empty() {
        parts.push(ContentPart::Text(response.output_text.clone()));
    }
    for call in &response.tool_calls {
        parts.push(ContentPart::ToolCall(call.clone()));
    }
    if parts.is_empty() {
        parts.push(ContentPart::Text(String::new()));
    }
    Message::assistant_with_parts(parts)
}

fn emit_on_finish(
    callback: Option<&crate::types::Hook<AgentFinish>>,
    response: &AgentResponse,
    finish_reason: &crate::types::FinishReason,
    step_results: &[AgentStep],
) {
    let Some(callback) = callback else {
        return;
    };

    callback(&AgentFinish {
        output_text: response.output_text.clone(),
        step_count: response.steps,
        finish_reason: finish_reason.clone(),
        usage_total: response.usage_total.clone(),
        transcript: response.transcript.clone(),
        step_results: step_results.to_vec(),
    });
}

#[derive(Debug, Clone)]
struct ExecutedToolCall {
    result: ToolResult,
}

async fn execute_tool_calls(
    registry: &ToolRegistry,
    calls: &[ToolCall],
    step: u8,
    policy: ToolErrorPolicy,
    on_tool_call_start: Option<&crate::types::Hook<AgentToolCallStart>>,
    on_tool_call_finish: Option<&crate::types::Hook<AgentToolCallFinish>>,
) -> Result<Vec<ExecutedToolCall>, Error> {
    let mut tasks = Vec::with_capacity(calls.len());
    for call in calls {
        let Some(registered) = registry.resolve(&call.tool_name) else {
            return Err(Error::new(
                ErrorCode::UnknownTool,
                format!("unknown tool `{}`", call.tool_name),
            ));
        };

        registered
            .validator
            .validate(&call.args_json)
            .map_err(|e| {
                Error::new(
                    ErrorCode::InvalidToolArgs,
                    format!(
                        "tool args for `{}` failed schema validation: {}",
                        call.tool_name, e
                    ),
                )
            })?;

        if let Some(callback) = on_tool_call_start {
            callback(&AgentToolCallStart {
                step,
                tool_call: call.clone(),
            });
        }

        let executor = Arc::clone(&registered.tool.executor);
        let call = call.clone();
        let args_json = call.args_json.clone();
        tasks.push(async move {
            #[cfg(feature = "telemetry")]
            let _span = tracing::info_span!("tool_call", tool.name = %call.tool_name).entered();
            let started_at = Instant::now();
            let result = executor.execute(args_json).await;
            let duration_ms = started_at.elapsed().as_millis().min(u64::MAX as u128) as u64;
            (call, result, duration_ms)
        });
    }

    let results = join_all(tasks).await;
    let mut executions = Vec::with_capacity(results.len());
    for (call, result, duration_ms) in results {
        let (output_json, is_error) = match result {
            Ok(output_json) => (output_json, false),
            Err(ToolExecError::Execution(message)) => {
                if policy == ToolErrorPolicy::FailFast {
                    return Err(Error::new(
                        ErrorCode::ToolExecutionFailed,
                        format!(
                            "tool `{}` execution failed for call `{}`: {}",
                            call.tool_name, call.call_id, message
                        ),
                    ));
                }
                (serde_json::json!({ "error": message }), true)
            }
            Err(ToolExecError::Timeout) => {
                if policy == ToolErrorPolicy::FailFast {
                    return Err(Error::new(
                        ErrorCode::ToolExecutionFailed,
                        format!(
                            "tool `{}` timed out for call `{}`",
                            call.tool_name, call.call_id
                        ),
                    ));
                }
                (serde_json::json!({ "error": "timeout" }), true)
            }
        };

        let tool_result = ToolResult {
            call_id: call.call_id.clone(),
            output_json,
            is_error,
        };

        if let Some(callback) = on_tool_call_finish {
            callback(&AgentToolCallFinish {
                step,
                tool_call: call.clone(),
                tool_result: tool_result.clone(),
                duration_ms,
            });
        }

        executions.push(ExecutedToolCall {
            result: tool_result,
        });
    }

    Ok(executions)
}

#[cfg(test)]
mod tests {
    use super::LlmClient;

    #[test]
    fn openai_builder_builds() {
        let client = LlmClient::openai("key")
            .build()
            .expect("client should build");
        let _ = client;
    }

    #[test]
    fn anthropic_builder_builds() {
        let client = LlmClient::anthropic("key")
            .build()
            .expect("client should build");
        let _ = client;
    }
}