Response max_tokens is explicitly set on all AI API calls
Why it matters
Without an explicit max_tokens ceiling, the model may generate up to its remaining context window in a single response. A model in a repetition loop or producing unexpectedly verbose output runs until it hits the absolute limit — billing you for every token generated. On a high-traffic endpoint, a single user who triggers runaway generation can produce a spike equivalent to thousands of normal requests. OWASP LLM10, CWE-770, and CWE-400 all apply. NIST AI RMF GOVERN 6.1 requires operational controls on AI resource consumption; max_tokens is the most direct such control.
Severity rationale
Critical because uncapped generation enables runaway token consumption that produces unbounded cost spikes with no application-level control mechanism.
Remediation
Set maxTokens on every streamText, generateText, and chat.completions.create call. Calibrate the value to what the task actually needs — a chat reply does not need the same ceiling as a document draft.
// Vercel AI SDK
const result = await streamText({
model: openai("gpt-4o"),
messages,
maxTokens: 1000, // sized for a chat reply; increase for long-form tasks
});
// OpenAI SDK directly
const completion = await openai.chat.completions.create({
model: "gpt-4o",
messages,
max_tokens: 1000,
});
Verify by searching the codebase for all streamText, generateText, and chat.completions.create call sites and confirming each has an explicit maxTokens or max_tokens property — not the model's absolute maximum.
DetectionPro
The detection method for this pattern is part of AuditBuffet Pro.
The full method (what to look for, pass and fail conditions, and how results get reported) ships inside the AI Token Optimization Audit. The pattern definition, severity rationale, and remediation remain openly licensed under CC-BY-4.0.
External references
- owasp-llm:2025 · LLM10 — Unbounded Consumption
- cwe · CWE-770 — Allocation of Resources Without Limits or Throttling
- cwe · CWE-400 — Uncontrolled Resource Consumption
- nist-ai-rmf:1.0 · GOVERN 6.1 — Policies for AI risk management are documented
- iso-25010:2011 · performance-efficiency.resource-utilization — Resource Utilization — bounded per-call token generation
Taxons
History
- 2026-04-18·v1.0.0·Initial import from ai-token-optimization·automated