Skip to main content

Overview

Request timeouts prevent your application from waiting indefinitely for slow or unresponsive LLM providers. The Gateway allows you to set granular timeout limits that automatically terminate requests exceeding the specified duration.

How It Works

When you set a request_timeout, the Gateway:
  1. Starts a timer when the request is sent
  2. Monitors the request progress
  3. Aborts the request if it exceeds the timeout
  4. Returns a 408 (Request Timeout) error with details
The timeout applies to the entire request lifecycle, including:
  • Network latency
  • Provider processing time
  • Response streaming
Timeouts are enforced at the Gateway level using AbortController, ensuring reliable timeout behavior across all providers.

Configuration

Basic Timeout

Set a timeout in milliseconds:
This configuration aborts any request taking longer than 30 seconds.

Provider-Specific Timeouts

Different timeouts for different providers:

Timeouts with Retries

Combine timeout with retry logic:
If a request times out (408), it can be retried if 408 is included in on_status_codes. This is useful for recovering from temporary network issues.

Usage Examples

Implementation Details

Timeout Mechanism

The Gateway uses AbortController to enforce timeouts:

Timeout Response Format

When a timeout occurs, the Gateway returns:
HTTP Status: 408 Request Timeout
The timeout response is already in OpenAI-compatible format. It won’t be transformed again by response transformers.

Timeout Strategies

Conservative Timeouts

For production applications with strict SLA requirements:
This configuration:
  • Sets 10-second timeout
  • Retries twice on timeout
  • Falls back to alternative provider
  • Maximum wait: ~30 seconds (10s + 10s + 10s)

Generous Timeouts

For batch processing or long-form content:

Model-Specific Timeouts

Different timeouts based on model characteristics:

Streaming Timeouts

Timeouts apply to the entire streaming response, not individual chunks:
For streaming requests:
  • Timeout starts when connection is established
  • Applies to the entire stream duration
  • Streaming ends if total time exceeds timeout
For long-running streaming responses, set a generous timeout or omit it entirely to avoid premature termination.

By Use Case

By Provider

Best Practices

Timeout should be longer than expected response time. Monitor P95/P99 latencies and set timeout accordingly. Typical recommendation: P99 latency × 1.5.
Even if providers are generally reliable, always set timeouts to prevent hanging requests from impacting your application.
Use retries with timeouts to recover from temporary slowdowns. Include 408 in on_status_codes to retry timed-out requests.
High timeout rates indicate provider performance issues. Track timeout frequency to identify patterns and adjust configurations.
For user-facing applications, set timeouts based on acceptable wait times. Users typically abandon after 5-10 seconds.
For streaming responses, timeout should cover the entire generation time, not just first chunk. Monitor end-to-end streaming duration.

Troubleshooting

Frequent Timeouts

If you’re experiencing frequent timeouts:
  1. Check provider status: Verify the provider isn’t experiencing outages
  2. Increase timeout: Current setting may be too aggressive
  3. Monitor latency: Use Gateway logs to track actual response times
  4. Consider fallbacks: Add backup providers for resilience
  5. Optimize prompts: Reduce token count to speed up generation

Timeout Too Short

Signs your timeout is too short:
  • Frequent 408 errors
  • Requests consistently timing out
  • Users reporting incomplete responses
Solution: Gradually increase timeout while monitoring success rate.

Timeout Too Long

Signs your timeout is too long:
  • Users waiting too long for errors
  • Resources held unnecessarily
  • Poor user experience
Solution: Reduce timeout based on P95 latency data.

Retries

Automatically retry timed-out requests

Fallbacks

Switch providers on timeout

Load Balancing

Distribute load to reduce timeouts

Streaming

Streaming-specific considerations