Skip to content

The agent engineering platform.

test(core): add tests for approximate token counting with multimodal messages (#34898)

Lewis Whitehill0c9d392d418f
diff --git a/libs/core/tests/unit_tests/messages/test_utils.py b/libs/core/tests/unit_tests/messages/test_utils.py
index 518a8a0905..faa2cf2f94 100644
--- a/libs/core/tests/unit_tests/messages/test_utils.py
+++ b/libs/core/tests/unit_tests/messages/test_utils.py
@@ -2726,3 +2726,87 @@ def test_count_tokens_approximately_with_custom_image_penalty() -> None:
# Should be ~1600 (image) + ~1 (text) + 3 (extra) = ~1604 tokens
assert 1600 < token_count < 1610
+
+
+def test_count_tokens_approximately_with_image_only_message() -> None:
+ """Test token counting for a message that only contains an image."""
+ message = HumanMessage(
+ content=[
+ {
+ "type": "image_url",
+ "image_url": {"url": "data:image/jpeg;base64,AAA"},
+ }
+ ]
+ )
+
+ token_count = count_tokens_approximately([message])
+
+ # Should be roughly tokens_per_image + role + extra per message.
+ # Default tokens_per_image is 85 and extra_tokens_per_message is 3,
+ # so we expect something in the ~90-110 range.
+ assert 80 < token_count < 120
+
+
+def test_count_tokens_approximately_with_unknown_block_type() -> None:
+ """Test that unknown multimodal block types still contribute to token count."""
+ text_only = count_tokens_approximately([HumanMessage(content="hello")])
+
+ message_with_unknown_block = HumanMessage(
+ content=[
+ {"type": "text", "text": "hello"},
+ {"type": "foo", "bar": "baz"}, # unknown type, falls back to repr(block)
+ ]
+ )
+
+ mixed = count_tokens_approximately([message_with_unknown_block])
+
+ # The message with an extra unknown block should be counted as more expensive
+ # than the text-only version.
+ assert mixed > text_only
+
+
+def test_count_tokens_approximately_ai_tool_calls_skipped_for_list_content() -> None:
+ """Test that tool_calls aren't double-counted for list (Anthropic-style) content."""
+ tool_calls = [
+ {
+ "id": "call_1",
+ "name": "foo",
+ "args": {"x": 1},
+ }
+ ]
+
+ # Case 1: content is a string -> tool_calls should be added to the char count.
+ ai_with_text_content = AIMessage(
+ content="do something",
+ tool_calls=tool_calls,
+ )
+ count_text = count_tokens_approximately([ai_with_text_content])
+
+ # Case 2: content is a list (e.g. Anthropic-style blocks) -> tool_calls are
+ # already represented in the content and should NOT be counted again.
+ ai_with_list_content = AIMessage(
+ content=[
+ {"type": "text", "text": "do something"},
+ {
+ "type": "tool_use",
+ "name": "foo",
+ "input": {"x": 1},
+ "id": "call_1",
+ },
+ ],
+ tool_calls=tool_calls,
+ )
+ count_list = count_tokens_approximately([ai_with_list_content])
+
+ assert count_text - 1 <= count_list <= count_text + 1
+
+
+def test_count_tokens_approximately_respects_count_name_flag() -> None:
+ """Test that the count_name flag controls whether names are included."""
+ message = HumanMessage(content="hello", name="user-name")
+
+ with_name = count_tokens_approximately([message], count_name=True)
+ without_name = count_tokens_approximately([message], count_name=False)
+
+ # When count_name is True, the name should contribute to the token count.
+ assert with_name > without_name