← Catalog

@tduyng/pi-cost

Session spend, live rate-limit windows, footer segments, and per-message timestamps. Display only, never in the model context.

Extension

Install

Install with PiG

pig install npm:@tduyng/pi-cost

Install with Pi

pi install npm:@tduyng/pi-cost
Review before installing

Packages run code with your permissions and influence agent behavior. Catalog inclusion is not a security audit. Review the source before installing a third-party package.

Pi manifest JSON

The pi field of package.json.

{
  "extensions": [
    "./src/cost/index.ts",
    "./src/statusline/index.ts",
    "./src/timestamps/index.ts"
  ]
}

README

Written by the package's publisher, shown as published in version 0.0.6 (README.md). PiG does not review it.

@tduyng/pi-cost

Session spend, live rate-limit windows, footer segments and per-message timestamps, read from files pi already wrote. None of it enters a request.

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.

The panel

/cost opens a TUI panel with session spend in USD, token totals, a per-provider token table, and the live rate-limit windows.

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.

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.

ProviderWindows shown
Command Codesession 5h, weekly, remaining credits
OpenCode Gorolling, weekly, monthly
Claude5h, weekly all models, Opus, Sonnet, as percent bars
Cursorbilling cycle total, Cursor models, other models, as percent bars

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.

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.

Where the numbers come from

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.

Two caches keep it quick to open. <agentDir>/pi-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.

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>/pi-cost-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. A model the registry prices at zero, such as cursor or commandcode, reports no price for <provider>/<model> and keeps its token totals, because $0.00 there reads as a free session when the price is only unknown.

Three segments added to pi's native footer:

  • tps: average output tokens per second across the session.
  • agents: running subagent tasks.
  • git: changed files and the PR number from gh pr view.

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.

/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.

Install

pi install npm:@tduyng/pi-cost

Optional:

  • OPENCODE_API_KEY for the OpenCode Go windows.
  • A Claude OAuth credential for the Claude windows, from the macOS keychain or ~/.claude/.credentials.json.
  • A Cursor session for the Cursor windows: CURSOR_SESSION_TOKEN (the WorkosCursorSessionToken cookie or the access JWT), or the cursor-access-token keychain item that cursor-agent writes. It reads Cursor's undocumented dashboard endpoint, so it can change without notice.
  • gh on PATH for the PR segment.

Config

<agentDir>/pi-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:

{
  "providers": ["claude", "opencode-go"],
  "cost": ["claude"],
  "limits": ["claude", "opencode-go"]
}
  • 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.
  • cost lists the providers whose cost cell is filled. Omit it to fill every shown provider.
  • limits lists the rate-limit sections to fetch. Omit it to show every section with a credential.

Limits

  • Network probes time out at 10 seconds and the keychain at 5, so a slow provider costs a pause.
  • Quota rows older than 24 hours are marked stale rather than deleted.
  • pi-cost-cache.json is capped at 8 MB.