| 1 |
# Claude Platform on Google Cloud |
| 2 |
|
| 3 |
`AnthropicGoogleCloud` is a client for the full Anthropic API — Messages, Models, |
| 4 |
Batches, Files, Admin, and every beta surface — served through Google Cloud. You |
| 5 |
authenticate with Google Cloud IAM credentials, billing flows through Google |
| 6 |
Cloud Marketplace, and model strings are the same first-party identifiers used |
| 7 |
with the `Anthropic` client. |
| 8 |
|
| 9 |
This client never reads `ANTHROPIC_API_KEY`, `ANTHROPIC_AUTH_TOKEN`, or |
| 10 |
`ANTHROPIC_BASE_URL`; it authenticates with Google credentials only. |
| 11 |
|
| 12 |
> **Note:** the class name and `ANTHROPIC_GOOGLE_CLOUD_*` environment variable |
| 13 |
> names are provisional and may change before a stable release. |
| 14 |
|
| 15 |
## Installation |
| 16 |
|
| 17 |
Google auth support is an optional dependency: |
| 18 |
|
| 19 |
```bash |
| 20 |
pip install "anthropic[google_cloud]" |
| 21 |
``` |
| 22 |
|
| 23 |
## Authentication |
| 24 |
|
| 25 |
Precedence (first match wins, unless `skip_auth=True`): |
| 26 |
|
| 27 |
1. `token_provider` — a callable returning a GCP access token, invoked on every |
| 28 |
request (so it can refresh internally). On the async client it may be async or |
| 29 |
return an awaitable; sync providers are run off the event loop. |
| 30 |
2. `credentials` — a `google.auth` Credentials object, refreshed as needed. |
| 31 |
3. Application Default Credentials — discovered via `google.auth.default()`, |
| 32 |
loaded lazily on the first request and cached for the life of the client. ADC |
| 33 |
covers `gcloud auth application-default login`, `GOOGLE_APPLICATION_CREDENTIALS`, |
| 34 |
workload identity on GKE/Cloud Run, and the GCE/Cloud Functions metadata server. |
| 35 |
|
| 36 |
A `workspace_id` is required unless `skip_auth=True` with an explicit |
| 37 |
`base_url`. `skip_auth` is mutually exclusive |
| 38 |
with the credential arguments. |
| 39 |
|
| 40 |
## Usage |
| 41 |
|
| 42 |
### Application Default Credentials (recommended) |
| 43 |
|
| 44 |
Run `gcloud auth application-default login` first, then: |
| 45 |
|
| 46 |
```python |
| 47 |
from anthropic import AnthropicGoogleCloud |
| 48 |
|
| 49 |
client = AnthropicGoogleCloud( |
| 50 |
project="your-gcp-project", # or ANTHROPIC_GOOGLE_CLOUD_PROJECT |
| 51 |
location="us-central1", |
| 52 |
workspace_id="wrkspc_...", # or ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID |
| 53 |
) |
| 54 |
|
| 55 |
message = client.messages.create( |
| 56 |
model="claude-haiku-4-5", |
| 57 |
max_tokens=1024, |
| 58 |
messages=[{"role": "user", "content": "Hello!"}], |
| 59 |
) |
| 60 |
print(message.content[0].text) |
| 61 |
``` |
| 62 |
|
| 63 |
`project` may be omitted: it is taken from an explicit `credentials=` object when |
| 64 |
it exposes one (e.g. a service-account credential's `project_id`), or back-filled |
| 65 |
lazily on the first request from the project ADC resolves to (a service-account |
| 66 |
keyfile, the `GOOGLE_CLOUD_PROJECT` environment variable, or instance metadata). |
| 67 |
If no project can be resolved — plain user ADC, an explicit `token_provider`, or |
| 68 |
credentials without a project — the first request raises an error asking for |
| 69 |
`project`. |
| 70 |
|
| 71 |
### Other credential modes |
| 72 |
|
| 73 |
<details> |
| 74 |
<summary>Explicit <code>google.auth</code> credentials</summary> |
| 75 |
|
| 76 |
```python |
| 77 |
from google.oauth2 import service_account |
| 78 |
from anthropic import AnthropicGoogleCloud |
| 79 |
|
| 80 |
credentials = service_account.Credentials.from_service_account_file( |
| 81 |
"service-account.json", |
| 82 |
scopes=["https://www.googleapis.com/auth/cloud-platform"], |
| 83 |
) |
| 84 |
|
| 85 |
client = AnthropicGoogleCloud( |
| 86 |
location="us-central1", |
| 87 |
workspace_id="wrkspc_...", |
| 88 |
credentials=credentials, |
| 89 |
# project= is taken from the service account when omitted |
| 90 |
) |
| 91 |
``` |
| 92 |
|
| 93 |
</details> |
| 94 |
|
| 95 |
<details> |
| 96 |
<summary>Custom token provider</summary> |
| 97 |
|
| 98 |
Use this when you already have a token-minting layer (a sidecar, a proxy, a |
| 99 |
broker) and don't want the SDK to talk to `google.auth` at all: |
| 100 |
|
| 101 |
```python |
| 102 |
from anthropic import AnthropicGoogleCloud |
| 103 |
|
| 104 |
client = AnthropicGoogleCloud( |
| 105 |
project="your-gcp-project", |
| 106 |
location="us-central1", |
| 107 |
workspace_id="wrkspc_...", |
| 108 |
token_provider=lambda: my_token_broker.fetch(), # invoked on every request |
| 109 |
) |
| 110 |
``` |
| 111 |
|
| 112 |
On `AsyncAnthropicGoogleCloud` the provider may also be `async`. |
| 113 |
|
| 114 |
</details> |
| 115 |
|
| 116 |
### Streaming |
| 117 |
|
| 118 |
```python |
| 119 |
with client.messages.stream( |
| 120 |
model="claude-haiku-4-5", |
| 121 |
max_tokens=1024, |
| 122 |
messages=[{"role": "user", "content": "Hello!"}], |
| 123 |
) as stream: |
| 124 |
for text in stream.text_stream: |
| 125 |
print(text, end="", flush=True) |
| 126 |
``` |
| 127 |
|
| 128 |
### Async |
| 129 |
|
| 130 |
```python |
| 131 |
from anthropic import AsyncAnthropicGoogleCloud |
| 132 |
|
| 133 |
client = AsyncAnthropicGoogleCloud( |
| 134 |
project="your-gcp-project", location="us-central1", workspace_id="wrkspc_..." |
| 135 |
) |
| 136 |
|
| 137 |
message = await client.messages.create( |
| 138 |
model="claude-haiku-4-5", |
| 139 |
max_tokens=1024, |
| 140 |
messages=[{"role": "user", "content": "Hello!"}], |
| 141 |
) |
| 142 |
|
| 143 |
async with client.messages.stream( |
| 144 |
model="claude-haiku-4-5", |
| 145 |
max_tokens=1024, |
| 146 |
messages=[{"role": "user", "content": "Hello!"}], |
| 147 |
) as stream: |
| 148 |
async for text in stream.text_stream: |
| 149 |
print(text, end="", flush=True) |
| 150 |
``` |
| 151 |
|
| 152 |
## Configuration |
| 153 |
|
| 154 |
| Argument | Environment variable | Notes | |
| 155 |
|---|---|---| |
| 156 |
| `project` | `ANTHROPIC_GOOGLE_CLOUD_PROJECT` | Only needed when the base URL is derived; if omitted, back-filled from Google credentials on the first request. | |
| 157 |
| `location` | — | Required only when deriving the base URL. | |
| 158 |
| `workspace_id` | `ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID` | Required unless `skip_auth=True` with an explicit `base_url`. | |
| 159 |
| `base_url` | `ANTHROPIC_GOOGLE_CLOUD_BASE_URL` | Overrides the derived gateway URL. | |
| 160 |
| `skip_auth` | — | For pre-authenticated proxies: skips token attachment and the workspace requirement. | |