Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

feat(db): stamp request history with the concrete model row (#1212)

suantea (asuan_suan)abe4c08bff6f
diff --git a/server/src/__tests__/db/migrate/roundtrip.test.ts b/server/src/__tests__/db/migrate/roundtrip.test.ts
index fdc216bd..13ee35a1 100644
--- a/server/src/__tests__/db/migrate/roundtrip.test.ts
+++ b/server/src/__tests__/db/migrate/roundtrip.test.ts
@@ -39,6 +39,7 @@ const ANALYTICS_LATENCY_PERCENTILE_INDEX_FILENAME = '20260902_000001_analytics_l
const MCP_ENABLED_DEFAULT_FILENAME = '20260903_000001_mcp_enabled_default.ts';
const RESPONSE_CACHE_FILENAME = '20260903_000002_response_cache.ts';
const KEY_MONTHLY_BUDGET_FILENAME = '20260904_000001_key_monthly_budget.ts';
+const REQUEST_MODEL_ATTRIBUTION_FILENAME = '20260913_000001_request_model_attribution.ts';
const KEY_MONTHLY_USAGE_FILENAME = '20260914_000001_key_monthly_usage.ts';
const QUOTA_SNAPSHOT_FRESHNESS_FILENAME = '20260915_000001_quota_snapshot_freshness.ts';
@@ -126,6 +127,7 @@ describe('migration round trip', () => {
MCP_ENABLED_DEFAULT_FILENAME,
RESPONSE_CACHE_FILENAME,
KEY_MONTHLY_BUDGET_FILENAME,
+ REQUEST_MODEL_ATTRIBUTION_FILENAME,
KEY_MONTHLY_USAGE_FILENAME,
QUOTA_SNAPSHOT_FRESHNESS_FILENAME,
]);
diff --git a/server/src/__tests__/routes/custom-endpoint-identity.test.ts b/server/src/__tests__/routes/custom-endpoint-identity.test.ts
index 3c755f67..22ed3514 100644
--- a/server/src/__tests__/routes/custom-endpoint-identity.test.ts
+++ b/server/src/__tests__/routes/custom-endpoint-identity.test.ts
@@ -7,6 +7,7 @@ import { setCooldown, isOnCooldown, clearCooldownsForKey } from '../../services/
import { getModelGroups, resolveRequestedIdToMembers, setUnifyOverrides } from '../../services/model-groups.js';
import { noteModelRetirementSignal, resetModelRetirementObservations } from '../../services/model-retirement.js';
import { endpointHandle, qualifiedModelMemberId } from '../../lib/endpoint-scope.js';
+import { logRequest } from '../../lib/request-log.js';
import { buildModelListing } from '../../services/model-listing.js';
import { mintDashboardToken, isGatedApiPath } from '../helpers/auth.js';
@@ -234,6 +235,30 @@ describe('per-endpoint identity for custom relay models (#651)', () => {
expect(rowsFor(SHARED_MODEL).map(r => r.enabled)).toEqual([1, 1]);
});
+
+ it('records the concrete model row selected for each relay (#1187)', async () => {
+ await registerBothRelays(app);
+ const a = rowOn(RELAY_A);
+ const b = rowOn(RELAY_B);
+ expect(a.id).not.toBe(b.id);
+
+ // Same platform + model id on both relays: only the key tells them apart.
+ logRequest('custom', SHARED_MODEL, b.key_id!, 'success', 10, 20, 30, null);
+ const logged = getDb().prepare(`
+ SELECT model_db_id FROM requests
+ WHERE platform = 'custom' AND model_id = ?
+ ORDER BY id DESC LIMIT 1
+ `).get(SHARED_MODEL) as { model_db_id: number };
+ expect(logged.model_db_id).toBe(b.id);
+
+ logRequest('custom', SHARED_MODEL, a.key_id!, 'success', 10, 20, 30, null);
+ const loggedA = getDb().prepare(`
+ SELECT model_db_id FROM requests
+ WHERE platform = 'custom' AND model_id = ?
+ ORDER BY id DESC LIMIT 1
+ `).get(SHARED_MODEL) as { model_db_id: number };
+ expect(loggedA.model_db_id).toBe(a.id);
+ });
});
describe('naming', () => {
diff --git a/server/src/db/migrate/defaults.ts b/server/src/db/migrate/defaults.ts
index 601f1023..82569d19 100644
--- a/server/src/db/migrate/defaults.ts
+++ b/server/src/db/migrate/defaults.ts
@@ -36,6 +36,7 @@ import * as responseCache from '../migrations/20260903_000002_response_cache.js'
import * as keyMonthlyBudget from '../migrations/20260904_000001_key_monthly_budget.js';
import * as keyMonthlyUsage from '../migrations/20260914_000001_key_monthly_usage.js';
import * as quotaSnapshotFreshness from '../migrations/20260915_000001_quota_snapshot_freshness.js';
+import * as requestModelAttribution from '../migrations/20260913_000001_request_model_attribution.js';
export interface MigrationModule {
up(db: Db): void;
@@ -82,6 +83,7 @@ export const ANALYTICS_LATENCY_PERCENTILE_INDEX_FILENAME = '20260902_000001_anal
export const MCP_ENABLED_DEFAULT_FILENAME = '20260903_000001_mcp_enabled_default.ts';
export const RESPONSE_CACHE_FILENAME = '20260903_000002_response_cache.ts';
export const KEY_MONTHLY_BUDGET_FILENAME = '20260904_000001_key_monthly_budget.ts';
+export const REQUEST_MODEL_ATTRIBUTION_FILENAME = '20260913_000001_request_model_attribution.ts';
export const KEY_MONTHLY_USAGE_FILENAME = '20260914_000001_key_monthly_usage.ts';
export const QUOTA_SNAPSHOT_FRESHNESS_FILENAME = '20260915_000001_quota_snapshot_freshness.ts';
@@ -121,6 +123,7 @@ export const DEFAULT_MIGRATIONS: readonly DefaultMigration[] = [
{ filename: MCP_ENABLED_DEFAULT_FILENAME, module: mcpEnabledDefault },
{ filename: RESPONSE_CACHE_FILENAME, module: responseCache },
{ filename: KEY_MONTHLY_BUDGET_FILENAME, module: keyMonthlyBudget },
+ { filename: REQUEST_MODEL_ATTRIBUTION_FILENAME, module: requestModelAttribution },
{ filename: KEY_MONTHLY_USAGE_FILENAME, module: keyMonthlyUsage },
{ filename: QUOTA_SNAPSHOT_FRESHNESS_FILENAME, module: quotaSnapshotFreshness },
];
diff --git a/server/src/db/migrations/20260913_000001_request_model_attribution.ts b/server/src/db/migrations/20260913_000001_request_model_attribution.ts
new file mode 100644
index 00000000..e6b64d21
--- /dev/null
+++ b/server/src/db/migrations/20260913_000001_request_model_attribution.ts
@@ -0,0 +1,75 @@
+// Migration: stamp request history with the concrete model row (#1187)
+//
+// DOWN: reversible
+//
+// Custom relays can expose the same upstream model id, so (platform, model_id)
+// no longer uniquely identifies the `models` row whose pricing, reliability,
+// and speed apply. Today request identity is derived at read time from
+// api_keys.base_url: deleting a key, or editing its base URL, retroactively
+// reattributes (or orphans) all of that key's historical requests.
+//
+// This migration freezes request identity by stamping each request with the
+// concrete `models.id` that served it. Catalog requests resolve by
+// (platform, model_id); custom requests resolve through the request's key
+// (its normalized base_url is the row's endpoint_scope). Custom requests
+// whose key never reached routing (key_id IS NULL) stay NULL — an
+// unattributable request must not land on the wrong relay.
+//
+// No foreign key on models(id) — request history must survive the user
+// deleting the model row that once served it.
+
+import type { Db } from '../types.js';
+
+function hasColumn(db: Db, table: string, column: string): boolean {
+ const columns = db.prepare(`PRAGMA table_info(${table})`).all() as { name: string }[];
+ return columns.some(c => c.name === column);
+}
+
+export function up(db: Db): void {
+ if (hasColumn(db, 'requests', 'model_db_id')) return;
+
+ // Add the column (nullable — unattributed requests keep NULL).
+ db.prepare('ALTER TABLE requests ADD COLUMN model_db_id INTEGER').run();
+
+ // Non-unique index for backfill scans and future analytics queries.
+ db.prepare('CREATE INDEX IF NOT EXISTS idx_requests_model_db_id ON requests(model_db_id)').run();
+
+ // Backfill catalog requests: (platform, model_id) → models.id
+ // These are unambiguous — catalog rows are unique on (platform, model_id).
+ // SQLite UPDATE has no table alias; correlate via the full table name.
+ db.prepare(`
+ UPDATE requests
+ SET model_db_id = (
+ SELECT m.id FROM models m
+ WHERE m.platform = requests.platform AND m.model_id = requests.model_id
+ LIMIT 1
+ )
+ WHERE requests.platform != 'custom'
+ `).run();
+
+ // Backfill custom requests: key_id → api_keys.base_url → models.endpoint_scope
+ // Only while the saved key still resolves to a matching row.
+ db.prepare(`
+ UPDATE requests
+ SET model_db_id = (
+ SELECT m.id FROM models m
+ JOIN api_keys k ON k.id = requests.key_id AND k.platform = 'custom'
+ WHERE m.platform = 'custom'
+ AND m.model_id = requests.model_id
+ AND m.endpoint_scope = rtrim(trim(k.base_url), '/')
+ LIMIT 1
+ )
+ WHERE requests.platform = 'custom' AND requests.key_id IS NOT NULL
+ `).run();
+
+ // Orphaned custom history stays NULL — better than being attributed to the
+ // wrong relay. The two UPDATEs above are best-effort: if a custom key was
+ // deleted or its base_url changed, the JOIN drops out and model_db_id stays
+ // NULL (which is exactly the "unattributable" semantics we want).
+}
+
+export function down(db: Db): void {
+ if (!hasColumn(db, 'requests', 'model_db_id')) return;
+ db.prepare('DROP INDEX IF EXISTS idx_requests_model_db_id').run();
+ db.prepare('ALTER TABLE requests DROP COLUMN model_db_id').run();
+}
diff --git a/server/src/lib/request-log.ts b/server/src/lib/request-log.ts
index 50288258..ca917ddd 100644
--- a/server/src/lib/request-log.ts
+++ b/server/src/lib/request-log.ts
@@ -29,6 +29,34 @@ function setSettingIfMissing(db: LogTx, key: string, value: string): void {
`).run(key, value);
}
+// Resolve the concrete models.id for a request (#1187). Catalog requests
+// resolve by (platform, model_id); custom requests resolve through the
+// request's key (its normalized base_url is the row's endpoint_scope).
+// Returns NULL when the request is unattributable — a custom request whose
+// key never reached routing, or whose key no longer resolves to a matching
+// model row. An unattributable request must not land on the wrong relay.
+function resolveModelDbId(
+ db: LogTx,
+ platform: string,
+ modelId: string,
+ keyId: number | null,
+): number | null {
+ if (platform !== 'custom') {
+ const row = db.prepare('SELECT id FROM models WHERE platform = ? AND model_id = ? LIMIT 1')
+ .get(platform, modelId) as { id: number } | undefined;
+ return row?.id ?? null;
+ }
+ if (keyId == null) return null;
+ const key = db.prepare("SELECT base_url FROM api_keys WHERE id = ? AND platform = 'custom'")
+ .get(keyId) as { base_url: string | null } | undefined;
+ if (!key?.base_url) return null;
+ const scope = key.base_url.trim().replace(/\/+$/, '');
+ const row = db.prepare(
+ 'SELECT id FROM models WHERE platform = ? AND model_id = ? AND endpoint_scope = ? LIMIT 1',
+ ).get('custom', modelId, scope) as { id: number } | undefined;
+ return row?.id ?? null;
+}
+
// Append a row to the request analytics table. Shared by the chat proxy, the
// responses path, and the fusion panel so every served (or failed) sub-request
// is logged identically. Lives in a neutral lib module to avoid an import cycle
@@ -86,9 +114,9 @@ export function logRequest(
const client = getClientContext();
const tx = db.transaction(() => {
const insert = db.prepare(`
- INSERT INTO requests (platform, model_id, key_id, status, input_tokens, output_tokens, latency_ms, error, ttfb_ms, requested_model, served_model, client_ip, client_user_agent, client_agent, caller)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
- `).run(platform, modelId, keyId, status, inputTokens, outputTokens, latencyMs, error, ttfbMs, requestedModel, servedModel, client.ip, client.userAgent, client.agent, caller);
+ INSERT INTO requests (platform, model_id, key_id, status, input_tokens, output_tokens, latency_ms, error, ttfb_ms, requested_model, served_model, client_ip, client_user_agent, client_agent, caller, model_db_id)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ `).run(platform, modelId, keyId, status, inputTokens, outputTokens, latencyMs, error, ttfbMs, requestedModel, servedModel, client.ip, client.userAgent, client.agent, caller, resolveModelDbId(db, platform, modelId, keyId));
// Report the row id back to the fallback loop's attempt trace (if one is
// active): the LAST id noted during a loop run is the terminal row the