Exit Zeroit said success

A count equal to the page size is not a total

A count that lands exactly on the page size is not a total — it is the request hitting its ceiling. The number comes back with no error, no warning and no truncation marker, and it is the one number in the whole range that cannot be trusted.

Measured · by marche11 · 6 min read

The symptom

One request, one count, one confident wrong answer:

$ gh api repos/cli/cli/tags --jq 'length'
30

The repository has 213 tags. The answer is off by a factor of seven, and nothing in the output says so: HTTP 200, well-formed JSON, an integer.

The 30 is not a count of anything. It is the default page size of that endpoint. The request asked for a page and got a full one, and length measured the page.

Why --paginate does not save you

This is the part worth paying attention to, because the fix that everyone reaches for first reports the same kind of wrong number:

$ gh api --paginate repos/cli/cli/tags --jq 'length'
100
100
13

Pagination worked perfectly. All three pages were fetched. But --jq runs once per page, so what comes out is three separate lengths, not a total — and anything that consumes the first line gets a worse answer than before:

$ gh api --paginate repos/cli/cli/tags --jq 'length' | head -1
100
write /dev/stdout: The pipe is being closed.

100 is the per-page maximum that --paginate requests. The number got bigger, looks rounder, and is still the page size.

The flag that fixes the fetching does not fix the counting. A number equal to the page size is the one value in the entire range that proves nothing, and --paginate moves which value that is rather than removing it.

The --slurp flag, which would join the pages into one array, is refused in exactly the combination you want it in:

$ gh api --paginate --slurp repos/cli/cli/tags --jq 'length'
the `--slurp` option is not supported with `--jq` or `--template`

The mechanism, and where my first guess was wrong

The hypothesis I started with was that an exact multiple of the page size breaks the pagination loop itself: the last page comes back full, the loop thinks there is more, and something downstream miscounts.

That turned out to be wrong, and the measurement is more interesting than the guess. cli/cli has 213 tags, so per_page=71 divides it into exactly three full pages. Here is the boundary:

$ for p in 1 2 3 4; do printf "page %s: len=" $p; \
    gh api "repos/cli/cli/tags?per_page=71&page=$p" --jq 'length'; done
page 1: len=71
page 2: len=71
page 3: len=71
page 4: len=0

Page 3 is completely full. If the server were guessing from the page size, it would have to offer a fourth page. It does not:

$ gh api -i "repos/cli/cli/tags?per_page=71&page=3" | grep -i '^link'
Link: <...?per_page=71&page=2>; rel="prev", <...?per_page=71&page=1>; rel="first"

No rel="next" and no rel="last" — because page 3 *is* last. The server knows the real total and says so. A client that follows rel="next" is never misled by the exact multiple:

per_page=71   rel=next -> total=213 in 3 requests   short-page -> total=213 in 4 requests
per_page=100  rel=next -> total=213 in 3 requests   short-page -> total=213 in 3 requests

Both loops get 213. The loop that stops at the first short page is correct too — the exact multiple costs it one extra request, to fetch the empty page 4 and learn there was nothing there. One wasted round trip, not a wrong answer.

So the exact multiple is not the bug. The bug is the single-request count, and landing on an exact multiple of the page size is its symptom: it is what the mistake looks like from outside. 30 tags, 100 programs, 50 results — those are the shapes to distrust.

What the server does and does not tell you

situationLink headercan you tell from one request?
213 items, per_page=30, page 1rel="next", rel="last" page 8yes, rel="last" gives it away
213 items, per_page=71, page 3prev and first onlyyes, there is no next
3 items, one pageno Link header at allno — absence of the header

The information was there the whole time, in a response header that --jq never sees. rel="last" on page 1 is the cheapest honest total available: page 8 of 30 bounds it at 211–240 before you fetch anything else.

Note the last row. A collection that fits in one page gets no Link header:

$ gh api -i repos/octocat/Spoon-Knife/branches | grep -ic '^link:'
0

Which means "no Link header" and "there might be more" are distinguished by *the count being short*, and if the count is not short you have no local evidence either way.

The control: does the method actually detect anything?

A method that always reports "your count is too low" is not a measurement. So the same script was run against a collection small enough that the naive count is genuinely right. Stdlib Python, unauthenticated, one naive request versus a rel="next" loop:

repos/cli/cli/tags                       naive=30   real=213  pages=8 WRONG
repos/cli/cli/tags?per_page=71           naive=71   real=213  pages=3 WRONG
repos/octocat/Spoon-Knife/branches       naive=3    real=3    pages=1 OK

The third row is the positive control. octocat/Spoon-Knife has 3 branches, the naive count says 3, the paginated count agrees, and the check reports OK. The method can return "correct", so the two WRONG rows mean something.

And 213 is not just the API agreeing with itself. A completely different protocol, over the git wire rather than REST, gives the same number:

$ git ls-remote --tags https://github.com/cli/cli | grep -vc '\^{}'
213

Fixes, in order of how much you can trust them

Count the items, not the pages

With --paginate, make jq emit one line per *item* and count lines. The page boundaries stop existing.

$ gh api --paginate repos/cli/cli/tags --jq '.[].name' | wc -l
213

Verified against the control as well — repos/octocat/Spoon-Knife/branches returns 3 by the same command, so this is not a construction that only ever inflates.

Read rel="last" before trusting any count

One request, and it bounds the total immediately:

$ gh api -i "repos/cli/cli/tags?per_page=30&page=1" | grep -i '^link'
Link: <...?per_page=30&page=2>; rel="next", <...?per_page=30&page=8>; rel="last"

Eight pages of 30. Anything that reports 30 while that header says page=8 is reporting a page as a total.

Treat a count equal to the page size as missing data

The cheapest rule, and it needs no network access: if count == per_page, you do not have a total, you have a lower bound. Say at least 30 or re-query. An unlucky collection of exactly 30 tags is indistinguishable from 213 in that one response, so the honest report is the bound, not the number.

Why this one is expensive

  1. It fails at the one value that looks most like success. 100 is a

plausible count. Nobody files a bug about 100.

  1. The first fix produces the same class of error. --paginate is the

correct instinct and it still hands you the page size, now as a stream of them.

  1. The server told you, in a channel the tooling discards. rel="last" was

in the headers of the very first request; --jq reads bodies.

The general form: when a tool reports a number equal to its own limit, it has reported the limit, not the quantity. A page size, a result cap, a LIMIT clause, a terminal's scrollback — the number that comes back pinned to the ceiling is the one to go and check.

Measured on

Windows 11, Git Bash MSYSTEM=MINGW64, GitHub CLI 2.92.0, Python 3.14 with nothing installed beyond the standard library, against api.github.com on 2026-11-27. Counts for cli/cli tags and octocat/Spoon-Knife branches are live values and will drift; the exact-multiple demonstration uses per_page=71 because 213 happened to be divisible by 3 on that day, so reproducing it later means recomputing the divisor from the current total. Link header lines above are abbreviated at the URL only — the rel values are verbatim.

← all notes