Files
loki/drizzle/0084_own_model_usage.sql
CatoandClaude cc1e95f416 fix(models): a key with no credits saves with the way forward; count what your keys spend (#1121)
The first real key pasted into Settings → AI (xAI, 2026-10-10) was refused:
the account had no credits, xAI answered the probe with a sentence about
credits, and Loki read every non-2xx as "bad key" — a wall with no way out.

- lib/own-model-verdict.ts: four verdicts from a probe — works / unfunded
  (402, or the vendor's words about credits, balance or a spending limit,
  never on a 401) / refused / unreachable. Test on xAI's exact sentence,
  OpenAI's insufficient_quota, DeepSeek's balance, a 401 that mentions billing
- config/own-model-vendors.ts: every vendor's billing page and its own word
  for the spending cap (test: every vendor has one)
- PUT /api/settings/model saves an unfunded key; the probe route returns the
  state and the billing link; the form shows the vendor's words, the link,
  a typed model id (the vendor would not list any) and "Save the key anyway";
  the list shows one line after such a save
- own_model_usage (drizzle/0084): what a user's OWN keys spent, per vendor,
  per model, per day — a third ledger beside ai_spend (rations the pool) and
  ai_usage (the operator's view of the pool), neither of which may count it.
  Recorded by the walker on every own-key call (OwnModel carries userId);
  each key's row in Settings shows today's and 30 days' tokens and calls
  and links to the vendor's credits and cap
- docs/architecture/models-and-money.md: the unfunded path, the counter,
  and "How this scales to a billion builders" — the free pool is a tasting
  menu, your own key is the path that scales, a pass pays for Loki not for
  tokens, and the brain (your key) is not the hands (the agent's sign-in)
- CHANGELOG


Claude-Session: https://claude.ai/code/session_01YKx4TPejLvFR9Uj4xrTf6Y

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-10 00:37:34 -07:00

23 lines
1.1 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Migration: own_model_usage — what a user's own keys spent, per vendor, per day.
--
-- WHY THIS EXISTS
-- A turn on the user's own key was recorded nowhere: not in ai_spend (that
-- rations the free pool) and not in ai_usage (that is the operator's view of
-- the free pool). Right for those two ledgers, but it left the person with no
-- count of their own to compare models by or check an invoice against.
-- One rollup row per (user, day, vendor, model); see src/db/schema/own-model-usage.ts.
--
-- Hand-written, like 0073–0083: drizzle/meta's snapshot is stale.
CREATE TABLE IF NOT EXISTS own_model_usage (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
day date NOT NULL,
vendor text NOT NULL,
model text NOT NULL,
tokens integer NOT NULL DEFAULT 0,
calls integer NOT NULL DEFAULT 0,
updated_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT uq_own_model_usage_bucket UNIQUE (user_id, day, vendor, model)
);
CREATE INDEX IF NOT EXISTS idx_own_model_usage_user_day ON own_model_usage (user_id, day);