Retries
Verified : Automated tests passed for: Configuration files: The configuration files on this page were checked against their schema. End-to-end run: Every step ran in a fresh test environment and the results were checked.Configure automatic retry attempts for failed backend requests.
Attaches to:
Note
Agentgateway supports more than one configuration style. Where a feature can also be configured in the simplified llm or mcp modes, the examples on this page show each option in tabs. For more information, see Routing-based configuration.
When a backend request fails, agentgateway can be configured to retryRetryA resiliency feature that automatically resends failed requests to backends. Retries can be configured with backoff strategies and specific conditions for when to retry. the request. Keep in mind that each retry goes to the same route backend as the original request. For LLM traffic, if you also want to configure failover so that an endpoint is marked as unhealthy and a request tried with a different endpoint, use failover routing with virtual models.
Configure retries
Add a retry policy to an MCP listener, an LLM gateway, or a route. If you omit attempts, a failed request is retried once. To disable retries, set attempts to 0.
# yaml-language-server: $schema=https://agentgateway.dev/schema/config
mcp:
port: 3000
policies:
retry:
# Maximum number of retries after the original request.
# Note: A value of 3 allows up to 3 retries after the initial attempt.
attempts: 3
# Optional; if set, a delay between each additional attempt
backoff: 500ms
# A list of HTTP response codes to consider retry-able.
# In addition, retries are always permitted if the request to a backend was never started.
codes: [429, 500, 503]
targets:
- name: everything
stdio:
cmd: npx
args: ["@modelcontextprotocol/server-everything"]Retry requests with large bodies
When a request has retries enabled and an HTTP body, the request body is buffered so that it can be resent.
If the body is larger than the frontend buffer limit, the request is not retried. The default limit is 2 MiB or 32 MiB for LLM requests.
To retry larger requests, raise the limit in the frontendPolicies.http.maxBufferSize field.
# yaml-language-server: $schema=https://agentgateway.dev/schema/config
frontendPolicies:
http:
# Maximum request or response body size to buffer, in bytes.
maxBufferSize: 10485760Choose which requests and responses are retried
By default, a response is retried when its status code is in codes. Two optional CEL expressions refine the retry decision.
preconditionis evaluated on the request before the request is sent. When the expression isfalse, the request is sent once and never retried. Use apreconditionto retry only idempotent requests, such asGETrequests.conditionis evaluated on each response. A response is retried when the status code is incodesor when theconditionexpression istrue.
# yaml-language-server: $schema=https://agentgateway.dev/schema/config
gateways:
default:
port: 3000
routes:
- policies:
retry:
attempts: 2
backoff: 200ms
codes: [503]
# Optional; a CEL expression on the request. When false, the request is never retried.
precondition: 'request.method == "GET"'
# Optional; a CEL expression on each response. When true, the response is also retried.
condition: 'response.headers["x-retry"] == "true"'
backends:
- host: localhost:8080With this configuration, a GET request that gets a 503 response reaches the backend three times. The three calls are the original request and two retries, 200 milliseconds apart. A POST request that gets a 503 response reaches the backend once. For a GET request, a 200 response with an x-retry: true header is retried the same way as a 503 response.
Each retry carries an x-retry-attempt header with the retry number, starting at 1. The original request has no x-retry-attempt header. The backend can use the header to tell a retry from a new request.