Skip to content

fix: continue after a reply hits the completion cap - #265

Draft
samsja wants to merge 1 commit into
mainfrom
fix/continue-after-completion-cap
Draft

samsja wants to merge 1 commit into
mainfrom
fix/continue-after-completion-cap

Conversation

@samsja

@samsja samsja commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Problem

A reply that hits the request's completion cap (finish_reason="length" below the advertised context window) ends the prompt. In long agentic sessions, a reasoning model sometimes spends one whole turn reasoning until the per-call output cap. The session then ends on a reply that never acted, and all the work before it is lost.

Behaviour change

  • A reply stopped by the completion cap below the context window now stays in the conversation, minus any tool call it was cut off in. This is how a reply cut off by the window is kept.
  • The engine then adds a runtime nudge (cap_hit_nudge). It tells the model that its last reply hit the output token limit before it acted. It asks the model to act now (make its next tool call or give its final answer) without restating its reasoning. The loop continues.
  • Only consecutive cap hits count. Any other reply resets the count. Once the limit is reached, the next cap hit ends the prompt as before: it is the turn's answer.
  • Replies that run into the context window are handled as before (kept and compacted). Without an advertised context window, a cap hit and a window cut cannot be told apart, so a length-cut reply still ends the prompt.
  • Token budgets work as before: a cap hit is not continued once max_tokens or a tree budget is spent.

New knob

max_cap_continuations (policy, default 2, >= 0) sets how many consecutive cap hits the engine continues from. 0 gives the previous behaviour: the first cap hit ends the prompt. The knob is in the runtime contract table, in the ACP limits snapshot, and in docs/context.md.

Tests

  • test_cap_hit_reply_is_kept_and_the_model_is_asked_to_act: the cut reply is kept without its tool call, the nudge follows, the cut tool call is not run, and the run continues to its answer.
  • test_cap_hit_continuations_are_bounded_and_reset_by_a_normal_reply: a normal reply resets the count, and the hit past the limit ends the prompt with that reply as the answer.
  • The existing below-the-window test now runs with max_cap_continuations=0 and still checks that the prompt ends.
  • Config default and ACP snapshot tests are updated.

Test plan

  • uv run ruff check . and uv run ruff format --check . pass.
  • uv run pytest tests/ --ignore=tests/e2e: 451 passed (tests/e2e needs a live model). The new tests fail without the engine change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant