Automatically handles tool execution loop changedcli-sdks-libraries/sdks/ruby
Nearest release: v2.1.332, published 6 hours before this site recorded the change. Shown because the two are within 24 hours of each other. Nothing here says the release caused the edit.
Recorded here
Lines+8added
Lines−41removed
From line
113
where the diff opens
First seen
14 Aug 2026
this site's first read of the page
Recorded edits21to this page, all time
The whole hunk
from line 113, old and new numbered
/
from line 113
113113
114114## Handling errors
115115
116When the library is unable to connect to the API, or if the API returns a non-success status code (that is, a 4xx or 5xx response), a subclass of `Anthropic::Errors::APIError` is raised:
116The SDK raises a subclass of `Anthropic::Errors::APIError` when it can’t connect to the API or when the API returns an error response (4xx or 5xx). [SDK error types](https://platform.claude.com/docs/en/api/errors#sdk-error-types) lists the classes, and [Handle errors](https://platform.claude.com/docs/en/api/handle-errors) shows how to rescue them.
117117
118```ruby
119anthropic = Anthropic::Client.new
120begin
121 message = anthropic.messages.create(
122 max_tokens: 1024,
123 messages: [{role: "user", content: "Hello, Claude"}],
124 model: Anthropic::Model::CLAUDE_OPUS_5_5
125 )
126rescue Anthropic::Errors::APIConnectionError => e
127 puts("The server could not be reached")
128 puts(e.cause) # an underlying Exception, likely raised within `net/http`
129rescue Anthropic::Errors::RateLimitError => e
130 puts("A 429 status code was received; we should back off a bit.")
131rescue Anthropic::Errors::APIStatusError => e
132 puts("Another non-200-range status code was received")
133 puts(e.status)
134end
135```
118To see why a connection failed or timed out, read `cause` on the `Anthropic::Errors::APIConnectionError` or its subclass `APITimeoutError`. `cause` holds the underlying exception.
136119
137Error codes are as follows:
138
139| Cause | Error type |
140| ----- | ----- |
141| HTTP 400 | `BadRequestError` |
142| HTTP 401 | `AuthenticationError` |
143| HTTP 403 | `PermissionDeniedError` |
144| HTTP 404 | `NotFoundError` |
145| HTTP 409 | `ConflictError` |
146| HTTP 422 | `UnprocessableEntityError` |
147| HTTP 429 | `RateLimitError` |
148| HTTP >= 500 | `InternalServerError` |
149| Other HTTP error | `APIStatusError` |
150| Timeout | `APITimeoutError` |
151| Network error | `APIConnectionError` |
152
153120## Retries
154121
155Certain errors are automatically retried 2 times by default, with a short exponential backoff.
122By default, the SDK retries certain errors up to two times (three attempts in all). It retries connection errors, timeouts, 408 Request Timeout, 409 Conflict, 429 Too Many Requests, and 5xx server errors.
156123
157Connection errors (for example, because of a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, >=500 Internal errors, and timeouts are all retried by default.
124If a response has an `x-should-retry` header (`true` or `false`), the SDK follows it instead of the status. Between attempts, the SDK waits as long as the `retry-after` header says, or it uses a short exponential backoff.
158125
159You can use the `max_retries` option to configure or disable this:
126Use the `max_retries` option to configure or disable retries:
160127
161128```ruby
162129# Configure the default for all requests:
No line in this hunk matches that.