Commit Graph
1 Commits
Author SHA1 Message Date
yetone 36af588e2c gateway: "Context window is full" is a conversation too long for the model
ergou0954_54426 on Discord (#feedback, 11-01) routes by intent across
several models, one a "kiro opus" of an API-key relay (not magpie's Kiro
plugin). On long conversations the relay answered 400 "Context window is
full. Reduce conversation history, system prompt, or tools. (request id:
…)". That is the wording kiro.rs (src/anthropic/handlers.rs) and
kiro2claude (error-mapper.ts) give Kiro's CONTENT_LENGTH_EXCEEDS_THRESHOLD,
always as a 400 invalid_request_error, with new-api's request id after
it. tooLongRe had no words for it, so the 400 was an ordinary request
error: the group never asked the next member, and the agent got the
relay's 400 as a plain error, with no context_length_exceeded to compact
on. In a stream's error event it stood for a 502 (streamStatus), a
passing vendor fault that rests the account and is asked again.

tooLongRe now takes "context (window )?(is )?full", so overflowed,
failure, retryable, streamStatus, lateRests and writeErrorSaid all read
it the same way: on 400, 413 or 422, or in a stream's error event, the
member is passed for one with room (failOverflow, no rest), and alone
the agent is told context_length_exceeded. Not changed: a 5xx carrying
the words is still the vendor's fault (tooLong is 4xx only, as
TestContextOverflowSaidTheClientsWay holds for a 502), and
tooLongUnsaid still covers a 5xx only where the window is known.

Routing could not have skipped the member up front: a custom relay's
model has a window only from its own /v1/models list, models.dev by id
(catalog.ContextOf, with its spellings), or the user's Context setting.
A relay id such as "kiro-opus" matches none, so the window is unknown
(0), roomy keeps it and withRoom counts it as having room. A probe in a
fresh HOME gave windowOf 0 for claude-opus-4-6, kiro-claude-opus-4.6 and
kiro-opus on a custom provider.

TestContextWindowFullIsAnOverflow and
TestContextWindowFullGoesToAMemberWithRoom are built from the literal
text, with and without the request id, Anthropic- and OpenAI-shaped,
plus kiro.rs's sibling "Input is too long. Reduce the size of your
messages."; ordinary 400s (invalid model, model not found, messages
required) stay non-overflows.

Found and filed, not fixed: the last candidate's stream error event that
says too long reaches the agent as the vendor wrote it (#4675).

Run on mote, on this change at base 6f3dfc282: gofmt -l internal/gateway
clean; go vet ./internal/gateway, GOOS=linux go vet -tags nogui,
GOOS=windows go vet: ok; go test -timeout 45m -count=1 ./internal/gateway:
ok (730s). At the pushed head (rebased on 445891cf7, which changed
internal/gateway and internal/provider): gofmt and go vet
./internal/gateway clean, and go test -run of the two new tests,
TestContextOverflowSaidTheClientsWay, TestEveryVendorsOverflow,
TestChineseNotTooLong, TestKiroPluginOverflowGoesToAMemberWithRoom and
TestOverflowGoesToAMemberWithRoom: ok.
Broken by hand: the two new tests at origin/main 6f3dfc282 (without the
regex change) fail: every status reads as not an overflow, the stream
error stands for 502 and rests, and the group answers the relay's 400
with the second member never asked.
Not run: the whole gateway package at the pushed head, the full ./... suite (another session's suite held mote), the
GUI suite (no GUI change), a real Kiro-backed relay pushed past its
window.
2026-11-01 12:02:09 +08:00