Skip to content

Commit 9aa2752

Browse files
adding groq api tokens tracking logic
1 parent 4beacbf commit 9aa2752

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

dist/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,18 @@ RESPOND ONLY WITH THE JSON OBJECT. NO OTHER TEXT.`;
12871287
throw new LLMApiError(response.status, `Groq API error: ${response.status} - ${errorData.error?.message || "Unknown error"}`);
12881288
}
12891289
const data = (await response.json());
1290+
// Track token usage from Groq response
1291+
if (data.usage) {
1292+
if (data.usage.prompt_tokens) {
1293+
this.totalTokens.prompt += data.usage.prompt_tokens;
1294+
}
1295+
if (data.usage.completion_tokens) {
1296+
this.totalTokens.completion += data.usage.completion_tokens;
1297+
}
1298+
if (data.usage.total_tokens) {
1299+
this.totalTokens.total += data.usage.total_tokens;
1300+
}
1301+
}
12901302
return data;
12911303
}
12921304
catch (error) {

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/llm/llm-client.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ interface GroqResponse {
5555
content?: string;
5656
};
5757
}>;
58+
usage?: {
59+
prompt_tokens?: number;
60+
completion_tokens?: number;
61+
total_tokens?: number;
62+
};
5863
data?: Array<Record<string, unknown>>;
5964
models?: Array<Record<string, unknown>>;
6065
error?: {
@@ -578,6 +583,20 @@ RESPOND ONLY WITH THE JSON OBJECT. NO OTHER TEXT.`;
578583
}
579584

580585
const data = (await response.json()) as GroqResponse;
586+
587+
// Track token usage from Groq response
588+
if (data.usage) {
589+
if (data.usage.prompt_tokens) {
590+
this.totalTokens.prompt += data.usage.prompt_tokens;
591+
}
592+
if (data.usage.completion_tokens) {
593+
this.totalTokens.completion += data.usage.completion_tokens;
594+
}
595+
if (data.usage.total_tokens) {
596+
this.totalTokens.total += data.usage.total_tokens;
597+
}
598+
}
599+
581600
return data;
582601
} catch (error) {
583602
if (attempt < this.maxRetries && this.isRetryableError(error)) {

0 commit comments

Comments
 (0)