Managed Codex turns fail with 400 Bad Request around each hourly ChatGPT token expiry
Before submitting
- [x] I searched existing issues and did not find a duplicate.
- [x] I included enough detail to reproduce or investigate the problem.
Area
apps/server
Steps to reproduce
- Use a managed Codex provider instance signed in with ChatGPT (token sharing).
- Keep one or more Codex threads running turns across the hourly access-token expiry.
- Watch the turns that are in progress when the token expires.
Expected behavior
Running Codex turns keep working across token renewal.
Actual behavior
Every Codex turn that sends a request in the minutes around the hourly expiry fails with:
Provider error
{"error":{"message":"Bad Request","type":null,"code":null,"param":null},"status":400}
The response from POST https://api.openai.com/v1/responses carries x-openai-authorization-error: 400, so the cause is an expired token. Because it is a 400 and not a 401, Codex does not ask the token helper again, and the turn fails.
Cause: CodexChatGptAuth.access returns the saved token unchanged until fewer than 60 s remain (if (record.expiresAt - now > 60_000) return record;). Codex calls the __managed-chatgpt-token helper only every ~5 minutes. So the helper usually hands Codex a token with less than 5 minutes left, and Codex keeps using it after it expires until its next call.
Trace evidence from one host: the helper is called every ~5 min (…13:55:28, 14:00:29…). Renewals (CodexChatGptAuth.exchange) happened at 14:00:29 and 15:01:03. Every running Codex thread failed with the 400 between 14:00:02–14:00:55 (4 threads) and between 15:00:43–15:01:24 (4 threads). Three of the 15:01 failures came after the 15:01:03 renewal, because Codex had not asked for the new token yet.
Impact
Major degradation or frequent failure
Any long-running managed Codex thread can fail once an hour; this hits unattended agent work hardest.
Version or commit
T3 Code 0.1.13 (server). The same code is on main (CodexChatGptAuth.ts, expiresAt - now > 60_000). Codex 0.172.0.
Environment
Linux x64 server (headless t3 serve), managed Codex provider, ChatGPT sign-in.
Logs or stack traces
codex_http_client::client Request completed method=POST url=https://api.openai.com/v1/responses status=400 Bad Request headers={... "x-openai-authorization-error": "400", ...}
codex_core::session::turn Turn error: {"error":{"message":"Bad Request","type":null,"code":null,"param":null},"status":400}
Workaround
Resend the failed turn after the renewal. A local fix is to raise the 60 s renewal margin above Codex's ~5 min helper interval.
Suggested fix (tested): renew whenever less than 10 minutes remain.
--- a/apps/server/src/provider/CodexChatGptAuth.ts
+++ b/apps/server/src/provider/CodexChatGptAuth.ts
@@
const DISCOVERY = "https://auth.openai.com/.well-known/openid-configuration";
+// Codex asks the token helper for a token only every few minutes and keeps the
+// answer until then; an expired token comes back from the API as a 400, not a
+// 401, so Codex does not ask again. Renew well before the next ask, or running
+// turns fail with "Bad Request" around each hourly expiry.
+const RENEW_BEFORE_EXPIRY_MS = 10 * 60_000;
@@
- if (record.expiresAt - now > 60_000) return record;
+ if (record.expiresAt - now > RENEW_BEFORE_EXPIRY_MS) return record;
With it, I added a regression test to CodexChatGptAuth.test.ts ("renews a token that would expire before Codex next asks for one"). It saves a token with 5 minutes left, checks that access renews it once, and checks that a second access does not renew again. The test fails on current code (expected +0 to equal 1) and passes with the fix. CodexChatGptAuth.test.ts and CodexManagedRuntime.test.ts pass in full (59 tests), and vp fmt --check is clean. I can open the PR if you want it.
Another data point, from T3 Code (Beta) 0.1.11 on macOS: the desktop app with managed Codex 0.166.0 and openai_token_sharing. On this build the failure isn't a one-off around renewal. The thread stays broken until the app-server restarts.
- Timing.
CodexChatGptAuth.exchangeran at 19:09:11Z. An app-server started at 20:06:05Z received that token; there was no other exchange in between. The thread's last 200 came at 20:09:12Z and its first 400 at 20:09:18Z, 60 min 07 s after the exchange, while the process was only 3 minutes old. After that, every turn in the thread failed in about 300 ms with the same{"error":{"message":"Bad Request",...},"status":400}andx-openai-authorization-error: 400. - Retries don't pick up the renewed token. Earlier the same day, a thread on a different app-server got its first 400 at 19:06:19Z. T3 renewed at 19:09:11Z. Retries at 19:09:16Z, 19:09:41Z and 19:10:07Z all still got the 400; the last one came after switching models. Each retry logged
Reloaded auth, changed: false. - New threads recover, old ones don't. A thread started at 20:27Z in the same app-server process logged
Reloaded auth, changed: trueand worked normally, while the old thread stayed broken. - Content is ruled out. One failing request added only a short text tool output. No image-count, payload-size, model or service-tier rule fits both threads.
Why the 10-minute margin alone may not cover this build: managedCodexLaunchArgs, which is still the same on main, launches Codex with refresh_interval_ms=0. With that setting Codex caches the token it gets, with no age check (codex-rs/login/src/auth/external_bearer.rs). It re-runs the helper only after a 401 (is_recoverable_auth_error), and that 401 never comes because the API answers 400. So the native-auth renewal path from #21787 never fires. A session that outlives its token stays wedged, even if the token had 10 or more minutes left when it was handed out.
Suggestion: alongside the margin change, do one or both of these:
- Pass a nonzero
refresh_interval_msshorter than the margin, for example 4 to 5 minutes. - Treat a 400 that carries
x-openai-authorization-errorlike a 401: re-run the helper and retry, or restart the app-server and resume the thread.
Workaround for 0.1.11 users: start a new thread or restart T3 Code. Retrying in the broken thread never recovers.
Additional confirmed occurrence on Windows x64, T3 Code Desktop 0.1.12, managed Codex 0.166.0, openai_token_sharing, on 2026-12-01 UTC. This adds a Windows / 0.1.12 data point to the cached-auth behavior described above.
The thread worked normally, then failed mid-turn and on all three follow-ups, including switching from gpt-6.2-sol to gpt-6.2-astra and back. The T3 thread was only about 10 minutes old at the first failure.
Sanitized evidence from T3 provider protocol logs, server trace, and Codex's local log database:
- 04:54:32.071–04:54:33.001Z:
CodexChatGptAuth.exchangecompleted successfully. - 04:55:00Z: affected native session's last successful
POST https://api.openai.com/v1/responses: HTTP 200. - 04:55:07Z: next request: HTTP 400,
x-openai-authorization-error: 400,x-openai-internal-caller: unknown_through_ide; request IDreq_988769b6451a45d19562104cdb099cb2. - 04:55:28Z:
continue/gpt-6.2-sol: same authorization 400;req_44c9492eb9cc46409dfa920418993265. - 04:55:44Z:
continue/gpt-6.2-astra: same authorization 400;req_c7f7380c09ec4b2aa272ac1aa1bc95c0. - 04:55:57Z: retry /
gpt-6.2-sol: same authorization 400;req_9e566dc8f2cf4a6b9eae62c39922cd19.
All requests used the same Codex process and native session. Each failing request logged Reloaded auth, changed: false auth_changed_for_refresh=false; feedback tags recorded auth_retry_after_unauthorized="false" and auth_error="400". T3 accepted thread/resume and turn/start, then received codexErrorInfo: "other", willRetry: false, and failed turn/completed. Each follow-up run failed about 1.1–1.2 seconds after starting, before further assistant/tool work.
Last reported context usage: 120,894 / 475,000 tokens, below the context limit. A preceding shell command encountered a missing Git remote; that is separate from the provider's authorization failure.
Interpretation: confirms authorization failure and no recovery after a successful T3-side token exchange, consistent with stale per-session credentials described in the previous comment. Token values and exact expiry were not inspected. The shared app-server was not restarted during this investigation, so restart recovery is not independently verified here.
Please include persistent cached-token/recovery behavior on Codex 0.166.0 when validating the fix, alongside the early-refresh margin.
Another data point on a newer build: T3 Code (Beta) 0.1.13 on macOS, managed Codex app-server 0.172.0, openai_token_sharing, 2026-12-01 UTC. Same root cause. Two details differ from the earlier reports.
1. New threads in an already-expired app-server also fail. All of these are Codex threads started through delegate_task. Each new thread opened in the same process after the cutoff got the authorization 400 on its very first request:
- 11:57:33Z: first 400 in the running thread. New threads at 11:57:58Z, 11:58:11Z and 12:01:06Z all failed on their first request, 6 to 13 s after start, before any model output.
- The same prompt, with byte-identical tools, instructions and world state, then succeeded at 12:22:50Z in a new app-server process.
So on this build "start a new thread" is not enough. Recovery came only with a new app-server process.
2. Clean per-process cutoffs, about one hour apart, independent of process age:
| App-server started | Last 200 | First 400 |
|---|---|---|
| ~11:04Z | 11:48:50Z | 11:57:33Z |
| 12:22:50Z | 12:57:07Z | 12:57:16Z |
| 13:43:24Z | 14:35:24Z | 14:35:32Z |
| 15:16:43Z | 15:35:26Z | 15:35:54Z |
The 15:16 process died 19 minutes after start, so it was handed a token that already had less than 20 minutes left. In two cases the guardian thread (a different model with a small context) got its 400 within 3 s of its parent thread, which rules out content.
All the 400s carry x-openai-authorization-error: 400 and x-openai-internal-caller: unknown_through_ide. The provider config logged at session init still shows refresh_interval_ms: 0. Sample request IDs: req_e91afa82959c4e83b10e2953536dc6c9, req_13980fc24d344618909dc7c907629e6c, req_78645fa733ce47399b2dca9f3b2c816b, req_a2a25ae9d95444e5bffd958f5a2adc5e, req_c401b4442add4d238f0cf808a3b0ce6b.
A control run at the same time, from codex exec with its own openai provider, worked normally.
+1 to the suggestions above: a nonzero refresh_interval_ms, and/or treat a 400 with x-openai-authorization-error like a 401 and re-run the auth helper. When the app-server is spawned, please also hand it a freshly exchanged token rather than the cached one.
This also happened on December 1, 2026 on an Apple silicon Mac running T3 Code Desktop 0.1.11 and managed Codex 0.166.0. The affected turn had been running for about 33 minutes with gpt-6.2-astra at max effort through ChatGPT sign-in (openai_token_sharing).
The local logs show this sequence in UTC.
20:57:13.517— The affected thread received HTTP 200 from/v1/responses.20:57:23.787— The same thread received HTTP 400 from the same endpoint with the headerx-openai-authorization-error: 400. Codex marked the turn as failed without retrying.21:00:08.509— T3 started a token exchange (CodexChatGptAuth.exchange) that completed successfully.21:00:23— A fresh thread using the same model, effort, and sign-in method received HTTP 200.
Triage by Codex (GPT-6.2 Astra) in T3 Code.
Confirmed on T3 Code 0.1.13 (Windows, ChatGPT authentication).
I encountered the same HTTP 400 Bad Request error. Restarting T3 Code resolved the issue, and importantly, I was able to continue using the original thread without creating a new one.
This suggests that the issue is related to stale authentication state in the managed Codex app-server rather than corrupted thread history.
Thanks for investigating this!