agent-sdk 0.8.0

Rust Agent SDK for building LLM 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
//! Cloudflare AI Gateway provider implementation.
//!
//! Routes requests through [Cloudflare's AI Gateway](https://developers.cloudflare.com/ai-gateway/)
//! using **provider-native proxy** endpoints, preserving all provider-specific
//! features (prompt caching, extended thinking, adaptive thinking, etc.).
//!
//! Unlike the Unified API (`/compat`), the provider-native proxy keeps each
//! provider's request/response format intact — the gateway only swaps the base
//! URL and handles authentication via BYOK.
//!
//! # Why provider-native over unified?
//!
//! The unified endpoint translates everything to `OpenAI` Chat Completions
//! format, which **loses** critical features:
//! - Anthropic prompt caching (`cache_control: ephemeral`) — significant cost savings
//! - Anthropic adaptive / budgeted thinking
//! - Gemini `cachedContent` handles
//! - Anthropic thought signatures for tool verification
//!
//! The provider-native proxy preserves all of these.
//!
//! # BYOK authentication
//!
//! Provider API keys are stored in the Cloudflare dashboard. At runtime only a
//! Cloudflare API token is needed — no provider secrets in code.
//!
//! # Example
//!
//! ```no_run
//! use agent_sdk::providers::CloudflareAIGatewayProvider;
//!
//! // BYOK — CF token is the only secret at runtime
//! let provider = CloudflareAIGatewayProvider::anthropic_sonnet(
//!     "your-cf-api-token",
//!     "your-cf-account-id",
//!     "your-gateway-id",
//! );
//!
//! // Pass-through — provider key at runtime, optional gateway auth
//! let provider = CloudflareAIGatewayProvider::anthropic(
//!     "your-anthropic-key".to_string(),
//!     "your-cf-account-id",
//!     "your-gateway-id",
//!     "claude-sonnet-4-6".to_string(),
//! ).with_gateway_token("your-cf-api-token");
//! ```

use crate::llm::{ChatOutcome, ChatRequest, LlmProvider, StreamBox, ThinkingConfig};
use crate::model_capabilities::ModelCapabilities;
use crate::providers::anthropic::AnthropicProvider;
use crate::providers::gemini::GeminiProvider;
use crate::providers::openai::OpenAIProvider;
use anyhow::Result;
use async_trait::async_trait;

const GATEWAY_BASE_URL: &str = "https://gateway.ai.cloudflare.com/v1";
const CF_AIG_AUTH_HEADER: &str = "cf-aig-authorization";

/// Upstream provider that the gateway routes to.
#[derive(Clone)]
enum Inner {
    Anthropic(AnthropicProvider),
    OpenAI(OpenAIProvider),
    Gemini(GeminiProvider),
}

/// Cloudflare AI Gateway LLM provider.
///
/// Wraps an upstream provider (Anthropic, `OpenAI`, or Gemini) and routes
/// requests through the provider-native proxy endpoint, preserving all
/// provider-specific features including prompt caching, extended thinking,
/// and streaming.
///
/// The gateway provides analytics, caching, rate limiting, logging, and
/// automatic fallback on top.
#[derive(Clone)]
pub struct CloudflareAIGatewayProvider {
    inner: Inner,
}

fn gateway_base(account_id: &str, gateway_id: &str, provider_segment: &str) -> String {
    format!("{GATEWAY_BASE_URL}/{account_id}/{gateway_id}/{provider_segment}")
}

fn byok_headers(cf_token: &str) -> Vec<(String, String)> {
    vec![(CF_AIG_AUTH_HEADER.to_owned(), format!("Bearer {cf_token}"))]
}

impl CloudflareAIGatewayProvider {
    // ========================================================================
    // Anthropic (provider-native: /anthropic/v1/messages)
    // ========================================================================

    /// Route to any Anthropic model via the provider-native proxy.
    ///
    /// In **BYOK mode** pass an empty `api_key` and call
    /// [`with_gateway_token`](Self::with_gateway_token).
    /// In **pass-through mode** pass the Anthropic API key directly.
    #[must_use]
    pub fn anthropic(api_key: String, account_id: &str, gateway_id: &str, model: String) -> Self {
        let base_url = gateway_base(account_id, gateway_id, "anthropic");
        let inner = AnthropicProvider::new(api_key, model).with_base_url(base_url);
        Self {
            inner: Inner::Anthropic(inner),
        }
    }

    /// Route to Claude Sonnet 4.6 — BYOK mode (CF token only).
    #[must_use]
    pub fn anthropic_sonnet(cf_token: &str, account_id: &str, gateway_id: &str) -> Self {
        Self::anthropic(
            String::new(),
            account_id,
            gateway_id,
            "claude-sonnet-4-6".to_owned(),
        )
        .with_gateway_token(cf_token)
    }

    /// Route to Claude Opus 4.6 — BYOK mode (CF token only).
    #[must_use]
    pub fn anthropic_opus(cf_token: &str, account_id: &str, gateway_id: &str) -> Self {
        Self::anthropic(
            String::new(),
            account_id,
            gateway_id,
            "claude-opus-4-6".to_owned(),
        )
        .with_gateway_token(cf_token)
    }

    // ========================================================================
    // OpenAI (provider-native: /openai/chat/completions)
    // ========================================================================

    /// Route to any `OpenAI` model via the provider-native proxy.
    #[must_use]
    pub fn openai(api_key: String, account_id: &str, gateway_id: &str, model: String) -> Self {
        let base_url = gateway_base(account_id, gateway_id, "openai");
        let inner = OpenAIProvider::with_base_url(api_key, model, base_url);
        Self {
            inner: Inner::OpenAI(inner),
        }
    }

    /// Route to GPT-5.4 — BYOK mode.
    #[must_use]
    pub fn openai_gpt54(cf_token: &str, account_id: &str, gateway_id: &str) -> Self {
        Self::openai(String::new(), account_id, gateway_id, "gpt-5.4".to_owned())
            .with_gateway_token(cf_token)
    }

    /// Route to GPT-5.4 Mini — BYOK mode.
    #[must_use]
    pub fn openai_gpt54_mini(cf_token: &str, account_id: &str, gateway_id: &str) -> Self {
        Self::openai(
            String::new(),
            account_id,
            gateway_id,
            "gpt-5.4-mini".to_owned(),
        )
        .with_gateway_token(cf_token)
    }

    /// Route to GPT-5.4 Nano — BYOK mode.
    #[must_use]
    pub fn openai_gpt54_nano(cf_token: &str, account_id: &str, gateway_id: &str) -> Self {
        Self::openai(
            String::new(),
            account_id,
            gateway_id,
            "gpt-5.4-nano".to_owned(),
        )
        .with_gateway_token(cf_token)
    }

    // ========================================================================
    // Gemini (provider-native: /google-ai-studio/v1beta/models/...)
    // ========================================================================

    /// Route to any Gemini model via the provider-native proxy.
    ///
    /// Automatically switches to header-based auth (`x-goog-api-key`) as
    /// required by the gateway.
    #[must_use]
    pub fn gemini(api_key: String, account_id: &str, gateway_id: &str, model: String) -> Self {
        let base_url = gateway_base(account_id, gateway_id, "google-ai-studio/v1beta");
        let inner = GeminiProvider::new(api_key, model)
            .with_base_url(base_url)
            .with_header_auth();
        Self {
            inner: Inner::Gemini(inner),
        }
    }

    /// Route to Gemini 3.1 Pro — BYOK mode.
    #[must_use]
    pub fn gemini_pro(cf_token: &str, account_id: &str, gateway_id: &str) -> Self {
        Self::gemini(
            String::new(),
            account_id,
            gateway_id,
            "gemini-3.1-pro-preview".to_owned(),
        )
        .with_gateway_token(cf_token)
    }

    /// Route to Gemini 3 Flash — BYOK mode.
    #[must_use]
    pub fn gemini_flash(cf_token: &str, account_id: &str, gateway_id: &str) -> Self {
        Self::gemini(
            String::new(),
            account_id,
            gateway_id,
            "gemini-3-flash-preview".to_owned(),
        )
        .with_gateway_token(cf_token)
    }

    // ========================================================================
    // Configuration
    // ========================================================================

    /// Set the Cloudflare AI Gateway authentication token.
    ///
    /// Sent via the `cf-aig-authorization` header. In BYOK mode this is the
    /// only auth needed. In pass-through mode it authenticates with the
    /// gateway while the provider API key authenticates with the upstream.
    #[must_use]
    pub fn with_gateway_token(mut self, token: &str) -> Self {
        let headers = byok_headers(token);
        match &mut self.inner {
            Inner::Anthropic(p) => {
                *p = std::mem::replace(p, AnthropicProvider::new(String::new(), String::new()))
                    .with_extra_headers(headers);
            }
            Inner::OpenAI(p) => {
                *p = std::mem::replace(p, OpenAIProvider::new(String::new(), String::new()))
                    .with_extra_headers(headers);
            }
            Inner::Gemini(p) => {
                *p = std::mem::replace(p, GeminiProvider::new(String::new(), String::new()))
                    .with_extra_headers(headers);
            }
        }
        self
    }

    /// Set the provider-owned thinking configuration.
    #[must_use]
    pub fn with_thinking(mut self, thinking: ThinkingConfig) -> Self {
        match &mut self.inner {
            Inner::Anthropic(p) => {
                *p = std::mem::replace(p, AnthropicProvider::new(String::new(), String::new()))
                    .with_thinking(thinking);
            }
            Inner::OpenAI(p) => {
                *p = std::mem::replace(p, OpenAIProvider::new(String::new(), String::new()))
                    .with_thinking(thinking);
            }
            Inner::Gemini(p) => {
                *p = std::mem::replace(p, GeminiProvider::new(String::new(), String::new()))
                    .with_thinking(thinking);
            }
        }
        self
    }
}

#[async_trait]
impl LlmProvider for CloudflareAIGatewayProvider {
    async fn chat(&self, request: ChatRequest) -> Result<ChatOutcome> {
        match &self.inner {
            Inner::Anthropic(p) => p.chat(request).await,
            Inner::OpenAI(p) => p.chat(request).await,
            Inner::Gemini(p) => p.chat(request).await,
        }
    }

    fn chat_stream(&self, request: ChatRequest) -> StreamBox<'_> {
        match &self.inner {
            Inner::Anthropic(p) => p.chat_stream(request),
            Inner::OpenAI(p) => p.chat_stream(request),
            Inner::Gemini(p) => p.chat_stream(request),
        }
    }

    fn model(&self) -> &str {
        match &self.inner {
            Inner::Anthropic(p) => p.model(),
            Inner::OpenAI(p) => p.model(),
            Inner::Gemini(p) => p.model(),
        }
    }

    fn provider(&self) -> &'static str {
        "cloudflare-ai-gateway"
    }

    fn configured_thinking(&self) -> Option<&ThinkingConfig> {
        match &self.inner {
            Inner::Anthropic(p) => p.configured_thinking(),
            Inner::OpenAI(p) => p.configured_thinking(),
            Inner::Gemini(p) => p.configured_thinking(),
        }
    }

    fn capabilities(&self) -> Option<&'static ModelCapabilities> {
        match &self.inner {
            Inner::Anthropic(p) => p.capabilities(),
            Inner::OpenAI(p) => p.capabilities(),
            Inner::Gemini(p) => p.capabilities(),
        }
    }

    fn validate_thinking_config(&self, thinking: Option<&ThinkingConfig>) -> Result<()> {
        match &self.inner {
            Inner::Anthropic(p) => p.validate_thinking_config(thinking),
            Inner::OpenAI(p) => p.validate_thinking_config(thinking),
            Inner::Gemini(p) => p.validate_thinking_config(thinking),
        }
    }

    fn default_max_tokens(&self) -> u32 {
        match &self.inner {
            Inner::Anthropic(p) => p.default_max_tokens(),
            Inner::OpenAI(p) => p.default_max_tokens(),
            Inner::Gemini(p) => p.default_max_tokens(),
        }
    }
}

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

    #[test]
    fn anthropic_sonnet_byok() {
        let p = CloudflareAIGatewayProvider::anthropic_sonnet("cf-tok", "acct", "gw");
        assert_eq!(p.model(), "claude-sonnet-4-6");
        assert_eq!(p.provider(), "cloudflare-ai-gateway");
    }

    #[test]
    fn anthropic_opus_byok() {
        let p = CloudflareAIGatewayProvider::anthropic_opus("cf-tok", "acct", "gw");
        assert_eq!(p.model(), "claude-opus-4-6");
    }

    #[test]
    fn openai_gpt54_byok() {
        let p = CloudflareAIGatewayProvider::openai_gpt54("cf-tok", "acct", "gw");
        assert_eq!(p.model(), "gpt-5.4");
    }

    #[test]
    fn openai_gpt54_mini_byok() {
        let p = CloudflareAIGatewayProvider::openai_gpt54_mini("cf-tok", "acct", "gw");
        assert_eq!(p.model(), "gpt-5.4-mini");
    }

    #[test]
    fn openai_gpt54_nano_byok() {
        let p = CloudflareAIGatewayProvider::openai_gpt54_nano("cf-tok", "acct", "gw");
        assert_eq!(p.model(), "gpt-5.4-nano");
    }

    #[test]
    fn gemini_pro_byok() {
        let p = CloudflareAIGatewayProvider::gemini_pro("cf-tok", "acct", "gw");
        assert_eq!(p.model(), "gemini-3.1-pro-preview");
    }

    #[test]
    fn gemini_flash_byok() {
        let p = CloudflareAIGatewayProvider::gemini_flash("cf-tok", "acct", "gw");
        assert_eq!(p.model(), "gemini-3-flash-preview");
    }

    #[test]
    fn capabilities_resolve_anthropic() {
        let p = CloudflareAIGatewayProvider::anthropic_sonnet("t", "a", "g");
        let caps = p.capabilities().unwrap();
        assert_eq!(caps.provider, "anthropic");
        assert_eq!(caps.model_id, "claude-sonnet-4-6");
        assert!(caps.supports_adaptive_thinking);
    }

    #[test]
    fn capabilities_resolve_openai() {
        let p = CloudflareAIGatewayProvider::openai_gpt54("t", "a", "g");
        let caps = p.capabilities().unwrap();
        assert_eq!(caps.provider, "openai");
        assert_eq!(caps.model_id, "gpt-5.4");
    }

    #[test]
    fn capabilities_resolve_gemini() {
        let p = CloudflareAIGatewayProvider::gemini_pro("t", "a", "g");
        let caps = p.capabilities().unwrap();
        assert_eq!(caps.provider, "gemini");
    }

    #[test]
    fn pass_through_with_gateway_token() {
        let p = CloudflareAIGatewayProvider::anthropic(
            "sk-ant-key".to_string(),
            "acct",
            "gw",
            "claude-sonnet-4-6".to_string(),
        )
        .with_gateway_token("cf-tok");
        assert_eq!(p.model(), "claude-sonnet-4-6");
    }

    #[test]
    fn with_thinking_is_applied() {
        let p = CloudflareAIGatewayProvider::anthropic_sonnet("t", "a", "g")
            .with_thinking(ThinkingConfig::adaptive());
        assert!(p.configured_thinking().is_some());
    }

    #[test]
    fn provider_is_cloneable() {
        let p = CloudflareAIGatewayProvider::anthropic_sonnet("t", "a", "g");
        let cloned = p.clone();
        assert_eq!(p.model(), cloned.model());
    }

    #[test]
    fn gateway_url_format() {
        assert_eq!(
            gateway_base("my-acct", "my-gw", "anthropic"),
            "https://gateway.ai.cloudflare.com/v1/my-acct/my-gw/anthropic"
        );
    }
}