Skip to content

[FEATURE]: Auto-continue when model hits output token limit (finish_reason: "length") #17471

@stephenstubbs

Description

@stephenstubbs

Feature hasn't been suggested before.

  • I have verified this feature I'm about to request hasn't been suggested before.

Describe the enhancement you want to request

When using models with large context windows (e.g., Claude Opus 4.6 with 1 million input context), the input context is rarely exhausted. However, the per-response output token cap (OUTPUT_TOKEN_MAX, default 32K) frequently causes the model to stop mid-task with finish_reason: "length". The user must then manually type "continue" to resume — which breaks the autonomous agent workflow.
Current behavior:
The loop in prompt.ts exits when finish_reason is anything other than "tool-calls" or "unknown":

if (
  lastAssistant?.finish &&
  !["tool-calls", "unknown"].includes(lastAssistant.finish) &&
  lastUser.id < lastAssistant.id
) {
  break
}

When finish_reason is "length", the loop breaks and waits for user input.
Requested behavior:
When finish_reason is "length", it would be nice for OpenCode to automatically inject a synthetic "continue" user message and re-enter the loop — the same pattern already used for subtask summaries. The model's output was truncated, not intentionally finished, so the loop should continue.
Suggested implementation

  1. Add "length" to the continuation conditions in prompt.ts, or handle it separately
  2. When finish_reason === "length" is detected, inject a synthetic user message (e.g., "Continue from where you left off.") and continue the loop instead of breaking
  3. The infrastructure for synthetic user messages already exists (used for subtask command summaries)
  4. Consider adding a config option (compaction.auto_continue or similar) to make this opt-in/out
  • The MessageV2.OutputLengthError type is already defined in message-v2.ts but appears unused
  • The plugin system cannot solve this, as there is no hook between the finish reason being set and the loop exit decision

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)discussionUsed for feature requests, proposals, ideas, etc. Open discussion

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions