Show HN: Comprehensive token usage analysis and cost tracking for opencode (github.com)

🤖 AI Summary
Show HN: tokenscope is a new OpenCode plugin that gives developers detailed, session-level token accounting and cost estimates for LLM usage. Installable via a one-line script or manual file drop into ~/.config/opencode, it runs inside the OpenCode TUI (invoke /tokenscope) and produces ASCII charts, a saved token-usage-output.txt report, and a per-session cost breakdown. The tool is designed to help teams understand where tokens are going (system prompts, user messages, assistant outputs, tool outputs, reasoning traces), identify top token consumers, and optimize expensive patterns—useful for anyone managing large-context agents, retrieval caches, or multi-tool pipelines. Technically, tokenscope uses API telemetry as ground truth, multi-tokenizer support (tiktoken for OpenAI and transformers/Hugging Face for others), and a pluggable pricing DB covering 41+ models (Claude, GPT variants, Llama, Mistral, DeepSeek, etc.). It infers hidden system prompt tokens via: System = (API Input + Cache Read) − (User + Tool tokens), properly accounts for cache read/write with discounted/read/write-specific rates, aggregates costs across all API calls for session-wide billing, and normalizes model names automatically. Processing is local (no external telemetry sent), tokenizers are cached after first download, and pricing can be customized in models.json. A sample run showed ~34K tokens in the current context, ~500K+ session total and an estimated cost of ~$0.62, illustrating how cached reads and many calls can produce billing far larger than the immediate context.
Loading comments...
loading comments...