Interactive engineering lab

Token cache and a late 401: invalidate or compare first

Token cache and a late 401: invalidate or compare first. Replay overlapping 401s on anthropic 2.14.0 and 2.15.0 and count refreshes and failed calls.

By Sandeep Belgavi · Published 2026-12-01

Synthetic fixtures computed locally. No model, token issuer or production service is contacted.

Experiment workspace

Decision trace

    The question this experiment answers

    When several requests are in flight with an access token the issuer has just revoked, what does a client token cache do with the 401s that arrive one after another? Choose the anthropic SDK version, how many requests carry the revoked token, how far apart their 401s arrive, how long a retry takes, max_retries, whether issuing a token revokes the previous one, and how many calls follow. The model replays the responses and counts refreshes, calls that end in a 401 and tokens revoked that no 401 had rejected.

    Change one assumption at a time

    Start with the 2.14.0 three-request preset, then switch only the version to 2.15.0 and compare refreshes and failed calls. Move the gap between 401s and the retry time to see when an extra refresh catches a retry still carrying the previous token. Turn rotation off to see refreshes without failures. Set max_retries to 0 and look at the follow-up calls.

    Expected outcomes

    2.14.0 with three late 401s and a rotating issuer: 4 refreshes, 2 requests return 401 to the caller, 1 succeeds, 3 unrejected tokens revoked. 2.15.0, same inputs: 1 refresh, all 3 succeed, nothing revoked. The measured two-request case: 2.14.0 refreshes 2 times and revokes 1 unrejected token, 2.15.0 refreshes once; both requests succeed on both. max_retries 0: on 2.14.0 the follow-ups keep sending the revoked token and 0 of 2 succeed; on 2.15.0 2 of 2 succeed. 2.14.0 without rotation: 3 refreshes and no failures.

    Read the metrics and the table

    Token refreshes should equal the number of real expiries or revocations, which is 1 in every preset. Requests that returned 401 to the caller are user-visible failures. Unrejected tokens revoked by a refresh are the cause of those failures under a rotating issuer. The table lists every response in time order with the token it carried and what the cache did.

    What to take back to a real system

    Record which token each request carried in the auth layer. On a 401, clear the cache only if the cached token is the one that was rejected, and do it whether or not the request is retried. Retry once with the replacement. Test with overlapping requests and a delayed 401, and track refreshes against token lifetimes in production.

    Limits

    A model of the SDK token cache, checked against anthropic 2.14.0 and 2.15.0 with a local fake API; no real token, issuer or provider is contacted. Retries happen immediately and in arrival order, while real retries add backoff. max_retries is modelled as 0 or 2. Refresh through a shared credentials file across processes is described in the guide but not modelled. Export the JSON to keep the inputs with the result.

    Views are my own and do not represent any employer, past or present.