Symptoms
- Short prompts work. Long summaries, big RAG contexts or reasoning models fail with
504 Gateway Timeout,502or a connection reset, usually at a round number of seconds such as 20, 30, 60 or 240. - Large prompts, base64 images or big batch payloads fail straight away with
413 Request Entity Too Large, before the model sees them. - The model provider's logs or metrics show the request succeeded, or it never arrived at all.
- Calling the backend directly from inside the network works, and the same call through the front door fails.
An LLM call is a long, quiet HTTP request. Without streaming, nothing comes back until the last token is generated. A reasoning model can sit for minutes before the first byte. Every proxy on the path has an idle or response timeout and a request size limit, and the lowest one wins.
Find the failing hop
Time the failure
Run the same request at each layer and note the elapsed time and status code. The hop that cuts you off is the one whose timeout matches the elapsed time.
BODY=$(python3 -c 'import json;print(json.dumps({"model":"gpt-4.1","max_tokens":4000,"messages":[{"role":"user","content":"Write a 3,000 word incident review template."}]}))')
for URL in https://ai.contoso.com/openai/v1/chat/completions \
https://apim-ai.azure-api.net/openai/v1/chat/completions \
http://10.20.0.15:8000/v1/chat/completions; do
curl -s -o /dev/null -w "$URL status=%{http_code} total=%{time_total}s ttfb=%{time_starttransfer}s\n" \
-H "Content-Type: application/json" -H "api-key: $KEY" -d "$BODY" "$URL"
done
Change the URLs and auth header to match your path. ttfb close to total with a 504 means nothing came back before the proxy gave up.
Read who produced the error
The response body and headers usually name the layer that generated the error:
curl -s -D - -o body.txt -H "Content-Type: application/json" -H "api-key: $KEY" -d "$BODY" "$URL" | head -20; head -c 400 body.txt
| Clue in the response | Layer |
|---|---|
Server: nginx plus an HTML 504 or 413 page | ingress-nginx or another NGINX |
Server: Microsoft-Azure-Application-Gateway/v2 | Application Gateway |
x-azure-ref header and a Front Door error page | Azure Front Door |
JSON body with statusCode and a message from APIM | API Management |
Server: awselb/2.0 | AWS Application Load Balancer |
JSON error in the provider's format, such as error.code | The model API itself, not a proxy |
Check the gateway logs
If APIM is in the path, look for requests where the backend took too long or was cut off:
ApiManagementGatewayLogs
| where TimeGenerated > ago(4h)
| where ResponseCode in (502, 504, 413) or isnotempty(LastErrorReason)
| project TimeGenerated, ApiId, ResponseCode, BackendResponseCode, TotalTime, BackendTime, LastErrorSource, LastErrorReason
| order by TimeGenerated desc
Column names depend on whether you use resource-specific diagnostic tables; check your workspace schema. A TotalTime that stops at the same value every time points to a fixed timeout.
Symptom, cause and fix
| Symptom | Likely cause | Fix |
|---|---|---|
| 504 at about 60 s through Kubernetes ingress | ingress-nginx proxy-read-timeout default of 60 s | Raise it on the LLM ingress only |
| 413 through Kubernetes ingress for prompts over about 1 MB | ingress-nginx proxy-body-size default of 1m | Raise it on the LLM ingress only |
| 502 or 504 at about 20 s through Application Gateway | Backend settings request timeout too low | Raise --timeout on the backend settings |
| 413 or 403 through Application Gateway WAF | WAF request body limit or a rule match on long text | Raise the body limit; tune or exclude rules for the prompt field |
| 504 at about 60 s on AWS ALB | ALB idle timeout default of 60 s | Raise idle_timeout.timeout_seconds |
| 504 from Front Door on slow models | Origin response timeout on the profile | Raise --origin-response-timeout-seconds, or stream |
| Failures at about 4 minutes with no data on the wire | Idle TCP timeout on a load balancer, NAT or firewall, or APIM | Stream, or move long jobs to async/batch |
| Client gives up first, server logs show success | SDK read timeout shorter than generation time | Raise the client timeout for that route only |
Fix each layer
Warning
Raise timeouts only on the routes that serve LLM traffic. Raising a global timeout or body limit on a shared gateway lets slow or oversized requests tie up connections for every other app.
ingress-nginx
kubectl -n ai annotate ingress vllm --overwrite \
nginx.ingress.kubernetes.io/proxy-read-timeout="600" \
nginx.ingress.kubernetes.io/proxy-send-timeout="600" \
nginx.ingress.kubernetes.io/proxy-body-size="20m"
kubectl -n ingress-nginx logs deploy/ingress-nginx-controller --since=15m | grep -E " (413|504) "
Application Gateway
az network application-gateway http-settings update \
-g rg-net --gateway-name agw-ai -n llm-backend-settings --timeout 600
# WAF policy body limit (property names vary by WAF/CRS version; check your policy JSON first)
az network application-gateway waf-policy show -g rg-net -n waf-ai --query policySettings
az network application-gateway waf-policy update -g rg-net -n waf-ai \
--set policySettings.maxRequestBodySizeInKb=2000
Check the current maximums for body size and timeout in the Application Gateway limits page before you pick values.
Azure Front Door Standard/Premium
az afd profile show -g rg-edge --profile-name afd-ai --query originResponseTimeoutSeconds
az afd profile update -g rg-edge --profile-name afd-ai --origin-response-timeout-seconds 240
The setting is per profile, so it applies to every route on it. Check the allowed range in the current docs.
API Management
In the API or operation policy for the LLM backend:
<backend>
<forward-request timeout="240" buffer-response="false" />
</backend>
The documented default is 300 seconds, but Microsoft warns that values above 240 seconds may not be honored because idle connections can be dropped underneath. buffer-response="false" also matters for streaming. If APIM itself returns 413, check the request size limits for your tier.
AWS ALB
aws elbv2 modify-load-balancer-attributes --load-balancer-arn "$ALB_ARN" \
--attributes Key=idle_timeout.timeout_seconds,Value=300
aws elbv2 describe-load-balancer-attributes --load-balancer-arn "$ALB_ARN" \
--query "Attributes[?Key=='idle_timeout.timeout_seconds']"
Self-hosted backends
vLLM, Ollama and other servers rarely time out first. But a uvicorn worker or sidecar in front of them may. Check any Envoy or service mesh route timeout too: Istio VirtualService timeout and Envoy route timeouts each add their own limit.
Set one timeout budget
Proxies that time out at random values make failures hard to reason about. Pick the longest normal request for the route, then make each outer layer a little longer than the layer inside it:
| Layer (outer to inner) | Example for a 180 s reasoning route |
|---|---|
| Client SDK read timeout | 250 s |
| Front Door / ALB / Application Gateway | 240 s |
APIM forward-request | 220 s |
| Ingress / mesh | 210 s |
Server-side max generation (max_tokens, server limit) | sized to finish in about 180 s |
The inner layers give up first, so the client gets a clear error instead of a reset from a load balancer with no detail.
Tip
Streaming solves most of this. When tokens flow every few hundred milliseconds, idle timeouts stop firing. A reasoning model that thinks for a long time before the first token can still hit them, so check how your provider streams during the reasoning phase.
For jobs that legitimately run for many minutes (long documents, bulk summaries), don't stretch HTTP. Put them on a queue or use the provider's batch API and return a job ID.
Shrink the request instead of raising 413 limits
A 413 on a prompt is often a design smell. Before raising body limits:
- Send images by URL or file ID where the API supports it, not base64 in the JSON body.
- Trim RAG context to the chunks you actually use. A smaller retrieval budget also cuts cost and latency.
- Check the payload size from the app's logs to see how big requests really are:
import json
payload = {"model": "gpt-4.1", "messages": messages}
print(f"{len(json.dumps(payload).encode()) / 1024:.0f} KB")
Prevent it next time
- Document the timeout budget and body limits for every LLM route next to the gateway config, and check them in code review.
- Add a synthetic check that sends a long-running request (large
max_tokens) through the full public path every hour and alerts on 504 or 413. - Alert on 504 and 413 counts per route at the gateway, separately from provider errors.
- When you add a proxy, WAF or mesh to the path, re-run the per-hop timing test above.
- Default LLM chat routes to streaming, and send long jobs to async or batch processing.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get