If you are here from a search, the short answer first.
Node's built-in fetch is undici. Undici's headersTimeout, the time it will wait for the response headers to arrive, defaults to 300,000 milliseconds. If the server takes longer than five minutes to send its first byte, the request fails with TypeError: fetch failed, and the reason is on the error's cause: err.cause.code === "UND_ERR_HEADERS_TIMEOUT". Passing AbortSignal.timeout(600_000) does not raise that limit. The signal can only end a request sooner, never later.
The fix is to give the request a dispatcher whose timeouts match the one you meant:
import { Agent } from "undici";
const agent = new Agent({ headersTimeout: 600_000, bodyTimeout: 600_000, connectTimeout: 10_000 });
const res = await fetch(url, {
method: "POST",
body,
signal: AbortSignal.timeout(600_000),
dispatcher: agent,
} as RequestInit & { dispatcher: Agent });
The rest of this post is how we found it, why it was hard to see, and what we changed so it cannot happen quietly again.
The setup
Memtro's job runner and its Slack bot both drive a model run by POSTing to Memtro's own chat completions endpoint with stream: false. A non-streaming response has no first byte until the whole run is finished: every tool call, every round of the model reading results and deciding what to do next. A job that gathers material from a few dozen tool calls can take four or five minutes before it says anything at all. That is normal, and the runner gave each round ten minutes.
It actually got five. Undici's headersTimeout counts precisely the wait between sending a request and receiving its headers, which for a non-streaming response is the entire run. The undici Client documentation gives the default for both headersTimeout and bodyTimeout as 300 seconds. Nobody had chosen that limit. It was sitting underneath the one we had chosen.
The symptom
A daily job had failed four of its last six runs. The failures lasted 301, 307 and 311 seconds. Every success finished in 281 seconds or less.
Put like that it is obvious. It was not put like that at first, because the job had grown slowly, a source here and a tool call there, until its runs sat right on the line. Some days the model was quick and the run finished at four and a half minutes. Other days it was not. So the job failed about half the time, which reads like flakiness rather than a limit.
Three things that hid it
The kill was on the client only. When undici gave up, it closed its side of the connection. The server did not know and did not care: it carried on, finished the run, called the model for another minute and billed for every token. The run row had already been marked failed. Nothing on the server side looked wrong, because nothing on the server side was wrong.
"fetch failed" says almost nothing. Undici reports a timeout, a refused connection and a socket that dropped halfway with the same message. The distinction lives only on err.cause.code: UND_ERR_HEADERS_TIMEOUT for this case, ECONNREFUSED for nothing listening, ECONNRESET or UND_ERR_SOCKET for a dropped connection. Our runner recorded only the message, so all three looked identical, and the obvious reading of "fetch failed" is a network blip.
The run looked as though it had never started. The run row showed zero tool calls. That count came from the response, and the client never received a response to count from. A run that had in fact been working for five minutes looked like one that died on the doorstep, which sent the investigation towards the network and away from the clock.
The fix
We added one small module, internal-fetch.ts, and moved both callers onto it.
internalFetch(url, init, timeoutMs) takes the caller's timeout and applies it twice: as the abort signal, and as the headersTimeout and bodyTimeout of an undici Agent passed as the request's dispatcher. There is then one limit, and it is the one the caller wrote. bodyTimeout is raised too because it is the gap allowed between chunks of the body, which for a non-streaming response is the same kind of wait. connectTimeout stays short at ten seconds, because failing to connect is a different problem from waiting for an answer, and if the app is not listening we want to know quickly.
An Agent is a connection pool, so creating one per request would mean a new pool for every run. The module keeps one agent per distinct duration and reuses it.
The second half is describeFetchFailure, which reads err.cause.code and turns it into a sentence somebody can act on. A headers or body timeout says the run produced no answer within the limit and that the work may have continued on the server, so check before running it again. That last clause matters: retrying blindly after a client-side timeout runs the whole job twice and pays for it twice. A refused connection says the service was probably mid-restart. A reset says the connection dropped. Anything unrecognised keeps its original message with the code appended, rather than being given an invented explanation.
Two things we checked rather than assumed
Node's global fetch honours a dispatcher from the npm undici package. Node bundles its own copy of undici, and we import Agent from the separately installed undici package (7.30.1 here). It is reasonable to wonder whether the two agree. They do, and a test now proves it on every run: it passes a thirty-second abort signal with a 900 millisecond agent timeout against a server that waits four seconds, and asserts the error's cause is UND_ERR_HEADERS_TIMEOUT. With the long signal, the only thing that can end that request is the agent. If a future Node stopped honouring the option, that test would fail instead of production quietly going back to a five-minute ceiling.
Undici's timers are coarse. Undici's timeouts tick in half-second steps, so a test timeout of a few hundred milliseconds rounds up to the next tick and races the response instead of beating it. We learned that while writing the test. Keep test durations far apart: a timeout under a second against a delay of several.
The rule we follow now
A plain fetch with a long AbortSignal.timeout on a call that can take minutes is a bug. The signal suggests a limit the request does not have. Any call to our own API that may legitimately run past a few minutes goes through internalFetch.
The Slack bot was not failing. Its limit was four minutes, under the default. It was moved anyway, because the day somebody raised that to six it would have broken in exactly the same silent way.
The honest long-term answer is for these callers to stream, which makes time to first byte irrelevant rather than generous. Matching the timeouts was the smaller change, and it made the limit the one we asked for.
Where Memtro stands
Scheduled jobs and the Slack bot now wait as long as they say they will, and a run that does fail records which of the three failures it was. If a job run reports that it produced no answer within its limit, the work may well have finished on the server, so check its output before running it again. How jobs run and what their limits are is in the jobs documentation, and the common failure messages are in troubleshooting.