Spaces:
Sleeping
Sleeping
Commit Β·
3443a75
1
Parent(s): edc0702
deploy: alert at 40/50/60/70/80/90/95/100% of each key's daily budget
Browse files- scripts/test-groq-keys.mjs +41 -9
- src/modules/ai/groq-key-pool.ts +10 -2
scripts/test-groq-keys.mjs
CHANGED
|
@@ -96,27 +96,59 @@ test('a key that reports room again is back in rotation', () => {
|
|
| 96 |
|
| 97 |
console.log('Alerts β once per budget window, not once per request');
|
| 98 |
|
| 99 |
-
test('
|
|
|
|
| 100 |
const p = pool();
|
| 101 |
-
|
| 102 |
-
assert.deepEqual(
|
| 103 |
-
assert.deepEqual(
|
| 104 |
-
assert.deepEqual(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
});
|
| 106 |
|
| 107 |
-
test('
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
const p = pool();
|
| 109 |
p.recordHeaders('GROQ_API_KEY', headers({ remaining: 200 }));
|
| 110 |
-
assert.deepEqual(p.recordHeaders('GROQ_API_KEY', headers({ remaining: 0 })), [1]);
|
| 111 |
});
|
| 112 |
|
| 113 |
-
test('a fresh window lets the
|
| 114 |
const p = pool();
|
| 115 |
p.recordHeaders('GROQ_API_KEY', headers({ remaining: 0, reset: '1h0m0s' }));
|
| 116 |
// Tomorrow's budget: the reset jumps forward and the key is full again.
|
| 117 |
const next = p.recordHeaders('GROQ_API_KEY', headers({ remaining: 1000, reset: '24h0m0s' }));
|
| 118 |
assert.deepEqual(next, []);
|
| 119 |
-
assert.deepEqual(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
});
|
| 121 |
|
| 122 |
test('a per-minute ceiling never emails anybody', () => {
|
|
|
|
| 96 |
|
| 97 |
console.log('Alerts β once per budget window, not once per request');
|
| 98 |
|
| 99 |
+
test('a day of steady use walks up the ladder, one mail per step', () => {
|
| 100 |
+
// 40, 50, 60, 70, 80, 90, 95, 100 β each fires as it is passed, and never again.
|
| 101 |
const p = pool();
|
| 102 |
+
const at = (remaining) => p.recordHeaders('GROQ_API_KEY', headers({ remaining }));
|
| 103 |
+
assert.deepEqual(at(700), []); // 30% used β below the first step
|
| 104 |
+
assert.deepEqual(at(600), [0.4]); // 40%
|
| 105 |
+
assert.deepEqual(at(500), [0.5]);
|
| 106 |
+
assert.deepEqual(at(400), [0.6]);
|
| 107 |
+
assert.deepEqual(at(300), [0.7]);
|
| 108 |
+
assert.deepEqual(at(200), [0.8]);
|
| 109 |
+
assert.deepEqual(at(100), [0.9]);
|
| 110 |
+
assert.deepEqual(at(50), [0.95]);
|
| 111 |
+
assert.deepEqual(at(0), [1]);
|
| 112 |
});
|
| 113 |
|
| 114 |
+
test('a threshold already passed never reports twice', () => {
|
| 115 |
+
const p = pool();
|
| 116 |
+
assert.deepEqual(p.recordHeaders('GROQ_API_KEY', headers({ remaining: 550 })), [0.4]);
|
| 117 |
+
assert.deepEqual(p.recordHeaders('GROQ_API_KEY', headers({ remaining: 540 })), []);
|
| 118 |
+
assert.deepEqual(p.recordHeaders('GROQ_API_KEY', headers({ remaining: 530 })), []);
|
| 119 |
+
});
|
| 120 |
+
|
| 121 |
+
test('a sudden jump reports every step it skipped past', () => {
|
| 122 |
+
// A burst can cross several at once. Reporting only the highest would lose
|
| 123 |
+
// the fact that it went from comfortable to nearly gone in one go.
|
| 124 |
+
const p = pool();
|
| 125 |
+
assert.deepEqual(
|
| 126 |
+
p.recordHeaders('GROQ_API_KEY', headers({ remaining: 40 })),
|
| 127 |
+
[0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95],
|
| 128 |
+
);
|
| 129 |
+
});
|
| 130 |
+
|
| 131 |
+
test('running out reports separately from the warnings before it', () => {
|
| 132 |
const p = pool();
|
| 133 |
p.recordHeaders('GROQ_API_KEY', headers({ remaining: 200 }));
|
| 134 |
+
assert.deepEqual(p.recordHeaders('GROQ_API_KEY', headers({ remaining: 0 })), [0.9, 0.95, 1]);
|
| 135 |
});
|
| 136 |
|
| 137 |
+
test('a fresh window lets the whole ladder fire again', () => {
|
| 138 |
const p = pool();
|
| 139 |
p.recordHeaders('GROQ_API_KEY', headers({ remaining: 0, reset: '1h0m0s' }));
|
| 140 |
// Tomorrow's budget: the reset jumps forward and the key is full again.
|
| 141 |
const next = p.recordHeaders('GROQ_API_KEY', headers({ remaining: 1000, reset: '24h0m0s' }));
|
| 142 |
assert.deepEqual(next, []);
|
| 143 |
+
assert.deepEqual(
|
| 144 |
+
p.recordHeaders('GROQ_API_KEY', headers({ remaining: 0, reset: '24h0m0s' })),
|
| 145 |
+
[0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 1],
|
| 146 |
+
);
|
| 147 |
+
});
|
| 148 |
+
|
| 149 |
+
test('the ladder is the one the operator asked for', () => {
|
| 150 |
+
const { ALERT_THRESHOLDS } = require('../dist/modules/ai/groq-key-pool.js');
|
| 151 |
+
assert.deepEqual([...ALERT_THRESHOLDS], [0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 1]);
|
| 152 |
});
|
| 153 |
|
| 154 |
test('a per-minute ceiling never emails anybody', () => {
|
src/modules/ai/groq-key-pool.ts
CHANGED
|
@@ -33,8 +33,16 @@ import Groq from 'groq-sdk';
|
|
| 33 |
/** A limit that recovers in under this long is a burst, not a budget. */
|
| 34 |
const BUDGET_WINDOW_MS = 5 * 60 * 1000;
|
| 35 |
|
| 36 |
-
/**
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
export interface KeyUsage {
|
| 40 |
/** 'GROQ_API_KEY', 'GROQ_API_KEY_2', β¦ β what an admin sees and can go fix. */
|
|
|
|
| 33 |
/** A limit that recovers in under this long is a burst, not a budget. */
|
| 34 |
const BUDGET_WINDOW_MS = 5 * 60 * 1000;
|
| 35 |
|
| 36 |
+
/**
|
| 37 |
+
* Fractions of a key's daily budget that are worth telling somebody about.
|
| 38 |
+
*
|
| 39 |
+
* Deliberately dense toward the top: the gap between 40% and 50% is a week of
|
| 40 |
+
* ordinary growth, while the gap between 95% and 100% is the difference between
|
| 41 |
+
* "add a key this afternoon" and "AI is down". Each one fires at most once per
|
| 42 |
+
* budget window per key, so a busy day sends a rising sequence rather than a
|
| 43 |
+
* repeat of the same warning.
|
| 44 |
+
*/
|
| 45 |
+
export const ALERT_THRESHOLDS = [0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95, 1] as const;
|
| 46 |
|
| 47 |
export interface KeyUsage {
|
| 48 |
/** 'GROQ_API_KEY', 'GROQ_API_KEY_2', β¦ β what an admin sees and can go fix. */
|