OpenClaw OpenAI Account Switcher
Use this skill for multiple OpenAI OAuth login accounts managed inside OpenClaw, especially OpenClaw's own openai-codex OAuth accounts, not just plain Codex CLI account files.
What this skill does
- - Save the current OpenClaw
openai-codex:default OAuth profile as a named snapshot - Add a new account by running INLINECODE2
- Switch all agents to a named snapshot
- Show the current active account (email/account id when available)
- Show cached/observed 5-hour and weekly usage when quota data can be collected
- Auto-switch to the account with remaining quota
- Fall back to a backup model when all accounts are exhausted
Files
- - Main script: INLINECODE3
- Regression test script: INLINECODE4
- State store: INLINECODE5
Run the bundled regression suite with:
CODEBLOCK0
Daily local check command:
CODEBLOCK1
It writes timestamped logs under:
First use
List current snapshots:
CODEBLOCK2
INLINECODE7 , add, use, auto, and cron-check all reconcile the current live OpenClaw login back into the saved account list before reporting or switching, so out-of-band openclaw models auth login --provider openai-codex changes are picked up automatically.
The same reconciliation also keeps related OpenClaw auth metadata in sync:
- - add missing
auth.profiles email aliases in INLINECODE14 - keep
auth.order.openai-codex aligned with the saved account list and current active account - add/remove matching named email profiles in each agent INLINECODE16
- prune stale aliases that no longer correspond to any saved account
Probe real quota via Codex CLI and show it in the list:
CODEBLOCK3
Add an account (interactive OAuth login):
CODEBLOCK4
Switch account:
CODEBLOCK5
Auto-pick best account or fall back:
CODEBLOCK6
Auto-check mode for cron/systemd (single-line notification-friendly output):
CODEBLOCK7
Important behavior
1. Canonical auth source
This skill treats OpenClaw agent auth files as the source of truth:
It updates all configured agents to keep openai-codex:default aligned.
2. Quota collection
Quota collection works in two modes:
- - Preferred: if a matching Codex CLI account snapshot exists and
codex is installed, collect real 5-hour / weekly rate-limit data - Fallback: if no Codex CLI snapshot exists, keep local observations and report that quota is unknown
3. Fallback model
Default backup model is:
You can also override defaults via environment variables:
- -
OPENCLAW_HOME — OpenClaw data root (default ~/.openclaw) - INLINECODE23 — canonical agent whose auth file is treated as the real active auth (default
taizi) - INLINECODE25 — backup model to use when all OpenAI accounts are exhausted (default
bailian/qwen3.5-plus)
Override it with:
CODEBLOCK8
4. Automatic checks
Run periodically with cron/systemd/loop. Example one-shot command:
CODEBLOCK9
A good interval is every 10-15 minutes.
For unattended auto-switching, prefer a two-stage threshold with a short inactivity guard. The bundled script now supports:
CODEBLOCK10
This means:
- - when 5-hour usage reaches 80% (about 20% left), attempt switching only if no non-cron session across configured agents has been active within the last 3 minutes
- when 5-hour usage reaches 90% (about 10% left), switch immediately to avoid hitting rate limits, even if sessions are active
- when weekly usage reaches 90% (about 10% left for the week), attempt switching only if no non-cron session across configured agents has been active within the last 3 minutes
- when weekly usage reaches 95% (about 5% left for the week), switch immediately to preserve the remaining weekly quota
Recommended policy
Use this policy by default:
- - Check every 10 minutes
- At 5-hour usage 80%: try switching only when all non-cron sessions have been inactive for 3 minutes
- At 5-hour usage 90%: switch immediately to avoid rate limits
- At weekly usage 90%: try switching only when all non-cron sessions have been inactive for 3 minutes
- At weekly usage 95%: switch immediately to preserve the remaining weekly quota
- Prefer staying on the current account if it is still under both soft thresholds
- Prefer same-model account rotation (
openai-codex/gpt-5.4 → another openai-codex/gpt-5.4 account) - Fall back to a backup model only when all accounts are above threshold or quota is unknown
Default thresholds in the script:
- - 5-hour soft switch threshold: INLINECODE29
- 5-hour hard switch threshold: INLINECODE30
- weekly soft switch threshold: INLINECODE31
- weekly hard switch threshold: INLINECODE32
- inactivity guard:
3 minutes
Recommended workflow
- 1. Add at least two accounts
- Run
list --verbose to verify identities - Run
auto to validate selection logic - When the user wants automation, schedule
auto periodically - When switching happens, report:
- active account name
- email
- 5-hour usage
- weekly usage
- current primary model / fallback model
Health states
The script now distinguishes these states:
- -
healthy: token works and rate limits were read successfully - INLINECODE38 : token/refresh/login is invalid and the account should be re-logged-in
- INLINECODE39 : token exists but the workspace/plan/model is not usable
- INLINECODE40 : quota could not be determined yet
These states appear in list --verbose and status.
Important implementation note
After inspecting OpenClaw source (/tmp/openclaw/src/commands/models/auth.ts and /tmp/openclaw/src/commands/onboard-auth.credentials.ts) plus live runtime files, the built-in openclaw models auth login --provider openai-codex has these practical semantics:
- 1. the live credential is used via INLINECODE46
- switching accounts writes the new token into INLINECODE47
- email-specific profiles such as
openai-codex:user@example.com are kept as named records - sibling agent auth stores are synced together
- existing
usageStats and lastGood are preserved instead of being cleared
This skill now mirrors that model:
- - real active-account detection keys off the live
openai-codex:default credential - snapshots keep a stable email-based identity instead of relying on ambiguous INLINECODE52
- switching rewrites
:default across all agents while also retaining/upserting the email-specific profile entry - metadata migration repairs older snapshot records that incorrectly treated
:default as a unique account id - email is the canonical human identity for deduplication;
accountId may change when the same email moves to a different workspace/team, so same-email re-logins update the existing saved account instead of creating a duplicate
Notes
- - This skill stores sensitive OAuth tokens under INLINECODE56
- Treat those files like credentials
- Prefer
list --verbose or status before making risky changes - Active account display is derived from the real OpenClaw auth file (
~/.openclaw/agents/<agent>/agent/auth-profiles.json), then synced back into skill metadata. This avoids stale ACTIVE markers after manual openclaw models auth login or other out-of-band auth changes. - During periodic checks, if the real current OpenClaw auth is not present in the list yet, the skill will auto-enroll it as a new
accountN snapshot and mark it active.
OpenClaw OpenAI 账户切换器
使用此技能管理 OpenClaw 中管理的多个 OpenAI OAuth 登录账户,特别是 OpenClaw 自带的 openai-codex OAuth 账户,而不仅仅是普通的 Codex CLI 账户文件。
技能功能
- - 将当前 OpenClaw openai-codex:default OAuth 配置文件保存为命名快照
- 通过运行 openclaw models auth login --provider openai-codex 添加新账户
- 将所有代理切换到指定命名快照
- 显示当前活跃账户(有可用信息时显示邮箱/账户ID)
- 在可收集配额数据时显示缓存/观测到的 5小时 和 每周 使用量
- 自动切换到有剩余配额的账户
- 当所有账户配额耗尽时回退到备用模型
文件
- - 主脚本:scripts/openclaw-openai-accounts.py
- 回归测试脚本:scripts/testopenclawopenai_accounts.py
- 状态存储:~/.openclaw/openai-codex-accounts/
运行捆绑的回归测试套件:
bash
python3 scripts/testopenclawopenai_accounts.py
每日本地检查命令:
bash
/home/djy/.openclaw/workspace-taizi/bin/check-openai-multi-account
日志文件写入以下目录并带有时间戳:
- - /home/djy/.openclaw/workspace-taizi/data/openai-multi-account-checks/
首次使用
列出当前快照:
bash
python3 scripts/openclaw-openai-accounts.py list
list、add、use、auto 和 cron-check 在报告或切换之前,都会将当前实时的 OpenClaw 登录状态同步回已保存的账户列表中,因此带外执行的 openclaw models auth login --provider openai-codex 变更会被自动捕获。
同样的同步操作还会保持相关的 OpenClaw 认证元数据一致:
- - 在 openclaw.json 中添加缺失的 auth.profiles 邮箱别名
- 保持 auth.order.openai-codex 与已保存的账户列表及当前活跃账户一致
- 在每个代理的 auth-profiles.json 中添加/移除匹配的命名邮箱配置文件
- 清理不再对应任何已保存账户的过期别名
通过 Codex CLI 探测真实配额并在列表中显示:
bash
python3 scripts/openclaw-openai-accounts.py list --verbose --probe
添加账户(交互式 OAuth 登录):
bash
python3 scripts/openclaw-openai-accounts.py add --name work
切换账户:
bash
python3 scripts/openclaw-openai-accounts.py use work
自动选择最佳账户或回退:
bash
python3 scripts/openclaw-openai-accounts.py auto
用于 cron/systemd 的自动检查模式(单行通知友好输出):
bash
python3 scripts/openclaw-openai-accounts.py cron-check
重要行为
1. 权威认证源
此技能将 OpenClaw 代理认证文件视为权威数据源:
- - ~/.openclaw/agents//agent/auth-profiles.json
它会更新所有已配置的代理以保持 openai-codex:default 一致。
2. 配额收集
配额收集有两种工作模式:
- - 首选模式:如果存在匹配的 Codex CLI 账户快照且已安装 codex,则收集真实的 5小时/每周速率限制数据
- 回退模式:如果不存在 Codex CLI 快照,则保留本地观测数据并报告配额未知
3. 回退模型
默认备用模型为:
你也可以通过环境变量覆盖默认值:
- - OPENCLAWHOME — OpenClaw 数据根目录(默认为 ~/.openclaw)
- OPENCLAWPRIMARYAGENT — 认证文件被视为真实活跃认证的规范代理(默认为 taizi)
- OPENCLAWFALLBACK_MODEL — 当所有 OpenAI 账户配额耗尽时使用的备用模型(默认为 bailian/qwen3.5-plus)
通过以下命令覆盖:
bash
python3 scripts/openclaw-openai-accounts.py auto --fallback-model bailian/glm-5
4. 自动检查
通过 cron/systemd/循环定期运行。示例一次性命令:
bash
python3 scripts/openclaw-openai-accounts.py auto --json
建议间隔为每 10-15 分钟。
对于无人值守的自动切换,建议使用带有短时非活动保护的两阶段阈值。捆绑脚本现在支持:
bash
python3 scripts/openclaw-openai-accounts.py auto --five-hour-switch-at 80 --five-hour-hard-switch-at 90 --weekly-switch-at 90 --weekly-hard-switch-at 95 --inactive-minutes 3
python3 scripts/openclaw-openai-accounts.py cron-check --five-hour-switch-at 80 --five-hour-hard-switch-at 90 --weekly-switch-at 90 --weekly-hard-switch-at 95 --inactive-minutes 3
这意味着:
- - 当 5小时使用量达到 80%(剩余约 20%)时,仅当所有已配置代理上的非 cron 会话在过去 3分钟 内没有活动时才尝试切换
- 当 5小时使用量达到 90%(剩余约 10%)时,立即切换以避免达到速率限制,即使会话处于活动状态
- 当每周使用量达到 90%(本周剩余约 10%)时,仅当所有已配置代理上的非 cron 会话在过去 3分钟 内没有活动时才尝试切换
- 当每周使用量达到 95%(本周剩余约 5%)时,立即切换以保留剩余的每周配额
推荐策略
默认使用此策略:
- - 每 10 分钟检查一次
- 5小时使用量达到 80%:仅当所有非 cron 会话已非活动 3 分钟时才尝试切换
- 5小时使用量达到 90%:立即切换以避免速率限制
- 每周使用量达到 90%:仅当所有非 cron 会话已非活动 3 分钟时才尝试切换
- 每周使用量达到 95%:立即切换以保留剩余的每周配额
- 如果当前账户仍低于两个软阈值,则优先保留在当前账户
- 优先进行同模型账户轮换(openai-codex/gpt-5.4 → 另一个 openai-codex/gpt-5.4 账户)
- 仅当所有账户都超过阈值或配额未知时才回退到备用模型
脚本中的默认阈值:
- - 5小时软切换阈值:80%
- 5小时硬切换阈值:90%
- 每周软切换阈值:90%
- 每周硬切换阈值:95%
- 非活动保护时间:3 分钟
推荐工作流程
- 1. 至少添加两个账户
- 运行 list --verbose 验证身份
- 运行 auto 验证选择逻辑
- 当用户需要自动化时,定期调度 auto
- 切换发生时报告:
- 活跃账户名称
- 邮箱
- 5小时使用量
- 每周使用量
- 当前主模型/备用模型
健康状态
脚本现在区分以下状态:
- - healthy:令牌有效且成功读取速率限制
- auth-invalid:令牌/刷新/登录无效,需要重新登录账户
- plan-unavailable:令牌存在但工作空间/计划/模型不可用
- quota-unknown:尚无法确定配额
这些状态出现在 list --verbose 和 status 中。
重要实现说明
在检查 OpenClaw 源码(/tmp/openclaw/src/commands/models/auth.ts 和 /tmp/openclaw/src/commands/onboard-auth.credentials.ts)以及运行时文件后,内置的 openclaw models auth login --provider openai-codex 具有以下实际语义:
- 1. 实时凭据通过 openai-codex:default 使用
- 切换账户会将新令牌写入 openai-codex:default
- 特定邮箱的配置文件(如 openai-codex:user@example.com)作为命名记录保留
- 同级代理认证存储同步更新
- 现有的 usageStats 和 lastGood 被保留而非清除
此技能现在镜像了该模型:
- - 真实活跃账户检测基于实时的 openai-codex:default 凭据
- 快照保持稳定的基于邮箱的身份,而非依赖模糊的 :default
- 切换会重写所有代理中的