@costcanary/sdk 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +13 -3
- package/src/index.js +9 -3
package/package.json
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@costcanary/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Track per-user LLM costs in 1 line of code",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
-
"files": [
|
|
7
|
-
|
|
6
|
+
"files": [
|
|
7
|
+
"src/",
|
|
8
|
+
"README.md"
|
|
9
|
+
],
|
|
10
|
+
"keywords": [
|
|
11
|
+
"llm",
|
|
12
|
+
"cost",
|
|
13
|
+
"profitability",
|
|
14
|
+
"openai",
|
|
15
|
+
"anthropic",
|
|
16
|
+
"tracking"
|
|
17
|
+
],
|
|
8
18
|
"license": "MIT",
|
|
9
19
|
"repository": {
|
|
10
20
|
"type": "git",
|
package/src/index.js
CHANGED
|
@@ -25,9 +25,15 @@ async function track({ userId, feature, fn }) {
|
|
|
25
25
|
const duration = Date.now() - start;
|
|
26
26
|
|
|
27
27
|
const usage = result?.usage || {};
|
|
28
|
-
const model = result?.model || "unknown";
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
const model = result?.model || result?.modelVersion || "unknown";
|
|
29
|
+
|
|
30
|
+
// OpenAI format: prompt_tokens / completion_tokens
|
|
31
|
+
// Gemini format: usageMetadata.promptTokenCount / candidatesTokenCount
|
|
32
|
+
const meta = result?.usageMetadata || {};
|
|
33
|
+
const promptTokens = usage.prompt_tokens || meta.promptTokenCount || 0;
|
|
34
|
+
const completionTokens = usage.completion_tokens || meta.candidatesTokenCount || 0;
|
|
35
|
+
const tokens = promptTokens + completionTokens;
|
|
36
|
+
const cost = _calculateCost(model, { prompt_tokens: promptTokens, completion_tokens: completionTokens });
|
|
31
37
|
|
|
32
38
|
_sendToIngest({ endUserId: userId, feature, model, tokens, cost, duration,
|
|
33
39
|
timestamp: new Date().toISOString() });
|