An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat(analytics): track overall chat limit checks (#920)
Yoshiki Miura599d2210962d
diff --git a/lib/analytics/index.ts b/lib/analytics/index.tsindex 9ed30dc4..7acf6094 100644--- a/lib/analytics/index.ts+++ b/lib/analytics/index.ts@@ -12,6 +12,10 @@ export {trackAdaptiveLimitEvent} from './track-adaptive-limit-event'export { trackChatEvent } from './track-chat-event'+export {+ type ChatLimitEventData,+ trackChatLimitEvent+} from './track-chat-limit-event'export type {ChatEventData,QueryLang,diff --git a/lib/analytics/track-chat-limit-event.ts b/lib/analytics/track-chat-limit-event.tsnew file mode 100644index 00000000..2463070c--- /dev/null+++ b/lib/analytics/track-chat-limit-event.ts@@ -0,0 +1,33 @@+import { capture } from './dispatch'++export interface ChatLimitEventData {+ /** Outcome of the overall chat rate-limit check */+ outcome: 'allowed' | 'blocked'+ /** Authenticated Supabase user ID */+ userId: string+ /** Current daily usage count after the check (incl. this attempt) */+ used: number+ /** Effective daily limit at the time of the check */+ limit: number+}++/**+ * Track the overall (all-modes) per-user daily chat limit events.+ *+ * Fires on every authenticated request:+ * - `outcome: "allowed"` while under the cap (with running counter), so we+ * can plot daily usage distribution and percentiles.+ * - `outcome: "blocked"` when the request is denied, so we can monitor the+ * 429 hit-rate over time and decide whether to relax / tighten the cap.+ */+export async function trackChatLimitEvent(+ data: ChatLimitEventData+): Promise<void> {+ const { userId, outcome, used, limit } = data++ await capture({+ event: 'chat_limit_check',+ distinctId: userId,+ properties: { outcome, userId, used, limit }+ })+}diff --git a/lib/rate-limit/chat-limits.ts b/lib/rate-limit/chat-limits.tsindex 93e6d561..7ab1dbf5 100644--- a/lib/rate-limit/chat-limits.ts+++ b/lib/rate-limit/chat-limits.ts@@ -1,5 +1,6 @@import { Redis } from '@upstash/redis'+import { trackChatLimitEvent } from '@/lib/analytics'import { perfLog } from '@/lib/utils/perf-logging'const DEFAULT_DAILY_CHAT_LIMIT = 100@@ -38,12 +39,23 @@ async function checkOverallChatLimit(userId: string): Promise<{remaining: numberresetAt: numberlimit: number+ /** Current daily usage count after the check (incl. this attempt) */+ used: number+ /** True when the check ran against Redis (i.e. enforced) */+ enforced: boolean}> {const limit = getDailyChatLimit()// If not in cloud deployment mode, allow unlimited requestsif (process.env.MORPHIC_CLOUD_DEPLOYMENT !== 'true') {- return { allowed: true, remaining: Infinity, resetAt: 0, limit }+ return {+ allowed: true,+ remaining: Infinity,+ resetAt: 0,+ limit,+ used: 0,+ enforced: false+ }}// If Upstash is not configured, allow unlimited requests@@ -51,7 +63,14 @@ async function checkOverallChatLimit(userId: string): Promise<{!process.env.UPSTASH_REDIS_REST_URL ||!process.env.UPSTASH_REDIS_REST_TOKEN) {- return { allowed: true, remaining: Infinity, resetAt: 0, limit }+ return {+ allowed: true,+ remaining: Infinity,+ resetAt: 0,+ limit,+ used: 0,+ enforced: false+ }}try {@@ -82,11 +101,20 @@ async function checkOverallChatLimit(userId: string): Promise<{allowed: count <= limit,remaining,resetAt,- limit+ limit,+ used: count,+ enforced: true}} catch (error) {console.error('Rate limit check failed:', error)- return { allowed: true, remaining: Infinity, resetAt: 0, limit }+ return {+ allowed: true,+ remaining: Infinity,+ resetAt: 0,+ limit,+ used: 0,+ enforced: false+ }}}@@ -99,6 +127,18 @@ export async function checkAndEnforceOverallChatLimit(): Promise<Response | null> {const result = await checkOverallChatLimit(userId)+ // Only emit analytics for real (Redis-backed) checks. Local dev / cloud+ // without Upstash returns enforced=false and we skip tracking to avoid+ // polluting the dashboard with no-op events.+ if (result.enforced) {+ void trackChatLimitEvent({+ outcome: result.allowed ? 'allowed' : 'blocked',+ userId,+ used: result.used,+ limit: result.limit+ })+ }+if (!result.allowed) {return new Response(JSON.stringify({