trickle-observe 0.2.123 → 0.2.125

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.
@@ -54,10 +54,10 @@ 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
57
+ // Graduated token budget enforcement: alert 50%, warn 80%, exceeded 100%
58
58
  let cumulativeTokens = 0;
59
59
  let cumulativeCost = 0;
60
- let budgetWarned = false;
60
+ let budgetLevel = 0; // 0=ok, 1=alert(50%), 2=warn(80%), 3=exceeded(100%)
61
61
  const TOKEN_BUDGET = parseInt(process.env.TRICKLE_TOKEN_BUDGET || '0', 10);
62
62
  const COST_BUDGET = parseFloat(process.env.TRICKLE_COST_BUDGET || '0');
63
63
  // Approximate pricing per 1M tokens (USD) — used for cost estimation
@@ -72,6 +72,11 @@ const PRICING = {
72
72
  'claude-3-5-sonnet-20241022': { input: 3, output: 15 },
73
73
  'claude-3-5-haiku-20241022': { input: 0.8, output: 4 },
74
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 },
75
80
  'gemini-2.5-flash-lite': { input: 0.1, output: 0.4 },
76
81
  'gemini-2.5-flash': { input: 0.3, output: 2.5 },
77
82
  'gemini-2.5-pro': { input: 1.25, output: 10 },
@@ -97,16 +102,26 @@ function writeLlmEvent(event) {
97
102
  // Track cumulative usage for budget enforcement
98
103
  cumulativeTokens += event.totalTokens || 0;
99
104
  cumulativeCost += event.estimatedCostUsd || 0;
100
- if (!budgetWarned) {
101
- if (TOKEN_BUDGET > 0 && cumulativeTokens > TOKEN_BUDGET) {
102
- console.warn(`[trickle] ⚠ Token budget exceeded: ${cumulativeTokens} tokens used (budget: ${TOKEN_BUDGET}). Set TRICKLE_TOKEN_BUDGET=0 to disable.`);
103
- budgetWarned = true;
105
+ // Graduated budget: alert at 50%, warn at 80%, exceeded at 100%
106
+ function checkBudget(current, budget, unit) {
107
+ if (budget <= 0)
108
+ return;
109
+ const pct = current / budget;
110
+ if (pct >= 1.0 && budgetLevel < 3) {
111
+ budgetLevel = 3;
112
+ console.warn(`[trickle] ❌ ${unit} budget EXCEEDED: ${current.toFixed(4)} / ${budget} (100%+). Consider stopping.`);
113
+ }
114
+ else if (pct >= 0.8 && budgetLevel < 2) {
115
+ budgetLevel = 2;
116
+ console.warn(`[trickle] ⚠ ${unit} budget at 80%: ${current.toFixed(4)} / ${budget}. Approaching limit.`);
104
117
  }
105
- if (COST_BUDGET > 0 && cumulativeCost > COST_BUDGET) {
106
- console.warn(`[trickle] ⚠ Cost budget exceeded: $${cumulativeCost.toFixed(4)} spent (budget: $${COST_BUDGET.toFixed(4)}). Set TRICKLE_COST_BUDGET=0 to disable.`);
107
- budgetWarned = true;
118
+ else if (pct >= 0.5 && budgetLevel < 1) {
119
+ budgetLevel = 1;
120
+ console.warn(`[trickle] ${unit} budget at 50%: ${current.toFixed(4)} / ${budget}.`);
108
121
  }
109
122
  }
123
+ checkBudget(cumulativeTokens, TOKEN_BUDGET, 'Token');
124
+ checkBudget(cumulativeCost, COST_BUDGET, 'Cost ($)');
110
125
  try {
111
126
  fs.appendFileSync(getLlmFile(), JSON.stringify(event) + '\n');
112
127
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trickle-observe",
3
- "version": "0.2.123",
3
+ "version": "0.2.125",
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",
@@ -18,10 +18,10 @@ let eventCount = 0;
18
18
  const MAX_LLM_EVENTS = 500;
19
19
  const TRUNCATE_LEN = 500;
20
20
 
21
- // Token budget enforcement
21
+ // Graduated token budget enforcement: alert 50%, warn 80%, exceeded 100%
22
22
  let cumulativeTokens = 0;
23
23
  let cumulativeCost = 0;
24
- let budgetWarned = false;
24
+ let budgetLevel = 0; // 0=ok, 1=alert(50%), 2=warn(80%), 3=exceeded(100%)
25
25
  const TOKEN_BUDGET = parseInt(process.env.TRICKLE_TOKEN_BUDGET || '0', 10);
26
26
  const COST_BUDGET = parseFloat(process.env.TRICKLE_COST_BUDGET || '0');
27
27
 
@@ -37,6 +37,11 @@ const PRICING: Record<string, { input: number; output: number }> = {
37
37
  'claude-3-5-sonnet-20241022': { input: 3, output: 15 },
38
38
  'claude-3-5-haiku-20241022': { input: 0.8, output: 4 },
39
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 },
40
45
  'gemini-2.5-flash-lite': { input: 0.1, output: 0.4 },
41
46
  'gemini-2.5-flash': { input: 0.3, output: 2.5 },
42
47
  'gemini-2.5-pro': { input: 1.25, output: 10 },
@@ -83,16 +88,23 @@ function writeLlmEvent(event: LlmEvent): void {
83
88
  cumulativeTokens += event.totalTokens || 0;
84
89
  cumulativeCost += event.estimatedCostUsd || 0;
85
90
 
86
- if (!budgetWarned) {
87
- if (TOKEN_BUDGET > 0 && cumulativeTokens > TOKEN_BUDGET) {
88
- console.warn(`[trickle] ⚠ Token budget exceeded: ${cumulativeTokens} tokens used (budget: ${TOKEN_BUDGET}). Set TRICKLE_TOKEN_BUDGET=0 to disable.`);
89
- budgetWarned = true;
90
- }
91
- if (COST_BUDGET > 0 && cumulativeCost > COST_BUDGET) {
92
- console.warn(`[trickle] Cost budget exceeded: $${cumulativeCost.toFixed(4)} spent (budget: $${COST_BUDGET.toFixed(4)}). Set TRICKLE_COST_BUDGET=0 to disable.`);
93
- budgetWarned = true;
91
+ // Graduated budget: alert at 50%, warn at 80%, exceeded at 100%
92
+ function checkBudget(current: number, budget: number, unit: string): void {
93
+ if (budget <= 0) return;
94
+ const pct = current / budget;
95
+ if (pct >= 1.0 && budgetLevel < 3) {
96
+ budgetLevel = 3;
97
+ console.warn(`[trickle] ${unit} budget EXCEEDED: ${current.toFixed(4)} / ${budget} (100%+). Consider stopping.`);
98
+ } else if (pct >= 0.8 && budgetLevel < 2) {
99
+ budgetLevel = 2;
100
+ console.warn(`[trickle] ⚠ ${unit} budget at 80%: ${current.toFixed(4)} / ${budget}. Approaching limit.`);
101
+ } else if (pct >= 0.5 && budgetLevel < 1) {
102
+ budgetLevel = 1;
103
+ console.warn(`[trickle] ℹ ${unit} budget at 50%: ${current.toFixed(4)} / ${budget}.`);
94
104
  }
95
105
  }
106
+ checkBudget(cumulativeTokens, TOKEN_BUDGET, 'Token');
107
+ checkBudget(cumulativeCost, COST_BUDGET, 'Cost ($)');
96
108
  try {
97
109
  fs.appendFileSync(getLlmFile(), JSON.stringify(event) + '\n');
98
110
  } catch {}