multi-llm 1.0.0

Unified multi-provider LLM client with support for OpenAI, Anthropic, Ollama, and LMStudio
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
//! Anthropic provider implementation

use super::caching;
use super::conversion;
use super::types::{AnthropicContentBlock, AnthropicRequest, AnthropicResponse, SystemField};
use crate::config::{AnthropicConfig, DefaultLLMParams};
use crate::error::{LlmError, LlmResult};
#[cfg(feature = "events")]
use crate::internals::events::{event_types, BusinessEvent, EventScope};
use crate::internals::retry::RetryExecutor;
use crate::logging::{log_debug, log_error, log_warn};
use crate::messages::UnifiedLLMRequest;
#[cfg(feature = "events")]
use crate::provider::LLMBusinessEvent;
use crate::provider::{LlmProvider, RequestConfig, Response, ResponseFormat, ToolCallingRound};
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
use std::time::Instant;
use tokio::sync::Mutex;

/// Anthropic Claude provider implementation
#[derive(Debug)]
pub struct AnthropicProvider {
    client: reqwest::Client,
    retry_executor: Mutex<RetryExecutor>,
    config: AnthropicConfig,
    default_params: DefaultLLMParams,
}

impl AnthropicProvider {
    /// Create a new Anthropic provider instance
    ///
    /// # Errors
    ///
    /// Returns [`LlmError::ConfigurationError`] if:
    /// - API key is missing or invalid
    /// - Provider configuration validation fails
    /// - HTTP client initialization fails
    pub fn new(config: AnthropicConfig, default_params: DefaultLLMParams) -> LlmResult<Self> {
        if config.api_key.is_none() {
            return Err(LlmError::configuration_error(
                "Anthropic API key is required",
            ));
        }

        log_debug!(
            provider = "anthropic",
            max_context_tokens = config.max_context_tokens,
            "Anthropic provider initialized"
        );

        Ok(Self {
            client: reqwest::Client::new(),
            retry_executor: Mutex::new(RetryExecutor::new(config.retry_policy.clone())),
            config,
            default_params,
        })
    }

    /// Sends a request to the Anthropic Messages API with retry logic
    async fn send_anthropic_request(
        &self,
        request: AnthropicRequest,
    ) -> LlmResult<AnthropicResponse> {
        let url = format!("{}/v1/messages", self.config.base_url);
        let api_key = self
            .config
            .api_key
            .as_ref()
            .ok_or_else(|| LlmError::configuration_error("Anthropic API key is required"))?;

        // Build headers required by Anthropic API
        let mut headers = HeaderMap::new();
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
        headers.insert(
            "x-api-key",
            HeaderValue::from_str(api_key).map_err(|e| {
                LlmError::configuration_error(format!("Invalid API key format: {e}"))
            })?,
        );
        headers.insert("anthropic-version", HeaderValue::from_static("2023-06-01"));

        let mut retry_executor = self.retry_executor.lock().await;
        retry_executor
            .execute(|| self.execute_single_anthropic_request(&url, &headers, &request))
            .await
    }

    /// Execute a single HTTP request to Anthropic API
    async fn execute_single_anthropic_request(
        &self,
        url: &str,
        headers: &HeaderMap,
        request: &AnthropicRequest,
    ) -> LlmResult<AnthropicResponse> {
        let response = self
            .client
            .post(url)
            .headers(headers.clone())
            .json(request)
            .send()
            .await
            .map_err(|e| {
                log_error!(
                    provider = "anthropic",
                    url = %url,
                    error = %e,
                    "HTTP request failed"
                );
                LlmError::request_failed(
                    format!("Anthropic request failed: {e}"),
                    Some(Box::new(e)),
                )
            })?;

        if !response.status().is_success() {
            return Err(self.handle_anthropic_error_response(response).await);
        }

        self.parse_anthropic_success_response(response).await
    }

    /// Check if error JSON indicates auth failure
    pub(crate) fn is_auth_error(error_json: &serde_json::Value) -> bool {
        error_json
            .get("error")
            .and_then(|obj| obj.get("type"))
            .and_then(|t| t.as_str())
            .map(|error_type| {
                error_type.contains("authentication") || error_type.contains("invalid_api_key")
            })
            .unwrap_or(false)
    }

    /// Parse authentication error from response text
    pub(crate) fn parse_auth_error(error_text: &str) -> LlmError {
        if let Ok(error_json) = serde_json::from_str::<serde_json::Value>(error_text) {
            if Self::is_auth_error(&error_json) {
                return LlmError::authentication_failed(
                    "Invalid Anthropic API key or authentication failed",
                );
            }
        }
        LlmError::authentication_failed("Anthropic authentication failed")
    }

    /// Extract retry-after value from headers
    fn extract_retry_after(headers: &reqwest::header::HeaderMap) -> u64 {
        headers
            .get("retry-after")
            .and_then(|h| h.to_str().ok())
            .and_then(|s| s.parse::<u64>().ok())
            .unwrap_or(60)
    }

    /// Handle non-success HTTP responses from Anthropic API
    async fn handle_anthropic_error_response(&self, response: reqwest::Response) -> LlmError {
        let status = response.status();
        let headers = response.headers().clone();
        let error_text = response
            .text()
            .await
            .unwrap_or_else(|_| "Unknown error".to_string());

        log_error!(
            provider = "anthropic",
            status = %status,
            error_text = %error_text,
            "Anthropic API error"
        );

        match status.as_u16() {
            401 => Self::parse_auth_error(&error_text),
            429 => {
                let retry_after_seconds = Self::extract_retry_after(&headers);
                LlmError::rate_limit_exceeded(retry_after_seconds)
            }
            _ => LlmError::request_failed(
                format!("Anthropic API error {status}: {error_text}"),
                None,
            ),
        }
    }

    /// Parse successful HTTP response from Anthropic API
    async fn parse_anthropic_success_response(
        &self,
        response: reqwest::Response,
    ) -> LlmResult<AnthropicResponse> {
        let raw_body = response.text().await.map_err(|e| {
            log_error!(
                provider = "anthropic",
                error = %e,
                "Failed to read Anthropic response body"
            );
            LlmError::response_parsing_error(format!("Failed to read response: {e}"))
        })?;

        let api_response: AnthropicResponse = serde_json::from_str(&raw_body).map_err(|e| {
            log_error!(
                provider = "anthropic",
                error = %e,
                raw_body = %raw_body,
                "Failed to parse Anthropic response"
            );
            LlmError::response_parsing_error(format!("Invalid Anthropic response: {e}"))
        })?;

        // Debug log the full network response JSON
        log_debug!(
            provider = "anthropic",
            response_json = %raw_body,
            "Network response JSON"
        );

        Ok(api_response)
    }

    /// Internal method for executor pattern - restore default retry policy
    pub(crate) async fn restore_default_retry_policy(&self) {
        // Anthropic provider doesn't need explicit retry policy restoration
        // The client manages retry state internally
    }

    /// Convert AnthropicResponse to Response
    fn convert_anthropic_to_executor_response(
        &self,
        api_response: AnthropicResponse,
        response_format: Option<ResponseFormat>,
        config: Option<RequestConfig>,
    ) -> crate::provider::Result<Response> {
        let (content, tool_calls) = self.extract_content_and_tools(&api_response);
        let usage = self.create_usage_stats(&api_response, config.as_ref());
        let (final_content, structured_response) =
            self.parse_structured_response(content, &tool_calls, response_format);

        Ok(crate::provider::Response {
            content: final_content,
            structured_response,
            tool_calls,
            usage: Some(usage),
            model: Some(api_response.model),
            raw_body: None,
        })
    }

    fn extract_content_and_tools(
        &self,
        api_response: &AnthropicResponse,
    ) -> (String, Vec<crate::provider::ToolCall>) {
        let mut content = String::new();
        let mut tool_calls = Vec::new();

        for content_block in &api_response.content {
            match content_block {
                AnthropicContentBlock::Text { text, .. } => {
                    if !content.is_empty() {
                        content.push('\n');
                    }
                    content.push_str(text);
                }
                AnthropicContentBlock::ToolUse { id, name, input } => {
                    tool_calls.push(crate::provider::ToolCall {
                        id: id.clone(),
                        name: name.clone(),
                        arguments: input.clone(),
                    });
                }
                AnthropicContentBlock::ToolResult { .. } => {}
            }
        }

        (content, tool_calls)
    }

    fn create_usage_stats(
        &self,
        api_response: &AnthropicResponse,
        config: Option<&RequestConfig>,
    ) -> crate::provider::TokenUsage {
        let cache_creation_tokens = api_response.usage.cache_creation_input_tokens.unwrap_or(0);
        let cache_read_tokens = api_response.usage.cache_read_input_tokens.unwrap_or(0);
        let total_tokens_with_cache = api_response.usage.input_tokens
            + api_response.usage.output_tokens
            + cache_creation_tokens
            + cache_read_tokens;

        if self.config.enable_prompt_caching {
            self.log_cache_usage(api_response, config);
        }

        crate::provider::TokenUsage {
            prompt_tokens: api_response.usage.input_tokens,
            completion_tokens: api_response.usage.output_tokens,
            total_tokens: total_tokens_with_cache,
        }
    }

    fn log_cache_usage(&self, api_response: &AnthropicResponse, config: Option<&RequestConfig>) {
        let provider_with_path = config
            .and_then(|cfg| cfg.llm_path.as_ref().map(|p| format!("{}:anthropic", p)))
            .unwrap_or_else(|| "anthropic".to_string());

        let cache_read = api_response.usage.cache_read_input_tokens.unwrap_or(0);
        let total_input = api_response.usage.input_tokens + cache_read;
        let cache_hit_rate = if total_input > 0 {
            cache_read as f64 / total_input as f64
        } else {
            0.0
        };

        log_debug!(
            provider = %provider_with_path,
            user_id = config.and_then(|c| c.user_id.as_deref()).unwrap_or("unknown"),
            session_id = config.and_then(|c| c.session_id.as_deref()).unwrap_or("unknown"),
            cache_usage = ?serde_json::json!({
                "input_tokens": api_response.usage.input_tokens,
                "output_tokens": api_response.usage.output_tokens,
                "cache_creation_input_tokens": api_response.usage.cache_creation_input_tokens.unwrap_or(0),
                "cache_read_input_tokens": cache_read,
                "cache_creation_5m": api_response.usage.cache_creation.as_ref()
                    .and_then(|c| c.ephemeral_5m_input_tokens).unwrap_or(0),
                "cache_creation_1h": api_response.usage.cache_creation.as_ref()
                    .and_then(|c| c.ephemeral_1h_input_tokens).unwrap_or(0),
                "cache_hit_rate": cache_hit_rate,
                "ttl_setting": self.config.cache_ttl
            }),
            "Anthropic cache usage statistics"
        );
    }

    pub(crate) fn parse_structured_response(
        &self,
        content: String,
        tool_calls: &[crate::provider::ToolCall],
        response_format: Option<ResponseFormat>,
    ) -> (String, Option<serde_json::Value>) {
        if response_format.is_none() {
            return (content, None);
        }

        // Try to extract from tool calls first
        if let Some(tool_data) = tool_calls
            .iter()
            .find(|tc| tc.name == "structured_response")
        {
            log_debug!(
                provider = "anthropic",
                "Successfully extracted structured response from tool_use"
            );
            return (content.clone(), Some(tool_data.arguments.clone()));
        }

        // Fallback: parse content as JSON
        self.parse_json_content(content)
    }

    pub(crate) fn parse_json_content(
        &self,
        content: String,
    ) -> (String, Option<serde_json::Value>) {
        let json_content = if content.trim_start().starts_with('{') {
            content.clone()
        } else {
            format!("{{{}", content)
        };

        match serde_json::from_str::<serde_json::Value>(&json_content) {
            Ok(json_value) => {
                log_debug!(
                    provider = "anthropic",
                    "Successfully parsed structured JSON response from content"
                );
                (json_content, Some(json_value))
            }
            Err(e) => {
                log_warn!(
                    provider = "anthropic",
                    content_preview = &json_content[..json_content.len().min(200)],
                    error = %e,
                    "Failed to parse structured JSON response from content"
                );
                (content, None)
            }
        }
    }

    /// Apply executor config to Anthropic request
    fn apply_executor_config(
        &self,
        request: &mut AnthropicRequest,
        config: RequestConfig,
        enable_caching: bool,
    ) -> Option<ResponseFormat> {
        // Apply LLM parameters
        // Anthropic doesn't allow both temperature and top_p - enforce mutual exclusivity
        if let Some(temp) = config.temperature {
            request.temperature = Some(temp as f32);
            request.top_p = None; // Clear top_p if temperature is set
        } else if let Some(top_p) = config.top_p {
            // Only set top_p if temperature wasn't provided
            request.top_p = Some(top_p as f32);
            request.temperature = None; // Clear temperature if top_p is set
        }
        if let Some(max_tokens) = config.max_tokens {
            request.max_tokens = max_tokens;
        }
        if let Some(top_k) = config.top_k {
            request.top_k = Some(top_k);
        }

        // Convert tools - only add user tools for User LLM path
        if !config.tools.is_empty() {
            // Check if this is a user LLM request based on llm_path
            let is_user_llm = config
                .llm_path
                .as_ref()
                .map(|path| path == "user_llm")
                .unwrap_or(true); // Default to user LLM for backwards compatibility

            if is_user_llm {
                request.tools = Some(caching::convert_executor_tools_to_anthropic(
                    &config.tools,
                    enable_caching,
                    &self.config.cache_ttl,
                ));
            }
            // For non-user LLM paths (story_analysis, nlp_llm), skip user tools
            // They will only get structured_response tool if response_schema is present
        }

        // Return response format for structured parsing
        config.response_format
    }

    /// Create base Anthropic request from unified request
    fn create_base_request(
        &self,
        request: &UnifiedLLMRequest,
        enable_caching: bool,
    ) -> AnthropicRequest {
        let sorted_messages = request.get_sorted_messages();
        let (system_messages, conversation_messages) =
            conversion::transform_unified_messages(&sorted_messages, &self.config, enable_caching);

        AnthropicRequest {
            model: self.config.default_model.clone(),
            max_tokens: self.default_params.max_tokens,
            system: if system_messages.is_empty() {
                None
            } else {
                Some(SystemField::Messages(system_messages))
            },
            messages: conversation_messages,
            temperature: Some(self.default_params.temperature as f32),
            top_p: None,
            top_k: Some(self.default_params.top_k),
            stop_sequences: None,
            tools: None,
        }
    }

    /// Determine if caching should be enabled for this request
    pub(crate) fn should_enable_caching(&self, config: Option<&RequestConfig>) -> bool {
        self.config.enable_prompt_caching
            && config
                .and_then(|c| c.llm_path.as_ref())
                .map(|path| path != "nlp_llm")
                .unwrap_or(true)
    }

    /// Apply response schema to request if present
    fn apply_response_schema(
        &self,
        request: &mut AnthropicRequest,
        schema: Option<serde_json::Value>,
    ) {
        if let Some(schema) = schema {
            request.tools = Some(vec![serde_json::json!({
                "name": "structured_response",
                "description": "Provide a structured response matching the required schema",
                "input_schema": schema
            })]);
        }
    }

    /// Core LLM execution logic shared between events and non-events versions
    ///
    /// Returns (Response, raw AnthropicResponse, duration_ms, AnthropicRequest)
    /// to allow event creation in events-enabled builds
    async fn execute_llm_internal(
        &self,
        request: UnifiedLLMRequest,
        config: Option<RequestConfig>,
    ) -> crate::provider::Result<(Response, AnthropicResponse, u64, AnthropicRequest)> {
        // Determine caching and create base request
        let enable_caching = self.should_enable_caching(config.as_ref());
        let mut anthropic_request = self.create_base_request(&request, enable_caching);

        // Apply executor config if provided
        let response_format = if let Some(ref cfg) = config {
            self.apply_executor_config(&mut anthropic_request, cfg.clone(), enable_caching)
        } else {
            None
        };

        // Apply response schema from request if present
        self.apply_response_schema(&mut anthropic_request, request.response_schema);

        // Debug log the full network request JSON
        log_debug!(
            provider = "anthropic",
            request_json = %serde_json::to_string(&anthropic_request).unwrap_or_default(),
            "Network request JSON"
        );

        // Clone request for event creation (all types are now Clone)
        let anthropic_request_for_events = anthropic_request.clone();

        // Send to Anthropic API
        let start_time = Instant::now();
        let api_response = self.send_anthropic_request(anthropic_request).await?;
        let duration_ms = start_time.elapsed().as_millis() as u64;

        // Convert Anthropic response to Response
        let response = self.convert_anthropic_to_executor_response(
            api_response.clone(),
            response_format,
            config,
        )?;

        Ok((
            response,
            api_response,
            duration_ms,
            anthropic_request_for_events,
        ))
    }

    /// Create LLM request business event
    #[cfg(feature = "events")]
    fn create_request_event(
        &self,
        anthropic_request: &AnthropicRequest,
        config: Option<&RequestConfig>,
    ) -> Option<LLMBusinessEvent> {
        let user_id = config.and_then(|c| c.user_id.clone())?;

        let mut event = BusinessEvent::new(event_types::LLM_REQUEST)
            .with_metadata("provider", "anthropic")
            .with_metadata("model", &anthropic_request.model);

        if let Some(cfg) = config {
            if let Some(ref path) = cfg.llm_path {
                event = event.with_metadata("llm_path", path);
            }
        }

        Some(LLMBusinessEvent {
            event,
            scope: EventScope::User(user_id),
        })
    }

    /// Create LLM error business event
    #[cfg(feature = "events")]
    fn create_error_event(
        &self,
        error: &LlmError,
        config: Option<&RequestConfig>,
    ) -> Option<LLMBusinessEvent> {
        let user_id = config.and_then(|c| c.user_id.clone())?;

        let event = BusinessEvent::new(event_types::LLM_ERROR)
            .with_metadata("provider", "anthropic")
            .with_metadata("error", error.to_string());

        Some(LLMBusinessEvent {
            event,
            scope: EventScope::User(user_id),
        })
    }

    /// Create LLM response business event
    #[cfg(feature = "events")]
    fn create_response_event(
        &self,
        api_response: &AnthropicResponse,
        duration_ms: u64,
        config: Option<&RequestConfig>,
    ) -> Option<LLMBusinessEvent> {
        let user_id = config.and_then(|c| c.user_id.clone())?;

        let mut event = BusinessEvent::new(event_types::LLM_RESPONSE)
            .with_metadata("provider", "anthropic")
            .with_metadata("model", &api_response.model)
            .with_metadata("input_tokens", api_response.usage.input_tokens)
            .with_metadata("output_tokens", api_response.usage.output_tokens)
            .with_metadata("duration_ms", duration_ms);

        if let Some(cache_write) = api_response.usage.cache_creation_input_tokens {
            event = event.with_metadata("cache_creation_tokens", cache_write);
        }
        if let Some(cache_read) = api_response.usage.cache_read_input_tokens {
            event = event.with_metadata("cache_read_tokens", cache_read);
        }

        if let Some(cfg) = config {
            if let Some(ref sess_id) = cfg.session_id {
                event = event.with_metadata("session_id", sess_id);
            }
            if let Some(ref path) = cfg.llm_path {
                event = event.with_metadata("llm_path", path);
            }
        }

        Some(LLMBusinessEvent {
            event,
            scope: EventScope::User(user_id),
        })
    }
}

#[async_trait::async_trait]
impl LlmProvider for AnthropicProvider {
    #[cfg(feature = "events")]
    async fn execute_llm(
        &self,
        request: UnifiedLLMRequest,
        _current_tool_round: Option<ToolCallingRound>,
        config: Option<RequestConfig>,
    ) -> crate::provider::Result<(Response, Vec<LLMBusinessEvent>)> {
        let mut events = Vec::new();

        // Execute core logic and collect event data
        let (response, api_response, duration_ms, anthropic_request) =
            match self.execute_llm_internal(request, config.clone()).await {
                Ok(result) => result,
                Err(e) => {
                    // On error, log error event
                    if let Some(event) = self.create_error_event(&e, config.as_ref()) {
                        events.push(event);
                    }
                    return Err(e);
                }
            };

        // Log request event
        if let Some(event) = self.create_request_event(&anthropic_request, config.as_ref()) {
            events.push(event);
        }

        // Log response event
        if let Some(event) = self.create_response_event(&api_response, duration_ms, config.as_ref())
        {
            events.push(event);
        }

        Ok((response, events))
    }

    #[cfg(not(feature = "events"))]
    async fn execute_llm(
        &self,
        request: UnifiedLLMRequest,
        _current_tool_round: Option<ToolCallingRound>,
        config: Option<RequestConfig>,
    ) -> crate::provider::Result<Response> {
        // Simple wrapper - just return the response
        let (response, _api_response, _duration_ms, _anthropic_request) =
            self.execute_llm_internal(request, config).await?;
        Ok(response)
    }

    #[cfg(feature = "events")]
    async fn execute_structured_llm(
        &self,
        mut request: UnifiedLLMRequest,
        current_tool_round: Option<ToolCallingRound>,
        schema: serde_json::Value,
        config: Option<RequestConfig>,
    ) -> crate::provider::Result<(Response, Vec<LLMBusinessEvent>)> {
        // Set the schema in the request
        request.response_schema = Some(schema);

        // Execute with the schema-enabled request (returns tuple with events)
        self.execute_llm(request, current_tool_round, config).await
    }

    #[cfg(not(feature = "events"))]
    async fn execute_structured_llm(
        &self,
        mut request: UnifiedLLMRequest,
        current_tool_round: Option<ToolCallingRound>,
        schema: serde_json::Value,
        config: Option<RequestConfig>,
    ) -> crate::provider::Result<Response> {
        // Set the schema in the request
        request.response_schema = Some(schema);

        // Execute with the schema-enabled request
        self.execute_llm(request, current_tool_round, config).await
    }

    fn provider_name(&self) -> &'static str {
        "anthropic"
    }
}