## TL;DR

The session expired and the agent kept firing queries that all failed with 401, wasting time and log space. Check the response status on every batch; on 401, stop querying, re-authenticate, and resume. An unauthenticated loop is pure waste.

## Error

```text
Auth error: thousands of 401 responses logged; the export made no progress after the session expired.
```

## Steps

1. Add a status check on every batch response. Expected: 401s are caught immediately.
2. On 401, halt the query loop at once; do not finish the batch plan unauthenticated. Expected: the waste stops.
3. Re-authenticate and verify with a cheap call. Expected: a confirmed-good session.
4. Resume the export from the last successful page. Expected: no gaps, no repeats.
5. Add a circuit breaker: N consecutive 401s halt the run even if the check is missed. Expected: defense in depth.

## When to use

- Exports log masses of 401s.
- An agent ignores auth failures and continues.
- After session timeout changes.

## When not to use

- Single 401s handled correctly (that is the good path).
- 403s (permissions, different handling).

## Tool compatibility

- Any session-based API client.
- Agent loop guards.

## Variant phrasings

### querying unauthenticated

Halt on 401; re-auth; resume.

### 401 storm in logs

The loop never checked auth.

## Why it happens

Query loops often check errors only at the end. Auth expiry mid-loop then produces thousands of failed calls before anyone notices.

## Edge cases

- Distinguish 401 (re-auth) from 403 (stop and escalate); the handling differs.
- Resume needs the last good page persisted; without it you restart.
- Proactive refresh before long exports avoids the whole class.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_OgTgLWI6vPnDanNyf2iPLA
