Skip to content

fix(anthropic): strip trailing assistant messages for Claude 4.6+#4973

Open
giulio-leone wants to merge 1 commit intolivekit:mainfrom
giulio-leone:fix/issue-4907-anthropic-prefill-strip
Open

fix(anthropic): strip trailing assistant messages for Claude 4.6+#4973
giulio-leone wants to merge 1 commit intolivekit:mainfrom
giulio-leone:fix/issue-4907-anthropic-prefill-strip

Conversation

@giulio-leone
Copy link

Summary

Claude 4.6 models (claude-sonnet-4-6, claude-opus-4-6) no longer support assistant message prefilling. Requests where the last message has role="assistant" now return a 400 Bad Request error, breaking existing agent workflows.

Root Cause

In multi-turn conversations, the chat context can naturally end with an assistant message (the last thing the agent said). When this context is sent to Claude 4.6 via messages.create(), the Anthropic API interprets the trailing assistant turn as a prefill attempt and rejects it.

Fix

Two surgical changes:

  1. livekit-agents/.../anthropic.py (provider format converter): Added a strip_trailing_assistant parameter to to_chat_ctx(). When enabled, trailing assistant messages are stripped from the request so it ends with a user turn. A len(messages) > 1 guard ensures at least one message is always preserved.

  2. livekit-plugins/.../anthropic/llm.py (Anthropic LLM plugin): Added _model_disables_prefill() to detect Claude 4.6+ models by name prefix. The to_provider_format() call now passes strip_trailing_assistant=True only for affected models.

This approach:

  • Only affects 4.6+ models — older models retain prefilling support
  • Uses startswith() matching for forward compatibility with future 4.6 variants
  • Falls back gracefully (default strip_trailing_assistant=False)

Testing

The fix is minimal and backward-compatible. Existing behavior is unchanged for all non-4.6 models.

Fixes #4907

@CLAassistant
Copy link

CLAassistant commented Feb 28, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 5 additional findings.

Open in Devin Review

@giulio-leone giulio-leone force-pushed the fix/issue-4907-anthropic-prefill-strip branch from b5ad333 to 88c3b35 Compare February 28, 2026 14:39
Claude 4.6 models no longer support prefilling (trailing assistant
messages in the request).  Requests ending with an assistant turn now
return a 400 error.

Add a `strip_trailing_assistant` parameter to the Anthropic provider
format converter and enable it automatically for Claude 4.6+ models
(claude-sonnet-4-6, claude-opus-4-6).  A `len(messages) > 1` guard
ensures at least one message is always preserved.

Fixes livekit#4907
@giulio-leone giulio-leone force-pushed the fix/issue-4907-anthropic-prefill-strip branch from 88c3b35 to 0b3dc1c Compare February 28, 2026 14:40
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.

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

2 participants