Files
drinktracker/src/lib/ai/switchboard-log.ts
JP 34afc497f4 Give members their own budget-capped gateway key
Members share the owner's AI budget, so each now gets a Switchboard key
minted on first use with a daily cap and a per-request ceiling. That
gives real spend limits and per-user attribution: the app's own rate
limiter lives in memory and resets on every deploy, so it could never be
a spend control. The gateway enforces the caps and answers 402
guardrail, which the error mapper already turns into a budget message.

Resolution order is the user's own key, then mint, then borrow the
owner's key for a single request if the gateway is unreachable - a
served request beats a hard failure, and the fallback logs loudly
because no per-user cap applies to it. The owner key is used only for
minting, never for inference.

API key management is now owner-only, enforced on GET, POST and DELETE.
GET matters as much as POST because it returns the masked key and the
gateway URL. Settings became a server component so the role is known
before first render: members never see the card and never issue the
request, rather than having it flash and disappear.

AiCall records one row per request from the gateway's own response
metadata, so member spend is queryable instead of a journald grep. The
write is fire-and-forget - tracking must never fail a working request.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-08 21:16:14 +00:00

65 lines
1.9 KiB
TypeScript

import type { SwitchboardMeta } from "./switchboard-types"
import { prisma } from "@/lib/prisma"
/**
* Persist one row per AI call so member spend is queryable.
*
* Deliberately not awaited by callers: an insert failure must never turn a working
* AI response into an error for the user. Failures are logged and dropped.
*/
export function recordAiCall(
userId: string,
feature: string,
meta: SwitchboardMeta | null
): void {
void prisma.aiCall
.create({
data: {
userId,
feature,
modelId: meta?.model_id ?? null,
provider: meta?.provider ?? null,
costUsd: meta?.cost_usd ?? null,
latencyMs: meta?.latency_ms ?? null,
failover: meta?.failover ?? false,
},
})
.catch((error) => {
console.warn("[switchboard] failed to record ai call:", error)
})
}
/**
* One line per gateway call so the cost and the model actually used are visible in
* the server log. Called from inside the provider, so every feature gets it for free.
*/
export function logSwitchboardMeta(
feature: string,
meta: SwitchboardMeta | null
): void {
if (!meta) {
console.warn(`[switchboard] feature=${feature} no meta block in response`)
return
}
console.log(
`[switchboard] feature=${feature} model=${meta.model_id} ` +
`provider=${meta.provider} locality=${meta.locality} category=${meta.category} ` +
`cost=${meta.cost_usd ?? "?"} latency_ms=${meta.latency_ms} request_id=${meta.request_id}`
)
// Both of these silently degrade output quality, so they warn rather than log.
if (meta.failover) {
console.warn(
`[switchboard] feature=${feature} FAILOVER intended=${meta.intended_model} ` +
`actual=${meta.model_id} reason=${meta.reason}`
)
}
if (meta.context_overflow) {
console.warn(
`[switchboard] feature=${feature} CONTEXT OVERFLOW model=${meta.model_id} ` +
`- the provider may have truncated this request`
)
}
}