Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle API errors when the token has issues (invalid or low credits) #29

Open
ignacio-chiazzo opened this issue Oct 9, 2024 · 0 comments

Comments

@ignacio-chiazzo
Copy link

Describe the bug
When the API token has issues, the gem raises a Faraday error. This can happen for several reasons, such as an invalid API token or low credit, etc.

To Reproduce

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "anthropic", path: "../"
end

require 'anthropic'


# secret key
client = Anthropic::Client.new(access_token: "invalid-key")

binding.pry
response = client.messages(
  parameters: {
    model: "claude-3-sonnet-20240229", 
    system: "Respond only in Spanish.",
    messages: [
      {"role": "user", "content": "Hello, Claude!"}
    ],
    max_tokens: 1000
  }
)

Expected behavior
A defined error is raised.

actual behavior
Faraday raises an error:

from (pry):3:in `on_complete'
gems/faraday-2.12.0/lib/faraday/response/raise_error.rb:31:in `on_complete': the server responded with status 400 (Faraday::BadRequestError)"
Screenshot 2024-10-09 at 4 36 05 PM

I inspected the API response and got the following error:

" @status=400 @reason_phrase="Bad Request" @response_body={"type"=>"error", "error"=>{"type"=>"invalid_request_error", "message"=>"Your credit balance is too low to access the Anthropic API. Please go to Plans & Billing to upgrade or purchase credits."}}>>

If you're okay with API consumers catching the API errors (4XX), I will close this issue and update the README. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant