adk-model 3.0.0

LLM model integrations for Rust Agent Development Kit (ADK-Rust) (Gemini, OpenAI, Claude, DeepSeek, etc.)
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
//! A provider-neutral fallback chain: an ordered list of models, each tried when the one
//! before it fails.
//!
//! [`FallbackLlm`] is an [`Llm`], so an agent takes it wherever it takes any other model. It
//! sends the request to its first model and moves to the next one only on a configured
//! [`FallbackTrigger`]; the first model that answers keeps the turn.
//!
//! | Step | What happens |
//! |------|--------------|
//! | Each attempt | The request goes out with `model` set to that model's name |
//! | The attempt fails before its first response | On a configured trigger the next model is tried; otherwise the error is returned |
//! | The attempt's first response is a refusal | With [`FallbackTrigger::Refusal`] the next model is tried |
//! | The attempt has yielded a response | The turn belongs to that model: a later error reaches the caller |
//! | Every model failed | The last model's error, or its refusal, is returned |
//!
//! Under a [`ModelCallScope`] (an agent's run) every attempt is its own metered call: it
//! counts against the run budget as a model call and is reserved and committed in the
//! spend ledger under the provider and model that served it, a refused attempt included.

use std::sync::Arc;

use adk_core::{
    AdkError, ErrorCategory, ErrorComponent, FinishReason, Llm, LlmRequest, LlmResponse,
    LlmResponseStream, ModelCallScope, NativeWebSearch, Result, SchemaAdapter,
    intra_compaction::is_context_limit_error,
};
use async_trait::async_trait;
use futures::StreamExt;
use serde_json::{Value, json};

/// The `provider_metadata` key a [`FallbackLlm`] response carries when a model after the
/// first one answered.
///
/// Its value names the model that answered, its position in the chain (1 is the first
/// fallback), and each model skipped before it with the trigger and error code that moved the
/// chain on.
///
/// # Example
///
/// ```rust
/// use adk_model::fallback::FALLBACK_METADATA_KEY;
///
/// assert_eq!(FALLBACK_METADATA_KEY, "fallback");
/// ```
pub const FALLBACK_METADATA_KEY: &str = "fallback";

/// A condition that moves a [`FallbackLlm`] on to its next model.
///
/// Only a failure the model reports counts: an error from the run's budget, spend ledger or
/// kill switch is never a reason to try another model, and is returned as is.
///
/// | Trigger | Default | Matches |
/// |---------|---------|---------|
/// | [`Retryable`](Self::Retryable) | on | Rate limiting, timeouts, server errors and provider outages ([`AdkError::is_retryable`]), once the model's own retries are used up |
/// | [`ModelNotFound`](Self::ModelNotFound) | on | The provider does not serve the model: an unknown, retired or unavailable model id ([`ErrorCategory::NotFound`]) |
/// | [`QuotaExhausted`](Self::QuotaExhausted) | on | The provider account's quota, credit balance or spend limit is used up ([`ErrorCategory::ResourceExhausted`]) |
/// | [`Refusal`](Self::Refusal) | off | The model declined the request ([`FinishReason::Refusal`]) before any output |
/// | [`ContextOverflow`](Self::ContextOverflow) | off | The request is longer than the model's context window |
/// | [`AuthFailure`](Self::AuthFailure) | off | The provider refused the credentials or the permission ([`ErrorCategory::Unauthorized`], [`ErrorCategory::Forbidden`]) |
///
/// # Example
///
/// ```rust
/// use adk_model::fallback::FallbackTrigger;
///
/// assert!(FallbackTrigger::DEFAULT.contains(&FallbackTrigger::Retryable));
/// assert!(!FallbackTrigger::DEFAULT.contains(&FallbackTrigger::Refusal));
/// assert_eq!(FallbackTrigger::ModelNotFound.as_str(), "model_not_found");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FallbackTrigger {
    /// A transient failure the model's own retries did not clear.
    Retryable,
    /// The provider does not serve the requested model.
    ModelNotFound,
    /// The provider account's quota or credit is used up.
    QuotaExhausted,
    /// The model declined the request before producing any output.
    Refusal,
    /// The request does not fit the model's context window.
    ContextOverflow,
    /// The provider refused the credentials or the permission.
    AuthFailure,
}

impl FallbackTrigger {
    /// The triggers a [`FallbackLlm`] uses unless configured otherwise: the failures another
    /// model can serve without a change to the request or to its permissions.
    pub const DEFAULT: [FallbackTrigger; 3] = [
        FallbackTrigger::Retryable,
        FallbackTrigger::ModelNotFound,
        FallbackTrigger::QuotaExhausted,
    ];

    /// The trigger's name in tracing and in [`FALLBACK_METADATA_KEY`] metadata.
    pub fn as_str(self) -> &'static str {
        match self {
            FallbackTrigger::Retryable => "retryable",
            FallbackTrigger::ModelNotFound => "model_not_found",
            FallbackTrigger::QuotaExhausted => "quota_exhausted",
            FallbackTrigger::Refusal => "refusal",
            FallbackTrigger::ContextOverflow => "context_overflow",
            FallbackTrigger::AuthFailure => "auth_failure",
        }
    }

    /// The trigger `error` matches, if any.
    ///
    /// Only an error from the model ([`ErrorComponent::Model`]) matches, so a refusal by the
    /// run's budget, spend ledger or kill switch never moves a chain on.
    ///
    /// # Example
    ///
    /// ```rust
    /// use adk_core::{AdkError, ErrorCategory, ErrorComponent};
    /// use adk_model::fallback::FallbackTrigger;
    ///
    /// let overloaded = AdkError::new(
    ///     ErrorComponent::Model,
    ///     ErrorCategory::Unavailable,
    ///     "model.anthropic.api_error",
    ///     "overloaded",
    /// );
    /// assert_eq!(FallbackTrigger::for_error(&overloaded), Some(FallbackTrigger::Retryable));
    ///
    /// let budget = AdkError::new(
    ///     ErrorComponent::Agent,
    ///     ErrorCategory::ResourceExhausted,
    ///     "budget.model_calls_exceeded",
    ///     "the run's model-call budget is spent",
    /// );
    /// assert_eq!(FallbackTrigger::for_error(&budget), None);
    /// ```
    pub fn for_error(error: &AdkError) -> Option<Self> {
        if error.component != ErrorComponent::Model {
            return None;
        }
        if is_context_limit_error(error) {
            return Some(FallbackTrigger::ContextOverflow);
        }
        match error.category {
            ErrorCategory::RateLimited | ErrorCategory::Timeout | ErrorCategory::Unavailable => {
                Some(FallbackTrigger::Retryable)
            }
            ErrorCategory::NotFound => Some(FallbackTrigger::ModelNotFound),
            ErrorCategory::ResourceExhausted => Some(FallbackTrigger::QuotaExhausted),
            ErrorCategory::Unauthorized | ErrorCategory::Forbidden => {
                Some(FallbackTrigger::AuthFailure)
            }
            ErrorCategory::InvalidInput
            | ErrorCategory::Cancelled
            | ErrorCategory::Internal
            | ErrorCategory::Unsupported
            | ErrorCategory::Conflict => error.is_retryable().then_some(FallbackTrigger::Retryable),
        }
    }
}

impl std::fmt::Display for FallbackTrigger {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// An ordered chain of models: each request goes to the first model, and to the next one
/// whenever the one before it fails on a configured [`FallbackTrigger`].
///
/// The counterpart of Google ADK's `FallbackModel`, for any mix of providers. The first
/// model that yields a response keeps the turn, so a streamed turn never splices two
/// models' output: a failure after the first response reaches the caller. When every model
/// fails, the last model's error (or refusal) is returned, and `LlmAgent`'s
/// `on_model_error` hooks see it as they see any model error.
///
/// | Signal | Where |
/// |--------|-------|
/// | Which model answered | [`LlmResponse::model`] and [`LlmResponse::provider`], set by the provider that answered |
/// | That a fallback happened | `provider_metadata["fallback"]` ([`FALLBACK_METADATA_KEY`]) on every complete response, and a `warn` tracing event per skipped model |
/// | What each attempt cost | Under a run, one spend-ledger entry and one budget model call per attempt, under the attempt's own provider |
///
/// For a mix of models, the chain reports:
///
/// - [`name`](Llm::name): the first model's name.
/// - [`context_window`](Llm::context_window): the smallest window among the models that
///   report one, so auto-compaction keeps a request within every model it may reach; with
///   [`FallbackTrigger::ContextOverflow`], the largest, since the chain is then built to move
///   a long request to a larger model.
/// - [`schema_adapter`](Llm::schema_adapter): the first model's. Tool schemas are normalized
///   once, before any attempt, so keep the chain's providers schema-compatible or put the
///   strictest one first.
/// - [`max_cost_micro_usd`](Llm::max_cost_micro_usd): the largest bound in the chain (a
///   failed call bills nothing), or the sum of the bounds with [`FallbackTrigger::Refusal`],
///   since a refused attempt is billed; `None` when a model has no bound. A run meters each
///   attempt on its own bound, so this applies only where the chain is metered as one call.
/// - [`native_web_search`](Llm::native_web_search): the search every model serves, or `None`
///   when they differ, since a provider's built-in search tool breaks another provider's
///   request.
///
/// A model's own retries ([`RetryConfig`](crate::RetryConfig)) run first: the chain sees a
/// failure only once they are used up, and it tries each model once.
///
/// # Example
///
/// ```rust
/// use std::sync::Arc;
/// use adk_core::{AdkError, Content, ErrorCategory, ErrorComponent, Llm, LlmRequest};
/// use adk_model::fallback::{FallbackLlm, FallbackTrigger};
/// use adk_model::{ScriptedLlm, ScriptedResponse};
/// use futures::StreamExt;
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() -> Result<(), AdkError> {
/// let overloaded = AdkError::new(
///     ErrorComponent::Model,
///     ErrorCategory::Unavailable,
///     "model.anthropic.api_error",
///     "overloaded",
/// );
/// let primary = Arc::new(ScriptedLlm::new("claude-sonnet-5-5", [ScriptedResponse::error(overloaded)]));
/// let backup = Arc::new(ScriptedLlm::new("gemini-3.7-flash", [ScriptedResponse::text("Mars.")]));
///
/// let model = FallbackLlm::builder(primary)
///     .fallback(backup.clone())
///     .fall_back_on(FallbackTrigger::Refusal)
///     .build();
///
/// let request = LlmRequest::new(model.name(), vec![Content::new("user").with_text("Name a planet.")]);
/// let response = model.generate_content(request, false).await?.next().await.unwrap()?;
/// assert_eq!(response.content.unwrap().parts[0].text(), Some("Mars."));
/// assert_eq!(response.provider_metadata.unwrap()["fallback"]["model"], "gemini-3.7-flash");
/// assert_eq!(backup.last_request().unwrap().model, "gemini-3.7-flash");
/// # Ok(())
/// # }
/// ```
pub struct FallbackLlm {
    models: Vec<Arc<dyn Llm>>,
    triggers: Vec<FallbackTrigger>,
}

impl std::fmt::Debug for FallbackLlm {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FallbackLlm")
            .field("models", &self.models.iter().map(|model| model.name()).collect::<Vec<_>>())
            .field("triggers", &self.triggers)
            .finish()
    }
}

/// Builds a [`FallbackLlm`]: the first model, the models to fall back to in order, and the
/// triggers that move the chain on.
///
/// # Example
///
/// ```rust
/// use std::sync::Arc;
/// use adk_core::Llm;
/// use adk_model::fallback::{FallbackLlm, FallbackTrigger};
/// use adk_model::{ScriptedLlm, ScriptedResponse};
///
/// let primary = Arc::new(ScriptedLlm::new("claude-sonnet-5-5", Vec::<ScriptedResponse>::new()));
/// let backup = Arc::new(ScriptedLlm::new("claude-haiku-5-5", Vec::<ScriptedResponse>::new()));
/// let model = FallbackLlm::builder(primary)
///     .fallback(backup)
///     .never_fall_back_on(FallbackTrigger::ModelNotFound)
///     .build();
/// assert_eq!(model.name(), "claude-sonnet-5-5");
/// assert_eq!(model.triggers(), [FallbackTrigger::Retryable, FallbackTrigger::QuotaExhausted]);
/// ```
pub struct FallbackLlmBuilder {
    models: Vec<Arc<dyn Llm>>,
    triggers: Vec<FallbackTrigger>,
}

impl FallbackLlm {
    /// Starts a chain whose first model is `primary`, with the [default
    /// triggers](FallbackTrigger::DEFAULT).
    pub fn builder(primary: Arc<dyn Llm>) -> FallbackLlmBuilder {
        FallbackLlmBuilder { models: vec![primary], triggers: FallbackTrigger::DEFAULT.to_vec() }
    }

    /// The models of the chain, first model first.
    pub fn models(&self) -> &[Arc<dyn Llm>] {
        &self.models
    }

    /// The triggers that move the chain to its next model.
    pub fn triggers(&self) -> &[FallbackTrigger] {
        &self.triggers
    }

    fn falls_back_on(&self, trigger: FallbackTrigger) -> bool {
        self.triggers.contains(&trigger)
    }

    /// Tries each model in turn, metering every attempt against `scope` when there is one.
    async fn generate(
        &self,
        scope: Option<&ModelCallScope>,
        request: LlmRequest,
        stream: bool,
    ) -> Result<LlmResponseStream> {
        let mut skipped: Vec<Value> = Vec::new();
        let mut models = self.models.iter().enumerate().peekable();
        while let Some((position, model)) = models.next() {
            let has_next = models.peek().is_some();
            let mut attempt = request.clone();
            attempt.model = model.name().to_string();
            let started = match scope {
                Some(scope) => scope.generate(model.as_ref(), attempt, stream).await,
                None => model.generate_content(attempt, stream).await,
            };
            let mut responses = match started {
                Ok(responses) => responses,
                Err(error) => {
                    let trigger =
                        FallbackTrigger::for_error(&error).filter(|t| self.falls_back_on(*t));
                    match trigger {
                        Some(trigger) if has_next => {
                            skipped.push(self.skip(model.as_ref(), trigger, Some(error.code)));
                            continue;
                        }
                        _ => return Err(error),
                    }
                }
            };
            let first = responses.next().await;
            let trigger = match &first {
                Some(Err(error)) => {
                    FallbackTrigger::for_error(error).filter(|t| self.falls_back_on(*t))
                }
                Some(Ok(response)) => (response.finish_reason == Some(FinishReason::Refusal)
                    && self.falls_back_on(FallbackTrigger::Refusal))
                .then_some(FallbackTrigger::Refusal),
                None => None,
            };
            if let Some(trigger) = trigger
                && has_next
            {
                let code = match &first {
                    Some(Err(error)) => Some(error.code),
                    _ => None,
                };
                // Read to the end so the attempt's spend settles — a refusal is billed —
                // before the next model reserves.
                while responses.next().await.is_some() {}
                skipped.push(self.skip(model.as_ref(), trigger, code));
                continue;
            }
            let responses: LlmResponseStream = match first {
                Some(first) => Box::pin(futures::stream::iter([first]).chain(responses)),
                None => responses,
            };
            if skipped.is_empty() {
                return Ok(responses);
            }
            tracing::info!(
                fallback.model = %model.name(),
                fallback.position = position,
                "fallback model answered"
            );
            let marker = json!({
                "model": model.name(),
                "position": position,
                "skipped": skipped,
            });
            return Ok(Box::pin(responses.map(move |item| {
                item.map(|mut response| {
                    if !response.partial {
                        mark(&mut response, &marker);
                    }
                    response
                })
            })));
        }
        Err(AdkError::new(
            ErrorComponent::Model,
            ErrorCategory::InvalidInput,
            "model.fallback.empty",
            "the fallback chain has no model; build it with FallbackLlm::builder",
        ))
    }

    /// Records one skipped model: a `warn` event and its entry in the response metadata.
    fn skip(&self, model: &dyn Llm, trigger: FallbackTrigger, code: Option<&str>) -> Value {
        tracing::warn!(
            fallback.from = %model.name(),
            fallback.reason = trigger.as_str(),
            error.code = code.unwrap_or(""),
            "model failed; trying the next model in the fallback chain"
        );
        let mut entry = json!({ "model": model.name(), "reason": trigger.as_str() });
        if let Some(code) = code {
            entry["error_code"] = json!(code);
        }
        entry
    }
}

/// Adds the fallback marker to `response`'s provider metadata, keeping what the provider set.
fn mark(response: &mut LlmResponse, marker: &Value) {
    match response.provider_metadata.get_or_insert_with(|| json!({})) {
        Value::Object(fields) => {
            fields.insert(FALLBACK_METADATA_KEY.to_string(), marker.clone());
        }
        other => {
            let original = std::mem::take(other);
            *other = json!({ "provider": original, FALLBACK_METADATA_KEY: marker.clone() });
        }
    }
}

impl FallbackLlmBuilder {
    /// Appends `model` to the chain, after the models already in it.
    pub fn fallback(mut self, model: Arc<dyn Llm>) -> Self {
        self.models.push(model);
        self
    }

    /// Moves the chain on when `trigger` matches.
    pub fn fall_back_on(mut self, trigger: FallbackTrigger) -> Self {
        if !self.triggers.contains(&trigger) {
            self.triggers.push(trigger);
        }
        self
    }

    /// Returns the error, or the refusal, when `trigger` matches, instead of moving on.
    pub fn never_fall_back_on(mut self, trigger: FallbackTrigger) -> Self {
        self.triggers.retain(|existing| *existing != trigger);
        self
    }

    /// Replaces the triggers with `triggers`.
    pub fn triggers(mut self, triggers: impl IntoIterator<Item = FallbackTrigger>) -> Self {
        self.triggers.clear();
        for trigger in triggers {
            self = self.fall_back_on(trigger);
        }
        self
    }

    /// Builds the chain.
    pub fn build(self) -> FallbackLlm {
        FallbackLlm { models: self.models, triggers: self.triggers }
    }
}

#[async_trait]
impl Llm for FallbackLlm {
    fn name(&self) -> &str {
        self.models.first().map_or("fallback", |model| model.name())
    }

    async fn generate_content(&self, req: LlmRequest, stream: bool) -> Result<LlmResponseStream> {
        self.generate(None, req, stream).await
    }

    async fn generate_content_metered(
        &self,
        scope: &ModelCallScope,
        req: LlmRequest,
        stream: bool,
    ) -> Result<LlmResponseStream> {
        self.generate(Some(scope), req, stream).await
    }

    fn schema_adapter(&self) -> &dyn SchemaAdapter {
        match self.models.first() {
            Some(model) => model.schema_adapter(),
            None => &adk_core::GenericSchemaAdapter,
        }
    }

    fn uses_interactions_api(&self) -> bool {
        self.models.iter().any(|model| model.uses_interactions_api())
    }

    fn max_cost_micro_usd(&self, request: &LlmRequest) -> Option<u64> {
        let bounds = self
            .models
            .iter()
            .map(|model| {
                let mut attempt = request.clone();
                attempt.model = model.name().to_string();
                model.max_cost_micro_usd(&attempt)
            })
            .collect::<Option<Vec<u64>>>()?;
        if self.falls_back_on(FallbackTrigger::Refusal) {
            Some(bounds.into_iter().fold(0u64, u64::saturating_add))
        } else {
            bounds.into_iter().max()
        }
    }

    fn context_window(&self) -> Option<u64> {
        let windows = self.models.iter().filter_map(|model| model.context_window());
        if self.falls_back_on(FallbackTrigger::ContextOverflow) {
            windows.max()
        } else {
            windows.min()
        }
    }

    fn native_web_search(&self) -> Option<NativeWebSearch> {
        let mut searches = self.models.iter().map(|model| model.native_web_search());
        let first = searches.next()??;
        searches.all(|search| search == Some(first)).then_some(first)
    }

    // A compaction request may fall back to any model in the chain, so it is native only when
    // every model compacts the same way.
    fn native_compaction(&self) -> Option<adk_core::NativeCompaction> {
        let mut compactions = self.models.iter().map(|model| model.native_compaction());
        let first = compactions.next()??;
        compactions.all(|compaction| compaction == Some(first)).then_some(first)
    }

    // A call may fall back to any model in the chain, so its data goes as far as the least
    // local of them.
    fn data_locality(&self) -> adk_core::DataLocality {
        adk_core::DataLocality::least_local(self.models.iter().map(|model| model.data_locality()))
    }

    // Every model's destinations, since a call may reach any of them; none when any model's
    // destination is unknown.
    fn data_destinations(&self) -> Vec<adk_core::DataDestination> {
        let mut destinations = Vec::new();
        for model in &self.models {
            let theirs = model.data_destinations();
            if theirs.is_empty() {
                return Vec::new();
            }
            for destination in theirs {
                if !destinations.contains(&destination) {
                    destinations.push(destination);
                }
            }
        }
        destinations
    }

    /// Prewarms the first model, which serves the next call; the fallbacks are prewarmed too,
    /// and their failures only logged.
    async fn prewarm(&self) -> Result<()> {
        let mut models = self.models.iter();
        let Some(first) = models.next() else { return Ok(()) };
        for model in models {
            if let Err(error) = model.prewarm().await {
                tracing::debug!(model = model.name(), error = %error, "a fallback model did not prewarm");
            }
        }
        first.prewarm().await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{ScriptedLlm, ScriptedResponse};
    use adk_core::{
        BudgetTracker, Content, GovernanceControl, InMemorySpendLedger, NativeWebSearchTool,
        RunBudget, SpendKey, SpendLedger, SpendLimits, SpendPeriod, UsageMetadata,
    };

    fn error(category: ErrorCategory, code: &'static str) -> AdkError {
        AdkError::new(ErrorComponent::Model, category, code, format!("{code} failure"))
    }

    fn unavailable() -> AdkError {
        error(ErrorCategory::Unavailable, "model.test.unavailable")
    }

    /// A complete answer `text` from `model` at `provider`, reporting `cost` in USD.
    fn answer(text: &str, model: &str, provider: &str, cost: f64) -> ScriptedResponse {
        let mut response = LlmResponse::new(Content::new("model").with_text(text));
        response.model = Some(model.to_string());
        response.provider = Some(provider.to_string());
        response.usage_metadata = Some(UsageMetadata {
            prompt_token_count: 10,
            candidates_token_count: 5,
            total_token_count: 15,
            cost: Some(cost),
            ..Default::default()
        });
        ScriptedResponse::response(response)
    }

    /// A refusal before any output, as Anthropic's classifiers return one.
    fn refusal(model: &str, provider: &str, cost: f64) -> ScriptedResponse {
        let response = LlmResponse {
            finish_reason: Some(FinishReason::Refusal),
            turn_complete: true,
            model: Some(model.to_string()),
            provider: Some(provider.to_string()),
            usage_metadata: Some(UsageMetadata {
                prompt_token_count: 10,
                total_token_count: 10,
                cost: Some(cost),
                ..Default::default()
            }),
            ..Default::default()
        };
        ScriptedResponse::response(response)
    }

    fn partial(text: &str) -> LlmResponse {
        let mut response = LlmResponse::new(Content::new("model").with_text(text));
        response.partial = true;
        response.turn_complete = false;
        response.finish_reason = None;
        response
    }

    fn scripted<R: Into<ScriptedResponse>>(
        name: &str,
        script: impl IntoIterator<Item = R>,
    ) -> Arc<ScriptedLlm> {
        Arc::new(ScriptedLlm::new(name, script))
    }

    fn request() -> LlmRequest {
        LlmRequest::new("claude-sonnet-5-5", vec![Content::new("user").with_text("hello")])
    }

    async fn collect(model: &FallbackLlm, stream: bool) -> Vec<Result<LlmResponse>> {
        model.generate_content(request(), stream).await.unwrap().collect().await
    }

    fn text(response: &LlmResponse) -> String {
        response.content.iter().flat_map(|c| c.parts.iter()).filter_map(|p| p.text()).collect()
    }

    #[tokio::test]
    async fn a_retryable_error_moves_to_the_next_model() {
        let primary = scripted("claude-sonnet-5-5", [ScriptedResponse::error(unavailable())]);
        let backup =
            scripted("gemini-3.7-flash", [answer("hi", "gemini-3.7-flash", "gemini", 0.0)]);
        let model = FallbackLlm::builder(primary.clone()).fallback(backup.clone()).build();

        let responses = collect(&model, false).await;
        let response = responses[0].as_ref().unwrap();
        assert_eq!(text(response), "hi");
        assert_eq!(response.model.as_deref(), Some("gemini-3.7-flash"));
        assert_eq!(
            response.provider_metadata.as_ref().unwrap()[FALLBACK_METADATA_KEY],
            json!({
                "model": "gemini-3.7-flash",
                "position": 1,
                "skipped": [{
                    "model": "claude-sonnet-5-5",
                    "reason": "retryable",
                    "error_code": "model.test.unavailable",
                }],
            })
        );
        assert_eq!(primary.last_request().unwrap().model, "claude-sonnet-5-5");
        assert_eq!(backup.last_request().unwrap().model, "gemini-3.7-flash");
    }

    #[tokio::test]
    async fn rate_limits_timeouts_unknown_models_and_spent_quotas_fall_back_by_default() {
        for (category, reason) in [
            (ErrorCategory::RateLimited, "retryable"),
            (ErrorCategory::Timeout, "retryable"),
            (ErrorCategory::NotFound, "model_not_found"),
            (ErrorCategory::ResourceExhausted, "quota_exhausted"),
        ] {
            let primary =
                scripted("claude-sonnet-5-5", [ScriptedResponse::error(error(category, "x"))]);
            let backup = scripted("claude-haiku-5-5", [ScriptedResponse::text("ok")]);
            let model = FallbackLlm::builder(primary).fallback(backup).build();
            let responses = collect(&model, false).await;
            let marker = &responses[0].as_ref().unwrap().provider_metadata.as_ref().unwrap()
                [FALLBACK_METADATA_KEY];
            assert_eq!(marker["skipped"][0]["reason"], reason, "{category:?}");
        }
    }

    #[tokio::test]
    async fn an_error_at_the_start_of_the_stream_falls_back() {
        let primary = scripted(
            "claude-sonnet-5-5",
            [ScriptedResponse::stream(Vec::<LlmResponse>::new()).then_error(unavailable())],
        );
        let backup = scripted("claude-haiku-5-5", [ScriptedResponse::text("ok")]);
        let model = FallbackLlm::builder(primary).fallback(backup).build();

        let responses = collect(&model, true).await;
        assert_eq!(responses.len(), 1);
        assert_eq!(text(responses[0].as_ref().unwrap()), "ok");
    }

    #[tokio::test]
    async fn auth_and_permission_errors_are_returned_unless_configured() {
        for category in [ErrorCategory::Unauthorized, ErrorCategory::Forbidden] {
            let primary =
                scripted("claude-sonnet-5-5", [ScriptedResponse::error(error(category, "auth"))]);
            let backup = scripted("claude-haiku-5-5", [ScriptedResponse::text("ok")]);
            let model = FallbackLlm::builder(primary).fallback(backup.clone()).build();
            let failed = model.generate_content(request(), false).await.err().unwrap();
            assert_eq!(failed.category, category);
            assert!(backup.requests().is_empty());

            let primary =
                scripted("claude-sonnet-5-5", [ScriptedResponse::error(error(category, "auth"))]);
            let backup = scripted("claude-haiku-5-5", [ScriptedResponse::text("ok")]);
            let model = FallbackLlm::builder(primary)
                .fallback(backup)
                .fall_back_on(FallbackTrigger::AuthFailure)
                .build();
            assert_eq!(text(collect(&model, false).await[0].as_ref().unwrap()), "ok");
        }
    }

    #[tokio::test]
    async fn a_refusal_falls_back_only_when_configured() {
        let primary =
            scripted("claude-sonnet-5-5", [refusal("claude-sonnet-5-5", "anthropic", 0.0)]);
        let backup = scripted("claude-opus-5-5", [ScriptedResponse::text("ok")]);
        let model = FallbackLlm::builder(primary).fallback(backup.clone()).build();
        let responses = collect(&model, false).await;
        assert_eq!(responses[0].as_ref().unwrap().finish_reason, Some(FinishReason::Refusal));
        assert!(backup.requests().is_empty());

        let primary =
            scripted("claude-sonnet-5-5", [refusal("claude-sonnet-5-5", "anthropic", 0.0)]);
        let backup = scripted("claude-opus-5-5", [ScriptedResponse::text("ok")]);
        let model = FallbackLlm::builder(primary)
            .fallback(backup)
            .fall_back_on(FallbackTrigger::Refusal)
            .build();
        let responses = collect(&model, false).await;
        let response = responses[0].as_ref().unwrap();
        assert_eq!(text(response), "ok");
        assert_eq!(
            response.provider_metadata.as_ref().unwrap()[FALLBACK_METADATA_KEY]["skipped"][0],
            json!({ "model": "claude-sonnet-5-5", "reason": "refusal" })
        );
    }

    #[tokio::test]
    async fn context_overflow_falls_back_only_when_configured() {
        let invalid = || {
            ScriptedResponse::error(error(ErrorCategory::InvalidInput, "model.anthropic.api_error"))
        };
        let overflow = || {
            AdkError::new(
                ErrorComponent::Model,
                ErrorCategory::InvalidInput,
                "model.anthropic.api_error",
                "400: prompt is too long: 1048577 tokens > 1000000 maximum",
            )
        };
        assert_eq!(FallbackTrigger::for_error(&overflow()), Some(FallbackTrigger::ContextOverflow));
        // An invalid request that is not about length never falls back.
        let primary = scripted("claude-haiku-4-5", [invalid()]);
        let backup = scripted("claude-sonnet-5-5", [ScriptedResponse::text("ok")]);
        let model = FallbackLlm::builder(primary)
            .fallback(backup.clone())
            .fall_back_on(FallbackTrigger::ContextOverflow)
            .build();
        assert!(model.generate_content(request(), false).await.is_err());
        assert!(backup.requests().is_empty());

        let primary = scripted("claude-haiku-4-5", [ScriptedResponse::error(overflow())]);
        let backup = scripted("claude-sonnet-5-5", [ScriptedResponse::text("ok")]);
        let model = FallbackLlm::builder(primary).fallback(backup.clone()).build();
        assert!(model.generate_content(request(), false).await.is_err());
        assert!(backup.requests().is_empty());

        let primary = scripted("claude-haiku-4-5", [ScriptedResponse::error(overflow())]);
        let backup = scripted("claude-sonnet-5-5", [ScriptedResponse::text("ok")]);
        let model = FallbackLlm::builder(primary)
            .fallback(backup)
            .fall_back_on(FallbackTrigger::ContextOverflow)
            .build();
        assert_eq!(text(collect(&model, false).await[0].as_ref().unwrap()), "ok");
    }

    #[tokio::test]
    async fn after_the_first_chunk_an_error_reaches_the_caller() {
        let primary = scripted(
            "claude-sonnet-5-5",
            [ScriptedResponse::stream([partial("Hel")]).then_error(unavailable())],
        );
        let backup = scripted("claude-haiku-5-5", [ScriptedResponse::text("ok")]);
        let model = FallbackLlm::builder(primary).fallback(backup.clone()).build();

        let responses = collect(&model, true).await;
        assert_eq!(text(responses[0].as_ref().unwrap()), "Hel");
        assert_eq!(responses[1].as_ref().unwrap_err().code, "model.test.unavailable");
        assert!(backup.requests().is_empty());
    }

    #[tokio::test]
    async fn a_refusal_after_streamed_output_reaches_the_caller() {
        let refused = LlmResponse {
            finish_reason: Some(FinishReason::Refusal),
            turn_complete: true,
            ..Default::default()
        };
        let primary =
            scripted("claude-sonnet-5-5", [ScriptedResponse::stream([partial("Sure"), refused])]);
        let backup = scripted("claude-opus-5-5", [ScriptedResponse::text("ok")]);
        let model = FallbackLlm::builder(primary)
            .fallback(backup.clone())
            .fall_back_on(FallbackTrigger::Refusal)
            .build();

        let responses = collect(&model, true).await;
        assert_eq!(responses.len(), 2);
        assert_eq!(responses[1].as_ref().unwrap().finish_reason, Some(FinishReason::Refusal));
        assert!(backup.requests().is_empty());
    }

    #[tokio::test]
    async fn when_every_model_fails_the_last_error_is_returned() {
        let primary = scripted("claude-sonnet-5-5", [ScriptedResponse::error(unavailable())]);
        let backup = scripted(
            "claude-haiku-5-5",
            [ScriptedResponse::error(error(ErrorCategory::RateLimited, "model.test.limited"))],
        );
        let model = FallbackLlm::builder(primary).fallback(backup).build();
        let failed = model.generate_content(request(), false).await.err().unwrap();
        assert_eq!(failed.code, "model.test.limited");

        // The last model's refusal is its answer.
        let primary = scripted("claude-sonnet-5-5", [ScriptedResponse::error(unavailable())]);
        let backup = scripted("claude-opus-5-5", [refusal("claude-opus-5-5", "anthropic", 0.0)]);
        let model = FallbackLlm::builder(primary)
            .fallback(backup)
            .fall_back_on(FallbackTrigger::Refusal)
            .build();
        let responses = collect(&model, false).await;
        assert_eq!(responses[0].as_ref().unwrap().finish_reason, Some(FinishReason::Refusal));
    }

    #[tokio::test]
    async fn a_model_that_answers_first_time_is_not_marked() {
        let primary = scripted("claude-sonnet-5-5", [ScriptedResponse::text("ok")]);
        let backup = scripted("claude-haiku-5-5", Vec::<ScriptedResponse>::new());
        let model = FallbackLlm::builder(primary).fallback(backup).build();
        let responses = collect(&model, false).await;
        assert!(responses[0].as_ref().unwrap().provider_metadata.is_none());
    }

    fn ledger() -> Arc<InMemorySpendLedger> {
        Arc::new(InMemorySpendLedger::new(SpendLimits::new()))
    }

    async fn spent(ledger: &InMemorySpendLedger, vendor: &str) -> u64 {
        ledger.spent(&SpendKey::org("acme").with_vendor(vendor), SpendPeriod::Day).await.unwrap()
    }

    #[tokio::test]
    async fn every_attempt_is_metered_under_its_own_provider() {
        let ledger = ledger();
        let tracker = Arc::new(BudgetTracker::new(RunBudget::new()));
        let scope = ModelCallScope::new()
            .with_budget(tracker.clone())
            .with_spend_ledger(ledger.clone(), "acme");
        // A billed refusal on OpenAI, an outage on Gemini, the answer on Anthropic.
        let refused = scripted("gpt-6.1-sol", [refusal("gpt-6.1-sol", "openai", 0.001)]);
        let down = scripted("gemini-3.7-flash", [ScriptedResponse::error(unavailable())]);
        let answers =
            scripted("claude-haiku-5-5", [answer("ok", "claude-haiku-5-5", "anthropic", 0.002)]);
        let model = FallbackLlm::builder(refused)
            .fallback(down)
            .fallback(answers)
            .fall_back_on(FallbackTrigger::Refusal)
            .build();

        let mut responses = scope.generate(&model, request(), false).await.unwrap();
        let response = responses.next().await.unwrap().unwrap();
        assert!(responses.next().await.is_none());
        assert_eq!(response.model.as_deref(), Some("claude-haiku-5-5"));

        assert_eq!(spent(&ledger, "openai").await, 1_000);
        assert_eq!(spent(&ledger, "gemini").await, 0);
        assert_eq!(spent(&ledger, "anthropic").await, 2_000);
        assert_eq!(ledger.spent(&SpendKey::org("acme"), SpendPeriod::Day).await.unwrap(), 3_000);
        let usage = tracker.usage();
        assert_eq!((usage.model_calls, usage.cost_micro_usd, usage.total_tokens), (3, 3_000, 25));
    }

    #[tokio::test]
    async fn a_budget_refusal_ends_the_chain_without_falling_back() {
        let tracker = Arc::new(BudgetTracker::new(RunBudget::new().max_model_calls(1)));
        let scope = ModelCallScope::new().with_budget(tracker.clone());
        let primary = scripted("claude-sonnet-5-5", [ScriptedResponse::error(unavailable())]);
        let backup = scripted("claude-haiku-5-5", [ScriptedResponse::text("ok")]);
        let model = FallbackLlm::builder(primary).fallback(backup.clone()).build();

        let refused = scope.generate(&model, request(), false).await.err().unwrap();
        assert_eq!(refused.category, ErrorCategory::ResourceExhausted);
        assert!(backup.requests().is_empty());
        assert_eq!(tracker.usage().model_calls, 1);
    }

    #[tokio::test]
    async fn a_frozen_kill_switch_stops_the_chain_before_the_next_model() {
        let control = GovernanceControl::new();
        let scope = ModelCallScope::new().with_governance(control.clone());
        let freezes = control.clone();
        // The first model's failure is the moment an operator freezes the run.
        struct FreezesOnCall {
            inner: Arc<ScriptedLlm>,
            control: GovernanceControl,
        }
        #[async_trait]
        impl Llm for FreezesOnCall {
            fn name(&self) -> &str {
                self.inner.name()
            }
            async fn generate_content(
                &self,
                req: LlmRequest,
                stream: bool,
            ) -> Result<LlmResponseStream> {
                self.control.freeze("incident");
                self.inner.generate_content(req, stream).await
            }
        }
        let primary = Arc::new(FreezesOnCall {
            inner: scripted("claude-sonnet-5-5", [ScriptedResponse::error(unavailable())]),
            control: freezes,
        });
        let backup = scripted("claude-haiku-5-5", [ScriptedResponse::text("ok")]);
        let model = FallbackLlm::builder(primary).fallback(backup.clone()).build();

        let refused = scope.generate(&model, request(), false).await.err().unwrap();
        assert_eq!(refused.code, adk_core::GOVERNANCE_FROZEN_CODE);
        assert!(backup.requests().is_empty());
    }

    struct Described {
        name: &'static str,
        window: Option<u64>,
        bound: Option<u64>,
        search: Option<NativeWebSearch>,
    }

    #[async_trait]
    impl Llm for Described {
        fn name(&self) -> &str {
            self.name
        }
        async fn generate_content(&self, _: LlmRequest, _: bool) -> Result<LlmResponseStream> {
            Ok(Box::pin(futures::stream::empty()))
        }
        fn context_window(&self) -> Option<u64> {
            self.window
        }
        fn max_cost_micro_usd(&self, _: &LlmRequest) -> Option<u64> {
            self.bound
        }
        fn native_web_search(&self) -> Option<NativeWebSearch> {
            self.search
        }
    }

    #[test]
    fn a_mixed_chain_reports_the_limits_every_model_can_serve() {
        let search =
            NativeWebSearch::new("anthropic", NativeWebSearchTool::AnthropicWebSearch, true);
        let small = Arc::new(Described {
            name: "claude-haiku-4-5",
            window: Some(200_000),
            bound: Some(10_000),
            search: Some(search),
        });
        let large = Arc::new(Described {
            name: "claude-sonnet-5-5",
            window: Some(1_000_000),
            bound: Some(40_000),
            search: Some(search),
        });
        let unknown =
            Arc::new(Described { name: "local", window: None, bound: None, search: None });

        let model = FallbackLlm::builder(small.clone()).fallback(large.clone()).build();
        assert_eq!(model.name(), "claude-haiku-4-5");
        assert_eq!(model.context_window(), Some(200_000));
        assert_eq!(model.max_cost_micro_usd(&request()), Some(40_000));
        assert_eq!(model.native_web_search(), Some(search));

        let model = FallbackLlm::builder(small.clone())
            .fallback(large.clone())
            .fall_back_on(FallbackTrigger::ContextOverflow)
            .fall_back_on(FallbackTrigger::Refusal)
            .build();
        assert_eq!(model.context_window(), Some(1_000_000));
        assert_eq!(model.max_cost_micro_usd(&request()), Some(50_000));

        let model = FallbackLlm::builder(small).fallback(large).fallback(unknown).build();
        assert_eq!(model.context_window(), Some(200_000));
        assert_eq!(model.max_cost_micro_usd(&request()), None);
        assert_eq!(model.native_web_search(), None);
    }

    #[test]
    fn the_builder_edits_the_trigger_set() {
        let primary = scripted("claude-sonnet-5-5", Vec::<ScriptedResponse>::new());
        let model = FallbackLlm::builder(primary.clone())
            .triggers([FallbackTrigger::Refusal, FallbackTrigger::Refusal])
            .fall_back_on(FallbackTrigger::Retryable)
            .build();
        assert_eq!(model.triggers(), [FallbackTrigger::Refusal, FallbackTrigger::Retryable]);
        assert_eq!(model.models().len(), 1);
        assert_eq!(
            format!("{model:?}"),
            "FallbackLlm { models: [\"claude-sonnet-5-5\"], triggers: [Refusal, Retryable] }"
        );
    }

    #[cfg(all(feature = "anthropic", feature = "gemini"))]
    #[test]
    fn a_chain_sends_to_every_models_destination_or_names_none() {
        use adk_core::DataDestination;
        let claude = Arc::new(
            crate::AnthropicClient::new(crate::anthropic::AnthropicConfig::new(
                "key",
                "claude-sonnet-5-5",
            ))
            .unwrap(),
        );
        let gemini = Arc::new(crate::GeminiModel::new("key", "gemini-3.8-flash").unwrap());
        let chain = FallbackLlm::builder(claude.clone()).fallback(gemini).build();
        assert_eq!(
            chain.data_destinations(),
            [
                DataDestination::new("anthropic", ["api.anthropic.com"]),
                DataDestination::new("gemini", ["generativelanguage.googleapis.com"]),
            ]
        );

        let unknown = scripted("local", Vec::<ScriptedResponse>::new());
        let chain = FallbackLlm::builder(claude).fallback(unknown).build();
        assert!(chain.data_destinations().is_empty());
    }
}