Class: Anthropic::Helpers::Foundry::Client

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

Overview

Client for Claude in Microsoft Foundry, which serves the Claude API under https://{resource}.services.ai.azure.com/anthropic/.

The model in a request is the name of your deployment, which defaults to the model ID. Foundry does not serve the Models API or the Message Batches API. Files and Skills need a deployment hosted on Anthropic.

Constant Summary collapse

ENTRA_ID_SCOPE =

Scope to request for the tokens azure_ad_token_provider returns.

Returns:

  • (String)
"https://ai.azure.com/.default"

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, #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(resource: nil, api_key: nil, azure_ad_token_provider: nil, base_url: nil, 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 and returns a new client for interacting with Claude in Microsoft Foundry.

Authentication is either an API key, sent as x-api-key, or a Microsoft Entra ID token from azure_ad_token_provider, sent as a bearer token.

Parameters:

  • resource (String, nil) (defaults to: nil) —

    Foundry resource name, e.g. "example-resource" for https://example-resource.services.ai.azure.com/anthropic/. Defaults to ENV["ANTHROPIC_FOUNDRY_RESOURCE"]. Mutually exclusive with base_url.

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

    Defaults to ENV["ANTHROPIC_FOUNDRY_API_KEY"], which is ignored when azure_ad_token_provider is given. Mutually exclusive with azure_ad_token_provider.

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

    A callable returning a Microsoft Entra ID access token for ENTRA_ID_SCOPE. Called per attempt, so it should cache the token.

  • base_url (String, nil) (defaults to: nil) —

    Override the resource-derived base URL, e.g. for a private link or proxy. Defaults to ENV["ANTHROPIC_FOUNDRY_BASE_URL"].

  • 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 Entra ID 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.

  • resource: (String, nil) (defaults to: nil)
  • api_key: (String, nil) (defaults to: nil)
  • azure_ad_token_provider: (^-> String, nil) (defaults to: nil)
  • base_url: (String, nil) (defaults to: nil)
  • 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)


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
96
97
98
99
100
101
102
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
# File 'lib/anthropic/helpers/foundry/client.rb', line 59

def initialize(
  resource: nil,
  api_key: nil,
  azure_ad_token_provider: nil,
  base_url: nil,
  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 azure_ad_token_provider
    unless api_key.to_s.strip.empty?
      raise ArgumentError.new(
        "`api_key` and `azure_ad_token_provider` are mutually exclusive; pass only one."
      )
    end
    api_key = nil
  else
    api_key = arg_or_env(api_key, "ANTHROPIC_FOUNDRY_API_KEY")
    if api_key.nil?
      raise ArgumentError.new(
        "Missing credentials. Set the `api_key` or `azure_ad_token_provider` argument, or the " \
        "`ANTHROPIC_FOUNDRY_API_KEY` environment variable."
      )
    end
  end
  @azure_ad_token_provider = azure_ad_token_provider

  @resource = arg_or_env(resource, "ANTHROPIC_FOUNDRY_RESOURCE")
  base_url = arg_or_env(base_url, "ANTHROPIC_FOUNDRY_BASE_URL")
  if base_url && @resource
    raise ArgumentError.new(
      "`base_url` (or `ANTHROPIC_FOUNDRY_BASE_URL`) and `resource` (or " \
      "`ANTHROPIC_FOUNDRY_RESOURCE`) are mutually exclusive; set only one."
    )
  end
  if base_url.nil? && @resource.nil?
    raise ArgumentError.new(
      "No endpoint was given. Set the `resource` or `base_url` argument, or the " \
      "`ANTHROPIC_FOUNDRY_RESOURCE` or `ANTHROPIC_FOUNDRY_BASE_URL` environment variable."
    )
  end
  unless @resource.nil? || @resource.match?(/\A[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\z/i)
    raise ArgumentError.new(
      "`resource` must be a Foundry resource name such as `example-resource`, not a hostname or " \
      "URL. Use `base_url` for any other endpoint."
    )
  end
  base_url ||= format(URL_TEMPLATE, resource: @resource)

  super(
    api_key: api_key,
    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
  )

  @beta.define_singleton_method(:models) do
    raise NotImplementedError.new("The Models API is not supported on Microsoft Foundry.")
  end
  [@messages, @beta.messages].each do |messages|
    messages.define_singleton_method(:batches) do
      raise NotImplementedError.new("The Message Batches API is not supported on Microsoft Foundry.")
    end
  end
end

Instance Attribute Details

#resource ⇒ String? (readonly)

Returns:

  • (String, nil)


21
22
23
# File 'lib/anthropic/helpers/foundry/client.rb', line 21

def resource
  @resource
end

Instance Method Details

#models ⇒ bot

Returns:

  • (bot)

Raises:

  • (NotImplementedError)


133
134
135
# File 'lib/anthropic/helpers/foundry/client.rb', line 133

def models
  raise NotImplementedError.new("The Models API is not supported on Microsoft Foundry.")
end