Conversation history is truncated or summarized for long sessions
Why it matters
Passing an unbounded messages array to the AI provider is a ticking time bomb. Every conversation turn adds tokens to the payload; once the cumulative history exceeds the model's context window, the API returns a hard 400 error mid-session. OWASP LLM10 and CWE-770 classify this as an uncontrolled resource consumption failure. The user's active session terminates without warning. On multi-user platforms this failure recurs predictably, silently degrading retention for your most engaged users — the ones with the longest conversation histories.
Severity rationale
Critical because unbounded history growth guarantees eventual API failure for active users, crashing live sessions with no recovery path.
Remediation
Implement a token-budget sliding window that fills from the newest message backward, discarding old messages when the budget is exceeded. Do not use a fixed message count — use token count, because message length varies.
// src/lib/ai/context-manager.ts
import { countMessageTokens } from "./token-counter";
const CONTEXT_BUDGET = 6000;
export function buildContextWindow(
messages: Array<{ role: string; content: string }>,
model: string
): Array<{ role: string; content: string }> {
const result: typeof messages = [];
for (let i = messages.length - 1; i >= 0; i--) {
const candidate = [messages[i]!, ...result];
if (countMessageTokens(candidate, model) > CONTEXT_BUDGET) break;
result.unshift(messages[i]!);
}
return result;
}
In your route handler, call buildContextWindow(allMessages, model) before passing messages to streamText. Verify by logging payload token counts during a long conversation — the count should plateau, not grow unboundedly.
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
- nist-ai-rmf:1.0 · MANAGE 2.2 — Mechanisms to sustain AI risk management are in place
- iso-25010:2011 · performance-efficiency.capacity — Capacity — bounded conversation history within context window
Taxons
History
- 2026-04-18·v1.0.0·Initial import from ai-token-optimization·automated