The Problem: Outdated Rates and Hidden Context Costs
While working with local setups and various AI APIs in OpenCode, I noticed a huge gap in cost tracking. OpenCode's side panel shows a rough estimate, but it only reflects the basic context size. For instance, it might show a 50k context, but due to how certain models handle caching, 800k tokens might actually be sent to the server.
To get accurate, real-time tracking, I integrated the tokscale utility into my workflow. However, I immediately hit two major roadblocks:
-
Outdated Rates: By default,
tokscalepulled rates that were half a year old (ancient history in the AI world), lacking support for newer models like DeepSeek V4. I fixed this by forcing it to read a custom, local custom-pricing.json file. - The New Challenge: DeepSeek just announced an upcoming time-of-use dual pricing schedule (peak vs. off-peak hours).
DeepSeek API service is expected to adopt a peak-valley pricing strategy starting in mid-July, with peak-hour prices being twice the regular price, applicable to all billing items.
Peak hours (in UTC): 1:00–4:00 AM and 6:00–10:00 AM.
(UTC+8 equivalent: 9:00 AM–12:00 noon and 2:00–6:00 PM.)
Since tokscale only reads its config file at startup, it has no native way to handle scheduled dynamic rates.
The Solution: Automation via Systemd and Bash
To automate cost tracking under this new dual-tariff system, I built a lightweight automation toolkit that dynamically alters configuration files based on the server's time schedule.
1. Dynamic Tariff Switching (tokscale-peak-pricing)
Instead of running a heavy background daemon, the switching logic is triggered cleanly via systemd user timers at the exact peak transition hours (UTC 01:00 / 04:00 / 06:00 / 10:00).
The script works as follows:
- It reads a static base file:
~/.config/tokscale/custom-pricing.base.json. - During peak hours (UTC 01:00–04:00, 06:00–10:00), it uses
jqto parse the JSON, automatically doubles the price for any model key starting withdeepseek-*, and writes the result to the activecustom-pricing.json. - During off-peak hours, it restores the base prices.
- To avoid redundant disk writes, it caches the current state in
/tmp/tokscale-pricing-zone. -
The Catch: Since
tokscaledoesn't hot-reload configs, the script kills runningtokscaleprocesses immediately after a switch to force a fresh configuration read.
2. Keeping the TUI Alive (tokscale-loop)
To prevent the dashboard from breaking when the pricing script kills the process, I wrapped the TUI launch in a resilient loop (tokscale-loop). If tokscale is terminated during a zone transition, the wrapper waits 0.5 seconds and automatically spins it back up, making the pricing swap completely seamless to the eye.
3. The Tmux Dashboard (opencode_tokscale.sh)
To bring everything together, a single script initializes a unified development environment using tmux. It splits your terminal screen into a highly efficient 3-pane layout:
┌──────────────────────┬──────────────┐
│ │ tokscale │
│ opencode │ TUI pane │
│ ├──────────────┤
│ │ bash shell │
└──────────────────────┴──────────────┘
-
Left pane (wide): Runs
opencode. -
Top right pane: Runs the resilient
tokscale-looptracking costs for theopencodecontext in real time. -
Bottom right pane: A free
bashshell for quick commands.
Tip: This specific layout is crucial because it allows OpenCode to instantly peek into the terminal logs in real time - something that only works smoothly when both are multiplexed side-by-side inside tmux.
As a result, checking by command shows dynamically changed prices
tokscale pricing list-overrides
Installation & Maintenance
To make this setup easily portable across machines, I wrote an interactive installation script (install-tokscale-peak-pricing.sh). It checks for dependencies (jq, tmux, tokscale, opencode), sets up the systemd timers, and even auto-detects your shell (bash, zsh, or fish) to offer a convenient optk alias for a one-command launch.
If DeepSeek ever cancels their peak pricing (or fixes it at a high level), there is a built-in kill switch:
Completely disable the timer and permanently restore base rates
bash disable-tokscale-peak-pricing.sh
Source Code & Architecture:
The full implementation, systemd unit templates, and installation scripts are fully open-source.
Check out the repository here: github.com/pathexplorer/tokscale

Top comments (0)