Heuristic:A616567126 GPT WEB JAVA OkHttp Timeout For Streaming
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Networking |
| Last Updated | 2026-10-13 18:00 GMT |
Overview
Set OkHttp connect, read, and write timeouts to 90 seconds each for GPT streaming chat to accommodate long OpenAI API response times.
Description
The `ChatGPTStream` class configures OkHttp with a default timeout of 90 seconds for all three timeout types: connect, write, and read. GPT streaming responses can take significant time, especially for long completions or when the OpenAI API is under heavy load. The 90-second timeout prevents indefinite hanging while allowing sufficient time for most streaming responses to complete. The timeout value is configurable via the builder pattern (`@Builder.Default private long timeout = 90`), allowing callers to override it for specific use cases. All three timeout types are set to the same value for simplicity.
Usage
Use this heuristic when configuring HTTP clients for streaming AI API responses. Set generous timeouts (90s) on all three timeout types (connect, read, write) to handle slow API responses. For non-streaming requests, shorter timeouts (30s) may be more appropriate.
The Insight (Rule of Thumb)
- Action: Set `connectTimeout`, `readTimeout`, and `writeTimeout` all to 90 seconds for OkHttp SSE clients.
- Value: 90 seconds (default, configurable via builder).
- Trade-off: 90 seconds allows long completions but means a hung connection takes up to 90s to detect; shorter timeouts would fail legitimate long responses.
- Compatibility: Applies to all OkHttp-based requests in the ChatGPTStream class; the proxy configuration uses the same client.
Reasoning
OpenAI's streaming API sends tokens incrementally. For long prompts or complex responses, the total streaming time can exceed 30-60 seconds. A 90-second timeout provides a safety margin while preventing indefinite hangs. Setting all three timeout types (connect, read, write) to the same value simplifies configuration and ensures no single phase becomes a bottleneck. The read timeout is especially critical for SSE — it governs the maximum time between data chunks, not the total response time. Since SSE chunks arrive frequently during active streaming, the 90-second read timeout effectively means "abort if no data for 90 seconds," which is appropriate for detecting stalled connections.