## TL;DR
Design ingestion to survive token expiry instead of discovering it at the end: check credential validity before a long ingestion, refresh proactively by schedule rather than on failure, and make each run's logging independently resumable so a 401 mid-stream loses at most one run. When the 401 arrives, renew the credential, restart the client, and replay only the runs not yet confirmed on the server.

```text
auth token expired mid-run ingestion and experiment logging failed with 401 error
```

## Use this when
- The run or call fails with this symptom: auth token expired mid-run ingestion and experiment logging failed with a 401 error.
- You are working on an agent ingesting results into a tracker over a long session and the failure repeats on the same run or configuration.
- You need the failing run, call, or stage isolated before changing code or thresholds.

## Not for this skill when
- A credential revoked rather than expired, where renewal is impossible and the fix is a newly issued credential.
- The error text names a different tool, stage, or root cause. Match the verbatim error first and use that skill.

## Steps
1. Reconstruct from the server which runs the ingestion completed before the 401, by querying for their names or time window. Verify: completed and missing runs are separated
2. Renew the credential through the job's normal secret path and restart the client so it loads the new value. Verify: a fresh client call succeeds
3. Replay ingestion for the missing runs only, confirming each lands before moving on. Verify: the gap closes run by run
4. Add a pre-flight expiry check and a scheduled refresh to the ingestion job, sized to the longest expected session. Verify: future sessions refresh before expiry

## Variant phrasings
### token expired mid ingestion experiment logging 401 agent
The verbatim phrasing.
### agent ingestion failed 401 token expiry resume runs
The resume phrasing.
### experiment logging authentication expired long session fix
The session phrasing.

Compatibility: Agent ingestion pipelines calling experiment tracking APIs with expiring credentials

## Why it happens
Credentials expire on the issuer's clock while ingestions run on the data's clock, so any session longer than the credential's life guarantees a mid-stream 401. The failure is predictable, which makes it a design omission rather than bad luck.

## Edge cases / pitfalls
- Clients cache credentials at construction; renewing the secret without rebuilding the client changes nothing.
- Log the credential's expiry time at job start so the failure, if it comes, is instantly diagnosable.

## Provenance

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