@@ -54,15 +54,6 @@ def _llm_type(self) -> str:
5454 return llm_type
5555
5656
57- def tiktoken_encoding_if_installed ():
58- try :
59- import tiktoken # type: ignore # noqa # pylint: disable=unused-import
60-
61- return "cl100k_base"
62- except ImportError :
63- return None
64-
65-
6657@pytest .mark .parametrize (
6758 "send_default_pii, include_prompts, use_unknown_llm_type" ,
6859 [
@@ -82,7 +73,6 @@ def test_langchain_agent(
8273 integrations = [
8374 LangchainIntegration (
8475 include_prompts = include_prompts ,
85- tiktoken_encoding_name = tiktoken_encoding_if_installed (),
8676 )
8777 ],
8878 traces_sample_rate = 1.0 ,
@@ -144,15 +134,33 @@ def test_langchain_agent(
144134 ),
145135 ChatGenerationChunk (
146136 type = "ChatGenerationChunk" ,
147- message = AIMessageChunk (content = "5" ),
137+ message = AIMessageChunk (
138+ content = "5" ,
139+ usage_metadata = {
140+ "input_tokens" : 142 ,
141+ "output_tokens" : 50 ,
142+ "total_tokens" : 192 ,
143+ "input_token_details" : {"audio" : 0 , "cache_read" : 0 },
144+ "output_token_details" : {"audio" : 0 , "reasoning" : 0 },
145+ },
146+ ),
148147 generation_info = {"finish_reason" : "function_call" },
149148 ),
150149 ],
151150 [
152151 ChatGenerationChunk (
153152 text = "The word eudca has 5 letters." ,
154153 type = "ChatGenerationChunk" ,
155- message = AIMessageChunk (content = "The word eudca has 5 letters." ),
154+ message = AIMessageChunk (
155+ content = "The word eudca has 5 letters." ,
156+ usage_metadata = {
157+ "input_tokens" : 89 ,
158+ "output_tokens" : 28 ,
159+ "total_tokens" : 117 ,
160+ "input_token_details" : {"audio" : 0 , "cache_read" : 0 },
161+ "output_token_details" : {"audio" : 0 , "reasoning" : 0 },
162+ },
163+ ),
156164 ),
157165 ChatGenerationChunk (
158166 type = "ChatGenerationChunk" ,
@@ -182,15 +190,22 @@ def test_langchain_agent(
182190 assert len (chat_spans ) == 2
183191
184192 # We can't guarantee anything about the "shape" of the langchain execution graph
185- assert len (list (x for x in tx ["spans" ] if x ["op" ] == "gen_ai.run " )) > 0
193+ assert len (list (x for x in tx ["spans" ] if x ["op" ] == "gen_ai.chat " )) > 0
186194
187- if use_unknown_llm_type :
188- assert "gen_ai.usage.input_tokens" in chat_spans [0 ]["data" ]
189- assert "gen_ai.usage.total_tokens" in chat_spans [0 ]["data" ]
190- else :
191- # important: to avoid double counting, we do *not* measure
192- # tokens used if we have an explicit integration (e.g. OpenAI)
193- assert "measurements" not in chat_spans [0 ]
195+ assert "gen_ai.usage.input_tokens" in chat_spans [0 ]["data" ]
196+ assert "gen_ai.usage.output_tokens" in chat_spans [0 ]["data" ]
197+ assert "gen_ai.usage.total_tokens" in chat_spans [0 ]["data" ]
198+
199+ assert chat_spans [0 ]["data" ]["gen_ai.usage.input_tokens" ] == 142
200+ assert chat_spans [0 ]["data" ]["gen_ai.usage.output_tokens" ] == 50
201+ assert chat_spans [0 ]["data" ]["gen_ai.usage.total_tokens" ] == 192
202+
203+ assert "gen_ai.usage.input_tokens" in chat_spans [1 ]["data" ]
204+ assert "gen_ai.usage.output_tokens" in chat_spans [1 ]["data" ]
205+ assert "gen_ai.usage.total_tokens" in chat_spans [1 ]["data" ]
206+ assert chat_spans [1 ]["data" ]["gen_ai.usage.input_tokens" ] == 89
207+ assert chat_spans [1 ]["data" ]["gen_ai.usage.output_tokens" ] == 28
208+ assert chat_spans [1 ]["data" ]["gen_ai.usage.total_tokens" ] == 117
194209
195210 if send_default_pii and include_prompts :
196211 assert (
@@ -311,15 +326,33 @@ def test_span_origin(sentry_init, capture_events):
311326 ),
312327 ChatGenerationChunk (
313328 type = "ChatGenerationChunk" ,
314- message = AIMessageChunk (content = "5" ),
329+ message = AIMessageChunk (
330+ content = "5" ,
331+ usage_metadata = {
332+ "input_tokens" : 142 ,
333+ "output_tokens" : 50 ,
334+ "total_tokens" : 192 ,
335+ "input_token_details" : {"audio" : 0 , "cache_read" : 0 },
336+ "output_token_details" : {"audio" : 0 , "reasoning" : 0 },
337+ },
338+ ),
315339 generation_info = {"finish_reason" : "function_call" },
316340 ),
317341 ],
318342 [
319343 ChatGenerationChunk (
320344 text = "The word eudca has 5 letters." ,
321345 type = "ChatGenerationChunk" ,
322- message = AIMessageChunk (content = "The word eudca has 5 letters." ),
346+ message = AIMessageChunk (
347+ content = "The word eudca has 5 letters." ,
348+ usage_metadata = {
349+ "input_tokens" : 89 ,
350+ "output_tokens" : 28 ,
351+ "total_tokens" : 117 ,
352+ "input_token_details" : {"audio" : 0 , "cache_read" : 0 },
353+ "output_token_details" : {"audio" : 0 , "reasoning" : 0 },
354+ },
355+ ),
323356 ),
324357 ChatGenerationChunk (
325358 type = "ChatGenerationChunk" ,
0 commit comments