@convex-dev/ai-budget 0.0.2-alpha.0 → 0.0.2-alpha.12
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/README.md +388 -185
- package/dist/client/dashboard.d.ts +1 -0
- package/dist/client/dashboard.js +223 -0
- package/dist/client/index.d.ts +545 -38
- package/dist/client/index.js +343 -59
- package/dist/component/_generated/component.d.ts +62 -24
- package/dist/component/lib.d.ts +144 -40
- package/dist/component/lib.js +648 -305
- package/dist/component/schema.d.ts +107 -53
- package/dist/component/schema.js +87 -38
- package/package.json +5 -5
- package/src/client/dashboard.ts +223 -0
- package/src/client/index.ts +513 -109
- package/src/component/_generated/component.ts +94 -29
- package/src/component/lib.test.ts +253 -15
- package/src/component/lib.ts +746 -333
- package/src/component/schema.ts +90 -38
package/src/component/schema.ts
CHANGED
|
@@ -6,67 +6,107 @@ export const vMessage = v.object({
|
|
|
6
6
|
content: v.string(),
|
|
7
7
|
});
|
|
8
8
|
|
|
9
|
+
// One attribution tag: a (dimension, value) pair, e.g. {dimension:"user",
|
|
10
|
+
// value:"alice"} or {dimension:"customer", value:"acme"}. `user` and `action`
|
|
11
|
+
// are built-in dimensions; apps can add any others (team, project, env, …).
|
|
12
|
+
export const vTag = v.object({ dimension: v.string(), value: v.string() });
|
|
13
|
+
|
|
9
14
|
export default defineSchema({
|
|
10
|
-
|
|
11
|
-
|
|
15
|
+
// A budget holder, keyed by (dimension, value). Unifies what used to be the
|
|
16
|
+
// `users` and `actions` tables — those are just the "user" and "action"
|
|
17
|
+
// dimensions now. Any tag a request carries can have its own budget here.
|
|
18
|
+
buckets: defineTable({
|
|
19
|
+
dimension: v.string(),
|
|
20
|
+
value: v.string(),
|
|
12
21
|
// limits (all optional — unlimited by default)
|
|
13
|
-
requestsPerMinute: v.optional(v.number()),
|
|
22
|
+
requestsPerMinute: v.optional(v.number()), // rolling limit for this bucket
|
|
23
|
+
maxConcurrent: v.optional(v.number()), // max in-flight (pending) requests
|
|
14
24
|
dailySpendLimitNanos: v.optional(v.number()),
|
|
25
|
+
monthlySpendLimitNanos: v.optional(v.number()),
|
|
15
26
|
lifetimeSpendLimitNanos: v.optional(v.number()),
|
|
16
27
|
dailyTokenLimit: v.optional(v.number()),
|
|
28
|
+
monthlyTokenLimit: v.optional(v.number()),
|
|
17
29
|
lifetimeTokenLimit: v.optional(v.number()),
|
|
18
|
-
blocked: v.optional(v.boolean()),
|
|
30
|
+
blocked: v.optional(v.boolean()), // hard block (was `blocked`/`disabled`)
|
|
31
|
+
// Fire an approaching-limit alert once usage crosses this fraction of a cap
|
|
32
|
+
// (e.g. 0.8 = warn at 80%). Falls back to the deployment default.
|
|
33
|
+
warnAtPct: v.optional(v.number()),
|
|
19
34
|
// "hard" (default): exceeding a budget blocks. "soft": warn but allow.
|
|
20
35
|
enforcement: v.optional(v.union(v.literal("hard"), v.literal("soft"))),
|
|
21
|
-
// one-time bumps ("approve another $X")
|
|
22
|
-
//
|
|
36
|
+
// one-time bumps ("approve another $X"). Daily/monthly bumps are scoped to
|
|
37
|
+
// their stamp (reset with the window); lifetime bump is permanent.
|
|
23
38
|
dailyBumpNanos: v.optional(v.number()),
|
|
39
|
+
monthlyBumpNanos: v.optional(v.number()),
|
|
24
40
|
lifetimeBumpNanos: v.optional(v.number()),
|
|
25
41
|
bumpDayStamp: v.optional(v.string()),
|
|
42
|
+
bumpMonthStamp: v.optional(v.string()),
|
|
26
43
|
// settled totals (from finished requests)
|
|
27
44
|
totalSpendNanos: v.number(),
|
|
28
45
|
totalRequests: v.number(),
|
|
29
46
|
totalTokens: v.number(),
|
|
30
47
|
// daily window
|
|
31
|
-
dayStamp: v.string(),
|
|
48
|
+
dayStamp: v.string(),
|
|
32
49
|
spendTodayNanos: v.number(),
|
|
33
50
|
tokensToday: v.optional(v.number()),
|
|
51
|
+
// monthly window (UTC calendar month, e.g. "2026-09")
|
|
52
|
+
monthStamp: v.optional(v.string()),
|
|
53
|
+
spendThisMonthNanos: v.optional(v.number()),
|
|
54
|
+
tokensThisMonth: v.optional(v.number()),
|
|
34
55
|
// in-flight reservations (pessimistic holds; released on settle/expiry)
|
|
35
56
|
reservedTodayNanos: v.optional(v.number()),
|
|
57
|
+
reservedMonthNanos: v.optional(v.number()),
|
|
36
58
|
reservedTotalNanos: v.optional(v.number()),
|
|
37
59
|
reservedTodayTokens: v.optional(v.number()),
|
|
60
|
+
reservedMonthTokens: v.optional(v.number()),
|
|
38
61
|
reservedTotalTokens: v.optional(v.number()),
|
|
39
62
|
pendingCount: v.optional(v.number()),
|
|
40
|
-
})
|
|
63
|
+
})
|
|
64
|
+
.index("dim_value", ["dimension", "value"])
|
|
65
|
+
.index("dimension", ["dimension"]),
|
|
41
66
|
|
|
42
|
-
// per-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
})
|
|
67
|
+
// Durable per-(bucket, period) spend history. Written from settled requests
|
|
68
|
+
// and manual adjustments; NEVER swept by request retention, so spend charts
|
|
69
|
+
// and "what did we spend last month" survive long after the raw request rows
|
|
70
|
+
// are purged. period is "day" ("2026-09-04") or "month" ("2026-09").
|
|
71
|
+
usage: defineTable({
|
|
72
|
+
dimension: v.string(),
|
|
73
|
+
value: v.string(),
|
|
74
|
+
period: v.union(v.literal("day"), v.literal("month")),
|
|
75
|
+
stamp: v.string(),
|
|
76
|
+
spendNanos: v.number(),
|
|
77
|
+
tokens: v.number(),
|
|
78
|
+
requests: v.number(),
|
|
79
|
+
})
|
|
80
|
+
.index("bucket_period_stamp", ["dimension", "value", "period", "stamp"])
|
|
81
|
+
.index("period_stamp", ["period", "stamp"]),
|
|
82
|
+
|
|
83
|
+
// Reverse index for filtering the request log by an arbitrary tag dimension
|
|
84
|
+
// (user/action are already indexed on `requests`). One row per extra tag per
|
|
85
|
+
// request; cleaned up with the request on retention/deletion.
|
|
86
|
+
requestTags: defineTable({
|
|
87
|
+
dimension: v.string(),
|
|
88
|
+
value: v.string(),
|
|
89
|
+
requestId: v.id("requests"),
|
|
90
|
+
})
|
|
91
|
+
.index("dim_value", ["dimension", "value"])
|
|
92
|
+
.index("requestId", ["requestId"]),
|
|
93
|
+
|
|
94
|
+
// Manual credits/debits applied to a bucket (comp a user, correct an
|
|
95
|
+
// overcharge). Negative delta = credit/refund, positive = extra charge.
|
|
96
|
+
adjustments: defineTable({
|
|
97
|
+
dimension: v.string(),
|
|
98
|
+
value: v.string(),
|
|
99
|
+
deltaNanos: v.number(),
|
|
100
|
+
tokens: v.optional(v.number()),
|
|
101
|
+
reason: v.optional(v.string()),
|
|
102
|
+
}).index("dim_value", ["dimension", "value"]),
|
|
66
103
|
|
|
67
104
|
requests: defineTable({
|
|
105
|
+
// `user` and `action` stay first-class + indexed (the hot-path filters and
|
|
106
|
+
// rate limiting); the full attribution incl. extra tags lives in `tags`.
|
|
68
107
|
userId: v.string(),
|
|
69
108
|
actionName: v.optional(v.string()),
|
|
109
|
+
tags: v.optional(v.array(vTag)),
|
|
70
110
|
model: v.string(),
|
|
71
111
|
// pessimistic holds placed at start; reconciled to actual on settle
|
|
72
112
|
estimatedNanos: v.optional(v.number()),
|
|
@@ -92,6 +132,9 @@ export default defineSchema({
|
|
|
92
132
|
completionTokens: v.optional(v.number()),
|
|
93
133
|
// subset of promptTokens served from the provider's prompt cache (cheaper).
|
|
94
134
|
cachedTokens: v.optional(v.number()),
|
|
135
|
+
// server-side tool invocations that bill a per-call fee on top of tokens
|
|
136
|
+
// (e.g. { web_search: 3 }). Priced via serverToolPrices at settle.
|
|
137
|
+
serverToolUses: v.optional(v.record(v.string(), v.number())),
|
|
95
138
|
costNanos: v.optional(v.number()),
|
|
96
139
|
latencyMs: v.optional(v.number()),
|
|
97
140
|
rerunOf: v.optional(v.id("requests")),
|
|
@@ -107,6 +150,9 @@ export default defineSchema({
|
|
|
107
150
|
model: v.string(),
|
|
108
151
|
inputNanosPerMTok: v.number(),
|
|
109
152
|
outputNanosPerMTok: v.number(),
|
|
153
|
+
// price for cached (prompt-cache-read) input tokens. Providers bill these
|
|
154
|
+
// at a fraction of the input rate; if unset, a default discount is applied.
|
|
155
|
+
cachedNanosPerMTok: v.optional(v.number()),
|
|
110
156
|
}).index("model", ["model"]),
|
|
111
157
|
|
|
112
158
|
// singleton component config (key === "singleton")
|
|
@@ -122,12 +168,12 @@ export default defineSchema({
|
|
|
122
168
|
)
|
|
123
169
|
),
|
|
124
170
|
models: v.optional(v.array(v.string())),
|
|
125
|
-
// Deployment-wide ("global") spend cap across ALL
|
|
126
|
-
//
|
|
127
|
-
// the limit config lives here.
|
|
128
|
-
// sharded total is read without a reservation, so under heavy
|
|
129
|
-
// it can overshoot by a bounded amount. Right for a global
|
|
130
|
-
// per-
|
|
171
|
+
// Deployment-wide ("global") spend cap across ALL requests. Running totals
|
|
172
|
+
// live in a sharded counter (high write throughput) since every request
|
|
173
|
+
// touches it; only the limit config lives here. Enforced approximately —
|
|
174
|
+
// the sharded total is read without a reservation, so under heavy
|
|
175
|
+
// concurrency it can overshoot by a bounded amount. Right for a global
|
|
176
|
+
// killswitch; per-bucket concurrent admission is atomic via reserve/settle.
|
|
131
177
|
globalDailySpendLimitNanos: v.optional(v.number()),
|
|
132
178
|
globalLifetimeSpendLimitNanos: v.optional(v.number()),
|
|
133
179
|
globalEnforcement: v.optional(
|
|
@@ -138,5 +184,11 @@ export default defineSchema({
|
|
|
138
184
|
globalBumpDayStamp: v.optional(v.string()),
|
|
139
185
|
// request-row retention window in ms (default 1h); 0 disables sweeping.
|
|
140
186
|
retentionMs: v.optional(v.number()),
|
|
187
|
+
// default approaching-limit alert threshold (fraction of a cap) for buckets
|
|
188
|
+
// that don't set their own warnAtPct. 0/unset disables threshold alerts.
|
|
189
|
+
defaultWarnAtPct: v.optional(v.number()),
|
|
190
|
+
// per-call price (nanodollars) overrides for provider server tools, keyed by
|
|
191
|
+
// tool name (e.g. { web_search: 12_000_000 }). Merged over the defaults.
|
|
192
|
+
serverToolPrices: v.optional(v.record(v.string(), v.number())),
|
|
141
193
|
}).index("key", ["key"]),
|
|
142
194
|
});
|