Class: Anthropic::Helpers::GoogleCloud::Client

Inherits:
Client show all
Defined in:
lib/anthropic/helpers/google_cloud/client.rb,
sig/anthropic/helpers/google_cloud/client.rbs

Overview

Client for Claude Models on Google Cloud (Anthropic managed) — the first-party Anthropic API served through the Google Cloud gateway.

This is distinct from VertexClient, which targets the :rawPredict publisher-model API (publisher model IDs, messages only). This client speaks the full first-party Anthropic API: requests pass through the gateway unchanged — standard /v1/* paths, standard model names.

Constant Summary collapse

SCOPE =

OAuth2 scope required to call the gateway.

Returns:

  • (String)
"https://www.googleapis.com/auth/cloud-platform"
URL_TEMPLATE =

Gateway base URL template; override via base_url or ANTHROPIC_GOOGLE_CLOUD_BASE_URL.

Returns:

  • (String)
"#{LOCATION_URL_TEMPLATE}/workspaces/%<workspace_id>s/invoke".freeze
ORGANIZATION_URL_TEMPLATE =

Gateway base URL template for organization-level routes, which are served from a base with no workspace in it.

Returns:

  • (String)
"#{LOCATION_URL_TEMPLATE}/claudeOrganization/invoke".freeze

Constants inherited from Client

Client::DEFAULT_INITIAL_RETRY_DELAY, Client::DEFAULT_MAX_RETRIES, Client::DEFAULT_MAX_RETRY_DELAY, Client::DEFAULT_TIMEOUT_IN_SECONDS, Client::MODEL_NONSTREAMING_TOKENS

Constants inherited from Internal::Transport::BaseClient

Internal::Transport::BaseClient::MAX_REDIRECTS, Internal::Transport::BaseClient::PLATFORM_HEADERS

Instance Attribute Summary collapse

Attributes inherited from Client

#api_key, #auth_token, #beta, #credentials, #files, #messages, #models, #organization, #skills, #token_cache, #user_profiles, #webhook_key

Attributes inherited from Internal::Transport::BaseClient

#base_url, #headers, #idempotency_header, #initial_retry_delay, #max_retries, #max_retry_delay, #middleware, #open_telemetry_max_content_bytes, #requester, #timeout

Instance Method Summary collapse

Methods inherited from Client

#calculate_nonstreaming_timeout, #message_threads?, #retry_request?, #timeout_overridden?

Methods inherited from Internal::Transport::BaseClient

follow_redirect, #inspect, #open_telemetry_attributes, #open_telemetry_content?, #open_telemetry_tracer, reap_connection!, #request, #retry_request?, #send_request, should_retry?, validate!

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Constructor Details

#initialize(project: ENV["ANTHROPIC_GOOGLE_CLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"], location: ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global", workspace_id: ENV["ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"], base_url: ENV["ANTHROPIC_GOOGLE_CLOUD_BASE_URL"], google_credentials: nil, token_provider: nil, skip_auth: false, max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: nil, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY, middleware: nil, open_telemetry: nil) ⇒ Client

Creates a new client for the Anthropic API on the Google Cloud gateway.

Authentication uses Google credentials by precedence (unless skip_auth is true):

1. `token_provider` — a callable returning a GCP access token, called
 per attempt
2. `google_credentials` — a `googleauth` credentials object (anything
 responding to `#apply`), refreshed by the library as needed
3. Application Default Credentials (`Google::Auth.get_application_default`)
 with the `cloud-platform` scope

Parameters:

  • project (String, nil) (defaults to: ENV["ANTHROPIC_GOOGLE_CLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"]) —

    GCP consumer project id. Defaults to ENV["ANTHROPIC_GOOGLE_CLOUD_PROJECT"], then ENV["GOOGLE_CLOUD_PROJECT"], then the project reported by the resolved Google credentials. Only required when the base URL must be derived.

  • location (String, nil) (defaults to: ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global") —

    GCP location. Defaults to ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"], then "global"; an explicit nil falls back to the same defaults.

  • workspace_id (String, nil) (defaults to: ENV["ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"]) —

    Defaults to ENV["ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"]. Required when the base URL is derived, except for organization-level routes (/v1/organizations/ and /v1/user_profiles), which need no workspace ID.

  • base_url (String, nil) (defaults to: ENV["ANTHROPIC_GOOGLE_CLOUD_BASE_URL"]) —

    Override the gateway base URL for every route. Defaults to ENV["ANTHROPIC_GOOGLE_CLOUD_BASE_URL"], otherwise derived from project, location, and workspace_id via URL_TEMPLATE.

  • google_credentials (#apply, nil) (defaults to: nil) —

    A googleauth-compatible credentials object. Mutually exclusive with skip_auth.

  • token_provider (#call, nil) (defaults to: nil) —

    A callable returning a GCP access token string. Takes precedence over google_credentials. Mutually exclusive with skip_auth.

  • skip_auth (Boolean) (defaults to: false) —

    When true, no token is attached. For callers fronting with their own authenticated proxy. Mutually exclusive with token_provider and google_credentials.

  • max_retries (Integer) (defaults to: self.class::DEFAULT_MAX_RETRIES) —

    Max number of retries to attempt after a failed retryable request.

  • timeout (Float, nil) (defaults to: nil) —

    Request timeout in seconds. Defaults to Anthropic::Client::DEFAULT_TIMEOUT_IN_SECONDS when nil.

  • initial_retry_delay (Float) (defaults to: self.class::DEFAULT_INITIAL_RETRY_DELAY)
  • max_retry_delay (Float) (defaults to: self.class::DEFAULT_MAX_RETRY_DELAY)
  • middleware (Array<#call>, #call, nil) (defaults to: nil) —

    Per-attempt HTTP around-middleware. See Middleware. The Google bearer is applied per attempt, after user middleware.

  • open_telemetry (Hash{Symbol=>Object}, Boolean, nil) (defaults to: nil) —

    OpenTelemetry tracing of this client's API calls. See Anthropic::Client#initialize.

  • project: (String, nil) (defaults to: ENV["ANTHROPIC_GOOGLE_CLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"])
  • location: (String, nil) (defaults to: ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global")
  • workspace_id: (String, nil) (defaults to: ENV["ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"])
  • base_url: (String, nil) (defaults to: ENV["ANTHROPIC_GOOGLE_CLOUD_BASE_URL"])
  • google_credentials: (Object) (defaults to: nil)
  • token_provider: (^-> String, nil) (defaults to: nil)
  • skip_auth: (Boolean) (defaults to: false)
  • max_retries: (Integer) (defaults to: self.class::DEFAULT_MAX_RETRIES)
  • timeout: (Float, nil) (defaults to: nil)
  • initial_retry_delay: (Float) (defaults to: self.class::DEFAULT_INITIAL_RETRY_DELAY)
  • max_retry_delay: (Float) (defaults to: self.class::DEFAULT_MAX_RETRY_DELAY)
  • middleware: (Anthropic::Middleware::entry_or_array, nil) (defaults to: nil)
  • open_telemetry: (Anthropic::Internal::Tracing::client_option, nil) (defaults to: nil)

Raises:

  • (ArgumentError)


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
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 103

def initialize(
  project: ENV["ANTHROPIC_GOOGLE_CLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"],
  location: ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global",
  workspace_id: ENV["ANTHROPIC_GOOGLE_CLOUD_WORKSPACE_ID"],
  base_url: ENV["ANTHROPIC_GOOGLE_CLOUD_BASE_URL"],
  google_credentials: nil,
  token_provider: nil,
  skip_auth: false,
  max_retries: self.class::DEFAULT_MAX_RETRIES,
  timeout: nil,
  initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY,
  middleware: nil,
  open_telemetry: nil
)
  if skip_auth && (token_provider || google_credentials)
    raise ArgumentError.new(
      "`skip_auth` is mutually exclusive with `token_provider` and `google_credentials`; " \
      "`skip_auth` disables authentication entirely."
    )
  end

  @skip_auth = skip_auth
  @token_provider = token_provider
  @authorization = google_credentials

  # Resolve credentials eagerly so the client is thread-safe (no lazy
  # ADC race) and so the credentials' project can backfill `project`
  # before the base URL is derived.
  if !skip_auth && token_provider.nil? && google_credentials.nil?
    require_googleauth
    @authorization = Google::Auth.get_application_default([SCOPE])
  end

  if project.nil? && @authorization.respond_to?(:project_id)
    project = @authorization.project_id
  end
  @project = project
  # The kwarg default doesn't fire on an explicitly-passed nil — coerce
  # so nil can't reach the derived URL (matches the other SDKs).
  @location = location || ENV["ANTHROPIC_GOOGLE_CLOUD_LOCATION"] || "global"

  @workspace_id = workspace_id

  base_url = nil if base_url.to_s.empty?
  unless base_url
    @organization_base_url = URI.parse(derive_base_url(ORGANIZATION_URL_TEMPLATE))
    # Without a workspace ID there is no workspace base to derive, so
    # `base_url` is the organization base.
    base_url = @workspace_id.nil? ? @organization_base_url.to_s : derive_base_url(URL_TEMPLATE)
  end

  # No first-party credential is ever passed up, and
  # {#resolve_default_credentials?} keeps the parent from discovering
  # one; `base_url` is always resolved by this point, so the parent's
  # `ANTHROPIC_BASE_URL` fallback never applies.
  super(
    base_url: base_url,
    timeout: timeout,
    max_retries: max_retries,
    initial_retry_delay: initial_retry_delay,
    max_retry_delay: max_retry_delay,
    middleware: middleware,
    open_telemetry: open_telemetry
  )
end

Instance Attribute Details

#location ⇒ String? (readonly)

Returns:

  • (String, nil)


37
38
39
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 37

def location
  @location
end

#project ⇒ String? (readonly)

Returns:

  • (String, nil)


34
35
36
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 34

def project
  @project
end

#workspace_id ⇒ String? (readonly)

Returns:

  • (String, nil)


40
41
42
# File 'lib/anthropic/helpers/google_cloud/client.rb', line 40

def workspace_id
  @workspace_id
end