trickle-observe 0.2.122 → 0.2.124
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/dist/llm-observer.js +24 -0
- package/package.json +1 -1
- package/src/llm-observer.ts +27 -0
package/dist/llm-observer.js
CHANGED
|
@@ -54,6 +54,12 @@ let llmFile = null;
|
|
|
54
54
|
let eventCount = 0;
|
|
55
55
|
const MAX_LLM_EVENTS = 500;
|
|
56
56
|
const TRUNCATE_LEN = 500;
|
|
57
|
+
// Token budget enforcement
|
|
58
|
+
let cumulativeTokens = 0;
|
|
59
|
+
let cumulativeCost = 0;
|
|
60
|
+
let budgetWarned = false;
|
|
61
|
+
const TOKEN_BUDGET = parseInt(process.env.TRICKLE_TOKEN_BUDGET || '0', 10);
|
|
62
|
+
const COST_BUDGET = parseFloat(process.env.TRICKLE_COST_BUDGET || '0');
|
|
57
63
|
// Approximate pricing per 1M tokens (USD) — used for cost estimation
|
|
58
64
|
const PRICING = {
|
|
59
65
|
'gpt-4o': { input: 2.5, output: 10 },
|
|
@@ -66,6 +72,11 @@ const PRICING = {
|
|
|
66
72
|
'claude-3-5-sonnet-20241022': { input: 3, output: 15 },
|
|
67
73
|
'claude-3-5-haiku-20241022': { input: 0.8, output: 4 },
|
|
68
74
|
'claude-3-haiku-20240307': { input: 0.25, output: 1.25 },
|
|
75
|
+
'mistral-large': { input: 2, output: 6 },
|
|
76
|
+
'mistral-small': { input: 0.1, output: 0.3 },
|
|
77
|
+
'codestral': { input: 0.3, output: 0.9 },
|
|
78
|
+
'command-r-plus': { input: 2.5, output: 10 },
|
|
79
|
+
'command-r': { input: 0.15, output: 0.6 },
|
|
69
80
|
'gemini-2.5-flash-lite': { input: 0.1, output: 0.4 },
|
|
70
81
|
'gemini-2.5-flash': { input: 0.3, output: 2.5 },
|
|
71
82
|
'gemini-2.5-pro': { input: 1.25, output: 10 },
|
|
@@ -88,6 +99,19 @@ function writeLlmEvent(event) {
|
|
|
88
99
|
if (eventCount >= MAX_LLM_EVENTS)
|
|
89
100
|
return;
|
|
90
101
|
eventCount++;
|
|
102
|
+
// Track cumulative usage for budget enforcement
|
|
103
|
+
cumulativeTokens += event.totalTokens || 0;
|
|
104
|
+
cumulativeCost += event.estimatedCostUsd || 0;
|
|
105
|
+
if (!budgetWarned) {
|
|
106
|
+
if (TOKEN_BUDGET > 0 && cumulativeTokens > TOKEN_BUDGET) {
|
|
107
|
+
console.warn(`[trickle] ⚠ Token budget exceeded: ${cumulativeTokens} tokens used (budget: ${TOKEN_BUDGET}). Set TRICKLE_TOKEN_BUDGET=0 to disable.`);
|
|
108
|
+
budgetWarned = true;
|
|
109
|
+
}
|
|
110
|
+
if (COST_BUDGET > 0 && cumulativeCost > COST_BUDGET) {
|
|
111
|
+
console.warn(`[trickle] ⚠ Cost budget exceeded: $${cumulativeCost.toFixed(4)} spent (budget: $${COST_BUDGET.toFixed(4)}). Set TRICKLE_COST_BUDGET=0 to disable.`);
|
|
112
|
+
budgetWarned = true;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
91
115
|
try {
|
|
92
116
|
fs.appendFileSync(getLlmFile(), JSON.stringify(event) + '\n');
|
|
93
117
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trickle-observe",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.124",
|
|
4
4
|
"description": "Zero-code runtime observability for JavaScript/TypeScript. Auto-instruments Express, OpenAI, Anthropic, Gemini, MCP. Captures functions, variables, LLM calls, agent workflows.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
package/src/llm-observer.ts
CHANGED
|
@@ -18,6 +18,13 @@ let eventCount = 0;
|
|
|
18
18
|
const MAX_LLM_EVENTS = 500;
|
|
19
19
|
const TRUNCATE_LEN = 500;
|
|
20
20
|
|
|
21
|
+
// Token budget enforcement
|
|
22
|
+
let cumulativeTokens = 0;
|
|
23
|
+
let cumulativeCost = 0;
|
|
24
|
+
let budgetWarned = false;
|
|
25
|
+
const TOKEN_BUDGET = parseInt(process.env.TRICKLE_TOKEN_BUDGET || '0', 10);
|
|
26
|
+
const COST_BUDGET = parseFloat(process.env.TRICKLE_COST_BUDGET || '0');
|
|
27
|
+
|
|
21
28
|
// Approximate pricing per 1M tokens (USD) — used for cost estimation
|
|
22
29
|
const PRICING: Record<string, { input: number; output: number }> = {
|
|
23
30
|
'gpt-4o': { input: 2.5, output: 10 },
|
|
@@ -30,6 +37,11 @@ const PRICING: Record<string, { input: number; output: number }> = {
|
|
|
30
37
|
'claude-3-5-sonnet-20241022': { input: 3, output: 15 },
|
|
31
38
|
'claude-3-5-haiku-20241022': { input: 0.8, output: 4 },
|
|
32
39
|
'claude-3-haiku-20240307': { input: 0.25, output: 1.25 },
|
|
40
|
+
'mistral-large': { input: 2, output: 6 },
|
|
41
|
+
'mistral-small': { input: 0.1, output: 0.3 },
|
|
42
|
+
'codestral': { input: 0.3, output: 0.9 },
|
|
43
|
+
'command-r-plus': { input: 2.5, output: 10 },
|
|
44
|
+
'command-r': { input: 0.15, output: 0.6 },
|
|
33
45
|
'gemini-2.5-flash-lite': { input: 0.1, output: 0.4 },
|
|
34
46
|
'gemini-2.5-flash': { input: 0.3, output: 2.5 },
|
|
35
47
|
'gemini-2.5-pro': { input: 1.25, output: 10 },
|
|
@@ -71,6 +83,21 @@ interface LlmEvent {
|
|
|
71
83
|
function writeLlmEvent(event: LlmEvent): void {
|
|
72
84
|
if (eventCount >= MAX_LLM_EVENTS) return;
|
|
73
85
|
eventCount++;
|
|
86
|
+
|
|
87
|
+
// Track cumulative usage for budget enforcement
|
|
88
|
+
cumulativeTokens += event.totalTokens || 0;
|
|
89
|
+
cumulativeCost += event.estimatedCostUsd || 0;
|
|
90
|
+
|
|
91
|
+
if (!budgetWarned) {
|
|
92
|
+
if (TOKEN_BUDGET > 0 && cumulativeTokens > TOKEN_BUDGET) {
|
|
93
|
+
console.warn(`[trickle] ⚠ Token budget exceeded: ${cumulativeTokens} tokens used (budget: ${TOKEN_BUDGET}). Set TRICKLE_TOKEN_BUDGET=0 to disable.`);
|
|
94
|
+
budgetWarned = true;
|
|
95
|
+
}
|
|
96
|
+
if (COST_BUDGET > 0 && cumulativeCost > COST_BUDGET) {
|
|
97
|
+
console.warn(`[trickle] ⚠ Cost budget exceeded: $${cumulativeCost.toFixed(4)} spent (budget: $${COST_BUDGET.toFixed(4)}). Set TRICKLE_COST_BUDGET=0 to disable.`);
|
|
98
|
+
budgetWarned = true;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
74
101
|
try {
|
|
75
102
|
fs.appendFileSync(getLlmFile(), JSON.stringify(event) + '\n');
|
|
76
103
|
} catch {}
|