Skip to content

Commit 5981ee1

Browse files
authored
fix(core): apply cap when scaling approximate token counts (#35005)
1 parent 643355f commit 5981ee1

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

libs/core/langchain_core/messages/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2320,7 +2320,7 @@ def count_tokens_approximately(
23202320
and approx_at_last_ai > 0
23212321
):
23222322
scale_factor = last_ai_total_tokens / approx_at_last_ai
2323-
token_count *= max(1.0, scale_factor)
2323+
token_count *= min(1.5, max(1.0, scale_factor))
23242324

23252325
# round up once more time in case extra_tokens_per_message is a float
23262326
return math.ceil(token_count)

libs/core/tests/unit_tests/messages/test_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,8 +1614,8 @@ def test_count_tokens_approximately_usage_metadata_scaling() -> None:
16141614
unscaled = count_tokens_approximately(messages)
16151615
scaled = count_tokens_approximately(messages, use_usage_metadata_scaling=True)
16161616

1617-
assert scaled == 200
1618-
assert unscaled < 100
1617+
ratio = scaled / unscaled
1618+
assert 1 <= ratio <= 1.5
16191619

16201620
messages.extend([ToolMessage("text", tool_call_id="abc123")] * 3)
16211621

@@ -1630,7 +1630,7 @@ def test_count_tokens_approximately_usage_metadata_scaling() -> None:
16301630

16311631
# And the scaled total should be the unscaled total multiplied by the same ratio.
16321632
# ratio = 200 / unscaled (as of last AI message)
1633-
expected_scaled_extended = math.ceil(unscaled_extended * (200 / unscaled))
1633+
expected_scaled_extended = math.ceil(unscaled_extended * ratio)
16341634
assert scaled_extended == expected_scaled_extended
16351635

16361636

0 commit comments

Comments
 (0)