agent-sdk-providers 0.9.2

LLM provider trait, streaming primitives, and first-party provider implementations for the Agent SDK
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
//! Provider wrapper that refreshes credentials on 401 and retries once.
//!
//! [`RefreshingProvider`] wraps any [`LlmProvider`] and adds a host-driven
//! credential refresh step: when the inner provider reports an unauthorized
//! error (HTTP 401, expired OAuth token, invalid API key, etc.), the wrapper
//! calls a host-supplied async callback to rebuild the inner provider with
//! fresh credentials and retries the original request once.
//!
//! This is the generic form of the per-provider refresh wrappers that
//! OAuth-backed hosts would otherwise copy across every provider they use.
//!
//! # Example
//!
//! ```no_run
//! # use std::sync::Arc;
//! # use anyhow::Result;
//! # use agent_sdk_providers::{LlmProvider, RefreshingProvider};
//! # async fn demo<P: LlmProvider + Clone + 'static>(initial: P) -> Result<()> {
//! let refreshed_count = Arc::new(std::sync::atomic::AtomicUsize::new(0));
//! let counter = Arc::clone(&refreshed_count);
//! let wrapped = RefreshingProvider::new(initial.clone(), move || {
//!     let counter = Arc::clone(&counter);
//!     let provider = initial.clone();
//!     async move {
//!         counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
//!         Ok(provider)
//!     }
//! });
//! # let _ = wrapped;
//! # Ok(())
//! # }
//! ```
//!
//! ## Streaming semantics
//!
//! For non-streaming [`chat`](LlmProvider::chat), the retry happens when the
//! first call resolves to [`ChatOutcome::InvalidRequest`] with a 401-looking
//! message. The second call replaces the first — callers only see the final
//! outcome.
//!
//! For [`chat_stream`](LlmProvider::chat_stream), retry happens only when the
//! 401 arrives before any content delta is forwarded. If a
//! [`StreamDelta::TextDelta`], [`StreamDelta::ThinkingDelta`], tool-call
//! delta, or thinking signature has already been yielded to the consumer,
//! the error is forwarded as-is — retrying would duplicate partial output.
//!
//! At most one retry happens per call, whether streaming or not. If the
//! retried call fails in the same way, the wrapper surfaces the second
//! error unchanged.

use std::future::Future;
use std::sync::Arc;

use agent_sdk_foundation::llm::{ChatOutcome, ChatRequest, ThinkingConfig};
use anyhow::Result;
use async_trait::async_trait;
use futures::StreamExt;
use tokio::sync::Mutex;

use crate::provider::LlmProvider;
use crate::streaming::{StreamBox, StreamDelta};

/// Wraps a provider with host-driven credential refresh on 401.
///
/// The inner provider is stored behind `Arc<Mutex<P>>` so it can be swapped
/// atomically when the refresh callback produces a new provider. Cloning a
/// wrapper is cheap — clones share the same inner state.
///
/// Metadata (`model`, `provider`, `configured_thinking`) is captured from the
/// initial provider at construction time and assumed constant across
/// refreshes (the refresh callback rebuilds the same provider shape with a
/// fresh token, not a different model).
pub struct RefreshingProvider<P, F> {
    inner: Arc<Mutex<P>>,
    refresh: Arc<F>,
    model: String,
    provider: &'static str,
    thinking: Option<ThinkingConfig>,
}

impl<P, F> Clone for RefreshingProvider<P, F> {
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
            refresh: Arc::clone(&self.refresh),
            model: self.model.clone(),
            provider: self.provider,
            thinking: self.thinking.clone(),
        }
    }
}

impl<P, F, Fut> RefreshingProvider<P, F>
where
    P: LlmProvider + Clone + 'static,
    F: Fn() -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<P>> + Send + 'static,
{
    /// Build a wrapper from an initial provider and a refresh callback.
    ///
    /// The refresh callback is invoked each time the inner provider emits a
    /// 401 response. It must be idempotent and safe to call concurrently.
    /// The callback should return a fully-built provider ready to use;
    /// typically it reads fresh credentials from its auth store and calls
    /// the inner provider's constructor.
    #[must_use]
    pub fn new(inner: P, refresh: F) -> Self {
        let model = inner.model().to_string();
        let provider = inner.provider();
        let thinking = inner.configured_thinking().cloned();
        Self {
            inner: Arc::new(Mutex::new(inner)),
            refresh: Arc::new(refresh),
            model,
            provider,
            thinking,
        }
    }

    async fn snapshot(&self) -> P {
        self.inner.lock().await.clone()
    }

    async fn run_refresh(&self) -> Result<()> {
        let fresh = (self.refresh)().await?;
        *self.inner.lock().await = fresh;
        Ok(())
    }
}

/// Classify a provider error message as a 401 / unauthorized condition.
///
/// Returns `true` when the message looks like the provider rejected the
/// request because of missing, invalid, or expired credentials. Detection is
/// case-insensitive and matches the error-body shapes emitted by the
/// first-party providers in this crate. Hosts that implement their own retry
/// logic on top of [`LlmProvider`] can gate on the same helper.
#[must_use]
pub fn is_unauthorized_error(message: &str) -> bool {
    let lower = message.to_ascii_lowercase();
    lower.contains(" 401")
        || lower.contains("status=401")
        || lower.contains("unauthorized")
        || lower.contains("authentication")
        || lower.contains("token_expired")
        || lower.contains("invalid api key")
        || lower.contains("invalid_api_key")
}

#[async_trait]
impl<P, F, Fut> LlmProvider for RefreshingProvider<P, F>
where
    P: LlmProvider + Clone + 'static,
    F: Fn() -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<P>> + Send + 'static,
{
    async fn chat(&self, request: ChatRequest) -> Result<ChatOutcome> {
        let outcome = self.snapshot().await.chat(request.clone()).await?;
        if let ChatOutcome::InvalidRequest(message) = &outcome
            && is_unauthorized_error(message)
        {
            match self.run_refresh().await {
                Ok(()) => return self.snapshot().await.chat(request).await,
                Err(error) => {
                    log::warn!("RefreshingProvider refresh after 401 failed: {error:#}");
                }
            }
        }
        Ok(outcome)
    }

    fn chat_stream(&self, request: ChatRequest) -> StreamBox<'_> {
        let this = self.clone();
        Box::pin(async_stream::stream! {
            let mut refreshed = false;
            'attempts: loop {
                let provider = this.snapshot().await;
                let mut stream = provider.chat_stream(request.clone());
                let mut saw_output = false;

                while let Some(item) = stream.next().await {
                    match item {
                        Ok(StreamDelta::Error { message, kind })
                            if !saw_output
                                && !refreshed
                                && is_unauthorized_error(&message) =>
                        {
                            match this.run_refresh().await {
                                Ok(()) => {
                                    refreshed = true;
                                    continue 'attempts;
                                }
                                Err(error) => {
                                    log::warn!(
                                        "RefreshingProvider refresh after streaming 401 failed: {error:#}"
                                    );
                                    yield Ok(StreamDelta::Error { message, kind });
                                    return;
                                }
                            }
                        }
                        Ok(delta) => {
                            if matches!(
                                delta,
                                StreamDelta::TextDelta { .. }
                                    | StreamDelta::ThinkingDelta { .. }
                                    | StreamDelta::ToolUseStart { .. }
                                    | StreamDelta::ToolInputDelta { .. }
                                    | StreamDelta::SignatureDelta { .. }
                                    | StreamDelta::RedactedThinking { .. }
                            ) {
                                saw_output = true;
                            }
                            let done = matches!(delta, StreamDelta::Done { .. });
                            yield Ok(delta);
                            if done {
                                return;
                            }
                        }
                        Err(error)
                            if !saw_output
                                && !refreshed
                                && is_unauthorized_error(&error.to_string()) =>
                        {
                            match this.run_refresh().await {
                                Ok(()) => {
                                    refreshed = true;
                                    continue 'attempts;
                                }
                                Err(refresh_error) => {
                                    log::warn!(
                                        "RefreshingProvider refresh after stream failure failed: {refresh_error:#}"
                                    );
                                    yield Err(error);
                                    return;
                                }
                            }
                        }
                        Err(error) => {
                            yield Err(error);
                            return;
                        }
                    }
                }
                return;
            }
        })
    }

    fn model(&self) -> &str {
        &self.model
    }

    fn provider(&self) -> &'static str {
        self.provider
    }

    fn configured_thinking(&self) -> Option<&ThinkingConfig> {
        self.thinking.as_ref()
    }
}

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

    use std::collections::VecDeque;
    use std::sync::Mutex as StdMutex;
    use std::sync::atomic::{AtomicUsize, Ordering};

    use agent_sdk_foundation::llm::{ChatResponse, ContentBlock, StopReason, Usage};
    use anyhow::Context;

    use crate::streaming::StreamErrorKind;

    #[derive(Clone)]
    enum MockStreamItem {
        Ok(StreamDelta),
        Err(String),
    }

    #[derive(Clone)]
    struct MockProvider {
        model: String,
        provider_name: &'static str,
        outcomes: Arc<StdMutex<VecDeque<ChatOutcome>>>,
        stream_batches: Arc<StdMutex<VecDeque<Vec<MockStreamItem>>>>,
        chat_calls: Arc<AtomicUsize>,
        stream_calls: Arc<AtomicUsize>,
    }

    impl MockProvider {
        fn new() -> Self {
            Self {
                model: "mock-model".to_string(),
                provider_name: "mock",
                outcomes: Arc::new(StdMutex::new(VecDeque::new())),
                stream_batches: Arc::new(StdMutex::new(VecDeque::new())),
                chat_calls: Arc::new(AtomicUsize::new(0)),
                stream_calls: Arc::new(AtomicUsize::new(0)),
            }
        }

        fn queue_chat(&self, outcome: ChatOutcome) -> Result<()> {
            self.outcomes
                .lock()
                .ok()
                .context("outcomes lock poisoned")?
                .push_back(outcome);
            Ok(())
        }

        fn queue_stream(&self, batch: Vec<MockStreamItem>) -> Result<()> {
            self.stream_batches
                .lock()
                .ok()
                .context("stream_batches lock poisoned")?
                .push_back(batch);
            Ok(())
        }

        fn chat_call_count(&self) -> usize {
            self.chat_calls.load(Ordering::SeqCst)
        }

        fn stream_call_count(&self) -> usize {
            self.stream_calls.load(Ordering::SeqCst)
        }
    }

    #[async_trait]
    impl LlmProvider for MockProvider {
        async fn chat(&self, _request: ChatRequest) -> Result<ChatOutcome> {
            self.chat_calls.fetch_add(1, Ordering::SeqCst);
            let mut queue = self
                .outcomes
                .lock()
                .ok()
                .context("outcomes lock poisoned")?;
            queue.pop_front().context("MockProvider: no queued outcome")
        }

        fn chat_stream(&self, _request: ChatRequest) -> StreamBox<'_> {
            self.stream_calls.fetch_add(1, Ordering::SeqCst);
            let batch: Vec<MockStreamItem> = self
                .stream_batches
                .lock()
                .ok()
                .and_then(|mut q| q.pop_front())
                .unwrap_or_else(|| vec![MockStreamItem::Err("no queued stream batch".into())]);
            Box::pin(async_stream::stream! {
                for item in batch {
                    match item {
                        MockStreamItem::Ok(delta) => yield Ok(delta),
                        MockStreamItem::Err(msg) => {
                            yield Err(anyhow::anyhow!(msg));
                            return;
                        }
                    }
                }
            })
        }

        fn model(&self) -> &str {
            &self.model
        }

        fn provider(&self) -> &'static str {
            self.provider_name
        }
    }

    fn success_response() -> ChatResponse {
        ChatResponse {
            id: "msg_test".to_string(),
            content: vec![ContentBlock::Text {
                text: "ok".to_string(),
            }],
            model: "mock-model".to_string(),
            stop_reason: Some(StopReason::EndTurn),
            usage: Usage {
                input_tokens: 1,
                output_tokens: 1,
                cached_input_tokens: 0,
                cache_creation_input_tokens: 0,
            },
        }
    }

    fn empty_request() -> ChatRequest {
        ChatRequest {
            system: String::new(),
            messages: Vec::new(),
            tools: None,
            max_tokens: 100,
            max_tokens_explicit: false,
            session_id: None,
            cached_content: None,
            thinking: None,
            tool_choice: None,
            response_format: None,
        }
    }

    type BoxedFut = std::pin::Pin<Box<dyn Future<Output = Result<MockProvider>> + Send>>;
    type RefreshFn = Box<dyn Fn() -> BoxedFut + Send + Sync + 'static>;
    type Wrapped = RefreshingProvider<MockProvider, RefreshFn>;

    fn wrap_success(mock: &MockProvider, counter: &Arc<AtomicUsize>) -> Wrapped {
        let counter = Arc::clone(counter);
        let template = mock.clone();
        let cb: RefreshFn = Box::new(move || {
            counter.fetch_add(1, Ordering::SeqCst);
            let provider = template.clone();
            Box::pin(async move { Ok(provider) })
        });
        RefreshingProvider::new(mock.clone(), cb)
    }

    fn wrap_failure(
        mock: &MockProvider,
        counter: &Arc<AtomicUsize>,
        error: &'static str,
    ) -> Wrapped {
        let counter = Arc::clone(counter);
        let cb: RefreshFn = Box::new(move || {
            counter.fetch_add(1, Ordering::SeqCst);
            Box::pin(async move { Err(anyhow::anyhow!(error)) })
        });
        RefreshingProvider::new(mock.clone(), cb)
    }

    // Test 1
    #[test]
    fn is_unauthorized_error_matches_expected_strings() {
        assert!(is_unauthorized_error("HTTP 401"));
        assert!(is_unauthorized_error("status=401 Unauthorized"));
        assert!(is_unauthorized_error("Invalid API key"));
        assert!(is_unauthorized_error("invalid_api_key"));
        assert!(is_unauthorized_error("token_expired"));
        assert!(is_unauthorized_error("Authentication failed"));
        assert!(is_unauthorized_error("UNAUTHORIZED"));

        assert!(!is_unauthorized_error("rate limited"));
        assert!(!is_unauthorized_error("network error"));
        assert!(!is_unauthorized_error(""));
        assert!(!is_unauthorized_error("internal server error"));
    }

    // Test 2
    #[tokio::test]
    async fn chat_successful_pass_through_does_not_refresh() -> Result<()> {
        let mock = MockProvider::new();
        mock.queue_chat(ChatOutcome::Success(success_response()))?;

        let refresh_count = Arc::new(AtomicUsize::new(0));
        let wrapped = wrap_success(&mock, &refresh_count);

        let outcome = wrapped.chat(empty_request()).await?;
        assert!(matches!(outcome, ChatOutcome::Success(_)));
        assert_eq!(refresh_count.load(Ordering::SeqCst), 0);
        assert_eq!(mock.chat_call_count(), 1);
        Ok(())
    }

    // Test 3
    #[tokio::test]
    async fn chat_401_triggers_refresh_and_retries() -> Result<()> {
        let mock = MockProvider::new();
        mock.queue_chat(ChatOutcome::InvalidRequest("401 Unauthorized".into()))?;
        mock.queue_chat(ChatOutcome::Success(success_response()))?;

        let refresh_count = Arc::new(AtomicUsize::new(0));
        let wrapped = wrap_success(&mock, &refresh_count);

        let outcome = wrapped.chat(empty_request()).await?;
        assert!(matches!(outcome, ChatOutcome::Success(_)));
        assert_eq!(refresh_count.load(Ordering::SeqCst), 1);
        assert_eq!(mock.chat_call_count(), 2);
        Ok(())
    }

    // Test 4
    #[tokio::test]
    async fn chat_surfaces_original_401_when_refresh_fails() -> Result<()> {
        let mock = MockProvider::new();
        mock.queue_chat(ChatOutcome::InvalidRequest(
            "status=401 Unauthorized".into(),
        ))?;

        let refresh_count = Arc::new(AtomicUsize::new(0));
        let wrapped = wrap_failure(&mock, &refresh_count, "refresh callback failed");

        let outcome = wrapped.chat(empty_request()).await?;
        match outcome {
            ChatOutcome::InvalidRequest(msg) => assert!(
                msg.contains("401"),
                "expected original 401 message, got {msg}"
            ),
            other => panic!("expected InvalidRequest, got {other:?}"),
        }
        assert_eq!(refresh_count.load(Ordering::SeqCst), 1);
        assert_eq!(mock.chat_call_count(), 1);
        Ok(())
    }

    async fn drain(mut stream: StreamBox<'_>) -> Vec<Result<StreamDelta>> {
        let mut out = Vec::new();
        while let Some(item) = stream.next().await {
            out.push(item);
        }
        out
    }

    // Test 5
    #[tokio::test]
    async fn chat_stream_successful_pass_through() -> Result<()> {
        let mock = MockProvider::new();
        mock.queue_stream(vec![
            MockStreamItem::Ok(StreamDelta::TextDelta {
                delta: "hi".into(),
                block_index: 0,
            }),
            MockStreamItem::Ok(StreamDelta::Done {
                stop_reason: Some(StopReason::EndTurn),
            }),
        ])?;

        let refresh_count = Arc::new(AtomicUsize::new(0));
        let wrapped = wrap_success(&mock, &refresh_count);

        let deltas = drain(wrapped.chat_stream(empty_request())).await;
        assert_eq!(deltas.len(), 2);
        assert!(matches!(
            deltas[0].as_ref().ok(),
            Some(StreamDelta::TextDelta { delta, .. }) if delta == "hi"
        ));
        assert!(matches!(
            deltas[1].as_ref().ok(),
            Some(StreamDelta::Done { .. })
        ));
        assert_eq!(refresh_count.load(Ordering::SeqCst), 0);
        assert_eq!(mock.stream_call_count(), 1);
        Ok(())
    }

    // Test 6
    #[tokio::test]
    async fn chat_stream_401_before_output_retries() -> Result<()> {
        let mock = MockProvider::new();
        mock.queue_stream(vec![MockStreamItem::Ok(StreamDelta::Error {
            message: "status=401 Unauthorized".into(),
            kind: StreamErrorKind::InvalidRequest,
        })])?;
        mock.queue_stream(vec![
            MockStreamItem::Ok(StreamDelta::TextDelta {
                delta: "retried".into(),
                block_index: 0,
            }),
            MockStreamItem::Ok(StreamDelta::Done {
                stop_reason: Some(StopReason::EndTurn),
            }),
        ])?;

        let refresh_count = Arc::new(AtomicUsize::new(0));
        let wrapped = wrap_success(&mock, &refresh_count);

        let deltas = drain(wrapped.chat_stream(empty_request())).await;
        // Consumer sees only the post-refresh stream.
        assert_eq!(deltas.len(), 2);
        assert!(matches!(
            deltas[0].as_ref().ok(),
            Some(StreamDelta::TextDelta { delta, .. }) if delta == "retried"
        ));
        assert!(matches!(
            deltas[1].as_ref().ok(),
            Some(StreamDelta::Done { .. })
        ));
        assert_eq!(refresh_count.load(Ordering::SeqCst), 1);
        assert_eq!(mock.stream_call_count(), 2);
        Ok(())
    }

    // Test 7
    #[tokio::test]
    async fn chat_stream_401_after_output_does_not_retry() -> Result<()> {
        let mock = MockProvider::new();
        mock.queue_stream(vec![
            MockStreamItem::Ok(StreamDelta::TextDelta {
                delta: "partial".into(),
                block_index: 0,
            }),
            MockStreamItem::Ok(StreamDelta::Error {
                message: "401 Unauthorized".into(),
                kind: StreamErrorKind::InvalidRequest,
            }),
        ])?;

        let refresh_count = Arc::new(AtomicUsize::new(0));
        let wrapped = wrap_success(&mock, &refresh_count);

        let deltas = drain(wrapped.chat_stream(empty_request())).await;
        assert_eq!(deltas.len(), 2);
        assert!(matches!(
            deltas[0].as_ref().ok(),
            Some(StreamDelta::TextDelta { delta, .. }) if delta == "partial"
        ));
        assert!(matches!(
            deltas[1].as_ref().ok(),
            Some(StreamDelta::Error { message, .. }) if message.contains("401")
        ));
        assert_eq!(refresh_count.load(Ordering::SeqCst), 0);
        assert_eq!(mock.stream_call_count(), 1);
        Ok(())
    }

    // Test 8
    #[tokio::test]
    async fn chat_stream_only_one_retry_per_call() -> Result<()> {
        let mock = MockProvider::new();
        mock.queue_stream(vec![MockStreamItem::Ok(StreamDelta::Error {
            message: "status=401 Unauthorized".into(),
            kind: StreamErrorKind::InvalidRequest,
        })])?;
        mock.queue_stream(vec![MockStreamItem::Ok(StreamDelta::Error {
            message: "still 401 Unauthorized".into(),
            kind: StreamErrorKind::InvalidRequest,
        })])?;

        let refresh_count = Arc::new(AtomicUsize::new(0));
        let wrapped = wrap_success(&mock, &refresh_count);

        let deltas = drain(wrapped.chat_stream(empty_request())).await;
        assert_eq!(deltas.len(), 1);
        assert!(matches!(
            deltas[0].as_ref().ok(),
            Some(StreamDelta::Error { message, .. }) if message == "still 401 Unauthorized"
        ));
        assert_eq!(refresh_count.load(Ordering::SeqCst), 1);
        assert_eq!(mock.stream_call_count(), 2);
        Ok(())
    }

    // Custom deterministic mock for the concurrent scenario: the first
    // two chat() calls both wait on a barrier so both concurrent tasks
    // observe a 401 on their initial call, then all subsequent calls
    // return Success. This avoids flakiness from scheduling order in a
    // shared FIFO queue.
    #[derive(Clone)]
    struct ConcurrentMock {
        model: String,
        provider_name: &'static str,
        total_calls: Arc<AtomicUsize>,
        initial_barrier: Arc<tokio::sync::Barrier>,
    }

    type CMFut = std::pin::Pin<Box<dyn Future<Output = Result<ConcurrentMock>> + Send>>;
    type CMRefresh = Box<dyn Fn() -> CMFut + Send + Sync + 'static>;

    #[async_trait]
    impl LlmProvider for ConcurrentMock {
        async fn chat(&self, _request: ChatRequest) -> Result<ChatOutcome> {
            let call_index = self.total_calls.fetch_add(1, Ordering::SeqCst);
            if call_index < 2 {
                self.initial_barrier.wait().await;
                Ok(ChatOutcome::InvalidRequest("401 Unauthorized".into()))
            } else {
                Ok(ChatOutcome::Success(success_response()))
            }
        }

        fn chat_stream(&self, _request: ChatRequest) -> StreamBox<'_> {
            Box::pin(async_stream::stream! {
                yield Err(anyhow::anyhow!("chat_stream not used in this test"));
            })
        }

        fn model(&self) -> &str {
            &self.model
        }

        fn provider(&self) -> &'static str {
            self.provider_name
        }
    }

    // Test 9
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn chat_concurrent_callers_share_refresh() -> Result<()> {
        let mock = ConcurrentMock {
            model: "mock-model".to_string(),
            provider_name: "mock",
            total_calls: Arc::new(AtomicUsize::new(0)),
            initial_barrier: Arc::new(tokio::sync::Barrier::new(2)),
        };
        let call_count = Arc::clone(&mock.total_calls);
        let refresh_count = Arc::new(AtomicUsize::new(0));
        let refresh_counter = Arc::clone(&refresh_count);
        let template = mock.clone();

        let cb: CMRefresh = Box::new(move || {
            refresh_counter.fetch_add(1, Ordering::SeqCst);
            let provider = template.clone();
            Box::pin(async move { Ok(provider) })
        });
        let wrapped = RefreshingProvider::new(mock, cb);

        let a = wrapped.clone();
        let b = wrapped.clone();
        let task_a = tokio::spawn(async move { a.chat(empty_request()).await });
        let task_b = tokio::spawn(async move { b.chat(empty_request()).await });

        let outcome_a = task_a.await.context("task_a join")??;
        let outcome_b = task_b.await.context("task_b join")??;

        assert!(matches!(outcome_a, ChatOutcome::Success(_)));
        assert!(matches!(outcome_b, ChatOutcome::Success(_)));
        assert_eq!(call_count.load(Ordering::SeqCst), 4);
        let refreshes = refresh_count.load(Ordering::SeqCst);
        assert!(
            refreshes <= 2,
            "expected at most 2 refresh calls (one per caller), got {refreshes}"
        );
        Ok(())
    }
}