Skip to content

Commit 7e78c20

Browse files
ouiliameclaude
andcommitted
docs: relax Ask AI request caps; count only user-authored input
The size cap previously measured the whole serialized message array, so a follow-up question failed (413) once the history carried the prior answer plus retrieved doc chunks. Count only user-authored text instead, and loosen all bounds ~20x so normal multi-turn use never hits them — they remain only as a backstop against egregious abuse. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 0b3d362 commit 7e78c20

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

apps/docs/app/api/chat/route.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,27 @@ const SEARCH_LIMIT = 6
1919
* route is a target for scripted "free inference". These bounds cap the cost of
2020
* any single request; durable per-IP rate limiting, a provider spend cap, and
2121
* edge bot protection are provisioned separately (see the PR checklist).
22+
*
23+
* The size cap counts only user-authored text — NOT the conversation history,
24+
* assistant turns, or retrieved doc chunks we add via the searchDocs tool, which
25+
* legitimately grow large over a multi-turn chat.
2226
*/
23-
const MAX_MESSAGES = 30
24-
const MAX_INPUT_CHARS = 12_000
25-
const MAX_OUTPUT_TOKENS = 800
26-
const MAX_STEPS = 3
27+
const MAX_MESSAGES = 200
28+
const MAX_USER_INPUT_CHARS = 400_000
29+
const MAX_OUTPUT_TOKENS = 4000
30+
const MAX_STEPS = 6
31+
32+
/** Total length of user-authored text across the conversation. */
33+
function userInputChars(messages: UIMessage[]): number {
34+
let total = 0
35+
for (const message of messages) {
36+
if (message.role !== 'user') continue
37+
for (const part of message.parts) {
38+
if (part.type === 'text') total += part.text.length
39+
}
40+
}
41+
return total
42+
}
2743

2844
/**
2945
* Reject obvious cross-origin calls. Same-origin browser requests send an
@@ -100,7 +116,7 @@ export async function POST(req: Request) {
100116
if (!Array.isArray(messages) || messages.length === 0 || messages.length > MAX_MESSAGES) {
101117
return new Response('Invalid request', { status: 400 })
102118
}
103-
if (JSON.stringify(messages).length > MAX_INPUT_CHARS) {
119+
if (userInputChars(messages) > MAX_USER_INPUT_CHARS) {
104120
return new Response('Request too large', { status: 413 })
105121
}
106122

0 commit comments

Comments
 (0)