@tduyng/pi-cost 0.0.2 → 0.0.4

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 (2) hide show
  1. package/README.md +41 -32
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,45 +1,46 @@
1
1
  # @tduyng/pi-cost
2
2
 
3
- Cost tracking, rate-limit windows, statusline segments, and per-message timestamps for pi sessions.
3
+ Session spend, live rate-limit windows, footer segments and per-message timestamps, read from files pi already wrote. None of it enters a request.
4
4
 
5
- ## What it does
5
+ Pi's footer prints a running total and not the split: no per-provider view over the month, no record of what a delegated child cost, no sight of the rate-limit window about to end the day.
6
6
 
7
- ### /cost panel
7
+ ## The panel
8
8
 
9
- Type `/cost` and get a TUI panel with session USD spend, token totals, provider rate-limit bars, and a per-provider token table.
9
+ `/cost` opens a TUI panel with session spend in USD, token totals, a per-provider token table, and the live rate-limit windows.
10
10
 
11
- The panel fits the window: its body takes at most 70% of the terminal rows, each line is truncated to the column width instead of wrapping, and the header line shows the range when the rest is scrollable. Up and down move one row, page up and page down move one screen, and Enter or Esc closes it.
11
+ The panel fits the window it opens in: body at most 70% of the rows, each line truncated to the column width, the header naming the range when the rest scrolls. Up and Down move a row, Page Up and Page Down a screen, Enter or Esc closes.
12
12
 
13
- An `Extensions` row appears when an extension spent a model call on this session's behalf: a `subagent` child, or the `harness-loop` writer. Those calls bill to the same plan but cannot land in the parent's transcript, so each one appends a line to `<agentDir>/overhead.jsonl` and the panel sums the lines for this session. The row names the cost, the tokens, the call count and each extension's share. The `Cost` row stays what the conversation itself cost, so the two are read side by side and never added twice.
13
+ The token table groups by provider across four trailing windows: 5h, 7d, 30d, calendar month. Input, output, cache read and cache write are separate, then the total and the cost. A zero column collapses to a dot, and a provider with no tokens is omitted.
14
14
 
15
- The rate-limit section covers three providers.
16
- Command Code shows the session 5h window, the weekly window, and remaining credits (monthly plus purchased plus free). Its section paints only when pi holds a Command Code credential in `auth.json`. Without one the probe returns nothing, the section drops, and its cached rows are deleted.
17
- OpenCode Go shows the rolling window, weekly, and monthly.
18
- Claude shows the 5h window and the weekly windows (all models, Opus, Sonnet) as percent bars. It reads the Claude Code login on this machine, and its section paints only when that login exists.
15
+ | Provider | Windows shown |
16
+ | --- | --- |
17
+ | Command Code | session 5h, weekly, remaining credits |
18
+ | OpenCode Go | rolling, weekly, monthly |
19
+ | Claude | 5h, weekly all models, Opus, Sonnet, as percent bars |
19
20
 
20
- The token table groups rows by provider (alphabetically) across four trailing windows: 5h, 7d, 30d, and calendar month. Input, output, cache read, and cache write are shown separately, then the total and the cost. A zero column collapses to a dot. Providers with no tokens in any window are omitted. The cost is the `usage.cost.total` each turn recorded in its session file, so it is the API-equivalent price for subscription providers such as `claude`, not a charge.
21
+ A section paints only when the credential it needs is on the machine. Without one the probe returns nothing and the section drops. Providers with no usage endpoint (opencode, mistral, gmi) appear in the token table only. Command Code stays out of the panel unless pi holds a credential for it in `auth.json`.
21
22
 
22
- ### Footer segments
23
+ Amounts under $0.01 print with four decimals, under $1 with three, and $1 or more with two. A model with no known pricing contributes zero, so the panel understates spend instead of guessing.
23
24
 
24
- Three extra segments appear in pi's native footer.
25
- `tps` shows average output tokens per second across the session.
26
- `agents` shows the count of running subagent tasks.
27
- `git` shows changed files and the PR number from `gh pr view`.
25
+ ## Where the numbers come from
28
26
 
29
- Session spend and the cache hit rate are left to pi's own footer, which prints both. They were `cost` and `cache` segments here until this version dropped them.
27
+ Token totals come from scanning the session `.jsonl` files. Cost is the `usage.cost.total` each turn recorded, not a recomputation from a price table. For a subscription provider such as `claude` that is the API-equivalent price rather than a charge, and the panel says so.
30
28
 
31
- Use `/statusline` to toggle these segments on or off. Use `--statusline-no-pr` to skip the `gh pr view` lookup. The git segment refreshes when a run settles and on a debounced filesystem watcher on `.git/HEAD` (250ms delay), so a branch switch paints at once and an idle session spawns nothing. Worktrees that store `.git` as a file are covered by the settle refresh.
29
+ Two caches keep it quick to open. `<agentDir>/cost-cache.json` stores parsed records keyed by file mtime and size, so a re-open skips unchanged files. Quota data is cached 24 hours, and files outside the scan window are skipped without a stat call.
32
30
 
33
- ### /timestamps
31
+ Work that bills to the same plan but cannot land in the transcript is recorded separately. A `subagent` child or the `harness-loop` writer appends a line to `<agentDir>/overhead.jsonl`, and the `Extensions` row sums them for this session with the call count and each extension's share. The `Cost` row stays what the conversation itself cost, so the two are read side by side and never added twice.
34
32
 
35
- Type `/timestamps` to toggle per-message clock markers in the transcript. Each marker shows the time of day, a relative offset (for example "3m ago"), and whether the message was a user message, AI response, or tool call. Markers are custom session entries, so they never enter model context and the cached prompt prefix stays stable. Two same-role entries within a 2-second window are detected as duplicates (from a double-loaded extension) and suppressed.
33
+ ## Footer and timestamps
36
34
 
37
- ## How it works
35
+ Three segments added to pi's native footer:
38
36
 
39
- - Token totals come from scanning session `.jsonl` files. A cache at `<agentDir>/cost-cache.json` stores parsed records keyed by file mtime and size, so repeated `/cost` opens skip unchanged files. Quota data is cached for 24 hours. Files older than the scan window are skipped without a stat call. The cost panel formats amounts under $0.01 with four decimal places, amounts under $1 with three, and amounts at $1 or above with two. Providers `gmi`, `mistral`, `google`, `claude-bridge`, and `commandcode` are excluded from the token table unless `cost.json` names them. A model with no known pricing contributes zero to the total, so the panel understates spend rather than guessing it.
40
- - Provider quota data is fetched from Command Code's billing/credits endpoint, OpenCode Go's usage endpoint, and Anthropic's OAuth usage endpoint.
41
- - Nothing enters the model's context. No tool is registered, no prompt text is injected, no session entry is created.
42
- - The extension adds no segment for session spend or the cache hit rate. It does not track spend across sessions (only the active branch).
37
+ - `tps`: average output tokens per second across the session.
38
+ - `agents`: running subagent tasks.
39
+ - `git`: changed files and the PR number from `gh pr view`.
40
+
41
+ Session spend and cache hit rate stay with pi's own footer, which already prints both. `/statusline` toggles the segments and `--statusline-no-pr` skips the `gh pr view` lookup. The git segment refreshes on settle and on a debounced watch of `.git/HEAD`, so a branch switch paints at once and an idle session spawns nothing.
42
+
43
+ `/timestamps` toggles per-message clock markers: time of day, a relative offset such as `3m ago`, and whether the entry is a user message, an assistant reply or a tool call. Markers are custom session entries, so they never enter model context, and two same-role entries inside a 2-second window are read as a double-loaded extension and suppressed.
43
44
 
44
45
  ## Install
45
46
 
@@ -49,13 +50,15 @@ pi install npm:@tduyng/pi-cost
49
50
 
50
51
  Local checkout: `pi install /path/to/dotfiles/pi/agent/extensions/pi-cost`
51
52
 
52
- ## Configure
53
+ Optional:
53
54
 
54
- Environment variables:
55
+ - `OPENCODE_API_KEY` for the OpenCode Go windows.
56
+ - A Claude OAuth credential for the Claude windows, from the macOS keychain or `~/.claude/.credentials.json`.
57
+ - `gh` on PATH for the PR segment.
55
58
 
56
- - `OPENCODE_API_KEY`: OpenCode Go API key.
59
+ ## Config
57
60
 
58
- `<agentDir>/cost.json` controls the token table and the rate-limit sections. It is read each time `/cost` opens, and an invalid file is ignored with a warning. All keys are optional:
61
+ `<agentDir>/cost.json` controls the table and the sections. It is read each time `/cost` opens, and an invalid file is ignored with a warning. Every key is optional:
59
62
 
60
63
  ```json
61
64
  {
@@ -65,6 +68,12 @@ Environment variables:
65
68
  }
66
69
  ```
67
70
 
68
- - `providers`: the only providers shown in the token table. Omit it to show every provider except the excluded ones above.
69
- - `cost`: the providers whose cost cell is filled. Omit it to fill the cost for every shown provider. A provider not listed shows a blank cost cell.
70
- - `limits`: the rate-limit sections to show, by provider id (`commandcode`, `opencode-go`, `claude`). Omit it to show every section that has a credential. A section not listed is not fetched.
71
+ - `providers` lists the providers in the token table. Omit it to show every provider except `gmi`, `mistral`, `google`, `claude-bridge` and `commandcode`, which are hidden unless named.
72
+ - `cost` lists the providers whose cost cell is filled. Omit it to fill every shown provider.
73
+ - `limits` lists the rate-limit sections to fetch. Omit it to show every section with a credential.
74
+
75
+ ## Limits
76
+
77
+ - Network probes time out at 10 seconds and the keychain at 5, so a slow provider costs a pause.
78
+ - Quota rows older than 24 hours are marked stale rather than deleted.
79
+ - `cost-cache.json` is capped at 8 MB.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tduyng/pi-cost",
3
- "version": "0.0.2",
4
- "description": "Session spend, live provider rate-limit windows, footer segments, and per-message timestamps.",
3
+ "version": "0.0.4",
4
+ "description": "Session spend, live rate-limit windows, footer segments, and per-message timestamps. Display only, never in the model context.",
5
5
  "keywords": [
6
6
  "pi",
7
7
  "pi-package",