fix: raise upstream header timeout, add image bypass & retry - #132
Open
kyto-agent wants to merge 2 commits into
Open
fix: raise upstream header timeout, add image bypass & retry#132kyto-agent wants to merge 2 commits into
kyto-agent wants to merge 2 commits into
Conversation
added 2 commits
July 29, 2026 14:56
- Increase UPSTREAM_HEADER_TIMEOUT_MS from 5_000 to 15_000ms. Benchmark data shows ~1% of text requests legitimately exceed 5s (cold caches, provider failovers, slow upstreams), causing intermittent 504s. 15s catches these while still protecting against true hangs. - Add isImageModalityRequest() bypass: image models (text-to-image, image editing) take 10-30s+ to produce headers. Skip the header timeout entirely for requests targeting image-capable models or explicitly requesting image output modalities. Fixes hackclub#124. - Add automatic retry (1 retry) on header timeout for non-streaming requests. Transparently retries once before returning 504, handling the ~1% intermittent case without requiring client-side retry logic. Fixes hackclub#124, closes hackclub#125
Retry on 429, 502, 503, 504 from upstream (not just our own header timeout). Respect Retry-After header on 429s with a 5s cap. Use exponential backoff (1s, 2s) between attempts. Cap at 2 retries (3 total attempts). Streaming requests still skip retries since body bytes may already be committed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes three related issues with the upstream header timeout guard added in PR #110: image generation/editing completely broken (#124), and intermittent 504s on regular text models (~1% of requests per benchmark).
Changes
Increased header timeout from 5s to 15s - Benchmark data shows ~1% of text requests legitimately exceed 5s (cold caches, provider failovers, slow upstreams). 15s matches industry standards and still catches true hangs.
Image modality bypass - Added
isImageModalityRequest()helper that checks if the model is inallowedImageModelsor ifmodalitiesincludes"image". These requests bypass the header timeout entirely since image models routinely take 10-30s+ to start responding. Fixes /chat/completions 5s upstream header timeout breaks image generation & editing #124 completely.Automatic retry on transient failures - The proxy now retries up to 2 times (3 total attempts) on:
Retry-Afterheader on 429s with a 5s capTesting
npm run typecheck)npm run lint)kimi-k2.7-codeand 2/100 toqwen3.7-plusexceeded 5s. With 15s timeout, these would succeed. Transient upstream errors would be retried transparently.References