browser-use/browser-use
AnthropicMessageSerializer applies cache_control to all blocks instead of only the last, risking Anthropic breakpoint overage
Summary
Context:
AnthropicMessageSerializerconverts browser-use’s internal message format to Anthropic’s API format, handling cache control for prompt caching optimization.Bug: When a message has
cache=True, the serializer appliescache_controlto every content block (text parts, images, tool calls) instead of only the last block.Actual vs. expected: The Anthropic API allows a maximum of 4 cache breakpoints per request. The current implementation can easily exceed this limit by adding cache_control to all blocks within messages that have
cache=True.Impact: Requests with multiple content blocks can exceed Anthropic’s 4 cache breakpoint limit, potentially causing API errors, unexpected caching behavior, or degraded performance. This is especially problematic for typical agent conversations with system prompts, screenshots, and tool calls.
Code with bug
Failing test
Test output:
Recommended fix
The fix should ensure that when use_cache=True for a message, only the last content block in that message receives cache_control:
Similar fixes needed in:
_serialize_content_to_str()_serialize_tool_calls_to_content()AssistantMessage serialization logic
Update tests to verify only the last block within each message has cache_control when cache=True.