withToken
function withToken<C>(source, build): Provider;Defined in: core/src/auth/with-token.ts:42
Give any provider the signed-in user’s token, read fresh on every call.
const provider = withToken(token, (auth) => createPresetProvider("github-copilot", auth));build runs once per attempt and must do no I/O, like withKeyPool’s
factory. It gets the access token as apiKey, the credential’s own host as
baseUrl (Copilot gives each plan its own) and its ChatGPT-Account-Id
header, which the Codex backend refuses the request without. To add headers
of your own, merge them with auth.headers rather than replacing them. On
a preset that sends a session id (chatgpt), pass instanceSessionId too:
a provider built per call mints a new id per call, and the backend’s prompt
cache misses on every one.
A 401 before any output renews the token and replays the call once: a token
can be refused before its expiry, and without this the caller sees an auth
failure that a fallback pool benches for an hour. A second 401 is a real one
and surfaces. A 401 after output never replays (invariant 1). With
StreamOptions.maxAttempts: 1, renewal still runs for future calls but this
call rethrows its original 401 without sending another inference request.
Only 401 counts. A 400 or 403 on a model call is a bad request or a plan that lacks the model, and a new token fixes neither.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
C extends Credential |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
source |
TokenSource<C> |
build |
(auth) => Provider |