Skip to content

Step-capped agents fail with an assistant prefill 400 #56250

Open

Description

@OddballGreg

Version: @opencode/cli 2.0.24.

Steps to reproduce

  1. Configure an agent with steps: 2.
  2. 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).
  3. 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.

Activity

  1. opencode-agent commented on Oct 16, 2026

    @opencode-agent
    Contributor

    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.

    Related, ordered by likelihood:

    recording.mp4

    Screenshot

    Reproduction script (drive.ts)
    // 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"
    
    const roleOf = (m: any) => m?.role ?? m?.type
    const messagesOf = (body: any): any[] => body?.messages ?? body?.input ?? body?.contents ?? []
    const textOf = (m: any) => JSON.stringify(m?.content ?? m).slice(0, 80)
    
    export default OpenCodeDriver.use(
      {
        ...(process.env.OC_DEV ? { opencode: { dev: process.env.OC_DEV } } : {}),
        tui: { recording: true },
        config: { agents: { build: { steps: 2 } } } as any,
        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) => {
            const msgs = messagesOf(request.body)
            const last = msgs[msgs.length - 1]
            const role = 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.
              return Stream.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)
              return Stream.make(
                Llm.toolCall({ index: 0, id: `call_${index}`, name: "read", input: { path: ["a.txt", "b.txt", "c.txt"][index] } }),
                Llm.finish("tool-calls"),
              )
            return Stream.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.

  2. tlmquintino commented on Oct 18, 2026

    @tlmquintino

    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.

  3. bioShaun commented on Nov 26, 2026

    @bioShaun

    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).

  4. ashemedai commented on Nov 26, 2026

    @ashemedai

    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.

  5. xyz1hang commented on Nov 26, 2026

    @xyz1hang

    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 REACHED assistant 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.
  6. eqenrico-hub commented on Nov 29, 2026

    @eqenrico-hub

    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.

  7. added a commit that references this issue on Dec 2, 2026
    9ac6834
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions