Skip to content

feat: add API usage widgets (session, weekly, reset, context bar)#168

Open
pcvelz wants to merge 1 commit intosirmalloc:mainfrom
pcvelz:feat/api-usage-widgets
Open

feat: add API usage widgets (session, weekly, reset, context bar)#168
pcvelz wants to merge 1 commit intosirmalloc:mainfrom
pcvelz:feat/api-usage-widgets

Conversation

@pcvelz
Copy link

@pcvelz pcvelz commented Feb 11, 2026

Summary

Adds four new widgets that display Claude Code API usage data, closing #94:

  • session-usage: 5-hour session utilization with progress bar
  • weekly-usage: 7-day utilization with progress bar
  • reset-timer: countdown to next session reset
  • context-bar: context window usage with progress bar (using context_window data from Claude Code stdin JSON)

Also enhances the existing ContextPercentage widget to use context_window data when transcript-based token metrics aren't available.

How it works

  • Fetches from GET /api/oauth/usage using existing OAuth credentials (macOS Keychain + Linux ~/.claude/.credentials.json fallback)
  • 180-second cache + 30-second rate limit lock to minimize API calls
  • Widgets register in the standard widget registry — users add them via the TUI like any other widget
  • No changes to default widget layout

Files changed

File Change
src/widgets/ApiUsage.tsx New — all 4 widget classes + credential reading + API fetch + caching
src/widgets/index.ts Export new widgets
src/utils/widgets.ts Register in widget registry
src/types/StatusJSON.ts Add context_window schema for Claude Code's context data
src/widgets/ContextPercentage.ts Enhanced to use context_window from stdin when available

Context

This was originally prototyped as a bash script and then implemented as native TypeScript widgets in a fork. This PR extracts just the widget code with no other changes.

Uses the same API approach as claude-pulse — same endpoint, same auth headers.

Test plan

  • bun run build succeeds
  • bun test passes
  • Widgets appear in TUI widget list and can be added to lines
  • Piped input renders session/weekly usage bars when credentials are available
  • Shows [No credentials] gracefully when OAuth token is unavailable
  • Cache file created at ~/.cache/ccstatusline-api.json after first API call
  • Verify on Linux with ~/.claude/.credentials.json
@sirmalloc
Copy link
Owner

@pcvelz Thanks, I'll review this soon, travelling at the moment. How does this handle Windows and WSL? If they are unsupported, I'd like to still merge it, but with a system to gate it properly and have it only appear on supported OS's.

@zhil
Copy link

zhil commented Feb 24, 2026

@sirmalloc can this be merged?

liafonx added a commit to liafonx/ccstatusline that referenced this pull request Feb 25, 2026
… reset, context bar)

Resolved conflicts: keep both fork widgets (session-name, free-memory) and PR widgets (session-usage, weekly-usage, reset-timer, context-bar).

Made-with: Cursor
@liafonx
Copy link

liafonx commented Feb 26, 2026

Hi, this is quite a handy feature for Subscription users. Since the PR is still pending merge, would you mind adding a small custom feature that allows users to choose whether to hide or unhide the progress bar? For example, I like to keep the status line compact and only show the used percentage.
PS: Also request to support rawValue mode

@pcvelz pcvelz force-pushed the feat/api-usage-widgets branch from e643ee6 to b7dae7c Compare February 26, 2026 11:25
@pcvelz
Copy link
Author

pcvelz commented Feb 26, 2026

@sirmalloc I've updated this PR with Windows support, based on @zac15987's working Windows snippet on issue #94.

I don't have access to a Windows machine myself, so I can't verify this with absolute certainty. That said, I'm fairly confident it works — I analyzed the differences between zac15987's working Windows script and our widget code, and the only deviation was process.env.HOME, which is undefined on Windows. Replaced it with os.homedir() which works cross-platform.

The rest of the code was already platform-agnostic:

  • Credential reading uses fs.readFileSync (works on all platforms)
  • API calls use Node's built-in https module (no curl/shell dependency)
  • All paths use path.join() (handles Windows backslashes)
  • macOS Keychain access is gated behind process.platform === 'darwin'

On Windows, credentials are read from ~/.claude/.credentials.json — same as Linux.

The fix is also published as v2.1.8 in the fork.

Add four new widgets that display Claude Code API usage data:
- session-usage: 5-hour session utilization with progress bar
- weekly-usage: 7-day utilization with progress bar
- reset-timer: countdown to session reset
- context-bar: context window usage with progress bar

Fetches data from /api/oauth/usage using existing OAuth credentials
(macOS Keychain + Linux file fallback). Includes 180s cache with
30s rate limiting to minimize API calls.

Also enhances ContextPercentage widget to use context_window data
from Claude Code's stdin JSON when available.

Closes sirmalloc#94
@pcvelz pcvelz force-pushed the feat/api-usage-widgets branch from b7dae7c to 07dd5d8 Compare February 26, 2026 11:38
@sirmalloc
Copy link
Owner

@pcvelz I've got a Windows machine I can test on. I'll dig into reviewing this today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants