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
fix(analytics): prune request history
Ricardo5bfbaffb2be4
diff --git a/.env.example b/.env.exampleindex 677a0666..e2a8d7bb 100644--- a/.env.example+++ b/.env.example@@ -16,6 +16,10 @@ PORT=3001# to disable proxy rate limiting.# PROXY_RATE_LIMIT_RPM=120+# Request analytics retention. Set either value to 0 to disable that limit.+REQUEST_ANALYTICS_RETENTION_DAYS=90+REQUEST_ANALYTICS_MAX_ROWS=100000+# Comma-separated extra origins allowed to call the API from a browser.# localhost:5173, 127.0.0.1:5173, [::1]:5173 are allowed by default for the# Vite dev dashboard. Only set this if you serve the dashboard from adiff --git a/README.md b/README.mdindex fe7a6742..cd4d6d57 100644--- a/README.md+++ b/README.md@@ -146,6 +146,10 @@ npm run devdatabase-stored development key when `DEV_MODE=true` and `NODE_ENV` is not`production`; do not use that fallback with real provider keys.+Request analytics are retained for 90 days or 100000 request rows by default,+whichever limit prunes first. Set `REQUEST_ANALYTICS_RETENTION_DAYS=0` or+`REQUEST_ANALYTICS_MAX_ROWS=0` in `.env` to disable either retention limit.+Open http://localhost:5173 (the Vite dev UI), add your provider keys on the **Keys** page, reorder the **Fallback Chain** to taste, and grab your unified API key from the **Keys** page header. That unified key is what you point your OpenAI SDK at.For a production build without Docker:diff --git a/server/src/__tests__/services/request-retention.test.ts b/server/src/__tests__/services/request-retention.test.tsnew file mode 100644index 00000000..5927b27d--- /dev/null+++ b/server/src/__tests__/services/request-retention.test.ts@@ -0,0 +1,88 @@+import { describe, it, expect, beforeEach, afterEach } from 'vitest';+import { initDb, getDb } from '../../db/index.js';+import { getRequestAnalyticsRetentionConfig, pruneRequestAnalytics } from '../../services/request-retention.js';++const ORIGINAL_RETENTION_DAYS = process.env.REQUEST_ANALYTICS_RETENTION_DAYS;+const ORIGINAL_MAX_ROWS = process.env.REQUEST_ANALYTICS_MAX_ROWS;++function restoreEnv() {+ if (ORIGINAL_RETENTION_DAYS === undefined) {+ delete process.env.REQUEST_ANALYTICS_RETENTION_DAYS;+ } else {+ process.env.REQUEST_ANALYTICS_RETENTION_DAYS = ORIGINAL_RETENTION_DAYS;+ }++ if (ORIGINAL_MAX_ROWS === undefined) {+ delete process.env.REQUEST_ANALYTICS_MAX_ROWS;+ } else {+ process.env.REQUEST_ANALYTICS_MAX_ROWS = ORIGINAL_MAX_ROWS;+ }+}++function insertRequest(createdAt: string, marker: string) {+ getDb().prepare(`+ INSERT INTO requests (platform, model_id, key_id, status, input_tokens, output_tokens, latency_ms, error, created_at)+ VALUES ('groq', 'groq/compound', 1, 'error', 0, 0, 10, ?, ?)+ `).run(marker, createdAt);+}++describe('request analytics retention', () => {+ beforeEach(() => {+ process.env.ENCRYPTION_KEY = '0'.repeat(64);+ initDb(':memory:');+ getDb().prepare('DELETE FROM requests').run();+ restoreEnv();+ });++ afterEach(() => {+ restoreEnv();+ });++ it('uses conservative defaults when env values are absent or invalid', () => {+ delete process.env.REQUEST_ANALYTICS_RETENTION_DAYS;+ delete process.env.REQUEST_ANALYTICS_MAX_ROWS;+ expect(getRequestAnalyticsRetentionConfig()).toEqual({ retentionDays: 90, maxRows: 100000 });++ process.env.REQUEST_ANALYTICS_RETENTION_DAYS = 'bad';+ process.env.REQUEST_ANALYTICS_MAX_ROWS = '-1';+ expect(getRequestAnalyticsRetentionConfig()).toEqual({ retentionDays: 90, maxRows: 100000 });+ });++ it('deletes request analytics older than the configured retention window', () => {+ process.env.REQUEST_ANALYTICS_RETENTION_DAYS = '7';+ process.env.REQUEST_ANALYTICS_MAX_ROWS = '0';++ insertRequest('2026-05-01 00:00:00', 'old');+ insertRequest('2026-05-25 00:00:00', 'recent');++ const result = pruneRequestAnalytics({+ db: getDb(),+ force: true,+ now: new Date('2026-05-26T00:00:00Z'),+ });++ expect(result.deleted).toBe(1);+ const rows = getDb().prepare('SELECT error FROM requests ORDER BY id').all() as Array<{ error: string }>;+ expect(rows.map(row => row.error)).toEqual(['recent']);+ });++ it('keeps only the newest configured number of request rows', () => {+ process.env.REQUEST_ANALYTICS_RETENTION_DAYS = '0';+ process.env.REQUEST_ANALYTICS_MAX_ROWS = '2';++ insertRequest('2026-05-01 00:00:00', 'row-1');+ insertRequest('2026-05-02 00:00:00', 'row-2');+ insertRequest('2026-05-03 00:00:00', 'row-3');+ insertRequest('2026-05-04 00:00:00', 'row-4');++ const result = pruneRequestAnalytics({+ db: getDb(),+ force: true,+ now: new Date('2026-05-26T00:00:00Z'),+ });++ expect(result.deleted).toBe(2);+ const rows = getDb().prepare('SELECT error FROM requests ORDER BY created_at ASC').all() as Array<{ error: string }>;+ expect(rows.map(row => row.error)).toEqual(['row-3', 'row-4']);+ });+});diff --git a/server/src/routes/proxy.ts b/server/src/routes/proxy.tsindex 31c5ecc8..f279736a 100644--- a/server/src/routes/proxy.ts+++ b/server/src/routes/proxy.ts@@ -5,6 +5,7 @@ import { z } from 'zod';import type { ChatMessage } from '@freellmapi/shared/types.js';import { routeRequest, recordRateLimitHit, recordSuccess, hasEnabledVisionModel, type RouteResult } from '../services/router.js';import { recordRequest, recordTokens, setCooldown, getCooldownDurationForLimit } from '../services/ratelimit.js';+import { pruneRequestAnalytics } from '../services/request-retention.js';import { getDb, getUnifiedApiKey } from '../db/index.js';import { contentToString, messageHasImage, normalizeOutboundContent } from '../lib/content.js';import { sanitizeProviderErrorMessage } from '../lib/error-redaction.js';@@ -554,6 +555,7 @@ export function logRequest(INSERT INTO requests (platform, model_id, key_id, status, input_tokens, output_tokens, latency_ms, error, ttfb_ms)VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(platform, modelId, keyId, status, inputTokens, outputTokens, latencyMs, error, ttfbMs);+ pruneRequestAnalytics({ db });} catch (e) {console.error('Failed to log request:', e);}diff --git a/server/src/services/request-retention.ts b/server/src/services/request-retention.tsnew file mode 100644index 00000000..d248ce2f--- /dev/null+++ b/server/src/services/request-retention.ts@@ -0,0 +1,72 @@+import { getDb } from '../db/index.js';++const DAY_MS = 24 * 60 * 60 * 1000;+const DEFAULT_RETENTION_DAYS = 90;+const DEFAULT_MAX_ROWS = 100_000;+const PRUNE_INTERVAL_MS = 60_000;++type RetentionDb = ReturnType<typeof getDb>;++export interface RequestAnalyticsRetentionConfig {+ retentionDays: number;+ maxRows: number;+}++let nextPruneAtMs = 0;++function readNonNegativeInt(name: string, defaultValue: number): number {+ const raw = process.env[name];+ if (raw === undefined || raw.trim() === '') return defaultValue;++ const parsed = Number(raw);+ if (!Number.isInteger(parsed) || parsed < 0) return defaultValue;+ return parsed;+}++function toSqliteTimestamp(date: Date): string {+ return date.toISOString().slice(0, 19).replace('T', ' ');+}++export function getRequestAnalyticsRetentionConfig(): RequestAnalyticsRetentionConfig {+ return {+ retentionDays: readNonNegativeInt('REQUEST_ANALYTICS_RETENTION_DAYS', DEFAULT_RETENTION_DAYS),+ maxRows: readNonNegativeInt('REQUEST_ANALYTICS_MAX_ROWS', DEFAULT_MAX_ROWS),+ };+}++export function pruneRequestAnalytics(options: {+ db?: RetentionDb;+ force?: boolean;+ now?: Date;+} = {}): { deleted: number; skipped: boolean } {+ const now = options.now ?? new Date();+ const nowMs = now.getTime();++ if (!options.force && nowMs < nextPruneAtMs) {+ return { deleted: 0, skipped: true };+ }+ nextPruneAtMs = nowMs + PRUNE_INTERVAL_MS;++ const db = options.db ?? getDb();+ const { retentionDays, maxRows } = getRequestAnalyticsRetentionConfig();+ let deleted = 0;++ if (retentionDays > 0) {+ const cutoff = toSqliteTimestamp(new Date(nowMs - retentionDays * DAY_MS));+ deleted += db.prepare('DELETE FROM requests WHERE created_at < ?').run(cutoff).changes;+ }++ if (maxRows > 0) {+ deleted += db.prepare(`+ DELETE FROM requests+ WHERE id IN (+ SELECT id+ FROM requests+ ORDER BY created_at DESC, id DESC+ LIMIT -1 OFFSET ?+ )+ `).run(maxRows).changes;+ }++ return { deleted, skipped: false };+}