Skip to content

Commit 7839104

Browse files
jsonbaileyclaude
andcommitted
refactor: Use LDAIMetricSummary fields as at-most-once guards
Remove the redundant _tracked dict from LDAIConfigTracker. The summary already stores each metric with None as the unset sentinel, so the nil-check on summary properties serves as the at-most-once guard. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d895e64 commit 7839104

1 file changed

Lines changed: 6 additions & 13 deletions

File tree

packages/sdk/server-ai/src/ldai/tracker.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ def __init__(
109109
self._graph_key = graph_key
110110
self._summary = LDAIMetricSummary()
111111
self._run_id = run_id
112-
self._tracked: Dict[str, bool] = {}
113112

114113
@property
115114
def resumption_token(self) -> str:
@@ -152,10 +151,9 @@ def track_duration(self, duration: int) -> None:
152151
153152
:param duration: Duration in milliseconds.
154153
"""
155-
if 'duration' in self._tracked:
154+
if self._summary.duration is not None:
156155
logger.warning("Duration has already been tracked for this execution.")
157156
return
158-
self._tracked['duration'] = True
159157
self._summary._duration = duration
160158
self._ld_client.track(
161159
"$ld:ai:duration:total", self._context, self.__get_track_data(), duration
@@ -167,10 +165,9 @@ def track_time_to_first_token(self, time_to_first_token: int) -> None:
167165
168166
:param time_to_first_token: Time to first token in milliseconds.
169167
"""
170-
if 'time_to_first_token' in self._tracked:
168+
if self._summary.time_to_first_token is not None:
171169
logger.warning("Time to first token has already been tracked for this execution.")
172170
return
173-
self._tracked['time_to_first_token'] = True
174171
self._summary._time_to_first_token = time_to_first_token
175172
self._ld_client.track(
176173
"$ld:ai:tokens:ttf",
@@ -296,10 +293,9 @@ def track_feedback(self, feedback: Dict[str, FeedbackKind]) -> None:
296293
297294
:param feedback: Dictionary containing feedback kind.
298295
"""
299-
if 'feedback' in self._tracked:
296+
if self._summary.feedback is not None:
300297
logger.warning("Feedback has already been tracked for this execution.")
301298
return
302-
self._tracked['feedback'] = True
303299
self._summary._feedback = feedback
304300
if feedback["kind"] == FeedbackKind.Positive:
305301
self._ld_client.track(
@@ -320,10 +316,9 @@ def track_success(self) -> None:
320316
"""
321317
Track a successful AI generation.
322318
"""
323-
if 'success' in self._tracked:
319+
if self._summary.success is not None:
324320
logger.warning("Success has already been tracked for this execution.")
325321
return
326-
self._tracked['success'] = True
327322
self._summary._success = True
328323
self._ld_client.track(
329324
"$ld:ai:generation:success", self._context, self.__get_track_data(), 1
@@ -333,10 +328,9 @@ def track_error(self) -> None:
333328
"""
334329
Track an unsuccessful AI generation attempt.
335330
"""
336-
if 'success' in self._tracked:
331+
if self._summary.success is not None:
337332
logger.warning("Success has already been tracked for this execution.")
338333
return
339-
self._tracked['success'] = True
340334
self._summary._success = False
341335
self._ld_client.track(
342336
"$ld:ai:generation:error", self._context, self.__get_track_data(), 1
@@ -403,10 +397,9 @@ def track_tokens(self, tokens: TokenUsage) -> None:
403397
404398
:param tokens: Token usage data from either custom, OpenAI, or Bedrock sources.
405399
"""
406-
if 'tokens' in self._tracked:
400+
if self._summary.usage is not None:
407401
logger.warning("Tokens have already been tracked for this execution.")
408402
return
409-
self._tracked['tokens'] = True
410403
self._summary._usage = tokens
411404
td = self.__get_track_data()
412405
if tokens.total > 0:

0 commit comments

Comments
 (0)