API_TIMEOUT_MS above 300000 is silently ineffective — undici headersTimeout is never configured
Summary
API_TIMEOUT_MS cannot exceed ~300s for time-to-first-header. Requests to slow upstreams fail with UND_ERR_HEADERS_TIMEOUT at ~301s even when API_TIMEOUT_MS is set to 600000, because Node's global fetch (undici) defaults headersTimeout to 300000ms and nothing in CCR configures the dispatcher.
The failure is silent in the sense that the configured value looks honoured but has no effect on this phase of the request.
Environment
- CCR v3.0.17, Docker deployment built from the repo
Dockerfile/docker compose - Runtime image Node 22 (
node:22-bookworm-slim) - Upstream: NVIDIA
https://integrate.api.nvidia.com/v1(openai_chat_completions), large models under load API_TIMEOUT_MS: 600000
Reproduction
- Configure a provider whose model takes longer than 300s to emit response headers (large NVIDIA-hosted models do this on non-trivial prompts).
- Set
API_TIMEOUT_MS: 600000. - Send a normal (non-trivial) completion request.
Observed, from CCR's own request log:
status_code=502 duration_ms≈301000 output_tokens=0
gateway_error: Upstream fetch failed: fetch failed
[cause=HeadersTimeoutError; code=UND_ERR_HEADERS_TIMEOUT]The same model returns 200 in ~1s for a trivial 16-token prompt, so the provider and credentials are fine — it is purely time-to-first-header.
A second model on the same provider succeeded once at ~258s and failed at ~301s, which is consistent with a hard 300s ceiling rather than a provider fault.
Analysis
API_TIMEOUT_MSis mapped toupstreamTimeoutMsinpackages/core/src/gateway/core-runtime/config-compiler.ts(upstreamTimeoutMs: Number(config.API_TIMEOUT_MS) || 0).- Grepping
packages/core/srcand the installed@musistudioruntime inside the container finds noheadersTimeout,bodyTimeout,new Agent(, orsetGlobalDispatcher. undici's defaults therefore apply. - undici's default
headersTimeoutis 300000ms, so it aborts the request before any largerAPI_TIMEOUT_MScan take effect.
Net effect: any API_TIMEOUT_MS greater than 300000 has no effect on time-to-first-header.
Suggested fix
Derive a dispatcher from the configured timeout, e.g.
import { Agent, setGlobalDispatcher } from "undici";
setGlobalDispatcher(new Agent({
headersTimeout: upstreamTimeoutMs,
bodyTimeout: upstreamTimeoutMs,
}));There is already a hook for this: the gateway preloads an undici module for proxy support (packages/core/src/gateway/core-runtime/supervisor.ts resolves undici and preloads undici-proxy-agent.js), so the dispatcher could be configured in the same place.
Alternatively, document that API_TIMEOUT_MS is bounded at 300s for headers, so users don't tune a value that cannot apply.
Possibly related
- #884 — user setting a large
API_TIMEOUT_MSand still seeing failures - #71 — question about what
API_TIMEOUT_MSactually controls
Both are consistent with the parameter not behaving as its name suggests.
Source: musistudio/claude-code-router