Skip to content

strykereye2/cloudcli-token-cost-pro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Token Cost Pro — CloudCLI Plugin

A CloudCLI tab plugin for tracking and calculating Claude Code API token costs in real time. Monitors active sessions, aggregates project-level and all-time spending, and fetches live model pricing from the LiteLLM pricing registry.

Based on cloudcli-plugin-token-cost-calculator by NightmareAway / kologram — MIT License. Significant enhancements added; see What's New.


Installation

  1. Clone this repo into your CloudCLI plugins directory:

    git clone https://github.com/strykereye2/cloudcli-token-cost-pro.git \
      ~/.claude-code-ui/plugins/token-cost-pro
  2. Install dev dependencies and build:

    cd ~/.claude-code-ui/plugins/token-cost-pro
    npm install
    npm run build
  3. Restart CloudCLI — the Token Cost Pro tab appears automatically.


How It Works

Data Sources

Session transcripts (~/.claude/projects/)

Claude Code writes every session as a JSONL file:

~/.claude/projects/<encoded-project-path>/<session-uuid>.jsonl

The path encoding mirrors Claude Code's own convention — each / in the project path is replaced with -. For example, /workspace/my-project becomes -workspace-my-project.

Each line in the JSONL is a JSON object. The plugin reads message.usage fields from assistant turns:

  • input_tokens — tokens sent to the model
  • output_tokens — tokens generated by the model
  • cache_creation_input_tokens — tokens written to the prompt cache
  • cache_read_input_tokens — tokens read back from the prompt cache
  • message.model — the model ID used for that turn

Sub-agent sessions (spawned by Claude Code's Task tool) are found under <sessionDir>/subagents/*.jsonl and are included in all totals.

LiteLLM pricing registry

Live pricing is fetched from:

https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json

This JSON file contains per-token pricing for 2,700+ models. The plugin filters to mode: "chat" entries only, refreshes every 6 hours, and falls back to hardcoded presets on timeout or network failure.

Model lookup uses exact match first, then strips trailing date suffixes (e.g. claude-haiku-4-5-20251001claude-haiku-4-5) for a second attempt.


UI Modes

The plugin provides five modes, selectable via the button bar at the top.

Manual

Enter token counts by hand for quick estimates or hypothetical scenarios. Quick-add buttons let you add +1K, +10K, +100K, +1M, or +10M tokens at a time. All inputs persist to localStorage between page loads.

Auto-detect (Active Session)

Monitors the currently active Claude Code session in real time. Reads the session JSONL directly, refreshes every 30 seconds, and shows a countdown timer. Automatically detects the model from the session transcript and populates prices from LiteLLM.

If no session context is available from the CloudCLI sidebar, a fallback input appears for manual project path and session ID entry.

Project

Aggregates all sessions under a single project folder. Enter the project path (e.g. /workspace/my-project) and the plugin scans ~/.claude/projects/<encoded-path>/ for all .jsonl files. Sessions are sorted by total token count descending and listed with per-session cost breakdowns.

All-time

Scans every subdirectory under ~/.claude/projects/ and aggregates totals across all projects since Claude Code was installed. Each project row shows token counts, session count, and a proportional usage bar. Projects are sorted by total token usage.

Daily

Slices session data by calendar date. Use the / navigation buttons or the Today shortcut to browse by day. Each day row expands to show per-session details. Useful for tracking daily spend against a budget.


Model Pricing Panel

Shared across all modes. Shows three price fields (per 1M tokens):

Field What it covers
Input Tokens sent to the model
Output Tokens generated by the model
Cache input Prompt cache read + write (averaged)

Auto-price: In Auto-detect, Project, and Daily modes, the plugin detects the model from session data and fills prices automatically from LiteLLM. A status indicator shows LiteLLM live (green), Preset fallback (amber), or LiteLLM unavailable (amber) depending on the pricing source.

Override: Click the override button to lock the price fields and edit them manually. Click auto to revert to auto-detected prices.

Preset dropdown: Eight built-in presets for quick selection:

  • Claude Opus 4.7, Sonnet 4.6, Haiku 4.5
  • GPT-4o, GPT-4o-mini
  • Gemini 2.5 Pro, Gemini 2.5 Flash
  • DeepSeek V4 Pro

API Endpoints (Server)

The plugin runs a local Node.js HTTP server. All endpoints are called internally via the CloudCLI plugin RPC bridge.

Method Path Description
GET /presets Returns the 8 hardcoded model presets
GET /session-usage Token counts for a single session (?projectPath=&sessionId=)
POST /calculate Server-side cost calculation verification
GET /project-usage All sessions in one project (?projectPath=)
GET /all-usage All sessions across all projects
GET /daily-usage All sessions grouped by calendar date
GET /model-price Live LiteLLM price lookup (?model=)
GET /pricing-status LiteLLM fetch status (ready/loading/error)
POST /refresh-prices Force re-fetch LiteLLM pricing data

What's New

Changes made in this fork relative to the original plugin:

LiteLLM Live Pricing Integration

The original plugin used only hardcoded preset prices. This fork adds a full live pricing system:

  • Fetches the LiteLLM model pricing JSON on startup and every 6 hours
  • 8-second fetch timeout with graceful fallback to presets
  • /model-price, /pricing-status, and /refresh-prices API endpoints added to the server
  • Status indicator in the UI shows source (live / preset / unavailable)

Automatic Model Price Detection

In Auto-detect, Project, and Daily modes, the plugin now reads the model name from the session JSONL and automatically populates the pricing fields — no manual preset selection needed.

Price Override / Auto Toggle

Added override and auto buttons so users can lock prices manually and revert to auto-detected values without losing their customization.

Linux Path Encoding Fix

The original encodeProjectPath function only handled Windows-style backslash separators. On Linux/macOS, project paths like /workspace/my-project were not encoded correctly, causing "Project directory not found" errors. Fixed by adding forward-slash replacement.

System Font Stack

Removed the Google Fonts CDN dependency (JetBrains Mono external load). Replaced with a system monospace font stack (SFMono-Regular, Cascadia Code, Consolas, Liberation Mono) — no external requests, faster load, works fully offline.

TypeScript Type Additions

Added PricingStatus and LivePriceResult interfaces for the new pricing system.


Credits

Original plugin by NightmareAway (GitHub: kologram). Repository: cloudcli-plugin-token-cost-calculator License: MIT

This fork extends the original with live pricing, automatic model detection, and Linux compatibility fixes.


License

MIT — see original license terms. Modifications are contributed under the same MIT License.

About

Enhanced CloudCLI plugin for real-time Claude Code token cost tracking with LiteLLM live pricing

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors