File tree Expand file tree Collapse file tree 2 files changed +4
-4
lines changed
tests/unit_tests/messages Expand file tree Collapse file tree 2 files changed +4
-4
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments