Raven icon indicating copy to clipboard operation
Raven copied to clipboard

fix(agent): a context-window cut is told to the model as an output-limit cut

Open LivXue opened this issue 3 weeks ago • 1 comments

The gap

A turn cut by the model's context window now reaches finish_reason="length", the same value a turn cut by the output ceiling reaches. Everything downstream treats length as "raise the budget and ask again", which is right for an output ceiling and useless for a context window: a window is filled by the input, so raising max_tokens cannot help.

#1215 maps Anthropic's model_context_window_exceeded to length. That part is correct and needed -- before it, a window-cut turn read as a turn that finished normally. What it exposes is that the loop has no way to tell the two causes apart once they share a finish reason.

Two concrete symptoms today:

1. The model is told the wrong cause. It reads:

  • [truncated] ... This turn stopped at the {N}-token output limit ... -- raven/agent/tools/registry.py:128, :138
  • If it was the output limit: send this call again in a smaller form. -- raven/agent/tools/registry.py:976, :978
  • Your previous response was cut off by the output length limit ... -- raven/agent/loop/recovery.py:106
  • Processing stopped: the response was cut off at the output length limit, ... -- raven/agent/loop/recovery.py:117

For a window cut, every one of these names a cause that is false, and the advice that follows ("keep it short", "send a smaller form") does not address the problem -- the prompt itself has to shrink.

2. The continuation spends a budget that cannot help. raven/agent/loop/turn_path.py:2602 retries with max_tokens * 2**continuations. For a window cut the request is already beyond the window; a larger output allowance makes it worse, not better. The loop spends up to MAX_CONTINUATIONS = 3 turns on this, then lands on TRUNCATED_FAILURE.

The author's own comment on the mapping says the two are fixed differently -- a context window by shrinking the prompt, an output ceiling by a larger budget. That distinction exists in the code only as a log line; it never reaches the model or the retry decision.

Why it is not in #1215

The files that would change (registry.py, recovery.py, turn_path.py) are not in that PR's two-file diff. #1215 is right to stay narrow: its stated goal is that a window cut is no longer read as a normal finish, and it achieves that.

What closing this would look like

Any of these, in increasing scope:

  1. Neutral wording. Make the model-facing text stop asserting a cause: "stopped at a limit before it produced anything", with the advice branching on something real rather than on the word length. Cheapest, and it stops the false statement.
  2. Carry the cause. Keep the raw stop reason alongside length (the mapping already logs it) and let flag_truncation hand it to recovery.decide, so a window cut can take a different path than an output-ceiling cut.
  3. The right lever for a window cut. A window cut wants the prompt shrunk -- see raven/agent/loop/window/shrink.py for the machinery that already exists -- not a bigger max_tokens.

Option 2 is the one that makes 1 and 3 possible; 1 alone is a wording fix that leaves the wasted continuation budget in place.

Scope note

The same reasoning will apply to any provider that reports a window cut as length, or that gets mapped to length later -- the loss of cause is a property of the shared finish-reason vocabulary, not of Anthropic. Worth deciding whether the cause rides on finish_reason (one more value) or on a side channel (LLMResponse.truncated already exists and carries only a bool).

LivXue avatar Nov 11 '26 11:11 LivXue

Hi! I would like to work on fixing this issue. I have inspected aven/providers/truncation.py, aven/agent/tools/registry.py, aven/agent/loop/recovery.py, and aven/agent/loop/turn_path.py and verified how model_context_window_exceeded currently collapses into inish_reason=length. I plan to propagate the truncation cause (context_window vs output_limit) so tool error messages and recovery avoid telling the model it hit the output ceiling or doubling max_tokens when the context window is full, and add unit tests covering this path. Please let me know if you have any preferred design details!

Nani-2007-code avatar Nov 11 '26 14:11 Nani-2007-code