---
title: "17 DynamoDB scans in my backend read only the first page, and no error said so"
date: 2026-11-24
tags: [dynamodb, pagination, silent-failure, mcp, aws-lambda, python]
type: success
status: n/a
reproducible: true
stack: Python on AWS Lambda, boto3 DynamoDB Table resource (any code that reads a paged store)
agent: Claude Code
model: claude-opus-5-5
---

# 17 DynamoDB scans in my backend read only the first page, and no error said so

Dean Lee wrote on dev.to about [an AI agent that totals only part of your data](https://dev.to/deanlee/when-an-ai-agent-totals-only-part-of-your-data-4n43): a tool returns a plausible fragment with no error, and the agent builds a confident answer on it. In the paper he discusses, partial data was the hardest fault to catch, because nothing ever fails. I wondered whether my own server could hand an agent a fragment like that.

It could, soon. worklore keeps everything in one DynamoDB table and reads it with `scan()`. A Scan returns at most 1 MB per call, and when there is more it doesn't raise an error. It only adds a `LastEvaluatedKey` to the response. 17 places in my backend read the first page and stopped. One of them builds the index behind the MCP tools `search_stories` and `suggest_for_project`. Others count a profile's stories, list similar stories, find an author by handle and list drafts. Only 2 places followed the key.

Nothing was broken yet. The table was 925 KB with 1,199 rows, and one Scan still returned all of them. My tests also passed, because the fake table in them returned everything in one page. Once the table grew past 1 MB, search would have quietly started missing stories.

The 1 MB counts data read before the projection and the filter. So asking for fewer fields, or filtering on the server, doesn't make the page hold more rows. Most rows weren't even stories: 1,108 of the 1,199 were tokens, feedback and other records, so the table grows faster than the story count.

The fix was one helper that follows `LastEvaluatedKey` until it's gone, plus switching all 17 readers to it. I also made the fake table able to return small pages. Then I added a test that reads the source and fails if any code calls `scan()` on the table directly. I checked that the test catches the problem: I put back one old call and both the guard and the search test failed.

## Check if this applies to you

Applies if: your code reads DynamoDB with `scan()` or `query()` (boto3 Table resource or client, or another SDK), or reads any API that returns pages with a continuation token.

1. Search the code for scan and query calls: `grep -rnE "\.(scan|query)\(" --include=*.py .` (adjust the extension to your language).
2. For each hit, read the code around it. Look for a loop that passes `LastEvaluatedKey` back as `ExclusiveStartKey`, or for a paginator (`client.get_paginator("scan")`).
3. Optional, read-only, to see how close you are: `aws dynamodb describe-table --table-name <table> --query "Table.[TableSizeBytes,ItemCount]"` (the size is updated about every six hours).

Has the problem: at least one `scan()` or `query()` result is used as the whole answer (for example `.get("Items", [])`) with no loop over `LastEvaluatedKey` and no paginator. It's already losing rows if one Scan of that table returns a `LastEvaluatedKey`.

Doesn't have it: every call either loops until `LastEvaluatedKey` is missing or goes through a paginator, or reads a single item by key.

## Reproduce this

Prerequisites: a Python service that reads DynamoDB through the boto3 Table resource, with a test suite that runs offline (a fake or stub table).

Your agent will need from you: the module where the table object is created, and permission to change the read paths.

Steps:
1. Next to the table object, add one helper that returns every page in the same shape as one Scan, so callers barely change:
   ```python
   def scan_all(**kwargs):
       kw, items = dict(kwargs), []
       while True:
           page = TABLE.scan(**kw)
           items.extend(page.get("Items", []))
           key = page.get("LastEvaluatedKey")
           if not key:
               return {"Items": items}
           kw["ExclusiveStartKey"] = key
   ```
   Do the same for `query()` if you use it. With the low-level client you can use `client.get_paginator("scan")` instead.
2. Replace every `TABLE.scan(...)` in the code with `scan_all(...)`. Keep the arguments as they are: the projection and filter work per page.
3. Give your fake table a page size, so a test can make the table "bigger than a page": return that many rows plus a `LastEvaluatedKey`, and start after `ExclusiveStartKey` on the next call.
4. Add two tests. The first sets the fake page size to 2 with 7 rows and checks that your main read path (search, listing, totals) still returns all 7. The second parses the source (Python `ast`) and fails if any `.scan(` or `.query(` on the table is called outside the helper.
5. Make each test fail once: put back one old direct call and run them. Then restore the call.

Verify: with the fake page size set, the read path returns every row, and the guard test lists the file and line of any direct `scan()` call you put back. In my backend: 721 tests passed with the fix. With one old call restored, both new tests failed: the guard named `mcp.py:741`, and search was missing 5 of 7 stories.