22import uuid
33from typing import Any , Dict , List , Optional
44
5+ from posthog .ai .types import TokenUsage
6+
57try :
68 import openai
79except ImportError :
@@ -120,7 +122,7 @@ def _create_streaming(
120122 ** kwargs : Any ,
121123 ):
122124 start_time = time .time ()
123- usage_stats : Dict [ str , int ] = {}
125+ usage_stats : TokenUsage = TokenUsage ()
124126 final_content = []
125127 response = self ._original .create (** kwargs )
126128
@@ -171,14 +173,13 @@ def _capture_streaming_event(
171173 posthog_privacy_mode : bool ,
172174 posthog_groups : Optional [Dict [str , Any ]],
173175 kwargs : Dict [str , Any ],
174- usage_stats : Dict [ str , int ] ,
176+ usage_stats : TokenUsage ,
175177 latency : float ,
176178 output : Any ,
177179 available_tool_calls : Optional [List [Dict [str , Any ]]] = None ,
178180 ):
179181 from posthog .ai .types import StreamingEventData
180182 from posthog .ai .openai .openai_converter import (
181- standardize_openai_usage ,
182183 format_openai_streaming_input ,
183184 format_openai_streaming_output ,
184185 )
@@ -195,7 +196,7 @@ def _capture_streaming_event(
195196 kwargs = kwargs ,
196197 formatted_input = sanitized_input ,
197198 formatted_output = format_openai_streaming_output (output , "responses" ),
198- usage_stats = standardize_openai_usage ( usage_stats , "responses" ) ,
199+ usage_stats = usage_stats ,
199200 latency = latency ,
200201 distinct_id = posthog_distinct_id ,
201202 trace_id = posthog_trace_id ,
@@ -316,7 +317,7 @@ def _create_streaming(
316317 ** kwargs : Any ,
317318 ):
318319 start_time = time .time ()
319- usage_stats : Dict [ str , int ] = {}
320+ usage_stats : TokenUsage = TokenUsage ()
320321 accumulated_content = []
321322 accumulated_tool_calls : Dict [int , Dict [str , Any ]] = {}
322323 if "stream_options" not in kwargs :
@@ -387,15 +388,14 @@ def _capture_streaming_event(
387388 posthog_privacy_mode : bool ,
388389 posthog_groups : Optional [Dict [str , Any ]],
389390 kwargs : Dict [str , Any ],
390- usage_stats : Dict [ str , int ] ,
391+ usage_stats : TokenUsage ,
391392 latency : float ,
392393 output : Any ,
393394 tool_calls : Optional [List [Dict [str , Any ]]] = None ,
394395 available_tool_calls : Optional [List [Dict [str , Any ]]] = None ,
395396 ):
396397 from posthog .ai .types import StreamingEventData
397398 from posthog .ai .openai .openai_converter import (
398- standardize_openai_usage ,
399399 format_openai_streaming_input ,
400400 format_openai_streaming_output ,
401401 )
@@ -412,7 +412,7 @@ def _capture_streaming_event(
412412 kwargs = kwargs ,
413413 formatted_input = sanitized_input ,
414414 formatted_output = format_openai_streaming_output (output , "chat" , tool_calls ),
415- usage_stats = standardize_openai_usage ( usage_stats , "chat" ) ,
415+ usage_stats = usage_stats ,
416416 latency = latency ,
417417 distinct_id = posthog_distinct_id ,
418418 trace_id = posthog_trace_id ,
0 commit comments