AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Using Compaction Causes Anthropic API Failure When Agent Is Invoked With Print_mode="Messages" Or Stream_mode="Messages"

When using Anthropic's compaction beta with LangChain agents, invoking with print_mode="messages" or streaming with stream_mode="messages" causes a BadRequestError because the message content includes an unsupported 'index' field in compaction blocks.

highConfidence 95%Langchain-AnthropicAffected Vlangchain-Anthropic==1.4.1Affected Vlangchain-Core==1.3.0Affected Vlanggraph==1.1.9Affected Vanthropic==0.96.0

Origin Analysis

LangChain's langchain_anthropic/chat_models.py _format_messages method does not filter out the 'index' field added by LangGraph's streaming in 'messages' mode when formatting compaction blocks, leading Anthropic API to reject the request with 'Extra inputs are not permitted'.
1. Create a ChatAnthropic model with betas=['compact-2026-01-12'] and context_management configured as in the issue.\n2. Create an agent with create_agent(model=model).\n3. Build a conversation history exceeding 50,000 input tokens using repeated filler messages, plus a final user message.\n4. Invoke agent.invoke({'messages': history}, print_mode='messages').\n5. Take the returned result['messages'] and append a HumanMessage asking a follow-up question.\n6. Invoke agent.invoke again with print_mode='messages'.\n7. The second invoke raises anthropic.BadRequestError with message 'messages.63.content.0.compaction.index: Extra inputs are not permitted'.

Fixing Code Block

Edge Case Audit

This change only affects formatting of compaction blocks and should not alter behavior in non-streaming or 'updates' modes. However, if Anthropic adds other valid fields for compaction blocks in future versions, this filter will discard them. It is recommended to review the Anthropic API spec when upgrading the langchain-anthropic or anthropic package and adjust the allowed keys accordingly. Rollback is simple: revert this branch to the original code that passed the entire block.

Ecosystem Topology