Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix: apply cache_control only to last block in Anthropic messages

Pyry Takala378738082940
diff --git a/browser_use/llm/anthropic/serializer.py b/browser_use/llm/anthropic/serializer.py
index b7fa638b8..834b3efda 100644
--- a/browser_use/llm/anthropic/serializer.py
+++ b/browser_use/llm/anthropic/serializer.py
@@ -98,9 +98,12 @@ class AnthropicMessageSerializer:
return content
serialized_blocks: list[TextBlockParam] = []
- for part in content:
+ for i, part in enumerate(content):
+ is_last = i == len(content) - 1
if part.type == 'text':
- serialized_blocks.append(AnthropicMessageSerializer._serialize_content_part_text(part, use_cache))
+ serialized_blocks.append(
+ AnthropicMessageSerializer._serialize_content_part_text(part, use_cache=use_cache and is_last)
+ )
return serialized_blocks
@@ -117,9 +120,12 @@ class AnthropicMessageSerializer:
return content
serialized_blocks: list[TextBlockParam | ImageBlockParam] = []
- for part in content:
+ for i, part in enumerate(content):
+ is_last = i == len(content) - 1
if part.type == 'text':
- serialized_blocks.append(AnthropicMessageSerializer._serialize_content_part_text(part, use_cache))
+ serialized_blocks.append(
+ AnthropicMessageSerializer._serialize_content_part_text(part, use_cache=use_cache and is_last)
+ )
elif part.type == 'image_url':
serialized_blocks.append(AnthropicMessageSerializer._serialize_content_part_image(part))
@@ -129,7 +135,7 @@ class AnthropicMessageSerializer:
def _serialize_tool_calls_to_content(tool_calls, use_cache: bool = False) -> list[ToolUseBlockParam]:
"""Convert tool calls to Anthropic's ToolUseBlockParam format."""
blocks: list[ToolUseBlockParam] = []
- for tool_call in tool_calls:
+ for i, tool_call in enumerate(tool_calls):
# Parse the arguments JSON string to object
try:
@@ -138,13 +144,14 @@ class AnthropicMessageSerializer:
# If arguments aren't valid JSON, use as string
input_obj = {'arguments': tool_call.function.arguments}
+ is_last = i == len(tool_calls) - 1
blocks.append(
ToolUseBlockParam(
id=tool_call.id,
input=input_obj,
name=tool_call.function.name,
type='tool_use',
- cache_control=AnthropicMessageSerializer._serialize_cache_control(use_cache),
+ cache_control=AnthropicMessageSerializer._serialize_cache_control(use_cache and is_last),
)
)
return blocks
@@ -186,22 +193,31 @@ class AnthropicMessageSerializer:
# Add content blocks if present
if message.content is not None:
if isinstance(message.content, str):
+ # String content: only cache if it's the only/last block (no tool calls)
blocks.append(
TextBlockParam(
text=message.content,
type='text',
- cache_control=AnthropicMessageSerializer._serialize_cache_control(message.cache),
+ cache_control=AnthropicMessageSerializer._serialize_cache_control(
+ message.cache and not message.tool_calls
+ ),
)
)
else:
# Process content parts (text and refusal)
- for part in message.content:
+ for i, part in enumerate(message.content):
+ # Only last content block gets cache if there are no tool calls
+ is_last_content = (i == len(message.content) - 1) and not message.tool_calls
if part.type == 'text':
- blocks.append(AnthropicMessageSerializer._serialize_content_part_text(part, use_cache=message.cache))
- # # Note: Anthropic doesn't have a specific refusal block type,
- # # so we convert refusals to text blocks
- # elif part.type == 'refusal':
- # blocks.append(TextBlockParam(text=f'[Refusal] {part.refusal}', type='text'))
+ blocks.append(
+ AnthropicMessageSerializer._serialize_content_part_text(
+ part, use_cache=message.cache and is_last_content
+ )
+ )
+ # # Note: Anthropic doesn't have a specific refusal block type,
+ # # so we convert refusals to text blocks
+ # elif part.type == 'refusal':
+ # blocks.append(TextBlockParam(text=f'[Refusal] {part.refusal}', type='text'))
# Add tool use blocks if present
if message.tool_calls:
diff --git a/browser_use/llm/tests/test_anthropic_cache.py b/browser_use/llm/tests/test_anthropic_cache.py
index e6989f045..e9574a147 100644
--- a/browser_use/llm/tests/test_anthropic_cache.py
+++ b/browser_use/llm/tests/test_anthropic_cache.py
@@ -152,8 +152,9 @@ class TestAnthropicCache:
# Check serialized messages
assert len(serialized_messages) == 4
- # User with cache should be list
- assert isinstance(serialized_messages[0]['content'], list)
+ # User with cache should be string (cache was cleaned to False by _clean_cache_messages)
+ # Only the last message with cache=True remains cached
+ assert isinstance(serialized_messages[0]['content'], str)
# Assistant without cache should be string
assert isinstance(serialized_messages[1]['content'], str)
@@ -161,7 +162,7 @@ class TestAnthropicCache:
# User without cache should be string
assert isinstance(serialized_messages[2]['content'], str)
- # Assistant with cache should be list
+ # Assistant with cache should be list (this is the last cached message)
assert isinstance(serialized_messages[3]['content'], list)
def test_system_message_cache_behavior(self):
@@ -271,6 +272,78 @@ class TestAnthropicCache:
logger.info(anthropic_messages)
logger.info(system_message)
+ def test_cache_only_last_block_in_message(self):
+ """Test that only the LAST block in a message gets cache_control when cache=True."""
+ # Test UserMessage with multiple text parts
+ user_msg = UserMessage(
+ content=[
+ ContentPartTextParam(text='Part 1', type='text'),
+ ContentPartTextParam(text='Part 2', type='text'),
+ ContentPartTextParam(text='Part 3', type='text'),
+ ],
+ cache=True,
+ )
+ serialized = AnthropicMessageSerializer.serialize(user_msg)
+ assert isinstance(serialized['content'], list)
+ content_blocks = serialized['content']
+
+ # Count blocks with cache_control
+ cache_count = sum(1 for block in content_blocks if block.get('cache_control') is not None)
+ assert cache_count == 1, f'Expected 1 cache_control block, got {cache_count}'
+
+ # Verify it's the last block
+ assert content_blocks[-1].get('cache_control') is not None
+ assert content_blocks[0].get('cache_control') is None
+ assert content_blocks[1].get('cache_control') is None
+
+ def test_cache_only_last_tool_call(self):
+ """Test that only the LAST tool_use block gets cache_control."""
+ tool_calls = [
+ ToolCall(id='id1', function=Function(name='func1', arguments='{"arg": "1"}')),
+ ToolCall(id='id2', function=Function(name='func2', arguments='{"arg": "2"}')),
+ ToolCall(id='id3', function=Function(name='func3', arguments='{"arg": "3"}')),
+ ]
+
+ assistant_msg = AssistantMessage(content=None, tool_calls=tool_calls, cache=True)
+ serialized = AnthropicMessageSerializer.serialize(assistant_msg)
+ assert isinstance(serialized['content'], list)
+ content_blocks = serialized['content']
+
+ # Count tool_use blocks with cache_control
+ cache_count = sum(1 for block in content_blocks if block.get('cache_control') is not None)
+ assert cache_count == 1, f'Expected 1 cache_control block, got {cache_count}'
+
+ # Verify it's the last tool_use block
+ assert content_blocks[-1].get('cache_control') is not None
+ assert content_blocks[0].get('cache_control') is None
+ assert content_blocks[1].get('cache_control') is None
+
+ def test_cache_assistant_with_content_and_tools(self):
+ """Test AssistantMessage with both content and tool calls - only last tool gets cache."""
+ tool_call = ToolCall(id='test_id', function=Function(name='test_function', arguments='{"arg": "value"}'))
+
+ assistant_msg = AssistantMessage(
+ content=[
+ ContentPartTextParam(text='Text part 1', type='text'),
+ ContentPartTextParam(text='Text part 2', type='text'),
+ ],
+ tool_calls=[tool_call],
+ cache=True,
+ )
+ serialized = AnthropicMessageSerializer.serialize(assistant_msg)
+ assert isinstance(serialized['content'], list)
+ content_blocks = serialized['content']
+
+ # Should have 2 text blocks + 1 tool_use block = 3 blocks total
+ assert len(content_blocks) == 3
+
+ # Only the last block (tool_use) should have cache_control
+ cache_count = sum(1 for block in content_blocks if block.get('cache_control') is not None)
+ assert cache_count == 1, f'Expected 1 cache_control block, got {cache_count}'
+ assert content_blocks[-1].get('cache_control') is not None # Last tool_use block
+ assert content_blocks[0].get('cache_control') is None # First text block
+ assert content_blocks[1].get('cache_control') is None # Second text block
+
if __name__ == '__main__':
test_instance = TestAnthropicCache()
@@ -287,4 +360,7 @@ if __name__ == '__main__':
test_instance.test_cache_cleaning_with_system_message()
test_instance.test_cache_cleaning_no_cached_messages()
test_instance.test_max_4_cache_blocks()
+ test_instance.test_cache_only_last_block_in_message()
+ test_instance.test_cache_only_last_tool_call()
+ test_instance.test_cache_assistant_with_content_and_tools()
print('All cache tests passed!')