@@ -109,6 +109,28 @@ def test_serde_round_trip(self):
109109 assert restored .tokens_per_image == 200
110110
111111
112+ @tool
113+ def search (query : Annotated [str , "the search query" ]) -> str :
114+ """Search the web for a query and return the top results."""
115+ return "result"
116+
117+
118+ class TestTiktokenCounterTools :
119+ def test_tool_schemas_add_to_the_count (self , fake_encoder ):
120+ # A provider is sent the schemas alongside the messages, so they consume tokens too.
121+ counter = TiktokenCounter ()
122+ messages = [ChatMessage .from_user ("hi" )]
123+
124+ assert counter .count (messages , tools = [search ]) > counter .count (messages )
125+
126+ def test_tools_can_be_counted_without_messages (self , fake_encoder ):
127+ assert TiktokenCounter ().count ([], tools = [search ]) > 0
128+
129+ def test_nothing_to_measure_is_zero (self ):
130+ assert TiktokenCounter ().count ([]) == 0
131+ assert TiktokenCounter ().count ([], tools = None ) == 0
132+
133+
112134@pytest .mark .integration
113135class TestTiktokenCounterIntegration :
114136 """Exercises the real encoder, which downloads its vocabulary on first use."""
@@ -137,25 +159,3 @@ def test_an_image_is_charged_at_the_flat_rate(self):
137159 counter = TiktokenCounter (tokens_per_image = 85 )
138160
139161 assert counter .count ([ChatMessage .from_user (content_parts = [IMAGE ])]) > 85
140-
141-
142- @tool
143- def search (query : Annotated [str , "the search query" ]) -> str :
144- """Search the web for a query and return the top results."""
145- return "result"
146-
147-
148- class TestTiktokenCounterTools :
149- def test_tool_schemas_add_to_the_count (self ):
150- # A provider is sent the schemas alongside the messages, so they consume tokens too.
151- counter = TiktokenCounter ()
152- messages = [ChatMessage .from_user ("hi" )]
153-
154- assert counter .count (messages , tools = [search ]) > counter .count (messages )
155-
156- def test_tools_can_be_counted_without_messages (self ):
157- assert TiktokenCounter ().count ([], tools = [search ]) > 0
158-
159- def test_nothing_to_measure_is_zero (self ):
160- assert TiktokenCounter ().count ([]) == 0
161- assert TiktokenCounter ().count ([], tools = None ) == 0
0 commit comments