Classify an Error as Retryable
Problem Statement
Decide whether a failed tool call should be retried based on its HTTP-style status code. Transient failures are worth retrying; client errors are not.
Background
By convention, 429 (rate limited) and any 5xx (server error) are retryable. 4xx other than 429 are client errors and are not retryable. Anything below 400 is a success and needs no retry.
Your Task
Implement:
def is_retryable(status):
Return True if status is 429 or in [500, 599], else False.
Input Format
- status (int): the status code.
Output Format
- A boolean.
Sample
print(is_retryable(503))
Output:
True
Example:
print(is_retryable(503))
True
- The input status code is 503, which is passed to the classification logic to determine if it represents a transient failure.
- The function first checks if the status is exactly 429 (rate limited); since 503î€ =429, this condition evaluates to false.
- Next, it checks if the status falls within the server error range [500,599]; since 500≤503≤599, this condition evaluates to true.
- Because the status satisfies the server error criterion, the overall retryability check returns true, indicating the call should be retried.
- The final output is True
Constraints:
- Retryable:
status == 429or500 <= status <= 599. - All other codes are non-retryable.
- Return a bool.
1. Background Knowledge
In distributed systems and API interactions, HTTP status codes communicate the outcome of a request. Codes in the 2xx range indicate success, 3xx indicate redirection, 4xx indicate client-side errors, and 5xx indicate server-side errors. Understanding these categories is fundamental to building resilient software.
Transient failures are temporary issues that may resolve themselves if the operation is repeated. Examples include network timeouts, server overload (503 Service Unavailable), or rate limiting (429 Too Many Requests). Permanent failures, such as 404 Not Found or 401 Unauthorized, will not succeed on retry because the underlying condition (missing resource, bad credentials) remains unchanged.
In reliability engineering, a common pattern is to distinguish between these two failure types. Retrying a permanent error wastes resources and can mask bugs, while failing to retry a transient error reduces system availability. This problem models that decision logic: you must classify a status code to determine if a retry is appropriate.
2. Algorithm Approach
This is a simple conditional classification problem. The logic involves checking the input integer against specific ranges and exact values.
The decision tree is:
- Is the status exactly 429? If yes, it is retryable.
- Is the status in the range [500, 599]? If yes, it is retryable.
- Otherwise, it is not retryable.
You can implement this using standard boolean logic (and, or) or a series of if statements. Since the conditions are mutually exclusive in terms of the final boolean result (i.e., you just need to know if any retryable condition is met), a single compound boolean expression is often the most Pythonic and concise approach.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.