You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Step-capped agents fail with an assistant prefill 400 #56250
Select a model behind a provider that does not accept assistant prefill. We saw it with an Anthropic model through the GitLab Duo provider (gitlab/duo-chat-opus-5-5).
Send a prompt that needs more than two tool calls.
Expected: on the last step, the model gets the MAX_STEPS prompt and answers with a text summary.
Result: the last step fails with this provider error (the request_id is removed).
Anthropic API error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"This model does not support assistant message prefill. The conversation must end with a user message."}}
Cause: on the last allowed step, SessionRunner.runStep sends [...messages, Message.assistant(MAX_STEPS_PROMPT)] to request.primary. The request therefore ends on an assistant message.
Suggested fix: send the MAX_STEPS prompt as Message.user(...). Also consider a guard in request.primary that appends a short user turn when the message list ends on an assistant or system message.
Reacted by nardo7 and Jeroen Ruigrok van der Werven
Thanks for the clear report and for pinpointing the cause. I reproduced this on 2.0.24, and it still happens on the latest dev (d4a6223).
I set the build agent to steps: 2 and used a simulated model that calls read on step 1. On step 2, the last allowed step, the request OpenCode sends ends with an assistant message containing the "CRITICAL - MAXIMUM STEPS REACHED" prompt. Anthropic models that don't support assistant prefill reject that kind of request with the 400 you saw. The simulated model can't return Anthropic's real error, so it shows a marker in the TUI when a request ends on an assistant message.
The line responsible is still in packages/core/src/session/runner/llm.ts: when the step limit is reached, it appends Message.assistant(MAX_STEPS_PROMPT) to the messages.
The script below reproduces it, and the recording and screenshot show the run.
// Repro for #56250: on the last allowed step (agent `steps: 2`), OpenCode sends a// request whose final message is an assistant turn (MAX_STEPS prompt as prefill).// Providers/models that forbid assistant prefill (e.g. Anthropic Claude via GitLab Duo)// reject such a request with "The conversation must end with a user message".import{Effect,Stream}from"effect"import{Llm,OpenCodeDriver}from"opencode-drive"constroleOf=(m: any)=>m?.role??m?.typeconstmessagesOf=(body: any): any[]=>body?.messages??body?.input??body?.contents??[]consttextOf=(m: any)=>JSON.stringify(m?.content??m).slice(0,80)exportdefaultOpenCodeDriver.use({
...(process.env.OC_DEV ? {opencode: {dev: process.env.OC_DEV}} : {}),tui: {recording: true},config: {agents: {build: {steps: 2}}}asany,project: {git: true,files: {"a.txt": "A\n","b.txt": "B\n","c.txt": "C\n"}},},({ ui, llm })=>Effect.gen(function*(){yield*llm.serve((request,index)=>{constmsgs=messagesOf(request.body)constlast=msgs[msgs.length-1]constrole=roleOf(last)console.log(`REQUEST ${index+1}: ${msgs.length} messages, last role = ${role}, last = ${textOf(last)}`)if(role==="assistant"){// Stand-in for the provider's documented 400 for prefill-forbidding models.returnStream.make(Llm.text(`PREFILL: request ${index+1} ends with an assistant message -> Anthropic would return 400 "This model does not support assistant message prefill. The conversation must end with a user message."`),)}if(index<3)returnStream.make(Llm.toolCall({index: 0,id: `call_${index}`,name: "read",input: {path: ["a.txt","b.txt","c.txt"][index]}}),Llm.finish("tool-calls"),)returnStream.make(Llm.text("Summary: read the files."))})yield*ui.submit("Read a.txt, b.txt and c.txt one at a time, then summarize.")yield*ui.waitFor(()=>Effect.map(Effect.all([ui.matches("PREFILL:"),ui.matches("Summary:")]),([a,b])=>a||b),{timeout: 60000},)yield*Effect.sleep(1500)yield*ui.screenshot("final")}),)
Run it with opencode-drive: opencode-drive run ./drive.ts. It drives opencode from your PATH; set OC_DEV to the path of an OpenCode checkout to run that from source instead.
Also seen on 2.0.28 with the anthropic provider (claude-sonnet-5-5), on subagents (mode: subagent, steps: 60) launched through the subagent tool. The impact for subagents is worse than a failed turn: the child's file changes up to the cap stay on disk, but the parent receives only the error, no summary, so its work is silently partial (logged as "Failed to drain Session … does not support assistant message prefill"). We hit it on three long-running subagent tasks in one day. Sending the MAX_STEPS prompt as a user message, as suggested above, would fix it for us.
Additional real-world occurrence on v2.4.1, with a subagent's steps cap hit exactly.
Agent: subagent with steps: 40; model claude-sonnet-5-5 behind a custom Anthropic-compatible provider.
The session logged exactly 40 assistant messages (the cap), 90 tool calls, and one automatic compaction before the failure.
The last two stored records (session_message / session_part) were:
seq
finish
detail
572
tool-calls
content: [], reasoning text empty, every tool part executed: false
590
error
provider.invalid-request: This model does not support assistant message prefill. The conversation must end with a user message.
590 arrived 16 ms after 572 completed, i.e. in the same run.
Worth noting for triage: in this occurrence the capped step carried no visible summary text at all — the stored assistant message had empty content plus unexecuted tool calls. That matches the Message.assistant(MAX_STEPS_PROMPT) path described in the report.
Impact here is the same as @tlmquintino describes: the whole subagent run is lost, and the parent only receives the error. We re-ran the work in a fresh session with a smaller handoff.
Also, the two community fixes for this, #56257 and #62725, were both closed without being merged, so the report still reproduces as far as I can tell.
Not re-tested on 2.4.2 (the failure predates that release).
I ran into this with 2.4.1. We are hitting this with Gemini models on Vertex AI as well (routed through a LiteLLM proxy). When an agent reaches its step cap, Vertex AI rejects the trailing synthetic assistant message with HTTP 400 INVALID_ARGUMENT ("Requests ending with a model turn are not supported.").
As a temporary workaround, we are using a plugin context hook that matches the exact synthetic max-steps message at the end of event.messages and rewrites its role from assistant to user. That unblocks the final summary turn with tools still disabled, and it would be great to see a fix land upstream so we can drop the workaround.
Also reproducible on v2.4.2 (OpenCode Desktop / CLI 2.4.2-arm64, macOS Darwin 25.6.0 arm64) with Gemini models via console-model-garden:
Models tested:console-model-garden/gemini-4-neon (agent with steps: 20 and a minimal steps: 2 repro) and console-model-garden/gemini-3.8-flash#medium (agent with steps: 15 and a steps: 2 repro).
Failure on the cap step (step >= steps):
AI.Error.InvalidRequest: Requests ending with a model turn are not supported.
HTTP 400 INVALID_ARGUMENT
Confirming workaround: Rewriting the trailing synthetic CRITICAL - MAXIMUM STEPS REACHEDassistant message to role: "user" in a V2 plugin session.hook("context", ...) (keeping toolChoice: "none") allows both gemini-4-neon and gemini-3.8-flash to finish the final step cleanly with a text-only summary. Appending the max-steps prompt as a user turn instead of an assistant turn in SessionRunner.runStep would fix this for Gemini and non-prefill Anthropic models alike.
Still happens on v2.3.2 (macOS), with Anthropic's own API (no GitLab Duo) and claude-opus-5-5 / claude-sonnet-5-5. I hit it twice in long agent turns with the default build agent capped at steps: 25. When the cap was reached the request failed with the prefill 400, and a new user message was needed to continue. Removing steps made it go away. The earlier fix PR #56257 was closed by the automatic cleanup but looks like it still applies, so maybe it could be reopened.
Activity
opencode-agent commentedon Oct 16, 2026
Thanks for the clear report and for pinpointing the cause. I reproduced this on 2.0.24, and it still happens on the latest
dev(d4a6223).I set the
buildagent tosteps: 2and used a simulated model that callsreadon step 1. On step 2, the last allowed step, the request OpenCode sends ends with an assistant message containing the "CRITICAL - MAXIMUM STEPS REACHED" prompt. Anthropic models that don't support assistant prefill reject that kind of request with the 400 you saw. The simulated model can't return Anthropic's real error, so it shows a marker in the TUI when a request ends on an assistant message.The line responsible is still in
packages/core/src/session/runner/llm.ts: when the step limit is reached, it appendsMessage.assistant(MAX_STEPS_PROMPT)to the messages.The script below reproduces it, and the recording and screenshot show the run.
Related, ordered by likelihood:
finish_reason: unknown.recording.mp4
Reproduction script (drive.ts)
Run it with opencode-drive:
opencode-drive run ./drive.ts. It drivesopencodefrom yourPATH; setOC_DEVto the path of an OpenCode checkout to run that from source instead.tlmquintino commentedon Oct 18, 2026
Also seen on 2.0.28 with the
anthropicprovider (claude-sonnet-5-5), on subagents (mode: subagent,steps: 60) launched through the subagent tool. The impact for subagents is worse than a failed turn: the child's file changes up to the cap stay on disk, but the parent receives only the error, no summary, so its work is silently partial (logged as "Failed to drain Session … does not support assistant message prefill"). We hit it on three long-running subagent tasks in one day. Sending the MAX_STEPS prompt as a user message, as suggested above, would fix it for us.bioShaun commentedon Nov 26, 2026
Additional real-world occurrence on v2.4.1, with a subagent's
stepscap hit exactly.steps: 40; modelclaude-sonnet-5-5behind a custom Anthropic-compatible provider.The last two stored records (
session_message/session_part) were:finishtool-callscontent: [], reasoning text empty, every tool partexecuted: falseerrorprovider.invalid-request:This model does not support assistant message prefill. The conversation must end with a user message.590 arrived 16 ms after 572 completed, i.e. in the same run.
Worth noting for triage: in this occurrence the capped step carried no visible summary text at all — the stored assistant message had empty
contentplus unexecuted tool calls. That matches theMessage.assistant(MAX_STEPS_PROMPT)path described in the report.Impact here is the same as @tlmquintino describes: the whole subagent run is lost, and the parent only receives the error. We re-ran the work in a fresh session with a smaller handoff.
Also, the two community fixes for this, #56257 and #62725, were both closed without being merged, so the report still reproduces as far as I can tell.
Not re-tested on 2.4.2 (the failure predates that release).
ashemedai commentedon Nov 26, 2026
I ran into this with 2.4.1. We are hitting this with Gemini models on Vertex AI as well (routed through a LiteLLM proxy). When an agent reaches its step cap, Vertex AI rejects the trailing synthetic
assistantmessage with HTTP 400INVALID_ARGUMENT("Requests ending with a model turn are not supported.").As a temporary workaround, we are using a plugin
contexthook that matches the exact synthetic max-steps message at the end ofevent.messagesand rewrites itsrolefromassistanttouser. That unblocks the final summary turn with tools still disabled, and it would be great to see a fix land upstream so we can drop the workaround.xyz1hang commentedon Nov 26, 2026
Also reproducible on v2.4.2 (OpenCode Desktop / CLI
2.4.2-arm64, macOSDarwin 25.6.0 arm64) with Gemini models viaconsole-model-garden:console-model-garden/gemini-4-neon(agent withsteps: 20and a minimalsteps: 2repro) andconsole-model-garden/gemini-3.8-flash#medium(agent withsteps: 15and asteps: 2repro).step >= steps):CRITICAL - MAXIMUM STEPS REACHEDassistantmessage torole: "user"in a V2 pluginsession.hook("context", ...)(keepingtoolChoice: "none") allows bothgemini-4-neonandgemini-3.8-flashto finish the final step cleanly with a text-only summary. Appending the max-steps prompt as auserturn instead of anassistantturn inSessionRunner.runStepwould fix this for Gemini and non-prefill Anthropic models alike.eqenrico-hub commentedon Nov 29, 2026
Still happens on v2.3.2 (macOS), with Anthropic's own API (no GitLab Duo) and claude-opus-5-5 / claude-sonnet-5-5. I hit it twice in long agent turns with the default
buildagent capped atsteps: 25. When the cap was reached the request failed with the prefill 400, and a new user message was needed to continue. Removingstepsmade it go away. The earlier fix PR #56257 was closed by the automatic cleanup but looks like it still applies, so maybe it could be reopened.fix(k8s): guard Vertex Gemini against trailing model turn