| 1 |
# /// script |
| 2 |
# requires-python = ">=3.9" |
| 3 |
# dependencies = [ |
| 4 |
# "anthropic[google_cloud]", |
| 5 |
# ] |
| 6 |
# |
| 7 |
# [tool.uv.sources] |
| 8 |
# anthropic = { path = "../", editable = true } |
| 9 |
# /// |
| 10 |
|
| 11 |
# Claude Platform on Google Cloud — the first-party Anthropic API served through Google's |
| 12 |
# gateway. Authentication uses your Google credentials (Application Default |
| 13 |
# Credentials by default), so run `gcloud auth application-default login` first. |
| 14 |
# |
| 15 |
# Configure via arguments or environment variables: |
| 16 |
# ANTHROPIC_GOOGLE_CLOUD_PROJECT GCP consumer project id |
| 17 |
# ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID Anthropic workspace id (wrkspc_...) |
| 18 |
# ANTHROPIC_GOOGLE_CLOUD_LOCATION override the GCP location (defaults to "global") |
| 19 |
# ANTHROPIC_GOOGLE_CLOUD_BASE_URL override the derived gateway URL |
| 20 |
|
| 21 |
from anthropic import AnthropicGoogleCloud |
| 22 |
|
| 23 |
client = AnthropicGoogleCloud( |
| 24 |
project="your-gcp-project", # or ANTHROPIC_GOOGLE_CLOUD_PROJECT |
| 25 |
workspace_id="wrkspc_...", # or ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID |
| 26 |
# `location` is optional and defaults to "global". |
| 27 |
# Credentials default to ADC; pass `token_provider=...`, `credentials=...`, or |
| 28 |
# `access_token=...` to override. |
| 29 |
) |
| 30 |
|
| 31 |
message = client.messages.create( |
| 32 |
model="claude-haiku-4-5", |
| 33 |
max_tokens=1024, |
| 34 |
messages=[{"role": "user", "content": "Hello!"}], |
| 35 |
) |
| 36 |
|
| 37 |
print(message.to_json()) |