MongoDB's 16MB document limit can silently crash AI agents storing chat history
MongoDB enforces a hard 16MB BSON document size limit that cannot be raised through any server configuration, and AI agents that store growing conversation histories in a single document are vulnerable to silent failures when this limit is breached. When a document exceeds the limit, the write is rejected entirely — nothing is truncated or partially saved — which can cause agent runs to stall without any visible error or stack trace. A real-world case surfaced in the Mastra framework, where long-running agent workflow snapshots crossed the size threshold, causing the snapshot to be deleted and the stream to hang indefinitely. Developers can identify oversized documents using MongoDB's $bsonSize aggregation operator to rank documents by size and isolate which fields are consuming the most space. The recommended fix is to separate growing message histories into their own collection and use MongoDB's $push with $slice to cap array length atomically, keeping snapshot documents lean and within safe limits.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in