Class: Anthropic::Client
- Inherits:
-
Internal::Transport::BaseClient
- Object
- Internal::Transport::BaseClient
- Anthropic::Client
- Defined in:
- lib/anthropic/client.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_MAX_RETRIES =
Default max number of retries to attempt after a failed retryable request.
2
- DEFAULT_TIMEOUT_IN_SECONDS =
Default per-request timeout.
600.0
- DEFAULT_INITIAL_RETRY_DELAY =
Default initial retry delay in seconds. Overall delay is calculated using exponential backoff + jitter.
0.5
- DEFAULT_MAX_RETRY_DELAY =
Default max retry delay in seconds.
8.0
- MODEL_NONSTREAMING_TOKENS =
Models that have specific non-streaming token limits
{ "claude-opus-4-20250514": 8_192, "claude-opus-4-0": 8_192, "claude-4-opus-20250514": 8_192, "anthropic.claude-opus-4-20250514-v1:0": 8_192, "claude-opus-4@20250514": 8_192, "claude-opus-4-1-20250805": 8192, "anthropic.claude-opus-4-1-20250805-v1:0": 8192, "claude-opus-4-1@20250805": 8192 }.freeze
Constants inherited from Internal::Transport::BaseClient
Internal::Transport::BaseClient::MAX_REDIRECTS, Internal::Transport::BaseClient::PLATFORM_HEADERS
Instance Attribute Summary collapse
- #api_key ⇒ String? readonly
- #auth_token ⇒ String? readonly
- #beta ⇒ Anthropic::Resources::Beta readonly
- #completions ⇒ Anthropic::Resources::Completions readonly
- #messages ⇒ Anthropic::Resources::Messages readonly
- #models ⇒ Anthropic::Resources::Models readonly
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
-
#calculate_nonstreaming_timeout(max_tokens, max_nonstreaming_tokens = nil) ⇒ Float
Calculate the timeout for non-streaming requests based on token count.
-
#initialize(api_key: ENV["ANTHROPIC_API_KEY"], auth_token: ENV["ANTHROPIC_AUTH_TOKEN"], base_url: ENV["ANTHROPIC_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
constructor
Creates and returns a new client for interacting with the API.
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: ENV["ANTHROPIC_API_KEY"], auth_token: ENV["ANTHROPIC_AUTH_TOKEN"], base_url: ENV["ANTHROPIC_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 the API.
‘“api.example.com/v2/”`. Defaults to `ENV`
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 |
# File 'lib/anthropic/client.rb', line 108 def initialize( api_key: ENV["ANTHROPIC_API_KEY"], auth_token: ENV["ANTHROPIC_AUTH_TOKEN"], base_url: ENV["ANTHROPIC_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 ) base_url ||= "https://api.anthropic.com" headers = { "anthropic-version" => "2023-06-01" } @api_key = api_key&.to_s @auth_token = auth_token&.to_s super( base_url: base_url, timeout: timeout, max_retries: max_retries, initial_retry_delay: initial_retry_delay, max_retry_delay: max_retry_delay, headers: headers ) @completions = Anthropic::Resources::Completions.new(client: self) @messages = Anthropic::Resources::Messages.new(client: self) @models = Anthropic::Resources::Models.new(client: self) @beta = Anthropic::Resources::Beta.new(client: self) end |
Instance Attribute Details
#api_key ⇒ String? (readonly)
31 32 33 |
# File 'lib/anthropic/client.rb', line 31 def api_key @api_key end |
#auth_token ⇒ String? (readonly)
34 35 36 |
# File 'lib/anthropic/client.rb', line 34 def auth_token @auth_token end |
#beta ⇒ Anthropic::Resources::Beta (readonly)
46 47 48 |
# File 'lib/anthropic/client.rb', line 46 def beta @beta end |
#completions ⇒ Anthropic::Resources::Completions (readonly)
37 38 39 |
# File 'lib/anthropic/client.rb', line 37 def completions @completions end |
#messages ⇒ Anthropic::Resources::Messages (readonly)
40 41 42 |
# File 'lib/anthropic/client.rb', line 40 def @messages end |
#models ⇒ Anthropic::Resources::Models (readonly)
43 44 45 |
# File 'lib/anthropic/client.rb', line 43 def models @models end |
Instance Method Details
#calculate_nonstreaming_timeout(max_tokens, max_nonstreaming_tokens = nil) ⇒ Float
Calculate the timeout for non-streaming requests based on token count
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/anthropic/client.rb', line 77 def calculate_nonstreaming_timeout(max_tokens, max_nonstreaming_tokens = nil) maximum_time = 60 * 60 # 1 hour in seconds default_time = 60 * 10 # 10 minutes in seconds expected_time = maximum_time * max_tokens / 128_000.0 if expected_time > default_time || (max_nonstreaming_tokens && max_tokens > max_nonstreaming_tokens) raise ArgumentError.new( "Streaming is required for operations that may take longer than 10 minutes. " \ "See https://github.com/anthropics/anthropic-sdk-ruby#long-requests for more details" ) end DEFAULT_TIMEOUT_IN_SECONDS end |