Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/server/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2061,11 +2061,11 @@ async function handleRequest(
// kernel's nudge/truncate bands sit below (window - reserved) and a
// context+output overflow can't happen on a small window (e.g. 100k with a
// large max_tokens — the most common "context blew up" cause; none of the
// three layers reserved room for the output before this). Anthropic is
// exempt: its input limit is enforced independently of max_tokens
// (separate output budget), so reserving would shift every band down by
// maxOutput on every session for no safety gain — see
// shouldReserveOutputHeadroom. The request's own budget field is the exact
// three layers reserved room for the output before this). #3900: the
// reservation now applies to EVERY wire including Anthropic — an over-full
// context costs attention everywhere, and wrapper providers on the
// Anthropic wire (DeepSeek /anthropic) count completion against the window.
// Per-provider opt-out for genuinely separate walls arrives with #3973. The request's own budget field is the exact
// output budget requested for THIS turn, so it is precise and per-request;
// when the harness omits every budget field (#924 fallback below) the
// model's declared max output stands in for it.
Expand Down
19 changes: 10 additions & 9 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,16 +593,17 @@ export function appendSystemText(

/**
* Whether the OUTPUT budget should be reserved from the context window at all.
* Anthropic's Messages API enforces the input limit INDEPENDENTLY of
* max_tokens (the output budget is separate — input up to the window works
* with any max_tokens), so reserving it would shift the nudge/truncate bands
* down by maxOutput on every session with no safety gain. The OpenAI-family
* APIs count output against the window, so the reservation is only needed
* there. Unknown/other protocols reserve (conservative — a missed reservation
* at worst overflows once and the self-heal corrects it).
* #3900: reserve on EVERY wire, including Anthropic. An over-full context degrades
* attention everywhere, so paying up to headroomCap × window of reservation on
* Claude routes is worth removing the "provider counts completion against the
* window" class of 400s (DeepSeek /anthropic counted it: 1,070,269 + 128,000 vs
* a 1,048,576 wall). Real-Claude routes — where the input limit IS enforced
* independently of max_tokens — can recover the band once #3973 persists the
* per-provider wall fact; until then every protocol gets the safe default. The
* parameter stays for call-site stability across that follow-up.
*/
export function shouldReserveOutputHeadroom(protocol: string | undefined): boolean {
return protocol !== "anthropic";
export function shouldReserveOutputHeadroom(_protocol?: string): boolean {
return true;
}

/** System-carrier texts of a parsed request body, covering all four wire shapes:
Expand Down
6 changes: 4 additions & 2 deletions tests/anthropic-beta-window.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,15 @@ test("plugin: effectiveContextLimit honors anthropic-beta context-1m, per-reques
};
// With the beta header → the window reported to the plugin is 1M.
await (await fetch(url(rig), { method: "POST", headers: { ...base, "anthropic-beta": BETA }, body: JSON.stringify({ model: MODEL, max_tokens: 1024, stream: true, messages: [{ role: "user", content: "hi" }] }) })).text();
assert.equal(listSessions()[0]?.metadata.effectiveContextLimit, 1_000_000, "beta → 1M reported to plugin");
// #3900: Anthropic now reserves like every other wire, so the reported
// limit is the negotiated 1M minus the 1024-token output reservation.
assert.equal(listSessions()[0]?.metadata.effectiveContextLimit, 1_000_000 - 1_024, "beta → 1M minus output reservation reported to plugin");

// Same session, header disappears → the window drops back to the model
// default 200K. This is the per-request resolution the issue requires:
// the beta may appear/disappear between requests of one session.
await (await fetch(url(rig), { method: "POST", headers: base, body: JSON.stringify({ model: MODEL, max_tokens: 1024, stream: true, messages: [{ role: "user", content: "hi again" }] }) })).text();
assert.equal(listSessions()[0]?.metadata.effectiveContextLimit, 200_000, "no beta → 200K (per-request resolution)");
assert.equal(listSessions()[0]?.metadata.effectiveContextLimit, 200_000 - 1_024, "no beta → 200K minus output reservation (per-request resolution)");
} finally {
await closeRig(rig);
}
Expand Down
12 changes: 6 additions & 6 deletions tests/context-overflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,12 @@ test("reserveOutputHeadroom: degenerate maxOutput >= window stays a no-op even w
assert.equal(reserveOutputHeadroom(100_000, 150_000, 0.25), 100_000);
});

// Protocol gate: Anthropic's Messages API enforces the input limit
// independently of max_tokens (separate output budget), so reserving there
// would shift every nudge/truncate band down by maxOutput for no safety gain.
// OpenAI-family APIs count output against the window — reserve there.
test("shouldReserveOutputHeadroom: anthropic exempt, other protocols reserve", () => {
assert.equal(shouldReserveOutputHeadroom("anthropic"), false, "Anthropic: output budget is separate from the context window");
// #3900: reservation applies to every wire (including Anthropic) — an over-full
// context degrades attention everywhere, and wrapper providers on the Anthropic
// wire count completion against the window. Per-provider opt-out for genuinely
// separate walls arrives with #3973.
test("shouldReserveOutputHeadroom: every protocol reserves (#3900)", () => {
assert.equal(shouldReserveOutputHeadroom("anthropic"), true, "#3900: over-full context costs more than the reserved band");
assert.equal(shouldReserveOutputHeadroom("openai"), true);
assert.equal(shouldReserveOutputHeadroom("responses"), true);
assert.equal(shouldReserveOutputHeadroom(undefined), true, "unknown protocol → conservative (reserve)");
Expand Down
5 changes: 4 additions & 1 deletion tests/preflight-max-output-tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,10 @@ test("e2e #663 (anthropic): header strip is a no-op — summary keeps auth, fold
await once(upstream, "listening");
const upstreamPort = (upstream.address() as { port: number }).port;

const proxy = await startProxy(upstreamPort, { "claude-test": { context: 10_000 } });
// #3900: Anthropic now reserves min(max_tokens, cap×window) like every other
// wire; +2_000 restores the margin longAnthropicMessages() had against the
// old no-reservation wall (12_000 − 1_024 reserved ≈ the old 10_000).
const proxy = await startProxy(upstreamPort, { "claude-test": { context: 12_000 } });
await once(proxy, "listening");
const proxyPort = (proxy.address() as { port: number }).port;

Expand Down
Loading