Skip to content

Anthropic 400 Error on Claude 4.6 - "Prefilling assistant messages is no longer supported" #4907

@AndrazP

Description

@AndrazP

Bug Description

Anthropic recently released Claude 4.6 models (including Sonnet 4.6), which introduces a breaking API change: prefilling assistant messages is no longer supported. Any request attempting to prefill an assistant message now returns a 400 Bad Request error.

In practice, this affects LiveKit agents because the chat context (ChatContext) can end up with the last message having role="assistant". When this happens, the Anthropic API rejects the request since it interprets a trailing assistant message as a prefill attempt.

According to Anthropic's(https://platform.claude.com/docs/en/about-claude/models/migration-guide#when-migrating-from-sonnet-4-5), developers are advised to remove assistant message prefilling and migrate to using structured outputs, output_config.format, or system prompt instructions.

I am opening this issue to ask what the roadmap/plan is for livekit/agents to address this compatibility issue, as it currently breaks agent flows when utilizing the newest Anthropic models.

Expected Behavior

The Anthropic LLM plugin (livekit-plugins-anthropic) should detect or natively format the message history in a way that complies with Claude 4.6's strict requirements (e.g., dynamically avoiding trailing assistant messages that act as prefills, or moving continuation prompts to the user role as Anthropic suggests).

Additional Context / References:
Other open-source projects integrating Claude are actively detecting this limitation and implementing mitigations (such as rewriting the message arrays before dispatching to the API).

See how oh-my-opencode is tracking and mitigating this exact issue: https://github.com/code-yeongyu/oh-my-opencode/issues/1528

Reproduction Steps

Sequential Replies: Calling await session.generate_reply() twice, one after another.
OR
Agent Handoffs: It is especially noticeable when switching between agents (passing the conversation context from one agent to another within a session).

Operating System

Any

Models Used

Sonnet 4.6

Package Versions

LiveKit 1.4.2

Session/Room/Call IDs

No response

Proposed Solution

One possible workaround:

if chat_ctx.messages() and chat_ctx.messages()[-1].role == "assistant":
  chat_ctx.add_message(role="user", content="[SYSTEM] Continue")

Additional Context

No response

Screenshots and Recordings

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions