Security, Governance and PlatformAction authorization and policyPrincipal
How should a credential service coordinate rotating OAuth refresh tokens across concurrent agent runs?
By Amit Kumar2 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
Two workers read refresh token R0. One exchanges it for R1 while the other reuses R0, and a later timeout may hide successful rotation. Design account-scoped coordination, versioned storage, stale-worker protection and unknown-outcome recovery under the provider’s actual grace policy, including when user reauthorization is unavoidable.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
Suppose both workers read refresh token R0 for the same connector account. Worker A exchanges R0 and receives R1. Worker B, still holding R0, tries to exchange it too. If the provider rotates refresh tokens and invalidates the old one, B can get a reuse error. A narrow grace period may allow both requests, but then their returned successors can race in our storage. The exact consequences are provider-specific. Amazon Cognito's rotation API explicitly describes issuing a new refresh token, invalidating the old one after an optional grace period, and a reuse exception.
The fix belongs in the credential service, not in a prompt telling agents to retry carefully. Give each connected account one refresh coordinator. On access-token expiry, a worker reads the current credential version, takes a short account-scoped lease or joins an in-flight refresh, and exchanges only that version. Store the new access and refresh tokens together with a compare-and-swap on the credential version. Other workers reread the stored result. A process-local mutex alone does not coordinate replicas.
A compare-and-swap protects stored credential versions. It does not stop an old worker making a remote exchange after its lease expires. Coordinate the in-flight exchange through durable account state, and block another ordinary refresh while its outcome is unresolved unless the provider’s documented grace or recovery mechanism makes that retry safe. A local lock cannot undo a rotation already accepted remotely.
The awkward case is a timeout after the provider rotated R0 but before we stored R1. Blindly replaying R0 can be wrong, and a local transaction cannot atomically commit the remote token exchange. Check whether the provider offers a grace or recovery mechanism. If it does not, mark the connection for explicit reauthorization rather than retrying indefinitely or claiming the agent completed its task. Keep the old credential encrypted only as long as the provider contract and recovery procedure require, and never log token values.
I would test two replicas refreshing simultaneously, a crash after provider success, a delayed stale writer and a revoked user grant. Metric-wise, distinguish temporary refresh contention from a permanently invalid connection and count affected runs by account. The identity provider rotated its signing key. Why did every agent tool call fail? covers verifying an identity provider's signing keys. This problem is about our ownership of a rotating secret used to obtain access tokens.
Related questions
Keep practising security, governance and platform
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux