Class: Anthropic::Helpers::AWS::Client

Inherits:
Client show all
Includes:
Anthropic::Helpers::AWSAuth
Defined in:
lib/anthropic/helpers/aws/client.rb

Constant Summary

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, #completions, #messages, #models

Attributes inherited from Internal::Transport::BaseClient

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

Instance Method Summary collapse

Methods inherited from Client

#calculate_nonstreaming_timeout

Methods inherited from Internal::Transport::BaseClient

follow_redirect, #inspect, reap_connection!, #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(api_key: nil, aws_access_key: nil, aws_secret_access_key: nil, aws_session_token: nil, aws_profile: nil, aws_region: ENV["AWS_REGION"] || ENV["AWS_DEFAULT_REGION"], workspace_id: ENV["ANTHROPIC_AWS_WORKSPACE_ID"], skip_auth: false, base_url: ENV["ANTHROPIC_AWS_BASE_URL"], max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY) ⇒ Client

Creates and returns a new client for interacting with Anthropic models via the AWS gateway.

AWS credentials are resolved in priority order:

  1. api_key constructor arg → API key mode (x-api-key header, no SigV4)
  2. aws_access_key + aws_secret_access_key args → SigV4 with explicit credentials
  3. aws_profile arg → SigV4 with named AWS profile
  4. ANTHROPIC_AWS_API_KEY env var → API key mode (only when no platform auth args given)
  5. Default AWS credential chain (env vars → shared config → instance profile) → SigV4

When skip_auth is true, all auth is skipped and workspace_id is not required.

Parameters:

  • api_key (String, nil) (defaults to: nil)

    Anthropic API key (bypasses SigV4, uses x-api-key header)

  • aws_access_key (String, nil) (defaults to: nil)

    AWS access key ID for SigV4 signing

  • aws_secret_access_key (String, nil) (defaults to: nil)

    AWS secret access key for SigV4 signing

  • aws_session_token (String, nil) (defaults to: nil)

    Optional AWS session token for temporary credentials

  • aws_profile (String, nil) (defaults to: nil)

    AWS profile name (uses SigV4 with named profile)

  • aws_region (String, nil) (defaults to: ENV["AWS_REGION"] || ENV["AWS_DEFAULT_REGION"])

    AWS region for the gateway URL and SigV4 signing. Defaults to ENV["AWS_REGION"], then ENV["AWS_DEFAULT_REGION"]. Required in SigV4 mode.

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

    Anthropic workspace ID, sent as anthropic-workspace-id header. Defaults to ENV["ANTHROPIC_AWS_WORKSPACE_ID"]

  • skip_auth (Boolean) (defaults to: false)

    When true, skip all auth (no SigV4, no API key) and do not require workspace_id. Useful for testing or when auth is handled externally.

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

    Override the default base URL for the API. Defaults to ENV["ANTHROPIC_AWS_BASE_URL"], then https://aws-external-anthropic.{region}.api.aws.

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

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

  • timeout (Float) (defaults to: self.class::DEFAULT_TIMEOUT_IN_SECONDS)
  • initial_retry_delay (Float) (defaults to: self.class::DEFAULT_INITIAL_RETRY_DELAY)
  • max_retry_delay (Float) (defaults to: self.class::DEFAULT_MAX_RETRY_DELAY)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/anthropic/helpers/aws/client.rb', line 56

def initialize(
  api_key: nil,
  aws_access_key: nil,
  aws_secret_access_key: nil,
  aws_session_token: nil,
  aws_profile: nil,
  aws_region: ENV["AWS_REGION"] || ENV["AWS_DEFAULT_REGION"],
  workspace_id: ENV["ANTHROPIC_AWS_WORKSPACE_ID"],
  skip_auth: false,
  base_url: ENV["ANTHROPIC_AWS_BASE_URL"],
  max_retries: self.class::DEFAULT_MAX_RETRIES,
  timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
  initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY
)
  effective_api_key, resolved_base_url = setup_aws_auth(
    api_key: api_key,
    aws_access_key: aws_access_key,
    aws_secret_access_key: aws_secret_access_key,
    aws_session_token: aws_session_token,
    aws_profile: aws_profile,
    aws_region: aws_region,
    workspace_id: workspace_id,
    skip_auth: skip_auth,
    base_url: base_url,
    service_name: "aws-external-anthropic",
    env_api_key: "ANTHROPIC_AWS_API_KEY",
    env_workspace_id: "ANTHROPIC_AWS_WORKSPACE_ID",
    derive_base_url: ->(region) { "https://aws-external-anthropic.#{region}.api.aws" }
  )

  super(
    api_key: effective_api_key,
    base_url: resolved_base_url,
    max_retries: max_retries,
    timeout: timeout,
    initial_retry_delay: initial_retry_delay,
    max_retry_delay: max_retry_delay
  )
end

Instance Attribute Details

#aws_regionString? (readonly)

Returns:

  • (String, nil)


10
11
12
# File 'lib/anthropic/helpers/aws/client.rb', line 10

def aws_region
  @aws_region
end

#workspace_idString? (readonly)

Returns:

  • (String, nil)


13
14
15
# File 'lib/anthropic/helpers/aws/client.rb', line 13

def workspace_id
  @workspace_id
end