How to Retry Safely When an Agent Times Out
In this article
This guide explains how to retry a timed-out AI agent request without causing duplicate work, data corruption, or wasted API calls. You’ll learn to check whether the previous attempt actually completed, design idempotent retries, and apply backoff strategies that reduce server load. You’ll need access to your agent’s logs, the ability to modify its code or configuration, and a basic understanding of HTTP status codes.
What You'll Need
- Access to the agent’s execution logs (e.g., server logs, cloud console, or local output)
- Ability to modify the agent’s code or configuration (e.g., a retry library, environment variables)
- Knowledge of whether your agent’s operations are idempotent (safe to repeat)
- A way to test the retry in a staging environment if possible
Step-by-Step
- Confirm the timeout is real. Check the agent’s logs for the exact error. A timeout may be reported by the client, but the server might still be processing. Look for a 200 OK, 202 Accepted, or a completion record in the database after the timeout timestamp.
- Determine if the operation is idempotent. Ask: “If I run this exact same request again, will it produce the same result without side effects?” Reads, status checks, and searches are idempotent. Writes (creating a file, sending an email, charging a card) are not.
- For non-idempotent operations, add a unique request ID. Generate a UUID or use an existing transaction ID, and include it in the request headers or body. The server can store this ID and, if it sees the same ID again, return the previous result instead of re-executing.
- Before retrying, check for a partial completion. Query the database or file system for any records created by the original attempt. If a partial record exists, either roll it back or resume from where it left off, depending on your application logic.
- Implement exponential backoff with jitter. Start with a short delay (e.g., 1 second), double it after each retry (2s, 4s, 8s), and add a random jitter (e.g., ±20%) to prevent thundering herd. Cap the maximum delay (e.g., 60 seconds) and set a retry limit (e.g., 5 attempts).
- Retry only on retryable errors. Timeouts (408, 504) and network errors are retryable. Do not retry on 4xx errors like 400 (bad request) or 403 (forbidden), as they will fail again.
- Use the same request payload for retries. Do not modify the payload unless you are intentionally resuming a partial operation. Changing it could cause inconsistent results.
- Log every retry attempt. Record the attempt number, timestamp, delay used, and the response received. This helps you diagnose whether retries are working and whether the original timeout was a one-off or a systemic issue.
- Test the retry logic in a staging environment. Simulate a timeout by adding artificial delays or using a proxy that drops connections. Verify that retries do not duplicate records or corrupt data.
- Monitor and tune. After deploying, watch the retry success rate and the average time to completion. If timeouts persist, consider increasing the agent’s timeout limit or optimizing the underlying operation.
Common Mistakes
- Retrying without checking if the first attempt succeeded. This is the most common cause of duplicate actions, especially for payments and file uploads.
- Using a fixed delay without backoff. This can overload the server and cause more timeouts.
- Retrying on all errors. Some errors are permanent; retrying them wastes resources and may even lock your account.
- Not including a request ID. Without it, the server cannot deduplicate requests.
- Retrying too many times. This can turn a minor hiccup into a prolonged outage. Set a reasonable limit.
Tips
- Make your agent idempotent by design. Use natural keys (like order numbers) instead of auto-increment IDs.
- Use a retry library (e.g., Tenacity for Python, Retry for Node.js) that handles backoff and jitter for you.
- Set a timeout on the client side that is slightly longer than the server’s expected processing time.
- If the agent is calling an external API, check the API’s documentation for its recommended retry strategy.
- Consider circuit breaker patterns: if the service is down, stop retrying for a while and fail fast.
Troubleshooting
Retry still times out. Increase the backoff delay and the retry limit, but also check if the underlying service is overloaded. If it is, wait longer between retries or schedule them during off-peak hours.
Duplicate records appear. Your deduplication is not working. Ensure the request ID is being sent and that the server stores it. If you are not using a request ID, add one immediately.
The agent returns a 4xx error after a timeout. This is likely a permanent error, not a timeout. Do not retry; fix the request payload instead.
Retries are not happening at all. Check that the retry logic is actually triggered on timeout. Some libraries only retry on specific exceptions. Ensure you catch the timeout exception.
Partial data is left behind. Implement a cleanup routine that runs after a timeout, rolling back any incomplete transactions.
Frequently Asked Questions
What is the difference between a timeout and a connection error?
A timeout means the request was sent but no response was received within the allotted time. A connection error means the request could not be sent at all (e.g., DNS failure, refused connection). Both are retryable, but timeouts are more likely to have caused server-side processing.
How many times should I retry?
A common pattern is 3 to 5 attempts with exponential backoff. More than that usually does not help and can worsen the problem.
What is idempotency and why does it matter?
An idempotent operation produces the same result no matter how many times it is executed. For example, setting a status to “completed” is idempotent; incrementing a counter is not. Idempotency makes retries safe.
Can I retry a request that timed out after 30 seconds?
Yes, but first check if the server actually completed the work. If it did, retrying may duplicate the action. Use a request ID to deduplicate.
Should I retry if the agent times out due to a slow external API?
Yes, but consider increasing the timeout for that specific call instead of retrying, because the API might be consistently slow.
How do I know if my retry logic is working?
Check the logs for retry attempts and the final success. You can also add metrics to count retries and measure the success rate after retries.