@@ -29,7 +29,7 @@ use serde_json::Value;
2929/// Client for interacting with Anthropic's API.
3030///
3131/// Provides methods for chat and completion requests using Anthropic's models.
32- #[ derive( Debug ) ]
32+ #[ derive( Debug , Clone ) ]
3333pub struct Anthropic {
3434 pub api_key : String ,
3535 pub model : String ,
@@ -506,6 +506,43 @@ impl Anthropic {
506506 client : builder. build ( ) . expect ( "Failed to build reqwest Client" ) ,
507507 }
508508 }
509+
510+ /// Creates a new Anthropic client with a pre-configured HTTP client.
511+ ///
512+ /// This allows sharing a single `reqwest::Client` across multiple providers,
513+ /// enabling connection pooling and reducing resource usage.
514+ #[ allow( clippy:: too_many_arguments) ]
515+ pub fn with_client (
516+ client : Client ,
517+ api_key : impl Into < String > ,
518+ model : Option < String > ,
519+ max_tokens : Option < u32 > ,
520+ temperature : Option < f32 > ,
521+ timeout_seconds : Option < u64 > ,
522+ system : Option < String > ,
523+ top_p : Option < f32 > ,
524+ top_k : Option < u32 > ,
525+ tools : Option < Vec < Tool > > ,
526+ tool_choice : Option < ToolChoice > ,
527+ reasoning : Option < bool > ,
528+ thinking_budget_tokens : Option < u32 > ,
529+ ) -> Self {
530+ Self {
531+ api_key : api_key. into ( ) ,
532+ model : model. unwrap_or_else ( || "claude-3-sonnet-20240229" . to_string ( ) ) ,
533+ max_tokens : max_tokens. unwrap_or ( 300 ) ,
534+ temperature : temperature. unwrap_or ( 0.7 ) ,
535+ system : system. unwrap_or_else ( || "You are a helpful assistant." . to_string ( ) ) ,
536+ timeout_seconds : timeout_seconds. unwrap_or ( 30 ) ,
537+ top_p,
538+ top_k,
539+ tools,
540+ tool_choice,
541+ reasoning : reasoning. unwrap_or ( false ) ,
542+ thinking_budget_tokens,
543+ client,
544+ }
545+ }
509546}
510547
511548#[ async_trait]
@@ -1391,4 +1428,81 @@ data: {"type": "ping"}
13911428 let result = parse_anthropic_sse_chunk_with_tools ( chunk, & mut tool_states) . unwrap ( ) ;
13921429 assert ! ( result. is_none( ) ) ;
13931430 }
1431+
1432+ #[ test]
1433+ fn test_anthropic_clone ( ) {
1434+ let anthropic = Anthropic :: new (
1435+ "test-api-key" ,
1436+ Some ( "claude-3-sonnet" . to_string ( ) ) ,
1437+ Some ( 1000 ) ,
1438+ Some ( 0.7 ) ,
1439+ Some ( 30 ) ,
1440+ Some ( "You are helpful." . to_string ( ) ) ,
1441+ None ,
1442+ None ,
1443+ None ,
1444+ None ,
1445+ None ,
1446+ None ,
1447+ ) ;
1448+
1449+ // Clone the provider
1450+ let cloned = anthropic. clone ( ) ;
1451+
1452+ // Verify both have the same configuration
1453+ assert_eq ! ( anthropic. api_key, cloned. api_key) ;
1454+ assert_eq ! ( anthropic. model, cloned. model) ;
1455+ assert_eq ! ( anthropic. max_tokens, cloned. max_tokens) ;
1456+ assert_eq ! ( anthropic. temperature, cloned. temperature) ;
1457+ assert_eq ! ( anthropic. system, cloned. system) ;
1458+ }
1459+
1460+ #[ test]
1461+ fn test_anthropic_with_client ( ) {
1462+ let shared_client = Client :: builder ( )
1463+ . timeout ( std:: time:: Duration :: from_secs ( 60 ) )
1464+ . build ( )
1465+ . expect ( "Failed to build client" ) ;
1466+
1467+ let anthropic = Anthropic :: with_client (
1468+ shared_client. clone ( ) ,
1469+ "test-api-key" ,
1470+ Some ( "claude-3-sonnet" . to_string ( ) ) ,
1471+ Some ( 1000 ) ,
1472+ Some ( 0.7 ) ,
1473+ Some ( 30 ) ,
1474+ Some ( "You are helpful." . to_string ( ) ) ,
1475+ None ,
1476+ None ,
1477+ None ,
1478+ None ,
1479+ None ,
1480+ None ,
1481+ ) ;
1482+
1483+ // Verify configuration
1484+ assert_eq ! ( anthropic. api_key, "test-api-key" ) ;
1485+ assert_eq ! ( anthropic. model, "claude-3-sonnet" ) ;
1486+ assert_eq ! ( anthropic. max_tokens, 1000 ) ;
1487+
1488+ // Create another provider with the same client
1489+ let anthropic2 = Anthropic :: with_client (
1490+ shared_client,
1491+ "test-api-key-2" ,
1492+ Some ( "claude-3-haiku" . to_string ( ) ) ,
1493+ None ,
1494+ None ,
1495+ None ,
1496+ None ,
1497+ None ,
1498+ None ,
1499+ None ,
1500+ None ,
1501+ None ,
1502+ None ,
1503+ ) ;
1504+
1505+ assert_eq ! ( anthropic2. api_key, "test-api-key-2" ) ;
1506+ assert_eq ! ( anthropic2. model, "claude-3-haiku" ) ;
1507+ }
13941508}
0 commit comments