Skip to content

fix: raise upstream header timeout, add image bypass & retry - #132

Open
kyto-agent wants to merge 2 commits into
hackclub:mainfrom
kyto-agent:fix/upstream-timeout-and-image-modality
Open

fix: raise upstream header timeout, add image bypass & retry#132
kyto-agent wants to merge 2 commits into
hackclub:mainfrom
kyto-agent:fix/upstream-timeout-and-image-modality

Conversation

@kyto-agent

@kyto-agent kyto-agent commented Jul 29, 2026

Copy link
Copy Markdown

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

  1. 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.

  2. Image modality bypass - Added isImageModalityRequest() helper that checks if the model is in allowedImageModels or if modalities includes "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.

  3. Automatic retry on transient failures - The proxy now retries up to 2 times (3 total attempts) on:

    • Header timeout (our local 15s guard)
    • Upstream HTTP 429 (rate limit), 502, 503, 504 (transient server errors)
    • Respects Retry-After header on 429s with a 5s cap
    • Uses exponential backoff (1s, 2s) between attempts
    • Streaming requests skip retries since body bytes may already be committed

Testing

  • TypeCheck passes (npm run typecheck)
  • Biome lint passes (npm run lint)
  • Benchmark: 1/100 requests to kimi-k2.7-code and 2/100 to qwen3.7-plus exceeded 5s. With 15s timeout, these would succeed. Transient upstream errors would be retried transparently.

References

kyto-agent 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/chat/completions 5s upstream header timeout breaks image generation & editing

1 participant