Skip to content

Commit 1fc9e76

Browse files
committed
feat: track timeToFirstToken in LDAIConfigTracker
1 parent 1ff96ce commit 1fc9e76

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

packages/sdk/server-ai/__tests__/LDAIConfigTrackerImpl.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ it('tracks duration of async function', async () => {
4646
);
4747
});
4848

49+
it('tracks time to first token', () => {
50+
const tracker = new LDAIConfigTrackerImpl(mockLdClient, configKey, variationKey, testContext);
51+
tracker.trackTimeToFirstToken(1000);
52+
53+
expect(mockTrack).toHaveBeenCalledWith(
54+
'$ld:ai:tokens:ttf',
55+
testContext,
56+
{ configKey, variationKey },
57+
1000,
58+
);
59+
});
60+
4961
it('tracks positive feedback', () => {
5062
const tracker = new LDAIConfigTrackerImpl(mockLdClient, configKey, variationKey, testContext);
5163
tracker.trackFeedback({ kind: LDFeedbackKind.Positive });

packages/sdk/server-ai/src/LDAIConfigTrackerImpl.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ export class LDAIConfigTrackerImpl implements LDAIConfigTracker {
4141
}
4242
}
4343

44+
trackTimeToFirstToken(timeToFirstTokenMs: number) {
45+
this._trackedMetrics.timeToFirstTokenMs = timeToFirstTokenMs;
46+
this._ldClient.track(
47+
'$ld:ai:tokens:ttf',
48+
this._context,
49+
this._getTrackData(),
50+
timeToFirstTokenMs,
51+
);
52+
}
53+
4454
trackFeedback(feedback: { kind: LDFeedbackKind }): void {
4555
this._trackedMetrics.feedback = feedback;
4656
if (feedback.kind === LDFeedbackKind.Positive) {

packages/sdk/server-ai/src/api/config/LDAIConfigTracker.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ export interface LDAIMetricSummary {
2323
* Any sentiment about the generation.
2424
*/
2525
feedback?: { kind: LDFeedbackKind };
26+
27+
/**
28+
* Time to first token for this generation.
29+
*/
30+
timeToFirstTokenMs?: number;
2631
}
2732

2833
/**
@@ -62,6 +67,13 @@ export interface LDAIConfigTracker {
6267
*/
6368
trackFeedback(feedback: { kind: LDFeedbackKind }): void;
6469

70+
/**
71+
* Track the time to first token for this generation.
72+
*
73+
* @param timeToFirstTokenMs The duration in milliseconds.
74+
*/
75+
trackTimeToFirstToken(timeToFirstTokenMs: number): void;
76+
6577
/**
6678
* Track the duration of execution of the provided function.
6779
*

0 commit comments

Comments
 (0)