@phantomcodes/cli 1.6.3 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/README.md +138 -117
  2. package/bin/phantom.mjs +37 -2
  3. package/dist/agents/command.d.mts +8 -27
  4. package/dist/agents/command.mjs +323 -59
  5. package/dist/agents/login.d.mts +3 -0
  6. package/dist/agents/login.mjs +97 -0
  7. package/dist/agents/opencode-check.d.mts +15 -0
  8. package/dist/agents/opencode-check.mjs +193 -0
  9. package/dist/agents/phantom-key.d.mts +7 -0
  10. package/dist/agents/phantom-key.mjs +40 -0
  11. package/dist/agents/proxy.d.mts +26 -28
  12. package/dist/agents/proxy.mjs +225 -33
  13. package/dist/chatgpt/command.d.mts +2 -0
  14. package/dist/chatgpt/command.mjs +119 -0
  15. package/dist/chatgpt/session.d.mts +87 -0
  16. package/dist/chatgpt/session.mjs +253 -0
  17. package/dist/cli/accounts.d.mts +21 -0
  18. package/dist/cli/accounts.mjs +96 -0
  19. package/dist/cli/app.d.mts +4 -24
  20. package/dist/cli/app.mjs +224 -139
  21. package/dist/cli/commands.d.mts +1 -16
  22. package/dist/cli/commands.mjs +144 -239
  23. package/dist/cli/help.d.mts +0 -19
  24. package/dist/cli/help.mjs +30 -55
  25. package/dist/cli/run.mjs +35 -79
  26. package/dist/cli/start.d.mts +2 -31
  27. package/dist/cli/start.mjs +13 -116
  28. package/dist/core/brand.d.mts +0 -7
  29. package/dist/core/brand.mjs +0 -8
  30. package/dist/core/errors.d.mts +0 -16
  31. package/dist/core/errors.mjs +0 -16
  32. package/dist/core/helpers.d.mts +0 -46
  33. package/dist/core/helpers.mjs +1 -64
  34. package/dist/core/node-trust.d.mts +0 -5
  35. package/dist/core/node-trust.mjs +0 -6
  36. package/dist/core/pick.d.mts +7 -55
  37. package/dist/core/pick.mjs +49 -106
  38. package/dist/core/request.d.mts +0 -46
  39. package/dist/core/request.mjs +1 -60
  40. package/dist/core/secrets.d.mts +0 -4
  41. package/dist/core/secrets.mjs +0 -10
  42. package/dist/core/state.d.mts +3 -1
  43. package/dist/core/state.mjs +147 -4
  44. package/dist/core/style.d.mts +0 -27
  45. package/dist/core/style.mjs +0 -20
  46. package/dist/core/version.d.mts +1 -2
  47. package/dist/core/version.mjs +1 -2
  48. package/dist/local/accounts.d.mts +25 -0
  49. package/dist/local/accounts.mjs +40 -0
  50. package/dist/local/agent-headers.d.mts +7 -0
  51. package/dist/local/agent-headers.mjs +44 -0
  52. package/dist/local/chatgpt-plan.d.mts +147 -0
  53. package/dist/local/chatgpt-plan.mjs +384 -0
  54. package/dist/local/command.d.mts +24 -0
  55. package/dist/local/command.mjs +211 -0
  56. package/dist/local/config.d.mts +49 -0
  57. package/dist/local/config.mjs +168 -0
  58. package/dist/local/decide.d.mts +37 -0
  59. package/dist/local/decide.mjs +201 -0
  60. package/dist/local/failure-signs.d.mts +4 -0
  61. package/dist/local/failure-signs.mjs +99 -0
  62. package/dist/local/keys.d.mts +32 -0
  63. package/dist/local/keys.mjs +239 -0
  64. package/dist/local/models.d.mts +53 -0
  65. package/dist/local/models.mjs +117 -0
  66. package/dist/local/replay.d.mts +59 -0
  67. package/dist/local/replay.mjs +143 -0
  68. package/dist/local/report.d.mts +43 -0
  69. package/dist/local/report.mjs +153 -0
  70. package/dist/local/request-log.d.mts +53 -0
  71. package/dist/local/request-log.mjs +69 -0
  72. package/dist/local/retry-after.d.mts +3 -0
  73. package/dist/local/retry-after.mjs +28 -0
  74. package/dist/local/rust-router.d.mts +39 -0
  75. package/dist/local/rust-router.mjs +54 -0
  76. package/dist/local/server.d.mts +27 -0
  77. package/dist/local/server.mjs +449 -0
  78. package/dist/local/status-line.d.mts +17 -0
  79. package/dist/local/status-line.mjs +81 -0
  80. package/dist/local/tiers.d.mts +3 -0
  81. package/dist/local/tiers.mjs +137 -0
  82. package/dist/local/traces.d.mts +69 -0
  83. package/dist/local/traces.mjs +341 -0
  84. package/dist/phantom.d.mts +0 -37
  85. package/dist/phantom.mjs +0 -31
  86. package/package.json +15 -9
  87. package/skills/phantom-ai/SKILL.md +28 -97
  88. package/dist/cli/screens.d.mts +0 -177
  89. package/dist/cli/screens.mjs +0 -398
  90. package/dist/core/free.d.mts +0 -13
  91. package/dist/core/free.mjs +0 -14
  92. package/dist/core/motion.d.mts +0 -47
  93. package/dist/core/motion.mjs +0 -156
  94. package/dist/core/shapes.d.mts +0 -13
  95. package/dist/core/shapes.mjs +0 -1
  96. package/dist/keys/command.d.mts +0 -16
  97. package/dist/keys/command.mjs +0 -143
  98. package/dist/keys/saved.d.mts +0 -96
  99. package/dist/keys/saved.mjs +0 -169
  100. package/dist/models/choice.d.mts +0 -7
  101. package/dist/models/choice.mjs +0 -27
  102. package/dist/models/command.d.mts +0 -8
  103. package/dist/models/command.mjs +0 -66
  104. package/dist/models/list.d.mts +0 -39
  105. package/dist/models/list.mjs +0 -39
  106. package/dist/receipts/command.d.mts +0 -7
  107. package/dist/receipts/command.mjs +0 -41
  108. package/dist/receipts/receipts.d.mts +0 -76
  109. package/dist/receipts/receipts.mjs +0 -118
  110. package/dist/setup/agents.d.mts +0 -149
  111. package/dist/setup/agents.mjs +0 -886
  112. package/dist/setup/apply.d.mts +0 -85
  113. package/dist/setup/apply.mjs +0 -301
  114. package/dist/setup/command.d.mts +0 -12
  115. package/dist/setup/command.mjs +0 -307
  116. package/dist/setup/editors.d.mts +0 -108
  117. package/dist/setup/editors.mjs +0 -500
  118. package/dist/setup/legacy.d.mts +0 -13
  119. package/dist/setup/legacy.mjs +0 -36
  120. package/dist/setup/plan.d.mts +0 -104
  121. package/dist/setup/plan.mjs +0 -132
  122. package/dist/setup/screen.d.mts +0 -30
  123. package/dist/setup/screen.mjs +0 -260
  124. package/dist/setup/shared.d.mts +0 -141
  125. package/dist/setup/shared.mjs +0 -88
  126. package/dist/setup/was.d.mts +0 -42
  127. package/dist/setup/was.mjs +0 -91
  128. package/dist/spend/command.d.mts +0 -5
  129. package/dist/spend/command.mjs +0 -235
  130. package/dist/spend/lineup.d.mts +0 -61
  131. package/dist/spend/lineup.mjs +0 -39
  132. package/dist/spend/reason.d.mts +0 -5
  133. package/dist/spend/reason.mjs +0 -60
  134. package/dist/spend/report.d.mts +0 -105
  135. package/dist/spend/report.mjs +0 -68
  136. package/dist/spend/routing.d.mts +0 -74
  137. package/dist/spend/routing.mjs +0 -57
  138. package/dist/workspace/command.d.mts +0 -59
  139. package/dist/workspace/command.mjs +0 -871
package/README.md CHANGED
@@ -1,190 +1,211 @@
1
1
  # Phantom CLI
2
2
 
3
- `phantom` connects Claude Code, Codex, pi and other coding agents to [Phantom AI](https://phantom.codes), an OpenAI-compatible API. It signs you in, keeps your key on your computer, adds credit and caps what each key can spend.
3
+ `phantom` starts your coding agents (pi, OpenCode, Codex and Claude Code) and routes their requests to accounts you already pay for. phantom-1 picks a tier for each request, Standard for most and Frontier for the hard ones, and sends it to the account and model you chose for that tier: the Vercel AI Gateway, Z.ai's GLM Coding Plan or your ChatGPT plan.
4
4
 
5
5
  [Docs](https://phantom.codes/docs) · [npm](https://www.npmjs.com/package/@phantomcodes/cli)
6
6
 
7
- ## Install and set up
7
+ ## Install and start
8
8
 
9
- You need Node 24 or newer and the coding agent itself.
9
+ You need the coding agent itself. The installer needs no Node. It puts `phantom` in `~/.local/bin`, adds that folder to your PATH, and checks the download against the checksum npm lists. It runs on macOS, and on Linux with glibc 2.34 or newer:
10
10
 
11
11
  ```bash
12
- npm i -g @phantomcodes/cli
13
- phantom setup
12
+ curl -fsSL https://phantom.codes/install | sh
13
+ phantom
14
14
  ```
15
15
 
16
- With no key saved yet, `phantom setup` signs you in first, with the same browser approval as `phantom login`. Then it finds the agents on your computer, shows what it will change in each file, and asks before it writes anything. Restart your agent, or open a new terminal, to use the connection.
16
+ On Windows, Alpine and older Linux, use Node 24 or newer: `npm i -g @phantomcodes/cli`.
17
17
 
18
- With no credit, phantom-1 runs on free models, up to 200 requests a day from a coding agent, and you don't need a card. Free calls leave out web search, which costs money on any model.
18
+ `phantom` shows three kinds of row: the agents that route on your own accounts (pi and OpenCode, each when it is installed), Settings and Report. Enter on an agent starts it; Report shows what routing did in the last day (`phantom report --since 1d`). Above the list, one line says where each tier goes and what still needs doing, for example:
19
19
 
20
- If you used pai, this CLI's old name, run `phantom setup` once after you install. Until then, Claude Code and the shell block that pai wrote still call `pai`, and phantom prints a reminder.
20
+ ```
21
+ Standard glm-5.4 on zai · Frontier gpt-6.1-sol on chatgpt · ! chatgpt needs sign-in
22
+ ```
21
23
 
22
- ### What setup changes
24
+ The first time, before there is any routing to show, `phantom` opens the settings first, then comes back to the list.
23
25
 
24
- Setup writes model settings for Claude Code, Codex, opencode, Continue, Goose, Aider, GitHub Copilot CLI, Cline, Gemini CLI, Crush, Qwen Code, Kilo Code, Kimi Code, Droid, Mistral Vibe and Zed. Cursor, VS Code, JetBrains AI Assistant, Warp and Trae keep these settings on their own settings screens, so setup prints the steps for you to follow. pi gets the skill only.
26
+ | Command | What it does |
27
+ | --- | --- |
28
+ | `phantom` | pi, OpenCode, Settings and Report |
29
+ | `phantom pi`, `phantom opencode`, `phantom codex`, `phantom claude` | starts that agent without asking; Codex and Claude Code run through Phantom's server, so the list leaves them out |
30
+ | `phantom config` | the settings: Standard, Frontier and providers |
31
+ | `phantom login`, `phantom logout` | sign in to Phantom and save your key, or remove it from this computer (see [Sign in and add credit](#sign-in-and-add-credit)) |
32
+ | `phantom report` | what routing did |
33
+ | `phantom replay` | other routing settings tried on saved traces (see [Traces](#traces-full-prompts-and-answers-off-by-default)) |
25
34
 
26
- It also installs the `phantom-ai` skill, a file that teaches the agent the phantom commands. To install the skill for one agent and change nothing else, run `phantom setup --agent claude` (or the agent's name).
35
+ Without a terminal (in a script or a pipe), `phantom` prints these as three lines and starts nothing.
27
36
 
28
- - `phantom setup --dry-run` shows the changes and writes nothing.
29
- - `--no-shell`, or unticking "Your shell" in the checklist, leaves your shell startup file alone. Setup then prints the lines to add yourself.
30
- - `--limit 20` caps what the key can spend each month, and `--limit none` removes the cap. At a terminal, setup asks once when the key has no cap.
31
- - `phantom setup --restore` puts back each file setup changed, from the copies it kept. A file you edited after setup keeps your edits: restore takes out only Phantom's own lines and keys, and leaves the copy beside the file. Restore also removes the `settings.json.bak` copy that setup makes for Claude Code and Gemini, unless a `.bak` was already there before setup or you changed it since. It exits 1 only when a file could not be undone.
32
- - `phantom setup --provider off` takes Phantom out of an agent and keeps the rest of its settings. In JSON settings files (Claude Code, Qwen Code, Gemini CLI and others), setup records each value it replaces and puts it back.
37
+ ## Settings
33
38
 
34
- Claude Code's background calls (titles, summaries and its Explore agent) run on the model you chose with `phantom models`, so picking `phantom-1:private` covers them too.
39
+ `phantom config` is one screen. At the top is a table of your providers' accounts: each one's kind, whether its key is found (or ChatGPT is signed in), where the key comes from, which tiers use it, and how many requests it answered in the last day. Under it are four rows. Choose one with the arrow keys and Enter; Esc goes back.
35
40
 
36
- ## Let your agent set it up
41
+ ```
42
+ ? Settings
43
+ ❯ Standard glm-5.4 · Z.ai
44
+ Frontier gpt-6.1-sol · ChatGPT
45
+ Providers ChatGPT ✓ · Z.ai ✓ · Vercel AI Gateway ✓
46
+ Back
47
+ ```
37
48
 
38
- Paste this into Claude Code, Codex, Cursor or another agent that can run terminal commands:
49
+ - **Standard** and **Frontier** each open one list of every model your connected providers offer, each beside its provider, with the marker on the tier's current model. Type to narrow it, or type `account:model`. Enter sets that model and comes straight back; the tier's fallback and the other tier stay as they were. The last row, **Fallback**, opens the same list with No fallback first. A provider is connected when its key is found, or when you are signed in to ChatGPT.
50
+ - **Providers** lists ChatGPT, Z.ai and the Vercel AI Gateway, connected or not. Each opens what you can do with it: sign in or out of ChatGPT in the browser, or for a key provider save a key (hidden typing), use an item already in the macOS Keychain, or remove the key.
39
51
 
40
- ```text
41
- Help me set up phantom for AI inference through Phantom AI.
52
+ Each choice runs the same command a script would, so you can see the script form in `phantom config --help`:
42
53
 
43
- Check that I have Node 24 or newer, then install @phantomcodes/cli if needed.
44
- Run phantom setup --agent <my agent> to install the phantom-ai skill, and read it.
45
- Check for an existing workspace and saved keys before changing anything.
46
- If I need to connect, start phantom login and let me complete the device approval.
47
- Check the workspace balance. Ask which coding agent I use and what budget I want,
48
- then create a named workspace key with that cap.
49
- Ask whether I want to connect my existing agent for future runs or use a temporary
50
- connection for one run. For saved setup, preview the changes with phantom setup --dry-run
51
- and show them to me before applying the setup I approve. Make sure any spending cap
52
- is on the key my agent uses. Ask before adding funds or starting paid inference.
53
- Keep keys and recovery credentials out of chat. Finish with the command to launch
54
- my agent, and explain how to undo any saved setup.
54
+ ```bash
55
+ phantom config --standard zai:glm-5.4 --standard-fallback gateway:deepseek/deepseek-v4.1-flash-1017 \
56
+ --frontier chatgpt:gpt-6.1-sol --frontier-fallback gateway:anthropic/claude-sonnet-5.5
57
+ pbpaste | phantom config key set gateway --stdin
58
+ phantom config key use-keychain zai --service zai-api-key --keychain-account you
59
+ phantom config key rm zai
60
+ phantom config chatgpt login
61
+ phantom config models gateway
62
+ phantom config show
55
63
  ```
56
64
 
57
- ## Run an agent for one session
65
+ A tier flag names the account first, then the model. A model the account's own list doesn't have is refused; when the list can't be read (or, for Z.ai, comes from its docs), phantom warns and writes it anyway. `phantom config show` prints the tiers, where each key comes from and whether it is found, and the ChatGPT sign-in, never a key.
58
66
 
59
- ```bash
60
- phantom login
61
- phantom fund --amount 10 --card
62
- phantom workspace status
63
- phantom codex
64
- ```
67
+ ### Where the keys come from
65
68
 
66
- `phantom claude`, `phantom codex` and `phantom pi` start the agent with a connection that lasts for that run, and leave your saved agent settings alone. When the agent exits, phantom prints what the run spent. The agent starts on phantom-1, or on the model you picked with `phantom models`; pass `--model` after `--` to choose another.
69
+ Each key account (the Gateway, Z.ai) reads its key from one place:
67
70
 
68
- `phantom login` opens a page in your browser. You check that it shows the same short code as your terminal, approve it, and phantom saves the key on this computer (in the macOS Keychain on a Mac). If you already have a key, `phantom login --paste` saves it.
71
+ - **Saved in phantom.** `phantom config key set <account>` asks for the key with hidden typing, or reads it from stdin with `--stdin`. On a Mac it goes in the login Keychain, and `local/keys/<account>` beside `local.json` holds only a pointer to it. Elsewhere that file holds the key, readable by you only. A key on the command line is refused, because shell history keeps it.
72
+ - **A Keychain item you already have** (macOS). `phantom config key use-keychain zai --service <service> --keychain-account <account>` points the account at it without copying it. It is read with `security find-generic-password -s <service> -a <account> -w`, and it has to be readable when you run the command.
73
+ - **An environment variable.** Name it in `local.json` (`"key": { "from": "env", "name": "ZAI_API_KEY" }`). A tier flag that adds the account (`--standard zai:glm-5.4`) names the kind's usual variable, `ZAI_API_KEY` or `AI_GATEWAY_API_KEY`.
69
74
 
70
- Put phantom's own flags before the agent's arguments, with `--` between them:
75
+ `phantom config key rm <account>` forgets an account's key: a saved key is removed, and a Keychain item you made stays where it is. An agent won't start while a tier's account has no key, and phantom says which command fixes it.
71
76
 
72
- ```bash
73
- phantom codex --key coding -- exec "explain this repository"
74
- ```
77
+ ### local.json
75
78
 
76
- ## Add credit
79
+ The settings live in `local.json`, beside your saved keys (`~/.config/phantom/`, or `PHANTOM_STATE_DIR`). Older versions used `~/.config/phantom-key/`: the first phantom command after an update moves that folder to `~/.config/phantom/` and leaves a link at the old name, so saved keys, the ChatGPT sign-in and the request log carry over. When moving isn't plainly safe (either name is a link you made, or `~/.config/phantom/` already holds other files), phantom keeps using `~/.config/phantom-key/` and says once why and how to finish. local.json holds names, never a key:
77
80
 
78
- ```bash
79
- phantom fund --amount 10 --card
80
- phantom fund --amount 10 --coin usdc
81
+ ```json
82
+ {
83
+ "accounts": {
84
+ "gateway": { "kind": "gateway", "key": { "from": "saved" } },
85
+ "zai": { "kind": "zai-coding-plan", "key": { "from": "keychain", "service": "zai-api-key", "account": "you" } },
86
+ "chatgpt": { "kind": "chatgpt-plan" }
87
+ },
88
+ "tiers": {
89
+ "standard": { "account": "zai", "model": "glm-5.4", "fallback": { "account": "gateway", "model": "deepseek/deepseek-v4.1-flash-1017" } },
90
+ "frontier": { "account": "chatgpt", "model": "gpt-6.1-sol", "fallback": { "account": "gateway", "model": "anthropic/claude-sonnet-5.5" } }
91
+ }
92
+ }
81
93
  ```
82
94
 
83
- `--card` opens Stripe Checkout, where the total includes Stripe's card fee. `--coin usdc` pays in USDC on Solana, and `--coin usdc-base` pays in USDC on Base. Pick one of `--card` and `--coin`. Card payments take $5 to $10,000, USDC on Solana $0.10 to $10,000 and USDC on Base $1 to $10,000. A wallet payment link is good for 30 minutes. The credit appears once Stripe confirms the payment. Check it with `phantom balance`.
95
+ - `kind` is `gateway` (Vercel AI Gateway), `zai-coding-plan` (Z.ai's GLM Coding Plan) or `chatgpt-plan` (your ChatGPT plan). Z.ai allows the plan only in the tools it supports, and phantom isn't one of them, so it says so at startup.
96
+ - `key` says where a key account's key comes from: `{ "from": "saved" }`, `{ "from": "env", "name": "..." }` or `{ "from": "keychain", "service": "...", "account": "..." }` (`account` is optional), and `null` until one is set. It holds a name, never a key. A file written before key sources existed says `"keyEnv": "ZAI_API_KEY"`; that still works, and the next change phantom writes turns it into `{ "from": "env", "name": "ZAI_API_KEY" }`.
97
+ - A `chatgpt-plan` account has no `key`: it uses the ChatGPT sign-in (`phantom config chatgpt login`), refreshed as it nears its end. phantom-1 translates each request to the plan's Responses API and the answer back, streamed or not.
98
+ - Each request goes to the tier phantom-1 picks. A file an older phantom wrote may still hold `"mode"`; phantom ignores it, and the next change it writes leaves it out.
99
+ - A tier's `fallback` gets one try when its account answers 429 or a 5xx. On the ChatGPT plan it also gets one when there is no usable sign-in, or when the plan refuses the request (`subscription_sharing_unsupported_capability`). A sign-in the plan refuses passes through, with a line on stderr that says whether signing in again will help. With no fallback and no sign-in, the answer is a 503 that says to run `phantom config chatgpt login`.
100
+ - When an answer carries `Retry-After`, phantom-1 waits that long (up to a minute, and the shortest wait any failed answer of the tier asked for) and tries the tier once more before passing the error on, because agents retry on their own schedule and ignore that header. A longer wait passes straight through, since sitting on the request that long looks like a hang. The wait that actually elapsed ends up in `waited=` on the request's stderr line.
101
+ - Each answer carries `x-phantom-local-tier`, `x-phantom-local-reason` and `x-phantom-local-request-id`, and each request prints one line to stderr with the tiers, account, model, status, tokens and total time, with no message text and no key.
84
102
 
85
- ## Give each agent its own key and cap
103
+ ## Start an agent
86
104
 
87
- Every key in a workspace spends from the same balance. Make one key per agent, each with its own cap:
105
+ `phantom pi` starts pi with phantom-1 running inside the same process, on a free port on 127.0.0.1, and points pi at it with the model `phantom-1`:
88
106
 
89
107
  ```bash
90
- phantom keys create --save coding --limit 5
91
- phantom claude --key coding
92
- phantom keys list
93
- phantom keys set --id coding --limit 10
108
+ phantom pi -- -p "list the files in this folder"
109
+ # phantom: pi routes on this computer: Standard glm-5.4 on zai, Frontier gpt-6.1-sol on chatgpt (local.json)
110
+ # phantom: pi made 7 requests: 5 Standard (zai), 2 Frontier (chatgpt), 1 fallback; log: ~/.config/phantom/local/requests.jsonl
94
111
  ```
95
112
 
96
- `phantom keys set` changes three caps:
113
+ Each request goes straight from your computer to the account its tier names. pi holds no account key: the router reads them, and pi's environment leaves out the variables `local.json` reads keys from. pi gets only a token for this run, and the router refuses requests without it. If a tier's key is missing or `local.json` is broken, phantom stops before pi starts, and says which key is missing and the command that sets it.
97
114
 
98
- - `--limit`: the most the key can spend in total.
99
- - `--budget`: the most it can spend each month, or each `--period` days.
100
- - `--rate`: the most it can spend in one minute, in dollars.
115
+ A task with an image (a screenshot you attach, or an image a tool read) goes to a tier whose model reads images, for every step until your next message. Some don't, and drop the image without an error: glm-5.4 on Z.ai's GLM Coding Plan does, glm-5.3-flash doesn't. phantom knows a Gateway model from the Gateway's `vision` tag and a Z.ai model from its own list; a ChatGPT plan model counts as able. A model that can't read images (when neither tier's can, or a fallback that can't) is told the image was left out, so its answer starts by saying it couldn't look at the image because your models don't have vision, then goes on from your text.
101
116
 
102
- Each takes a number of zero or more, or `none`. phantom refuses a mistyped value such as `5usd` and changes nothing. `--id` takes the key's name, its id, or the first 4 or more characters of its id, as `phantom keys list` shows them.
117
+ While pi runs, its footer shows who answered the last request and why, such as `phantom: Frontier, gpt-6.1-sol on chatgpt, the same failure twice`. When something goes wrong, pi shows a warning once: a tier used its fallback (and why), an account answered with an error or couldn't be reached, the ChatGPT sign-in isn't usable or was refused, the router didn't answer, or the request log or a trace couldn't be saved. The same warning shows again after 10 minutes if it's still happening. Switching pi to a model that isn't phantom's clears the footer line.
103
118
 
104
- Revoking a key turns it off for good; its credit stays in the workspace.
119
+ For the run, `phantom/phantom-1` joins the models you scoped in pi (your own `--models`, or `enabledModels` in pi's settings: the project's `.pi/settings.json` when pi trusts the project, else `settings.json` in pi's agent folder), so Ctrl+P and `/scoped-models` offer it next to them and you can switch back to it. phantom only reads pi's settings. With no scope, pi already offers every model, phantom-1 included. phantom leaves pi's scope as it is, without phantom-1, in three cases: the scope already has a `phantom/` model; the project has its own scope and pi hasn't been told whether to trust the project yet (answer pi's question once, and the next run adds phantom-1); or a pattern has a comma, such as `claude-{sonnet,opus}*`, which `--models` can't carry (phantom says so when pi starts).
105
120
 
106
- If phantom can't save a new or rotated key on this computer (a locked Keychain over SSH, for example), it prints the key once and exits with an error. Copy the key before you close the terminal.
121
+ Pressing Ctrl+S in `/scoped-models` during a `phantom pi` run saves the scope pi shows, `phantom/phantom-1` included, into `enabledModels` in pi's agent `settings.json`. pi has no way to save it without that. Plain `pi` then has a pattern that matches no model. Later `phantom pi` runs see the saved `phantom/` model and add no second one. To remove it, delete `"phantom/phantom-1"` from `enabledModels` in pi's `settings.json` (`~/.pi/agent/settings.json`, unless `--agent-dir` or `PI_CODING_AGENT_DIR` moves it).
107
122
 
108
- ## Choose the model
123
+ `phantom opencode` does the same for [OpenCode](https://opencode.ai):
109
124
 
110
- Phantom has three models of its own. `phantom models` shows what each can do (chat, images in, embeddings, rerank, images, video, speech, transcription) and the model behind each job:
125
+ ```bash
126
+ phantom opencode # OpenCode's own screen
127
+ phantom opencode -- run "list the files in this folder"
128
+ # phantom: opencode routes on this computer: Standard glm-5.4 on zai, Frontier gpt-6.1-sol on chatgpt (local.json)
129
+ # phantom: opencode made 3 requests: 2 Standard (zai), 1 Frontier (chatgpt); log: ~/.config/phantom-key/local/requests.jsonl
130
+ ```
111
131
 
112
- - `phantom-1` does every job. Chat starts each task on the standard tier and moves hard steps to frontier: a failed try, or an agent that is stuck.
113
- - `phantom-1:free` is chat on free models at $0, with no card.
114
- - `phantom-1:private` is chat (with images) on providers that keep none of your data and don't train on your prompts. It replaced `phantom-1:zdr`, which now answers 400.
132
+ OpenCode gets a provider for this run only, in the `OPENCODE_CONFIG_CONTENT` variable, with the model `phantom-relay-3f9a/phantom-1`. The four letters after `phantom-relay-` are new for each run, so no setting in your own config can name that provider and change where it sends calls. OpenCode lays the run's config over your own `~/.config/opencode/opencode.jsonc`, which phantom never changes. Every other provider is turned off for the run, your own `phantom` provider included, so OpenCode's own jobs (session titles, summaries) go through phantom-1 too. OpenCode runs on a server of its own for the run (`--standalone`), because its shared background server wouldn't see this run's provider. Its subagents' requests stay on Standard, as Claude Code's helper agents do.
115
133
 
116
- At a terminal, `phantom models` asks which one your agents should use. To pick without the menu:
134
+ Before each run phantom asks OpenCode, with the run's settings, which models it could use (`opencode models`) and which config files it read (`opencode debug config`). It does this in the folder OpenCode starts in, in a folder you name in the arguments, and, for a session you name with `-s` or `--session`, in the folder that session was started in, since OpenCode continues a session with that folder's settings. phantom stops before starting OpenCode, names the files and says what to remove, when:
117
135
 
118
- ```bash
119
- phantom models use phantom-1:private
120
- ```
136
+ - a model outside the run's provider is offered;
137
+ - a config file turns another provider on with `enabled_providers` or an `allow` rule in `policies`, even one with no key yet, because `/connect` in OpenCode's screen could add a key during the run;
138
+ - a config file changes the run's provider (an address, a model, a package);
139
+ - OpenCode can't say where a session named with `-s` lives, or doesn't answer within 30 seconds.
121
140
 
122
- `phantom claude`, `phantom codex`, `phantom pi` and the next `phantom setup` start on the model you pick. Agents you set up earlier keep their model until you run `phantom setup` again.
141
+ An agent or `small_model` that names another provider's model doesn't stop the run: OpenCode doesn't offer that model, so the agent runs on phantom-1. `phantom opencode -- -c` continues the last session of the folder phantom already checked.
123
142
 
124
- ### Pick the model in each tier
143
+ What the check doesn't cover:
125
144
 
126
- Your key's lineup sets the model phantom-1 runs in each tier, and a fallback model to try when that one fails:
145
+ - **OpenCode plugins** run their own code inside OpenCode, and one can make its own network calls. phantom doesn't read or limit them.
146
+ - **Changes during a run.** phantom checks once, before OpenCode starts. If you edit an OpenCode config file during the run and OpenCode picks it up (`opencode reload`, for example), the new settings aren't checked.
127
147
 
128
- ```bash
129
- phantom lineup --table
130
- phantom lineup set --frontier <model> --standard-fallback <model>
131
- phantom lineup reset
132
- ```
148
+ A session saved during a run records the run's provider, which no longer exists after the run. If you later continue it with plain `opencode -c`, outside phantom, OpenCode continues it on your own default model. If that model is on a `phantom` provider that points at Phantom's server, the rest of that session is paid from your Phantom key, not routed on your accounts. Use `phantom opencode -- -c` to keep it routed here.
133
149
 
134
- `phantom lineup` lists suggested models for each tier. A tier can run any chat model that calls tools, reads images and holds at least 128,000 tokens. Write `default` in place of a model to put that tier back to its default.
150
+ `phantom codex` and `phantom claude` don't route on this computer yet. They go through Phantom's server, on your Phantom key (from `phantom login`, or `PHANTOM_API_KEY`), and print what the run cost when the agent exits. With a ChatGPT sign-in, Codex's `openai/` models run on your ChatGPT plan. `phantom pi --server` and `phantom opencode --server` send pi and OpenCode that way too. With no key, Codex and Claude Code start the sign-in at a terminal, and in a script they stop and say to run `phantom login`. With no routing, pi and OpenCode don't start, and phantom says in one line to run `phantom config`.
135
151
 
136
- ### Set rules for `auto`
152
+ ## Sign in and add credit
137
153
 
138
- A route policy is your key's own rules for which model answers a request for `auto`. It can prefer cheaper models, and switch models for tool use, reasoning or a budget that is running low:
154
+ Routing on your own accounts needs no Phantom account. `phantom login` is for Phantom's server: `phantom codex`, `phantom claude` and `--server` runs, paid from credit.
139
155
 
140
- ```bash
141
- PHANTOM_KEY_NAME=coding phantom route set --models <model-a>,<model-b>
142
- PHANTOM_KEY_NAME=coding phantom route test --table
143
- ```
156
+ `phantom login` prints a link and a code, opens your browser, and waits. Check that the page shows the same code, sign in to your account, and approve. phantom saves the key on this computer (the macOS Keychain on a Mac) and never prints it. If the account has no credit, phantom says where to add it: https://phantom.codes/keys. `--no-browser` only prints the link, to open on another device. `phantom logout` removes the saved key from this computer; the key still works until you revoke it on `/keys`.
144
157
 
145
- A request that names its own model skips the policy, unless you set `--applies-to all`. `phantom route set --sort fast` sends each request to the provider that starts answering soonest, and `--sort default` goes back to cheapest first.
158
+ phantom's own flags go before the agent's arguments, with `--` between them: `phantom codex -- exec "review this repository"`.
146
159
 
147
- ## See what you spent
160
+ ## See what routing did
148
161
 
149
- ```bash
150
- phantom spend --table
151
- phantom spend --from 2026-09-01 --to 2026-09-30
152
- phantom spend --workspace --table
162
+ Each request also adds one line of JSON to `requests.jsonl` in the `local` folder beside `local.json`. The line holds no message text, key or header:
163
+
164
+ ```json
165
+ {"v":2,"at":"2026-11-28T10:00:00.000Z","id":"r_3f9a0c1b","run":"run_51c0e2aa","phantom":"1.6.3","router_version":"c6a372d12946","sensitivity":"balanced","conversation":"c_8d2e41f0","messages":12,"stream":true,"picked":"standard","reason":"router:dimensions","account":"zai","model":"glm-5.4","fallback":false,"status":200,"outcome":"ok","ms":{"decide":4,"first_byte":310,"total":2840},"tokens":{"in":18210,"out":412},"signals":{"messages":12,"user_turns":1,"tool_calls":5,"tool_errors_last_turn":1,"images":0,"tokens_est":19020},"router":{"tier":"standard","source":"dimensions","confidence":0.87,"threshold":0.5,"signals":{"severity":0.3,"spinning":0,"exploring":0,"productionIntensity":0.5}}}
153
166
  ```
154
167
 
155
- Dates are UTC days, and the default is this month. Without `--workspace` you see the key you run as; with it, every key in your workspace by name. The "saved" figure compares what phantom-1 cost with the same tokens at the frontier model's price today, so it is an estimate.
168
+ - `picked` is the tier phantom-1 chose, and the request went to it. Lines an older phantom wrote also hold `"sent"` or `"mode"`; `phantom report` reads them and ignores those fields.
169
+ - `v` is the line's shape (2). `run` is the same on every request one `phantom pi` made. `phantom` is the CLI's version, `router_version` the build of Phantom's router phantom-1 asked (`null` without one), and `sensitivity` its setting.
170
+ - `router` is what Phantom's router answered, or `null` when a rule decided first: its tier and `source` (`dimensions`, `fall_open`, `override` or `capable_hold`), on a `dimensions` turn its confidence in that tier and the threshold the sensitivity sets, the four `signals` it weighed, and on an override the hard reason (`override`). Its picks' `reason` is `router:<source>`. `signals` at the top level are counts read from the messages, never their text.
171
+ - Lines saved before Phantom's router replaced Switchyard say `switchyard_sha`, `switchyard` and `switchyard:<source>`. `phantom report` and `phantom replay` read them as `router_version`, `router` and `router:<source>`.
172
+ - `id` matches the answer's `x-phantom-local-request-id` header, so an error your agent shows can be found in the file.
173
+ - `conversation` groups one task's requests. It is a hash that means nothing outside the run that wrote it, and `null` when the request has no user message.
174
+ - `outcome` is `ok`, `upstream-error` (the account answered with an error status), `stream-failed` (the account's connection broke mid-answer), `client-closed` (your agent hung up first), `unreachable` (no account answered) or `not-signed-in` (a ChatGPT plan tier with no usable sign-in and no fallback). `status` is the account's status, or `null` when none answered.
175
+ - On a fallback's line, `because` says why the first account was skipped, such as `zai:429`, `chatgpt:not-signed-in` or `zai:unreachable:ENOTFOUND` (the network's reason, when it gave one). `code` is the ChatGPT plan's error code when its answer was an error, or the network's reason (`ENOTFOUND`, `ECONNRESET`, `ETIMEDOUT`) when no account answered.
176
+ - `ms.decide` is the time phantom-1 took to pick. `ms.first_byte` counts from the request's arrival to the account's first answer, and `ms.total` to the end of the answer. These three times are the whole trace: there is no OpenTelemetry and nothing is sent anywhere.
177
+ - Past 10 MB the file becomes `requests.1.jsonl`, replacing the older one, and a new file starts.
178
+ - `"log": false` in `local.json` turns the file off.
156
179
 
157
- ## Commands by task
180
+ `phantom report` reads both files and prints the requests by tier, by account and model (tokens, median and 95th percentile total time, median first byte), the reasons, how many tasks (one per message you typed) ran on Frontier from the start, moved up to it partway or came back down, fallbacks and failures. `--since 1h`, `1d`, `7d` or a UTC date such as `2026-11-28` narrows it, and `--json` prints it as JSON.
158
181
 
159
- | Task | Commands |
160
- | --- | --- |
161
- | Sign in and out | `phantom login`, `phantom logout` |
162
- | Set up your agents | `phantom setup`, `phantom setup --dry-run`, `phantom setup --restore` |
163
- | Start an agent for one run | `phantom claude`, `phantom codex`, `phantom pi` |
164
- | Add credit and check spending | `phantom fund`, `phantom balance`, `phantom spend`, `phantom workspace status` |
165
- | Manage the workspace's keys | `phantom keys create`, `phantom keys set`, `phantom keys rotate`, `phantom keys revoke` |
166
- | Manage the keys saved on this computer | `phantom keys saved`, `phantom keys save`, `phantom keys rm` |
167
- | Pick phantom-1, phantom-1:free or phantom-1:private | `phantom models`, `phantom models use <id>` |
168
- | Pick the model in each tier | `phantom lineup`, `phantom lineup set`, `phantom lineup reset` |
169
- | Set rules for `auto` | `phantom route` |
170
- | Use the providers that answer soonest | `phantom route set --sort fast` (`--sort default` for cheapest first) |
182
+ ### Traces: full prompts and answers, off by default
171
183
 
172
- ## Output and exit codes
184
+ phantom never stores message text unless you turn traces on:
173
185
 
174
- At a terminal, commands draw a screen, and choices are pickers (arrow keys, Enter, Esc to go back). When scripts and agents run them through a pipe, they print JSON on stdout and errors on stderr. `--json` prints JSON at a terminal, and `--table` draws the screen in a pipe. phantom refuses a flag it doesn't know.
186
+ ```bash
187
+ phantom config --traces on # start saving from the next phantom pi
188
+ phantom config --traces off # stop saving from the next phantom pi; what is saved stays
189
+ rm -r ~/.config/phantom/local/traces # delete what is saved
190
+ ```
175
191
 
176
- `phantom claude`, `phantom codex` and `phantom pi` run in your terminal and print a spending summary on stderr when the agent exits.
192
+ A `phantom pi` reads `local.json` when it starts, so one already running keeps its setting until it ends.
177
193
 
178
- Exit codes: 0 for success (and for Esc), 1 for an error, 2 for a rejected key (including `phantom balance` on a key that was revoked or has expired), 130 for Ctrl+C. `phantom setup` returns 1 when it could only install the skills because there was no key yet (`"complete": false`). An agent run returns the agent's exit code.
194
+ With traces on, phantom-1 saves the whole of every request and answer: the request body pi sent (system prompt, every message with its tool output and images, tools and the other fields) and the answer pi received (its text, the model's reasoning when the account sends it, every tool call with its arguments, the finish reason and usage). That is your code and your conversations, so they stay on this computer only: one file per day and `phantom pi` run in `local/traces/` beside `local.json` (mode 600, folder 700), up to 2 GB, past which the oldest files are deleted. pi sends the whole chat with every request, so a request that only adds to an earlier one in the same file is saved as its new messages and a pointer to that earlier request; `phantom replay` puts each request back together. A day of long pi chats takes a few megabytes instead of a few hundred. No header, key or token goes in. Nothing is sent anywhere. `phantom pi` says when traces are on, and `phantom report` and `phantom config show` say whether they are and where.
179
195
 
180
- <details>
181
- <summary><strong>Advanced and older commands</strong></summary>
196
+ `phantom replay` runs the saved traces through phantom-1's tier decision again, with no model call: once with the settings they were recorded with, as a check that the replay matches what happened, then with what you pass (`--sensitivity balanced|conservative|strict`, `--max-move-up-tokens <n>`, `--since`). It prints the requests that would change tier, the share on Frontier before and after, and how many of the router's picks scored within 0.1 of its move-up threshold; `--json` adds the router's score for every request (from -1, sure of Standard, to +1, sure of Frontier). A run that started before `--since` is replayed from its start, so its holds are as they were live, and only its requests from then on are counted.
182
197
 
183
- `phantom --help` shows the main commands. `phantom --help --all` lists every command and flag.
198
+ `phantom config --sensitivity balanced|conservative|strict` sets how soon Phantom's router moves a task up to Frontier and how long it keeps it there (`"sensitivity"` in `local.json`, `balanced` unless set), from the next `phantom pi`.
184
199
 
185
- The older `phantom key` commands still work. `phantom keys list` reads the workspace, and `phantom keys saved` lists the keys saved on this computer. `phantom keys show` prints a saved key for shell use, as in `PHANTOM_API_KEY=$(phantom keys show)`; keep it out of chat and logs.
200
+ `phantom config --trial on|off` turns on a test of the router's move up (`"trial"` in `local.json`, off unless set). When the router moves a task up to Frontier, a coin decides the whole task: it runs on Frontier as the router picks, or stays on Standard. Failure words, hard errors and images still move a Standard task up. The status line says when the trial kept a task on Standard, and the request log and traces save each task's side, so the finished tasks on each side can be graded and compared. Turn traces on with it.
186
201
 
187
- </details>
202
+ phantom-1 asks Phantom's router, the Rust add-on in `routers/phantom-1/router`, when no rule decides first. The npm package doesn't include it, so without it phantom-1 picks Frontier. Run phantom from a repo checkout (`pnpm install` builds the router), or set `PHANTOM_ROUTER_DIR` to a built `routers/phantom-1/router` folder.
203
+
204
+ ## Output and exit codes
205
+
206
+ At a terminal, the screens are pickers (arrow keys, Enter, Esc to go back). Through a pipe, scripts and agents get JSON on stdout and errors on stderr. `--json` prints JSON at a terminal, and `--table` draws the screen in a pipe. phantom refuses a flag it doesn't know; `phantom <command> --help` lists each command's flags.
207
+
208
+ Exit codes: 0 for success (and for Esc), 1 for an error, 130 for Ctrl+C. An agent run returns the agent's exit code.
188
209
 
189
210
  <details>
190
211
  <summary><strong>Development and contributing</strong></summary>
package/bin/phantom.mjs CHANGED
@@ -9,6 +9,41 @@
9
9
  * even if it would. So `npm run build` compiles the source to dist/ and this
10
10
  * file, which never needs compiling, is what npm points at.
11
11
  */
12
- import { main } from '../dist/phantom.mjs';
12
+ import { spawn } from 'node:child_process';
13
+ import { createRequire } from 'node:module';
14
+ import { constants } from 'node:os';
15
+ import path from 'node:path';
13
16
 
14
- await main();
17
+ function platformPackageFile() {
18
+ const platformPackage = `@phantomcodes/cli-${process.platform}-${process.arch}`;
19
+ try {
20
+ const manifest = createRequire(import.meta.url).resolve(`${platformPackage}/package.json`);
21
+ return path.join(path.dirname(manifest), 'bin', 'phantom');
22
+ } catch {
23
+ return null;
24
+ }
25
+ }
26
+
27
+ function leaveToTheChild() {}
28
+
29
+ function runPlatformPackageFile(file) {
30
+ const child = spawn(file, process.argv.slice(2), { stdio: 'inherit' });
31
+ process.on('SIGINT', leaveToTheChild);
32
+ process.on('SIGHUP', leaveToTheChild);
33
+ process.on('SIGTERM', () => child.kill('SIGTERM'));
34
+ child.on('error', (err) => {
35
+ console.error(`phantom: could not start ${file}: ${err.message}`);
36
+ process.exit(1);
37
+ });
38
+ child.on('exit', (code, signal) => {
39
+ process.exit(signal ? 128 + constants.signals[signal] : (code ?? 1));
40
+ });
41
+ }
42
+
43
+ const file = platformPackageFile();
44
+ if (file) {
45
+ runPlatformPackageFile(file);
46
+ } else {
47
+ const { main } = await import('../dist/phantom.mjs');
48
+ await main();
49
+ }
@@ -1,36 +1,17 @@
1
1
  import type { CommandInput } from '../core/helpers.mts';
2
2
  import type { Env } from '../core/state.mts';
3
- /**
4
- * `phantom claude`, `phantom codex` and `phantom pi` run a coding agent against Phantom
5
- * AI through a local proxy ({@link startProxy}). The agent gets the proxy's
6
- * address and the run's secret token as its key, in PHANTOM_AGENT_TOKEN; the
7
- * proxy checks the token, drops it, adds the real key, and phantom prints what
8
- * the run paid when the agent exits. The token never goes on a command line,
9
- * where other programs could read it, or to Phantom AI.
10
- */
11
- /** How phantom starts one agent against the proxy. */
12
- export type AgentSpec = {
13
- /** The program phantom runs. */
3
+ export declare function onPath(bin: string): boolean;
4
+ type AgentSpec = {
14
5
  bin: string;
15
- /** Phantom AI's path for this agent: `/claude-code`, `/codex` or `/coding-agent`. */
16
6
  prefix: string;
17
- /** The arguments and environment that point the agent at `proxy`, with `token` as its key. `tmp` is a folder phantom deletes after the run. */
18
- launch(proxy: string, args: string[], env: Env, tmp: string, token: string): {
19
- /** Everything the agent is started with: phantom's settings, then the user's arguments. */
7
+ launch(proxy: string, args: string[], env: Env, tmp: string, token: string, model?: string): {
20
8
  args: string[];
21
- /** The agent's environment. */
22
9
  env: Env;
10
+ warning?: string;
23
11
  };
24
12
  };
25
- /** The agents phantom can run, by command name. */
26
- export declare const AGENTS: Record<'claude' | 'codex' | 'pi', AgentSpec>;
27
- /**
28
- * `phantom claude`, `phantom codex` and `phantom pi`: runs the agent through the proxy,
29
- * passing every other argument through, and returns its exit code. The Phantom CLI's own
30
- * flags come first and stop at the first other argument or at `--`; they are
31
- * read here, not with `flags`, because everything after them belongs to the
32
- * agent. Run by `run()`.
33
- */
13
+ export type Agent = 'claude' | 'codex' | 'pi' | 'opencode';
14
+ export declare const AGENTS: Record<Agent, AgentSpec>;
15
+ export declare const ROUTES_HERE: readonly Agent[];
34
16
  export declare function agentCommand(input: CommandInput): Promise<number>;
35
- /** The message when an agent's program isn't installed, in plain words, with where to get it. */
36
- export declare function notInstalled(bin: string): string;
17
+ export {};