Building a Local-First Quota Monitor and Account Switcher for Codex
This is the fourth article in my product-engineering series. The previous article covered CodexPigeon, a repository-local mailbox for non-interrupting agent guidance. This one focuses on account state, quota accuracy, and cross-platform desktop behavior.
Codex account management looks simple until several accounts have different quota windows, different reset times, and different local session states.
At that point, two questions become surprisingly difficult:
- Which account is actually usable right now?
- Which account is the local Codex installation currently using?
I wanted a focused desktop utility that could answer both without opening dashboards, estimating usage from local activity, or manually replacing authentication files.
That became CodexControl, an open-source, local-first quota monitor and account switcher for Codex on macOS and Windows.
The product intentionally does two things:
- read live Codex quota state;
- switch the active local Codex account.
Keeping the interface narrow made the product easier to understand. The implementation—especially on Windows—was less narrow than it first appeared.
Why quota should come from live account state
A usage estimate can be useful when no authoritative quota source exists.
Codex already exposes account state through the authenticated local session, so I did not want to infer remaining capacity from token counts, request logs, or time spent working.
CodexControl loads each saved account's local authentication state and requests live quota information directly.
Estimates can drift for many reasons:
- usage may happen from another computer;
- background activity may not be visible locally;
- different operations may consume capacity differently;
- cached responses can outlive the real window;
- the server may normalize or adjust the account state.
The app therefore treats the provider response as the source of truth.
Quota is not one percentage
A Codex account may expose multiple usage windows. The two most important to the app are:
- a short five-hour window;
- a longer seven-day window.
These windows can be in very different states.
An account may still have weekly capacity while its short-term window is exhausted. Another may have a nearly full short-term window but little weekly capacity remaining.
Combining both into one percentage would hide the decision the user needs to make.
CodexControl keeps every window independent and displays its own:
- usage state;
- reset time;
- current availability;
- normalized presentation.
Exact reset timestamps are preserved instead of showing only vague labels such as “later today.”
Accuracy became the first-order requirement
A quota monitor is only useful if the numbers can be trusted.
The app uses several safeguards:
- cache-bypassing network sessions;
- repeated live reads with equivalence checks;
- rejection of inconsistent responses;
- per-window normalization without merging independent values;
- token refresh before retrying when required;
- stale snapshot clearing after refresh failures.
The most important principle is:
A missing value is better than a confident but stale value.
If a refresh fails, continuing to display old quota as though it were current can lead the user to activate an unusable account.
The app prefers to expose the failure and request another refresh.
The account list is ordered for action
An alphabetical list does not answer the practical question: which account should I use next?
CodexControl sorts accounts by usefulness so usable accounts remain easier to reach. The presentation considers live window state, authentication health, and which account is currently active.
The app supports local management actions such as:
- adding an account;
- refreshing quota;
- reauthenticating;
- relabeling;
- removing;
- opening an account directory;
- switching the ambient Codex identity.
Periodic refresh keeps the list current, while manual refresh remains available when the user needs an immediate answer.
Separate Codex homes
Each managed account has its own local Codex home and authentication state.
The ambient ~/.codex directory represents the account currently used by the normal Codex CLI and desktop workflow.
A simplified model is:
managed account A home ─┐
managed account B home ─┼──> selected account copied/synchronized to ~/.codex
managed account C home ─┘
The app reads account state from the managed homes, but switching updates the ambient Codex environment.
This makes local files security-sensitive. Real auth.json files, quota snapshots, tokens, and desktop-session backups must never be committed or included in screenshots.
The repository uses synthetic accounts for demos and documents explicit hygiene rules for security reports.
macOS: a focused menu-bar workflow
The macOS application is built with SwiftUI and AppKit and lives in the menu bar.
The user can scan quota windows, refresh an account, and switch identity without opening a full dashboard.
When an account is selected, CodexControl updates the ambient Codex state and restarts Codex Desktop when necessary so the application adopts the new identity.
Packaging includes support for signing and notarization when release credentials are available.
Windows: copying auth.json was not enough
The first Windows implementation switched accounts by replacing only:
~/.codex/auth.json
That changed one layer of identity, but current Codex Desktop builds also involve other state.
Two layers mattered:
1. Global CLI identity state
~/.codex/auth.json
~/.codex/.codex-global-state.json
~/.codex/.codex-global-state.json.bak
The global state can contain a stale creator_id. Replacing only auth.json can leave the old account identity behind.
2. Desktop session state
The Windows desktop app stores browser/session state under the Codex MSIX package cache:
%LOCALAPPDATA%\Packages\OpenAI.Codex_*\LocalCache\Roaming\Codex
If this state remains associated with another account, Codex Desktop may reopen with a login screen or restore the wrong session.
The corrected switch flow therefore:
- stops Codex Desktop;
- backs up the current desktop session into the account being left;
- copies the target authentication state;
- rewrites account-aware global identity fields;
- restores the target account's desktop session when available;
- relaunches Codex Desktop.
The backup and restore process operates per entry with logging so one file-copy failure does not abort the entire restart.
When an account has no saved desktop-session snapshot yet, the app avoids wiping the current live state and allows Codex Desktop to reconcile the session.
This was the strongest lesson from the Windows work:
“The active account” is often distributed across more than one file and more than one runtime layer.
Token recovery and account identity
Saved accounts can become stale when refresh tokens expire or are replaced.
CodexControl supports reauthentication without rebuilding the whole account list. It also treats provider account IDs as part of identity, because different accounts may share the same email address.
An email address alone is not always a safe primary key.
Removal also has to be explicit. Deleting a managed account must not accidentally delete the ambient account or another account's session data.
These lifecycle details are less visible than the quota bars, but they determine whether the product remains trustworthy after weeks of real use.
Different platform implementations, shared behavior
The macOS app uses Swift, while the Windows app uses Python with a tray-first workflow.
I did not force both platforms into one UI framework because the valuable shared contract is behavioral:
- the same quota concepts;
- the same account lifecycle;
- the same live-read accuracy rules;
- the same privacy boundary;
- the same switching outcome.
Platform-native implementations can share a product model without sharing every line of code.
What I learned
1. Monitoring products should prefer authoritative data
When live account state is available, reconstructing it from local activity adds unnecessary uncertainty.
2. Multiple quota windows should remain independent
Collapsing them into one score makes the interface simpler but the decision worse.
3. Stale data should fail visibly
A confident old number is more dangerous than an explicit refresh failure.
4. Desktop identity is distributed state
Authentication files, global state, browser caches, and running processes may all participate in the active account.
5. Cross-platform consistency is about outcomes
The macOS and Windows implementations can use different native approaches while preserving the same user-facing contract.
Project links
- Website: https://codexcontrol.app
- GitHub: https://github.com/ademisler/codexcontrol
- Releases: https://github.com/ademisler/codexcontrol/releases
- License: MIT
The final article in this series covers Myna Player: a local-first AI video player that processes audio ahead of playback to generate contextual subtitles at the correct time.
Comments
Join the discussionNo comments yet. Be the first to comment.
