@@ -67,11 +67,6 @@ def initialize(ld_client:, variation_key:, config_key:, version:, model_name:, p
6767 @context = context
6868 @summary = MetricSummary . new
6969 @run_id = run_id
70- @tracked_duration = false
71- @tracked_time_to_first_token = false
72- @tracked_tokens = false
73- @tracked_success = nil
74- @tracked_feedback = false
7570 @logger = LaunchDarkly ::Server ::AI . default_logger
7671 end
7772
@@ -124,11 +119,10 @@ def self.from_resumption_token(token:, ld_client:, context:)
124119 # @param duration [Integer] The duration in milliseconds
125120 #
126121 def track_duration ( duration )
127- if @tracked_duration
122+ unless @summary . duration . nil?
128123 @logger &.warn ( "Duration has already been tracked for this execution." )
129124 return
130125 end
131- @tracked_duration = true
132126 @summary . duration = duration
133127 @ld_client . track (
134128 '$ld:ai:duration:total' ,
@@ -158,11 +152,10 @@ def track_duration_of(&block)
158152 # @param duration [Integer] The duration in milliseconds
159153 #
160154 def track_time_to_first_token ( time_to_first_token )
161- if @tracked_time_to_first_token
155+ unless @summary . time_to_first_token . nil?
162156 @logger &.warn ( "Time to first token has already been tracked for this execution." )
163157 return
164158 end
165- @tracked_time_to_first_token = true
166159 @summary . time_to_first_token = time_to_first_token
167160 @ld_client . track (
168161 '$ld:ai:tokens:ttf' ,
@@ -178,11 +171,10 @@ def track_time_to_first_token(time_to_first_token)
178171 # @param kind [Symbol] The kind of feedback (:positive or :negative)
179172 #
180173 def track_feedback ( kind :)
181- if @tracked_feedback
174+ unless @summary . feedback . nil?
182175 @logger &.warn ( "Feedback has already been tracked for this execution." )
183176 return
184177 end
185- @tracked_feedback = true
186178 @summary . feedback = kind
187179 event_name = kind == :positive ? '$ld:ai:feedback:user:positive' : '$ld:ai:feedback:user:negative'
188180 @ld_client . track (
@@ -197,11 +189,10 @@ def track_feedback(kind:)
197189 # Track a successful AI generation
198190 #
199191 def track_success
200- unless @tracked_success . nil?
192+ unless @summary . success . nil?
201193 @logger &.warn ( "Success or error has already been tracked for this execution." )
202194 return
203195 end
204- @tracked_success = true
205196 @summary . success = true
206197 @ld_client . track (
207198 '$ld:ai:generation:success' ,
@@ -215,11 +206,10 @@ def track_success
215206 # Track an error in AI generation
216207 #
217208 def track_error
218- unless @tracked_success . nil?
209+ unless @summary . success . nil?
219210 @logger &.warn ( "Success or error has already been tracked for this execution." )
220211 return
221212 end
222- @tracked_success = false
223213 @summary . success = false
224214 @ld_client . track (
225215 '$ld:ai:generation:error' ,
@@ -235,11 +225,10 @@ def track_error
235225 # @param token_usage [TokenUsage] An object containing token usage details
236226 #
237227 def track_tokens ( token_usage )
238- if @tracked_tokens
228+ unless @summary . usage . nil?
239229 @logger &.warn ( "Tokens have already been tracked for this execution." )
240230 return
241231 end
242- @tracked_tokens = true
243232 @summary . usage = token_usage
244233 if token_usage . total . positive?
245234 @ld_client . track (
0 commit comments