0
# Platform-Specific Clients
1
2
Client classes for using Anthropic models through cloud platform integrations. These clients provide seamless integration with AWS Bedrock, Google Vertex AI, and Azure AI Foundry, handling platform-specific authentication, request formatting, and API routing.
3
4
## Overview
5
6
Platform clients enable you to access Claude models through your existing cloud infrastructure and billing:
7
8
- **AWS Bedrock**: Use Claude through Amazon Bedrock with AWS IAM authentication
9
- **Google Vertex AI**: Access Claude via Google Cloud Platform with service account credentials
10
- **Azure AI Foundry**: Deploy Claude through Azure with API key or Azure AD authentication
11
12
All platform clients provide the same core API as the standard Anthropic client, with platform-specific authentication and configuration options.
13
14
## Capabilities
15
16
### AWS Bedrock Client
17
18
Synchronous client for accessing Anthropic models through AWS Bedrock with AWS SigV4 authentication.
19
20
```python { .api }
21
class AnthropicBedrock:
22
"""
23
Client for using Anthropic models through AWS Bedrock.
24
25
Supports AWS IAM authentication via access keys, session tokens,
26
or AWS profiles. Automatically handles SigV4 request signing.
27
"""
28
29
def __init__(
30
self,
31
aws_secret_key: str | None = None,
32
aws_access_key: str | None = None,
33
aws_region: str | None = None,
34
aws_profile: str | None = None,
35
aws_session_token: str | None = None,
36
base_url: str | httpx.URL | None = None,
37
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
38
max_retries: int = DEFAULT_MAX_RETRIES,
39
default_headers: Mapping[str, str] | None = None,
40
default_query: Mapping[str, object] | None = None,
41
http_client: httpx.Client | None = None,
42
_strict_response_validation: bool = False,
43
) -> None:
44
"""
45
Initialize AWS Bedrock client.
46
47
Parameters:
48
- aws_secret_key: AWS secret access key (defaults to AWS_SECRET_ACCESS_KEY env var)
49
- aws_access_key: AWS access key ID (defaults to AWS_ACCESS_KEY_ID env var)
50
- aws_region: AWS region (defaults to AWS_REGION env var or us-east-1)
51
- aws_profile: AWS profile name from ~/.aws/credentials
52
- aws_session_token: AWS session token for temporary credentials
53
- base_url: Override base URL (defaults to bedrock-runtime.{region}.amazonaws.com)
54
- timeout: Request timeout in seconds
55
- max_retries: Maximum number of retries (default: 2)
56
- default_headers: Default headers for all requests
57
- default_query: Default query parameters for all requests
58
- http_client: Custom httpx client instance
59
- _strict_response_validation: Enable response validation
60
"""
61
62
messages: Messages
63
completions: Completions
64
beta: Beta
65
66
def copy(
67
self,
68
*,
69
aws_secret_key: str | None = None,
70
aws_access_key: str | None = None,
71
aws_region: str | None = None,
72
aws_session_token: str | None = None,
73
base_url: str | httpx.URL | None = None,
74
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
75
http_client: httpx.Client | None = None,
76
max_retries: int | NotGiven = NOT_GIVEN,
77
default_headers: Mapping[str, str] | None = None,
78
set_default_headers: Mapping[str, str] | None = None,
79
default_query: Mapping[str, object] | None = None,
80
set_default_query: Mapping[str, object] | None = None,
81
) -> Self:
82
"""Create new client instance with optional parameter overrides."""
83
84
with_options = copy
85
```
86
87
#### Usage Examples
88
89
**Basic usage with AWS credentials:**
90
91
```python
92
from anthropic import AnthropicBedrock
93
94
# Using explicit credentials
95
client = AnthropicBedrock(
96
aws_access_key="AKIAIOSFODNN7EXAMPLE",
97
aws_secret_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
98
aws_region="us-east-1",
99
)
100
101
message = client.messages.create(
102
model="claude-3-5-sonnet-20241022",
103
max_tokens=1024,
104
messages=[{"role": "user", "content": "Hello from Bedrock!"}],
105
)
106
print(message.content[0].text)
107
```
108
109
**Using AWS profile:**
110
111
```python
112
from anthropic import AnthropicBedrock
113
114
# Use credentials from ~/.aws/credentials profile
115
client = AnthropicBedrock(
116
aws_profile="production",
117
aws_region="us-west-2",
118
)
119
120
message = client.messages.create(
121
model="claude-3-5-sonnet-20241022",
122
max_tokens=1024,
123
messages=[{"role": "user", "content": "What's in a name?"}],
124
)
125
```
126
127
**Cross-region deployment:**
128
129
```python
130
from anthropic import AnthropicBedrock
131
132
# Create clients for different regions
133
us_client = AnthropicBedrock(aws_region="us-east-1")
134
eu_client = AnthropicBedrock(aws_region="eu-west-1")
135
136
# Route requests based on user location
137
def get_completion(text: str, region: str):
138
client = us_client if region == "us" else eu_client
139
return client.messages.create(
140
model="claude-3-5-sonnet-20241022",
141
max_tokens=1024,
142
messages=[{"role": "user", "content": text}],
143
)
144
```
145
146
**Error handling for Bedrock:**
147
148
```python
149
from anthropic import AnthropicBedrock, AuthenticationError, RateLimitError
150
151
client = AnthropicBedrock(aws_region="us-east-1")
152
153
try:
154
message = client.messages.create(
155
model="claude-3-5-sonnet-20241022",
156
max_tokens=1024,
157
messages=[{"role": "user", "content": "Hello!"}],
158
)
159
except AuthenticationError as e:
160
print(f"AWS authentication failed: {e}")
161
# Check AWS credentials and permissions
162
except RateLimitError as e:
163
print(f"Rate limit exceeded: {e}")
164
# Implement backoff strategy
165
```
166
167
### Async AWS Bedrock Client
168
169
Asynchronous client for AWS Bedrock with non-blocking I/O operations.
170
171
```python { .api }
172
class AsyncAnthropicBedrock:
173
"""
174
Async client for using Anthropic models through AWS Bedrock.
175
176
Provides the same functionality as AnthropicBedrock but with
177
asynchronous methods for non-blocking operations.
178
"""
179
180
def __init__(
181
self,
182
aws_secret_key: str | None = None,
183
aws_access_key: str | None = None,
184
aws_region: str | None = None,
185
aws_profile: str | None = None,
186
aws_session_token: str | None = None,
187
base_url: str | httpx.URL | None = None,
188
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
189
max_retries: int = DEFAULT_MAX_RETRIES,
190
default_headers: Mapping[str, str] | None = None,
191
default_query: Mapping[str, object] | None = None,
192
http_client: httpx.AsyncClient | None = None,
193
_strict_response_validation: bool = False,
194
) -> None:
195
"""
196
Initialize async AWS Bedrock client.
197
198
Parameters are identical to AnthropicBedrock, but http_client
199
must be an httpx.AsyncClient instance if provided.
200
"""
201
202
messages: AsyncMessages
203
completions: AsyncCompletions
204
beta: AsyncBeta
205
206
def copy(
207
self,
208
*,
209
aws_secret_key: str | None = None,
210
aws_access_key: str | None = None,
211
aws_region: str | None = None,
212
aws_session_token: str | None = None,
213
base_url: str | httpx.URL | None = None,
214
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
215
http_client: httpx.AsyncClient | None = None,
216
max_retries: int | NotGiven = NOT_GIVEN,
217
default_headers: Mapping[str, str] | None = None,
218
set_default_headers: Mapping[str, str] | None = None,
219
default_query: Mapping[str, object] | None = None,
220
set_default_query: Mapping[str, object] | None = None,
221
) -> Self:
222
"""Create new async client instance with optional parameter overrides."""
223
224
with_options = copy
225
```
226
227
#### Usage Examples
228
229
**Async Bedrock usage:**
230
231
```python
232
import asyncio
233
from anthropic import AsyncAnthropicBedrock
234
235
async def main():
236
client = AsyncAnthropicBedrock(aws_region="us-east-1")
237
238
message = await client.messages.create(
239
model="claude-3-5-sonnet-20241022",
240
max_tokens=1024,
241
messages=[{"role": "user", "content": "Hello async!"}],
242
)
243
print(message.content[0].text)
244
245
asyncio.run(main())
246
```
247
248
**Concurrent requests:**
249
250
```python
251
import asyncio
252
from anthropic import AsyncAnthropicBedrock
253
254
async def process_batch(prompts: list[str]):
255
client = AsyncAnthropicBedrock(aws_region="us-east-1")
256
257
async def process_one(prompt: str):
258
return await client.messages.create(
259
model="claude-3-5-sonnet-20241022",
260
max_tokens=1024,
261
messages=[{"role": "user", "content": prompt}],
262
)
263
264
# Process all prompts concurrently
265
results = await asyncio.gather(*[process_one(p) for p in prompts])
266
return results
267
268
prompts = ["What is AI?", "Explain quantum computing", "Define blockchain"]
269
results = asyncio.run(process_batch(prompts))
270
```
271
272
### Google Vertex AI Client
273
274
Synchronous client for accessing Anthropic models through Google Cloud Vertex AI with service account authentication.
275
276
```python { .api }
277
class AnthropicVertex:
278
"""
279
Client for using Anthropic models through Google Vertex AI.
280
281
Supports authentication via Google Cloud credentials, access tokens,
282
or Application Default Credentials (ADC).
283
"""
284
285
def __init__(
286
self,
287
*,
288
region: str | NotGiven = NOT_GIVEN,
289
project_id: str | NotGiven = NOT_GIVEN,
290
access_token: str | None = None,
291
credentials: GoogleCredentials | None = None,
292
base_url: str | httpx.URL | None = None,
293
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
294
max_retries: int = DEFAULT_MAX_RETRIES,
295
default_headers: Mapping[str, str] | None = None,
296
default_query: Mapping[str, object] | None = None,
297
http_client: httpx.Client | None = None,
298
_strict_response_validation: bool = False,
299
) -> None:
300
"""
301
Initialize Google Vertex AI client.
302
303
Parameters:
304
- region: GCP region (required, or set CLOUD_ML_REGION env var)
305
- project_id: GCP project ID (defaults to ANTHROPIC_VERTEX_PROJECT_ID env var)
306
- access_token: Pre-generated access token
307
- credentials: Google credentials object (from google.auth)
308
- base_url: Override base URL (defaults to {region}-aiplatform.googleapis.com)
309
- timeout: Request timeout in seconds
310
- max_retries: Maximum number of retries (default: 2)
311
- default_headers: Default headers for all requests
312
- default_query: Default query parameters for all requests
313
- http_client: Custom httpx client instance
314
- _strict_response_validation: Enable response validation
315
316
Raises:
317
- ValueError: If region is not provided via argument or CLOUD_ML_REGION env var
318
"""
319
320
messages: Messages
321
beta: Beta
322
323
def copy(
324
self,
325
*,
326
region: str | NotGiven = NOT_GIVEN,
327
project_id: str | NotGiven = NOT_GIVEN,
328
access_token: str | None = None,
329
credentials: GoogleCredentials | None = None,
330
base_url: str | httpx.URL | None = None,
331
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
332
http_client: httpx.Client | None = None,
333
max_retries: int | NotGiven = NOT_GIVEN,
334
default_headers: Mapping[str, str] | None = None,
335
set_default_headers: Mapping[str, str] | None = None,
336
default_query: Mapping[str, object] | None = None,
337
set_default_query: Mapping[str, object] | None = None,
338
) -> Self:
339
"""Create new client instance with optional parameter overrides."""
340
341
with_options = copy
342
```
343
344
#### Usage Examples
345
346
**Basic usage with Application Default Credentials:**
347
348
```python
349
from anthropic import AnthropicVertex
350
351
# Uses ADC from environment (GOOGLE_APPLICATION_CREDENTIALS)
352
client = AnthropicVertex(
353
region="us-east5",
354
project_id="my-gcp-project",
355
)
356
357
message = client.messages.create(
358
model="claude-3-5-sonnet-20241022",
359
max_tokens=1024,
360
messages=[{"role": "user", "content": "Hello from Vertex!"}],
361
)
362
print(message.content[0].text)
363
```
364
365
**Using explicit credentials:**
366
367
```python
368
from anthropic import AnthropicVertex
369
from google.oauth2 import service_account
370
371
# Load service account credentials
372
credentials = service_account.Credentials.from_service_account_file(
373
"path/to/service-account.json",
374
scopes=["https://www.googleapis.com/auth/cloud-platform"],
375
)
376
377
client = AnthropicVertex(
378
region="us-east5",
379
project_id="my-gcp-project",
380
credentials=credentials,
381
)
382
383
message = client.messages.create(
384
model="claude-3-5-sonnet-20241022",
385
max_tokens=1024,
386
messages=[{"role": "user", "content": "Authenticated request"}],
387
)
388
```
389
390
**Using access token:**
391
392
```python
393
from anthropic import AnthropicVertex
394
395
# Pre-generated access token (e.g., from gcloud auth print-access-token)
396
client = AnthropicVertex(
397
region="us-east5",
398
project_id="my-gcp-project",
399
access_token="ya29.c.b0Aaekm1...",
400
)
401
402
message = client.messages.create(
403
model="claude-3-5-sonnet-20241022",
404
max_tokens=1024,
405
messages=[{"role": "user", "content": "Token-based auth"}],
406
)
407
```
408
409
**Multi-region setup:**
410
411
```python
412
from anthropic import AnthropicVertex
413
414
# Deploy across multiple GCP regions
415
us_client = AnthropicVertex(region="us-east5", project_id="my-project")
416
eu_client = AnthropicVertex(region="europe-west4", project_id="my-project")
417
asia_client = AnthropicVertex(region="asia-southeast1", project_id="my-project")
418
419
def route_request(text: str, user_region: str):
420
client_map = {
421
"us": us_client,
422
"eu": eu_client,
423
"asia": asia_client,
424
}
425
client = client_map.get(user_region, us_client)
426
return client.messages.create(
427
model="claude-3-5-sonnet-20241022",
428
max_tokens=1024,
429
messages=[{"role": "user", "content": text}],
430
)
431
```
432
433
### Async Google Vertex AI Client
434
435
Asynchronous client for Google Vertex AI with non-blocking I/O operations.
436
437
```python { .api }
438
class AsyncAnthropicVertex:
439
"""
440
Async client for using Anthropic models through Google Vertex AI.
441
442
Provides the same functionality as AnthropicVertex but with
443
asynchronous methods for non-blocking operations.
444
"""
445
446
def __init__(
447
self,
448
*,
449
region: str | NotGiven = NOT_GIVEN,
450
project_id: str | NotGiven = NOT_GIVEN,
451
access_token: str | None = None,
452
credentials: GoogleCredentials | None = None,
453
base_url: str | httpx.URL | None = None,
454
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
455
max_retries: int = DEFAULT_MAX_RETRIES,
456
default_headers: Mapping[str, str] | None = None,
457
default_query: Mapping[str, object] | None = None,
458
http_client: httpx.AsyncClient | None = None,
459
_strict_response_validation: bool = False,
460
) -> None:
461
"""
462
Initialize async Google Vertex AI client.
463
464
Parameters are identical to AnthropicVertex, but http_client
465
must be an httpx.AsyncClient instance if provided.
466
"""
467
468
messages: AsyncMessages
469
beta: AsyncBeta
470
471
def copy(
472
self,
473
*,
474
region: str | NotGiven = NOT_GIVEN,
475
project_id: str | NotGiven = NOT_GIVEN,
476
access_token: str | None = None,
477
credentials: GoogleCredentials | None = None,
478
base_url: str | httpx.URL | None = None,
479
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
480
http_client: httpx.AsyncClient | None = None,
481
max_retries: int | NotGiven = NOT_GIVEN,
482
default_headers: Mapping[str, str] | None = None,
483
set_default_headers: Mapping[str, str] | None = None,
484
default_query: Mapping[str, object] | None = None,
485
set_default_query: Mapping[str, object] | None = None,
486
) -> Self:
487
"""Create new async client instance with optional parameter overrides."""
488
489
with_options = copy
490
```
491
492
#### Usage Examples
493
494
**Async Vertex AI usage:**
495
496
```python
497
import asyncio
498
from anthropic import AsyncAnthropicVertex
499
500
async def main():
501
client = AsyncAnthropicVertex(
502
region="us-east5",
503
project_id="my-gcp-project",
504
)
505
506
message = await client.messages.create(
507
model="claude-3-5-sonnet-20241022",
508
max_tokens=1024,
509
messages=[{"role": "user", "content": "Hello async Vertex!"}],
510
)
511
print(message.content[0].text)
512
513
asyncio.run(main())
514
```
515
516
### Azure AI Foundry Client
517
518
Synchronous client for accessing Anthropic models through Azure AI Foundry with API key or Azure AD authentication.
519
520
```python { .api }
521
class AnthropicFoundry:
522
"""
523
Client for using Anthropic models through Azure AI Foundry.
524
525
Supports authentication via API key or Azure Active Directory (Entra ID)
526
token provider. Provides Messages and Beta resources only.
527
"""
528
529
def __init__(
530
self,
531
*,
532
resource: str | None = None,
533
api_key: str | None = None,
534
azure_ad_token_provider: AzureADTokenProvider | None = None,
535
base_url: str | None = None,
536
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
537
max_retries: int = DEFAULT_MAX_RETRIES,
538
default_headers: Mapping[str, str] | None = None,
539
default_query: Mapping[str, object] | None = None,
540
http_client: httpx.Client | None = None,
541
_strict_response_validation: bool = False,
542
) -> None:
543
"""
544
Initialize Azure AI Foundry client.
545
546
Must provide either (resource) or (base_url), and either
547
(api_key) or (azure_ad_token_provider).
548
549
Parameters:
550
- resource: Foundry resource name (e.g., "my-resource" for
551
https://my-resource.services.ai.azure.com/anthropic/)
552
Defaults to ANTHROPIC_FOUNDRY_RESOURCE env var
553
- api_key: API key for authentication (defaults to ANTHROPIC_FOUNDRY_API_KEY env var)
554
- azure_ad_token_provider: Function returning Azure AD token (called on every request)
555
- base_url: Full base URL (mutually exclusive with resource)
556
Defaults to ANTHROPIC_FOUNDRY_BASE_URL env var
557
- timeout: Request timeout in seconds
558
- max_retries: Maximum number of retries (default: 2)
559
- default_headers: Default headers for all requests
560
- default_query: Default query parameters for all requests
561
- http_client: Custom httpx client instance
562
- _strict_response_validation: Enable response validation
563
564
Raises:
565
- AnthropicError: If neither api_key nor azure_ad_token_provider provided
566
- ValueError: If neither resource nor base_url provided, or both provided
567
"""
568
569
messages: MessagesFoundry
570
beta: BetaFoundry
571
models: None # Not supported in Foundry
572
573
def copy(
574
self,
575
*,
576
api_key: str | None = None,
577
azure_ad_token_provider: AzureADTokenProvider | None = None,
578
auth_token: str | None = None,
579
base_url: str | httpx.URL | None = None,
580
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
581
http_client: httpx.Client | None = None,
582
max_retries: int | NotGiven = NOT_GIVEN,
583
default_headers: Mapping[str, str] | None = None,
584
set_default_headers: Mapping[str, str] | None = None,
585
default_query: Mapping[str, object] | None = None,
586
set_default_query: Mapping[str, object] | None = None,
587
) -> Self:
588
"""Create new client instance with optional parameter overrides."""
589
590
with_options = copy
591
```
592
593
#### Usage Examples
594
595
**Basic usage with API key:**
596
597
```python
598
from anthropic import AnthropicFoundry
599
600
client = AnthropicFoundry(
601
api_key="your-api-key", # or set ANTHROPIC_FOUNDRY_API_KEY env var
602
resource="my-resource", # or set ANTHROPIC_FOUNDRY_RESOURCE env var
603
)
604
605
message = client.messages.create(
606
model="claude-3-5-sonnet-20241022",
607
max_tokens=1024,
608
messages=[{"role": "user", "content": "Hello from Azure!"}],
609
)
610
print(message.content[0].text)
611
```
612
613
**Using Azure AD (Entra ID) authentication:**
614
615
```python
616
from anthropic import AnthropicFoundry
617
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
618
619
# Create Azure AD token provider
620
credential = DefaultAzureCredential()
621
token_provider = get_bearer_token_provider(
622
credential,
623
"https://ai.azure.com/.default"
624
)
625
626
client = AnthropicFoundry(
627
azure_ad_token_provider=token_provider,
628
resource="my-resource",
629
)
630
631
message = client.messages.create(
632
model="claude-3-5-sonnet-20241022",
633
max_tokens=1024,
634
messages=[{"role": "user", "content": "Azure AD authenticated request"}],
635
)
636
print(message.content[0].text)
637
```
638
639
**Using base URL directly:**
640
641
```python
642
from anthropic import AnthropicFoundry
643
644
# Specify full base URL instead of resource
645
client = AnthropicFoundry(
646
api_key="your-api-key",
647
base_url="https://custom-endpoint.services.ai.azure.com/anthropic/",
648
)
649
650
message = client.messages.create(
651
model="claude-3-5-sonnet-20241022",
652
max_tokens=1024,
653
messages=[{"role": "user", "content": "Custom endpoint"}],
654
)
655
```
656
657
**Streaming with Foundry:**
658
659
```python
660
from anthropic import AnthropicFoundry
661
662
client = AnthropicFoundry(
663
api_key="your-api-key",
664
resource="my-resource",
665
)
666
667
with client.messages.stream(
668
model="claude-3-5-sonnet-20241022",
669
max_tokens=1024,
670
messages=[{"role": "user", "content": "Write a haiku about Azure"}],
671
) as stream:
672
for text in stream.text_stream:
673
print(text, end="", flush=True)
674
```
675
676
### Async Azure AI Foundry Client
677
678
Asynchronous client for Azure AI Foundry with non-blocking I/O operations.
679
680
```python { .api }
681
class AsyncAnthropicFoundry:
682
"""
683
Async client for using Anthropic models through Azure AI Foundry.
684
685
Provides the same functionality as AnthropicFoundry but with
686
asynchronous methods for non-blocking operations. Supports both
687
synchronous and asynchronous token providers.
688
"""
689
690
def __init__(
691
self,
692
*,
693
resource: str | None = None,
694
api_key: str | None = None,
695
azure_ad_token_provider: AsyncAzureADTokenProvider | None = None,
696
base_url: str | None = None,
697
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
698
max_retries: int = DEFAULT_MAX_RETRIES,
699
default_headers: Mapping[str, str] | None = None,
700
default_query: Mapping[str, object] | None = None,
701
http_client: httpx.AsyncClient | None = None,
702
_strict_response_validation: bool = False,
703
) -> None:
704
"""
705
Initialize async Azure AI Foundry client.
706
707
Parameters are identical to AnthropicFoundry, except:
708
- azure_ad_token_provider: Can return str or Awaitable[str]
709
- http_client: Must be an httpx.AsyncClient instance if provided
710
"""
711
712
messages: AsyncMessagesFoundry
713
beta: AsyncBetaFoundry
714
models: None # Not supported in Foundry
715
716
def copy(
717
self,
718
*,
719
api_key: str | None = None,
720
azure_ad_token_provider: AsyncAzureADTokenProvider | None = None,
721
auth_token: str | None = None,
722
base_url: str | httpx.URL | None = None,
723
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
724
http_client: httpx.AsyncClient | None = None,
725
max_retries: int | NotGiven = NOT_GIVEN,
726
default_headers: Mapping[str, str] | None = None,
727
set_default_headers: Mapping[str, str] | None = None,
728
default_query: Mapping[str, object] | None = None,
729
set_default_query: Mapping[str, object] | None = None,
730
) -> Self:
731
"""Create new async client instance with optional parameter overrides."""
732
733
with_options = copy
734
```
735
736
#### Usage Examples
737
738
**Async Foundry usage:**
739
740
```python
741
import asyncio
742
from anthropic import AsyncAnthropicFoundry
743
744
async def main():
745
client = AsyncAnthropicFoundry(
746
api_key="your-api-key",
747
resource="my-resource",
748
)
749
750
message = await client.messages.create(
751
model="claude-3-5-sonnet-20241022",
752
max_tokens=1024,
753
messages=[{"role": "user", "content": "Hello async Azure!"}],
754
)
755
print(message.content[0].text)
756
757
asyncio.run(main())
758
```
759
760
**Async streaming:**
761
762
```python
763
import asyncio
764
from anthropic import AsyncAnthropicFoundry
765
766
async def main():
767
client = AsyncAnthropicFoundry(
768
api_key="your-api-key",
769
resource="my-resource",
770
)
771
772
async with client.messages.stream(
773
model="claude-3-5-sonnet-20241022",
774
max_tokens=1024,
775
messages=[{"role": "user", "content": "Write a haiku about async"}],
776
) as stream:
777
async for text in stream.text_stream:
778
print(text, end="", flush=True)
779
780
asyncio.run(main())
781
```
782
783
**Async Azure AD token provider:**
784
785
```python
786
import asyncio
787
from anthropic import AsyncAnthropicFoundry
788
from azure.identity.aio import DefaultAzureCredential
789
790
async def get_token() -> str:
791
"""Async token provider."""
792
credential = DefaultAzureCredential()
793
token = await credential.get_token("https://ai.azure.com/.default")
794
return token.token
795
796
async def main():
797
client = AsyncAnthropicFoundry(
798
azure_ad_token_provider=get_token,
799
resource="my-resource",
800
)
801
802
message = await client.messages.create(
803
model="claude-3-5-sonnet-20241022",
804
max_tokens=1024,
805
messages=[{"role": "user", "content": "Async AD auth"}],
806
)
807
print(message.content[0].text)
808
809
asyncio.run(main())
810
```
811
812
## Authentication Methods
813
814
### AWS Bedrock Authentication
815
816
AWS Bedrock clients use AWS SigV4 request signing with multiple credential sources:
817
818
**1. Explicit credentials:**
819
```python
820
client = AnthropicBedrock(
821
aws_access_key="AKIAIOSFODNN7EXAMPLE",
822
aws_secret_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
823
aws_region="us-east-1",
824
)
825
```
826
827
**2. AWS profile:**
828
```python
829
# Uses credentials from ~/.aws/credentials
830
client = AnthropicBedrock(
831
aws_profile="production",
832
aws_region="us-east-1",
833
)
834
```
835
836
**3. Environment variables:**
837
```python
838
# Set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION
839
client = AnthropicBedrock() # Automatically reads from environment
840
```
841
842
**4. Session tokens (temporary credentials):**
843
```python
844
client = AnthropicBedrock(
845
aws_access_key="...",
846
aws_secret_key="...",
847
aws_session_token="...", # For temporary credentials
848
aws_region="us-east-1",
849
)
850
```
851
852
**5. Boto3 session (fallback):**
853
```python
854
# If boto3 is installed, falls back to boto3.Session credentials
855
import boto3
856
session = boto3.Session(profile_name="production")
857
# AnthropicBedrock will use session credentials automatically
858
```
859
860
### Google Vertex AI Authentication
861
862
Vertex AI clients support multiple Google Cloud authentication methods:
863
864
**1. Application Default Credentials (ADC):**
865
```python
866
# Set GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
867
client = AnthropicVertex(
868
region="us-east5",
869
project_id="my-gcp-project",
870
)
871
```
872
873
**2. Explicit credentials:**
874
```python
875
from google.oauth2 import service_account
876
877
credentials = service_account.Credentials.from_service_account_file(
878
"path/to/service-account.json",
879
scopes=["https://www.googleapis.com/auth/cloud-platform"],
880
)
881
882
client = AnthropicVertex(
883
region="us-east5",
884
project_id="my-gcp-project",
885
credentials=credentials,
886
)
887
```
888
889
**3. Access token:**
890
```python
891
# Pre-generated token (e.g., from gcloud auth print-access-token)
892
client = AnthropicVertex(
893
region="us-east5",
894
project_id="my-gcp-project",
895
access_token="ya29.c.b0Aaekm1...",
896
)
897
```
898
899
**4. Environment variables:**
900
```python
901
# Set CLOUD_ML_REGION and ANTHROPIC_VERTEX_PROJECT_ID
902
client = AnthropicVertex() # Automatically reads from environment
903
```
904
905
### Azure AI Foundry Authentication
906
907
Foundry clients support two authentication methods:
908
909
**1. API key:**
910
```python
911
client = AnthropicFoundry(
912
api_key="your-api-key", # or ANTHROPIC_FOUNDRY_API_KEY env var
913
resource="my-resource",
914
)
915
```
916
917
**2. Azure AD (Entra ID) token provider:**
918
```python
919
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
920
921
credential = DefaultAzureCredential()
922
token_provider = get_bearer_token_provider(
923
credential,
924
"https://ai.azure.com/.default"
925
)
926
927
client = AnthropicFoundry(
928
azure_ad_token_provider=token_provider,
929
resource="my-resource",
930
)
931
```
932
933
API key and Azure AD token provider are mutually exclusive - only one can be provided.
934
935
## Available Resources
936
937
### AWS Bedrock Resources
938
939
```python
940
client = AnthropicBedrock(aws_region="us-east-1")
941
942
# Available resources
943
client.messages # Messages API (create, stream, count_tokens)
944
client.completions # Legacy completions API
945
client.beta # Beta features (messages, models)
946
```
947
948
**Limitations:**
949
- Message batches API not supported
950
- Token counting not supported
951
952
### Google Vertex AI Resources
953
954
```python
955
client = AnthropicVertex(region="us-east5", project_id="my-project")
956
957
# Available resources
958
client.messages # Messages API (create, stream, count_tokens)
959
client.beta # Beta features (messages, models)
960
```
961
962
**Limitations:**
963
- Legacy completions API not available
964
- Message batches API not supported
965
- Models listing not available
966
967
### Azure AI Foundry Resources
968
969
```python
970
client = AnthropicFoundry(api_key="...", resource="my-resource")
971
972
# Available resources
973
client.messages # Messages API (create, stream) - no batches
974
client.beta # Beta features (messages) - no batches
975
```
976
977
**Limitations:**
978
- Message batches API not supported (client.messages.batches returns None)
979
- Models API not supported (client.models returns None)
980
- Legacy completions API not available
981
- Token counting available through messages.count_tokens
982
983
## Platform-Specific Limitations
984
985
### AWS Bedrock
986
987
**Not supported:**
988
- `client.messages.batches.*` - Batch API endpoints
989
- `client.messages.count_tokens()` - Token counting
990
- Raises `AnthropicError` when attempting to use these features
991
992
**Region availability:**
993
- Defaults to `us-east-1` if not specified
994
- Check AWS documentation for Claude model availability by region
995
996
### Google Vertex AI
997
998
**Not supported:**
999
- `client.completions.*` - Legacy completions API
1000
- `client.messages.batches.*` - Batch API endpoints
1001
- `client.models.list()` / `client.models.retrieve()` - Models listing
1002
- Raises `AnthropicError` when attempting to use batch API
1003
1004
**Supported:**
1005
- Token counting via `client.messages.count_tokens()`
1006
1007
**Region format:**
1008
- Use region identifiers like `us-east5`, `europe-west4`, `asia-southeast1`
1009
- Or `"global"` for global endpoint
1010
1011
**Project ID:**
1012
- Required for API calls
1013
- Can be inferred from credentials or set explicitly
1014
1015
### Azure AI Foundry
1016
1017
**Not supported:**
1018
- `client.models` - Returns `None`, models API not available
1019
- `client.messages.batches` - Returns `None`, batches not supported
1020
- `client.beta.messages.batches` - Returns `None`, batches not supported
1021
- `client.completions.*` - Legacy completions API
1022
1023
**Supported:**
1024
- All standard Messages API operations (create, stream)
1025
- Token counting via `client.messages.count_tokens()`
1026
- Beta features (extended thinking, etc.)
1027
1028
**Resource format:**
1029
- Resource name: `"my-resource"` becomes `https://my-resource.services.ai.azure.com/anthropic/`
1030
- Or provide full `base_url` directly
1031
1032
## Error Handling
1033
1034
All platform clients raise the same exception types as the standard Anthropic client:
1035
1036
```python
1037
from anthropic import (
1038
AnthropicError, # Base exception
1039
APIError, # API-related errors
1040
APIStatusError, # HTTP status errors
1041
BadRequestError, # 400 errors
1042
AuthenticationError, # 401 errors
1043
PermissionDeniedError, # 403 errors
1044
NotFoundError, # 404 errors
1045
RateLimitError, # 429 errors
1046
InternalServerError, # 5xx errors
1047
)
1048
```
1049
1050
**Platform-specific errors:**
1051
1052
```python
1053
from anthropic import AnthropicBedrock, AnthropicError
1054
1055
client = AnthropicBedrock(aws_region="us-east-1")
1056
1057
try:
1058
# Attempting unsupported operation
1059
batch = client.messages.batches.create(requests=[...])
1060
except AnthropicError as e:
1061
print(f"Feature not supported: {e}")
1062
# "The Batch API is not supported in Bedrock yet"
1063
```
1064
1065
**Authentication errors:**
1066
1067
```python
1068
from anthropic import AnthropicVertex, AuthenticationError
1069
1070
try:
1071
client = AnthropicVertex(
1072
region="us-east5",
1073
project_id="my-project",
1074
access_token="invalid-token",
1075
)
1076
message = client.messages.create(...)
1077
except AuthenticationError as e:
1078
print(f"Authentication failed: {e}")
1079
# Check credentials and permissions
1080
```
1081
1082
## Types
1083
1084
```python { .api }
1085
from typing import Callable, Awaitable
1086
1087
# AWS Bedrock - no special types, uses standard AWS credentials
1088
1089
# Google Vertex AI
1090
from google.auth.credentials import Credentials as GoogleCredentials
1091
1092
# Azure AI Foundry
1093
AzureADTokenProvider = Callable[[], str]
1094
AsyncAzureADTokenProvider = Callable[[], str | Awaitable[str]]
1095
1096
# Standard types from main client
1097
from anthropic import (
1098
NOT_GIVEN,
1099
NotGiven,
1100
Timeout,
1101
DEFAULT_MAX_RETRIES,
1102
)
1103
1104
# HTTP client types
1105
import httpx
1106
```
1107
1108
## Import Paths
1109
1110
```python
1111
# AWS Bedrock clients
1112
from anthropic import AnthropicBedrock, AsyncAnthropicBedrock
1113
1114
# Google Vertex AI clients
1115
from anthropic import AnthropicVertex, AsyncAnthropicVertex
1116
1117
# Azure AI Foundry clients
1118
from anthropic import AnthropicFoundry, AsyncAnthropicFoundry
1119
1120
# Alternative import paths (equivalent)
1121
from anthropic.lib.bedrock import AnthropicBedrock, AsyncAnthropicBedrock
1122
from anthropic.lib.vertex import AnthropicVertex, AsyncAnthropicVertex
1123
from anthropic.lib.foundry import AnthropicFoundry, AsyncAnthropicFoundry
1124
```
1125