AI Agent Tool Pagination: Stop One Response From Swallowing the Context Window A developer has published a pattern for paginated AI agent tool results that keeps a single response from consuming the context window, built around a typed result contract with a durable, tenant-scoped result store and a compact model-visible page summary. The contract exposes coverage.complete, hasMore, an opaque nextCursor, applied filters and server-generated summaries so agents can see when results are incomplete and choose a narrower follow-up retrieval instead of looping through every page. The writeup identifies three recurring production failures: first-page certainty, context flooding and blind looping. An agent asks a search tool for invoices, tickets, or documents. The tool returns 20 tidy JSON objects. The model writes a confident answer. The answer can still be wrong. Those 20 objects may be page one of 2,000. Or they may be the only records the model can afford to read before its context becomes mostly old tool output. In both cases, a perfectly valid API response creates a bad decision. The fix is not telling the model to “remember pagination.” Give it a result contract that makes incompleteness visible, bounds how much data enters context, and lets the agent choose the next useful retrieval step. This guide shows a practical pattern for paginated tool results that works with ordinary HTTP APIs, internal tools, and MCP-backed workflows. Conventional APIs can assume a program will loop until nextCursor disappears. An agent cannot safely make that assumption. It has a task, a limited context window, and a tool response whose meaning is partly described in natural language. Three failure modes repeat in production: | Failure | What the agent sees | What goes wrong | |---|---|---| | First-page certainty | A plausible list of records | It answers as if there are no more matches. | | Context flooding | Hundreds of rows, fields, and snippets | Important instructions and evidence are pushed out of the working context. | | Blind looping | A continuation cursor | It fetches every page even when one aggregation or a better filter would answer the task. | Pagination is therefore a reasoning and interface-design problem, not just an API detail. The right question is not “How can the agent load everything?” It is “What is the smallest next slice of evidence that can change the answer?” That change in framing also improves user experience. A chat interface can show progress such as “I found 1,840 matches; I’m comparing the top accounts by open-ticket count” rather than silently issuing ten calls. The user can then refine the task before the system spends time and tokens browsing records that will not affect the conclusion. Keep the complete response in a durable, access-controlled result store. Put only a compact page summary in the model-visible response. Give the model a reference it can use to fetch a narrow follow-up slice. This architecture has four pieces: The result store is not long-term agent memory. Treat it as short-lived evidence for one task: tenant-scoped, expiry-bound, auditable, and deleted or redacted according to your data policy. This separation also makes retries safer. A transport retry can retrieve the same page by reference without asking the model to reconstruct a query or blindly repeat a broad search. Keep retrieval state deterministic; let model reasoning operate on a compact, explicitly scoped view of that state. Offset pagination is easy for a UI but fragile for changing data. Cursor pagination is usually safer because the server can preserve ordering and make the cursor opaque. The agent should never manufacture or inspect a cursor. More important than the cursor is the surrounding contract. A useful response says what the page proves and what it does not prove: type ToolPage