Skip to content

An AI-powered search engine with a generative UI

Refine chat limit messaging and rename limiter

Yoshiki Miuraa5d414bbfc55
diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts
index 17c76cbe..2bc9e164 100644
--- a/app/api/chat/route.ts
+++ b/app/api/chat/route.ts
@@ -5,7 +5,10 @@ import { loadChat } from '@/lib/actions/chat'
import { calculateConversationTurn, trackChatEvent } from '@/lib/analytics'
import { getCurrentUserId } from '@/lib/auth/get-current-user'
import { checkAndEnforceGuestLimit } from '@/lib/rate-limit/guest-limit'
-import { checkAndEnforceQualityLimit } from '@/lib/rate-limit/quality-limit'
+import {
+ checkAndEnforceQualityLimit,
+ checkAndEnforceOverallChatLimit
+} from '@/lib/rate-limit/chat-limits'
import { createChatStreamResponse } from '@/lib/streaming/create-chat-stream-response'
import { createEphemeralChatStreamResponse } from '@/lib/streaming/create-ephemeral-chat-stream-response'
import { SearchMode } from '@/lib/types/search'
@@ -116,11 +119,14 @@ export async function POST(req: Request) {
: undefined
const modelType = isGuest ? 'speed' : resolvedModelType
if (!isGuest) {
- const rateLimitResponse = await checkAndEnforceQualityLimit(
+ const qualityLimitResponse = await checkAndEnforceQualityLimit(
userId,
modelType === 'quality'
)
- if (rateLimitResponse) return rateLimitResponse
+ if (qualityLimitResponse) return qualityLimitResponse
+
+ const overallLimitResponse = await checkAndEnforceOverallChatLimit(userId)
+ if (overallLimitResponse) return overallLimitResponse
}
const streamStart = performance.now()
diff --git a/lib/rate-limit/quality-limit.ts b/lib/rate-limit/chat-limits.ts
similarity index 56%
rename from lib/rate-limit/quality-limit.ts
rename to lib/rate-limit/chat-limits.ts
index 1004eef8..a1961dfd 100644
--- a/lib/rate-limit/quality-limit.ts
+++ b/lib/rate-limit/chat-limits.ts
@@ -2,7 +2,8 @@ import { Redis } from '@upstash/redis'
import { perfLog } from '@/lib/utils/perf-logging'
-const DAILY_QUALITY_LIMIT = 5
+const DAILY_QUALITY_LIMIT = 10
+const DAILY_CHAT_LIMIT = 100
/**
* Internal function to check rate limit using Upstash Redis
@@ -84,6 +85,59 @@ function getNextMidnightTimestamp(): number {
return midnight.getTime()
}
+async function checkOverallChatLimit(userId: string): Promise<{
+ allowed: boolean
+ remaining: number
+ resetAt: number
+}> {
+ // If not in cloud deployment mode, allow unlimited requests
+ if (process.env.MORPHIC_CLOUD_DEPLOYMENT !== 'true') {
+ return { allowed: true, remaining: Infinity, resetAt: 0 }
+ }
+
+ // If Upstash is not configured, allow unlimited requests
+ if (
+ !process.env.UPSTASH_REDIS_REST_URL ||
+ !process.env.UPSTASH_REDIS_REST_TOKEN
+ ) {
+ return { allowed: true, remaining: Infinity, resetAt: 0 }
+ }
+
+ try {
+ const redis = new Redis({
+ url: process.env.UPSTASH_REDIS_REST_URL,
+ token: process.env.UPSTASH_REDIS_REST_TOKEN
+ })
+
+ const dateKey = new Date().toISOString().split('T')[0] // YYYY-MM-DD
+ const key = `rl:chat:${userId}:${dateKey}`
+
+ const count = await Promise.race([
+ redis.incr(key),
+ new Promise<number>((_, reject) =>
+ setTimeout(() => reject(new Error('Redis timeout')), 3000)
+ )
+ ])
+
+ if (count === 1) {
+ const secondsUntilMidnight = getSecondsUntilMidnight()
+ await redis.expire(key, secondsUntilMidnight)
+ }
+
+ const remaining = Math.max(0, DAILY_CHAT_LIMIT - count)
+ const resetAt = getNextMidnightTimestamp()
+
+ return {
+ allowed: count <= DAILY_CHAT_LIMIT,
+ remaining,
+ resetAt
+ }
+ } catch (error) {
+ console.error('Rate limit check failed:', error)
+ return { allowed: true, remaining: Infinity, resetAt: 0 }
+ }
+}
+
/**
* Check and enforce quality chat rate limit
* Returns a 429 Response if limit is exceeded, null if allowed
@@ -102,7 +156,7 @@ export async function checkAndEnforceQualityLimit(
if (!result.allowed) {
return new Response(
JSON.stringify({
- error: `You've reached your daily limit of ${DAILY_QUALITY_LIMIT} quality mode requests.`,
+ error: 'Daily quality limit reached. Please try again tomorrow.',
remaining: 0,
resetAt: result.resetAt,
limit: DAILY_QUALITY_LIMIT
@@ -126,3 +180,39 @@ export async function checkAndEnforceQualityLimit(
return null
}
+
+/**
+ * Check and enforce speed chat rate limit
+ * Returns a 429 Response if limit is exceeded, null if allowed
+ */
+export async function checkAndEnforceOverallChatLimit(
+ userId: string,
+): Promise<Response | null> {
+ const result = await checkOverallChatLimit(userId)
+
+ if (!result.allowed) {
+ return new Response(
+ JSON.stringify({
+ error: 'Daily chat limit reached. Please try again tomorrow.',
+ remaining: 0,
+ resetAt: result.resetAt,
+ limit: DAILY_CHAT_LIMIT
+ }),
+ {
+ status: 429,
+ headers: {
+ 'Content-Type': 'application/json',
+ 'X-RateLimit-Limit': String(DAILY_CHAT_LIMIT),
+ 'X-RateLimit-Remaining': '0',
+ 'X-RateLimit-Reset': String(result.resetAt)
+ }
+ }
+ )
+ }
+
+ perfLog(
+ `Chat usage: ${DAILY_CHAT_LIMIT - result.remaining}/${DAILY_CHAT_LIMIT}`
+ )
+
+ return null
+}