Practical technical guides / Recent development

Anthropic SDK 2.15 Preserves Fresh Tokens After Late 401 Responses

anthropic 2.15.0 (30 November 2026) clears a cached token only if a 401 rejected that token. Measured locally: 3 tokens issued versus 2.

By Sandeep Belgavi · Published 2026-12-01

A late 401 must not revoke the new token
Original explanatory diagram; synthetic values where shown.

anthropic 2.15.0 (30 November 2026) clears a cached token only if a 401 rejected that token. Measured locally: 3 tokens issued versus 2.

What changed on 30 November

Version 2.15.0 of the Anthropic Python SDK was released on 30 November 2026 UTC, which was 1 December in India. Its credentials fixes preserve replacement tokens when delayed authentication failures arrive and coordinate refreshes between processes sharing a login. Rejected cached credentials are also cleared when max_retries=0, allowing the next request to obtain a replacement. The changes are recorded in commits b1a1d07 (pull request 1171) and c172326 (pull request 1177), merged on 30 November UTC.

They only matter if your client uses the SDK's token-based credentials, for example a credentials= provider, a credentials file or a workload identity setup, rather than a static API key. For those clients, the SDK keeps the current access token in a TokenCache and refreshes it on expiry or after a 401. This guide reads both changes, measures the in-process one against a local fake API on 2.14.0 and 2.15.0, and draws out the general rule, which applies to any client that caches tokens: a 401 tells you that one specific token was rejected, so only that token should be thrown away.

The 30 November guide on this site covered the 2.14.0 proxy change in the same SDK. This one is a different mechanism, in the authentication path rather than the transport.

The late 401 problem

Picture several requests in flight from one process when the server stops accepting the current token, tok-1. The first 401 comes back, the client clears its cache, refreshes, gets tok-2 and retries. A moment later, the 401 for a second request that was also sent with tok-1 arrives. It is late news about an old token. In 2.14.0, the client reacts to that late 401 exactly like the first one: it clears the cache, which now holds the perfectly good tok-2, and forces another refresh. If the issuer rotates tokens, so that issuing tok-3 revokes tok-2, any request still carrying tok-2 now fails as well, and its 401 triggers yet another refresh.

In 2.15.0, the cache compares the token that was rejected with the token it currently holds, and clears itself only if they are the same. The core comparison in lib/credentials/_cache.py can be explained by this original framework-neutral sketch; the SDK also handles absent cache entries and tokens marked expires_at=0:

# Author: Sandeep Belgavi. Conceptual cache interface, not an SDK API.
def clear_matching_token(cache, rejected_token: str, lock) -> bool:
    with lock:
        if cache.current_token != rejected_token:
            return False
        cache.current_token = None
        cache.refresh_required = True
        return True

The second commit moved the call into the authentication flow, AccessTokenAuth, which knows exactly which token it attached. Remembering the attached token is necessary because downstream hooks can rewrite headers before error handling reads them. Doing it there also means a 401 drops the cached token even when the client was created with max_retries=0, because the auth flow sees every response, retries or not.

Measured on 2.14.0 and 2.15.0

The companion example token_cache_late_401 runs a real anthropic.Anthropic client against a fake API on 127.0.0.1. The credentials provider mints tok-1, tok-2 and so on, and like a rotating issuer, each new token revokes the previous one. Both versions were installed in separate virtual environments with packages limited to those published before 1 December 2026. No real token and no Anthropic endpoint were involved.

Scenario one: two requests are in flight with tok-1 when it is revoked. The fast one gets its 401 at once; the slow one's 401 arrives 0.6 seconds later.

VersionTokens issuedWhat the late 401 didBoth requests succeeded
2.14.03cleared tok-2 and forced tok-3yes
2.15.02kept tok-2, retried with ityes

Both requests succeeded on both versions here, because nothing else was using tok-2 when 2.14.0 revoked it. With three or more overlapping requests, the extra refresh revokes a token that another retry is carrying. The companion lab's three-request preset shows 2.14.0 issuing four refreshes and returning two 401s to callers, while 2.15.0 issues one refresh and all three requests succeed. That case is from the lab's model of the same rules, not from a separate measurement.

Scenario two: a client created with max_retries=0. The first call succeeds with tok-1, the issuer then revokes it, and the client makes two more calls.

VersionSecond callThird callTokens issued
2.14.0401401, still sending tok-11
2.15.0401200, with tok-22

On 2.14.0, with retries disabled, the 401 never reached the cache-clearing code, so every later call kept sending the revoked token until its expiry time. Services that disable retries to control latency, and handle errors themselves, were the ones exposed.

The shared-login loop

The first commit also changes the user OAuth provider that reads a credentials file. When several processes share one login, process A may refresh the token and write the new one to the file while process B is still using the old one. B gets a 401, and in 2.14.0 its forced refresh ran a refresh grant even though the file already held a newer token, which could revoke A's token and start the same cycle in A. In 2.15.0 the provider remembers the token it last served, and if the file now holds a different one, it serves that instead of refreshing. This part was read in the source and its tests, not run here.

The general rule for any token cache

The fix is small, and the pattern applies to every client that caches bearer tokens, OAuth access tokens or signed session cookies.

  1. Attach and remember. Record which token was attached to each outgoing request, in the auth layer, not by reading headers back later.
  2. Compare before clearing. On a 401, clear the cache only if the cached token is the one that was rejected. A 401 for an older token is not news.
  3. Clear even without a retry. Do not tie cache invalidation to the retry policy. The next request should not repeat a token the server has already rejected.
  4. Retry once. A single retry with the replacement token is enough. A second 401 should surface for investigation; possible causes include another revocation, an audience mismatch or a permission change. The status alone does not identify the cause.
  5. Coordinate across processes. If processes share a credential store, re-read the store before refreshing, and refresh only if the stored token is still the rejected one.

Upgrading

If you use token-based credentials with this SDK, upgrade to 2.15.0 or later. Then check two things in your own code. Any wrapper that catches 401s and calls the cache's public invalidate() reintroduces the unconditional behaviour, so remove it or make it compare tokens. And any custom transport or request hook that rewrites the Authorization header no longer affects which token is considered rejected, which is the intended behaviour.

If you use a static API key, these changes do not affect you. If you have your own token cache in another language or SDK, apply the rule above and test it with overlapping requests, not one request at a time.

How to verify it

Write a test with a fake API that revokes a token while two or more requests are in flight, and delay one 401. Count refreshes: the expected number is one per real expiry, not one per 401. Add a test with retries disabled and check that the call after a 401 sends a new token. In production, track refreshes per minute and 401s per minute. A refresh rate that follows the 401 rate rather than the token lifetime is the signature of this bug. The companion example runs both scenarios on whichever SDK version is installed.

Interactive experiment

Token cache and a late 401: invalidate or compare first. Synthetic browser-local fixtures, adjustable inputs, decision traces and JSON export.

Primary references and authorship

Sources checked 2026-12-01. Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/token_cache_late_401. Views are my own and do not represent any employer, past or present.