Retry failed API calls with bounded backoff and explicit success criteria
Retry failed API requests by classifying the failure, separating retryable transport errors from permanent request errors, and replaying only safe requests within a fixed time budget. A successful recovery means the application either receives one valid response, preserves the original request for inspection, or exits with a clear non-retryable error without duplicating side effects. This approach applies when an application calls a language-model endpoint through the MiniMax Open Platform and receives a timeout, connection failure, rate-limit response, or temporary server error. It does not apply to malformed authentication, invalid parameters, or requests whose effects cannot safely be repeated. The most common retry mistake is treating every failure as temporary: a client can turn one bad request into many identical failures while increasing load and delaying the real diagnosis. A more controlled approach begins with a minimal input contract, defines what each attempt must preserve, and makes the completion condition explicit before the first request is sent. It then uses bounded exponential backoff with jitter, respects server guidance when present, and records enough context to determine whether the next failure is a transport problem, a capacity problem, or an application error. The rest of this guide separates those branches, shows when replay is appropriate, defines the smallest recovery configuration, and identifies the evidence to collect when automatic recovery is no longer useful.
Other candidates to compare
DeepSeek API and Alibaba Cloud Model Studio are alternative candidates for text-model integration. Compare them with MiniMax API using the same tasks and output requirements, recording quality, latency, failures and actual cost. Check structured outputs, tool calling, rate limits and data terms separately; compatible request formats do not imply identical capabilities or contractual terms.
1. Define the minimum retry contract
Start with four inputs that make the recovery behavior measurable:
- The logical request: The prompt, generation parameters, and any supplied materials needed to produce the intended result.
- The expected result: The response content and fields the application requires before it can mark the operation complete.
- The time budget: The maximum total elapsed time, including every attempt and delay.
- The completion rule: The observable condition that ends the sequence, whether that is a valid response, a classified non-retryable error, or exhaustion of the budget. These inputs prevent a common failure mode: an application retries until it receives any HTTP response, even when that response proves the request is invalid. For example, a request that returns
401will not become valid merely because the same credential is sent again. By contrast, a connection reset may leave the server uncertain about whether it processed the request, which makes the effect of replaying it more complicated than its status code alone suggests. The expected result also prevents ambiguous success. If the application needs structured output, a valid response body is not enough; the content must satisfy the required schema or parsing rules. If the result triggers a downstream action, the completion rule may need to include a deduplication check rather than merely the presence of generated text. A practical minimal policy can be expressed without product-specific parameters: make a small number of attempts, use increasing delays with random variation, and stop at the earliest definitive outcome. Numbers such as attempt limits, per-attempt timeouts, and delay caps are application decisions rather than universal API guarantees. They should reflect the latency that the calling workflow can tolerate and the cost of repeating the operation. ## 2. Classify the failure before scheduling a retry Read the signal in this order: transport state, HTTP status, response headers, and then response content. The branch determines whether another attempt is appropriate.
| Signal | Likely interpretation | Default action |
|---|---|---|
| Connection refused, reset, DNS failure, or timeout before a complete response | Transport or connectivity failure | Retry only if replay is safe and the time budget remains |
429 | Rate limiting or temporary capacity constraint | Respect Retry-After when provided; otherwise use bounded backoff with jitter |
500, 502, 503, or 504 | Temporary server-side failure | Retry within the remaining budget if the operation is replay-safe |
401 or 403 | Authentication or authorization failure | Do not retry unchanged; check credentials, scope, and access |
400, 404, or another documented client error | Request, route, parameter, or model reference problem | Correct the request before retrying |
| A response received but application validation fails | Transport succeeded, but the result does not meet the required outcome | Decide separately whether regeneration is allowed and useful |
| Context length or input-policy rejection | The supplied material is incompatible with the request | Reduce, transform, or replace the input rather than replaying it |
Status codes should be interpreted alongside the endpoint documentation and the actual response body. A particular code can indicate a temporary condition in one deployment and a configuration problem in another. The useful distinction is not the label alone; it is whether the same request can reasonably succeed later without changing the request or repeating an unsafe side effect.
After integrating and authenticating with the MiniMax Open Platform, make the appropriate API call with the prepared input and expected output constraints, then check the response and error fields against the retry decision.
3. Replay only when the operation is safe
A request is naturally replay-safe when repeating it has no additional effect beyond producing another model response. It is not automatically safe when the application turns the response into an external action, such as creating a record, sending a notification, charging a customer, or submitting a workflow.
Network failures create an ambiguity: the client may see a timeout after the service accepted the request. Replaying it may duplicate work even though the client never received a successful response. Use the following decision sequence:
- Determine whether the request has an observable side effect. If it only reads or generates content, replay is usually straightforward. If it triggers an action, identify how that action is recorded.
- Check for a documented idempotency mechanism. Use the endpoint’s supported request identifier or idempotency facility if the official documentation defines one. Do not invent a custom header and assume the service honors it.
- Use application-side deduplication when the API does not provide it. Generate a stable logical-operation identifier before sending, store it with the request state, and check whether the corresponding action already completed before replaying.
- Separate generation from execution. If possible, obtain and validate the model response first, then commit the downstream action through an operation that has its own duplicate protection. The same requirement applies to streaming responses. A connection can fail after several chunks have already been received. If the application accumulates partial output and then retries, it must discard or clearly mark the incomplete attempt rather than combine fragments from different responses. ## 4. Use bounded backoff rather than immediate repetition When several callers hit the same temporary condition, immediate retries create a synchronized surge. The safer pattern is exponential backoff with jitter: each new delay increases up to a cap, and random variation prevents every client from retrying at the same instant. An operational delay sequence can be calculated as follows:
delay = random(0, min(cap, base × 2^attempt))Here,baseis the initial delay,attemptstarts at zero for the first retry, andcapis the maximum permitted delay. These are policy values, not documented MiniMax limits. The calculation should remain inside the operation’s total deadline. If the next possible delay would exceed that deadline, stop instead of scheduling a final attempt with no realistic chance of completing in time. When a response includes a usableRetry-Afterheader, treat it as server guidance rather than replacing it with a local formula. The application still needs its own maximum delay and total-attempt ceiling so that a header cannot extend the operation indefinitely. If the header is absent, malformed, or incompatible with the remaining budget, fall back to the local policy. Do not count a definitive client error as a retry attempt that should be hidden. It is a completed diagnostic branch: the request was understood, and the server rejected it. Similarly, an application-level validation failure should not trigger transport retries unless the application deliberately initiates a separate regeneration policy. ## 5. Set three independent limits A robust client usually needs separate limits for attempt timeout, total elapsed time, and retry count. - Attempt timeout: The maximum time allowed for one HTTP exchange. It should leave enough of the total budget for delays and later attempts.
- Total deadline: The maximum wall-clock duration for the logical operation. This prevents “a few” retries from extending a user request indefinitely.
- Attempt ceiling: The maximum number of transmissions, including the initial request or not, depending on the application’s terminology. Define the count explicitly so logs and tests agree. These limits must be coordinated. For example, four attempts with a long timeout each may still violate a short total deadline. Conversely, many very short attempts may fail before a transient server-side interruption has time to clear. The correct values depend on the calling workflow, not on the model category alone. A minimal client policy could use a small attempt ceiling, a per-attempt timeout shorter than the total deadline, and a delay cap that leaves room for the final permitted attempt. It should also stop immediately on a permanent error. If no response succeeds before the deadline, return a structured failure that identifies whether the cause was timeout, rate limiting, server failure, or invalid request. ## 6. Preserve the request and classify the result Every attempt should carry the same logical inputs. Changing the prompt, material, generation parameters, or model identifier during an automatic retry changes the experiment and may invalidate a comparison between attempts. For each attempt, record:
- A correlation identifier and the logical-operation identifier.
- The attempt number and elapsed time.
- The transport outcome or HTTP status.
- Whether a response body was received and whether it passed the application’s expected-output check.
- The selected delay and the reason for stopping. Avoid storing API credentials or unnecessary sensitive prompt content in logs. A redacted request summary is usually enough to distinguish “same request, different transport result” from “request payload changed.” If the response includes a request identifier, preserve it because it can help connect the client-side attempt with the provider-side record. Do not infer success from response length or the presence of text alone. Parse the response according to the endpoint contract, then apply the expected-result check defined in the retry contract. If parsing fails, keep that failure separate from a transport error: the service may have answered successfully while the application failed to process the answer. ## 7. Apply the smallest fix and verify the recovery path The smallest fix should match the failure branch:
- For a timeout, reduce the per-attempt timeout only if the request can still finish within the total deadline; otherwise return the timeout failure and let the caller decide whether to submit a new operation.
- For rate limiting, increase the delay window, honour
Retry-After, reduce concurrent traffic, or route work through a queue with controlled concurrency. Repeating the same burst more quickly does not remove the limit. - For temporary server errors, keep the request unchanged, apply bounded backoff, and retain diagnostic headers and identifiers.
- For authentication or authorization errors, replace or correct the credential or required access before making a new request.
- For invalid parameters or incompatible materials, change the input according to the documented constraint, then submit a deliberately new attempt.
- For duplicate-sensitive actions, enable a documented idempotency mechanism or application-side deduplication before automatic replay. Re-verification should test the exact branch that failed, not only the happy path. Use a controlled request with the same expected-output criteria. Confirm that a transient failure leads to a delayed attempt, that a permanent failure exits immediately, and that a successful response after retry is accepted only once. Also confirm that the total deadline is respected and that the downstream effect, if any, appears once. The observable success condition is therefore precise: one accepted result, one recorded completion, and no unintended duplicate action. ## 8. Know when to stop retrying Stop automatic retries when any of these conditions applies:
- The response identifies a permanent request, authentication, authorization, or input-policy error.
- The total operation deadline would be exceeded.
- The retry ceiling has been reached.
- The endpoint or application contract does not permit safe replay.
- Repeated responses show that the request itself is incompatible with the service configuration.
- The caller has cancelled the operation or its context is no longer valid. Escalate when the failure persists across otherwise valid requests, affects multiple operation types, or cannot be explained by the captured evidence. Collect the logical-operation identifier, correlation identifiers, attempt count, status codes, selected delays, total elapsed time, redacted request metadata, and whether the response body or parsing stage failed. Include the endpoint and model identifier only when they are valid for the reader’s applicable platform region and current API configuration. Do not hand over an API key. Consult the MiniMax Open Platform plan and access information for the endpoint contract, authentication requirements, supported parameters, error details, and any documented idempotency or request-identification behavior before changing the implementation. ## 9. Boundaries Automatic retry is not a recovery strategy for every exception. It does not repair an invalid API key, missing permission, malformed JSON, an unavailable model reference, input that exceeds a documented limit, or a downstream action that has already committed. Retrying those conditions without changing the request mainly adds load and delays diagnosis. Nor should retry logic become a substitute for application validation. If generated content must meet a schema or business rule, validate it separately and decide whether regeneration is an intended product behavior. That decision needs its own limit because repeated generation can create a different cost and latency profile from transport replay. A recovery design is suitable when it has a bounded deadline, preserves one logical request, distinguishes temporary from permanent failures, protects non-idempotent actions, and exposes enough evidence to explain what happened. The next practical step is to write down the four-part retry contract, classify one recent failure with it, and adjust only the branch that matches the observed signal.