Introduction¶
DSH uses plugins to extend workflows. In multi-round Agent calls, the model API usually returns token usage, but developers also want to know if prefix cache hits, how much was spent per round, and if cumulative costs are approaching the budget. dsh-cache-cost-monitor is a Cordis plugin for DSH v0.1.x used to organize this information into observable metrics from logs and response fields.
What is this¶
eurt-nano/dsh-cache-cost-monitor is an MIT-licensed DSH plugin. It listens to every round of the Agent’s model API calls, extracts prompt_cache_hit_tokens, prompt_cache_miss_tokens, and completion_tokens, calculates cache hit rate in real-time, and estimates API costs based on official pricing (including peak and off-peak hours).
The plugin registers the cache_report tool, allowing the Agent to proactively check cache hit rates and costs. It also displays a persistent statistics bar and round consumption in RMB at the end of each assistant reply in the browser.
Core Features¶
Cache and Cost Statistics¶
The plugin extracts the following fields:
prompt_cache_hit_tokens
prompt_cache_miss_tokens
completion_tokens
Based on these fields, it calculates cache hit rate in real-time and estimates API costs based on official pricing (including peak and off-peak hours).
Providing the cache_report Tool¶
cache_report is the tool registered by the plugin for the Agent. It can output cache hit rate, costs, and cache health information, and provide a sparkline trend.
Displaying Persistent Statistics Bar¶
In the browser, the plugin can display a persistent statistics bar below the input box, showing tokens, costs, and hit rate in real-time.
Displaying Round Consumption at Message End¶
The plugin displays round tokens and RMB consumption at the end of each assistant reply.
Threshold and Budget Alerts¶
The plugin supports threshold and cumulativeThreshold threshold alerts, as well as budgetUsd cost budget and overspend alerts.
Graceful Degradation¶
In cases of exceptions such as missing fields, no pricing, or service unavailability, the plugin only logs errors and does not crash the main program.
Installation and Activation¶
Prerequisites:
Node.js >= 18
pnpm
DSH v0.1.x
Fixed version installation command:
dsh plugin --profile web add https://github.com/eurt-nano/dsh-cache-cost-monitor.git#v0.3.0 --config.node-linker=isolated
After installation, DSH needs to be restarted:
dsh web
The footer and statistics bar require a browser page refresh.
When installing or removing on Windows, you need to preserve:
--config.node-linker=isolated
If pnpm intercepts build scripts during git installation, you need to add dsh-cache-cost-monitor to the allowBuilds list in the profile’s pnpm-workspace.yaml.
Typical Usage¶
You can directly say to the Agent:
调用 cache_report
Or ask it to:
看看缓存命中率和花了多少钱
If round-by-round details are needed, you can pass optional parameters:
{ "detail": true, "limit": 10 }
This parameter is used to output round-by-round details, including pricing tiers, actual unit prices, and timestamps.
You can also look directly at the interface provided by the plugin:
- Look at the persistent statistics bar: cumulative consumption below the input box is displayed in real-time without any action.
- Look at the footer: round tokens and RMB consumption are displayed at the end of each assistant reply.
- Look at the logs:
[cache-monitor]prefixed warn/info outputs alert and startup information.
Use Cases and Notes¶
This plugin is suitable for developers using DSH v0.1.x who wish to observe cache hit rates and API cost estimates. The plugin runs with the permissions of the current dsh process; it is recommended to check the source code and license before installing.
Costs are estimated as “configured pricing × actual token count”; discounts, retries, and gateway markup are not included; models without configured pricing are counted as 0.
In-process cumulative statistics for cache_report reset to zero when DSH restarts; footer data comes from session projection, and historical rounds are still displayed after restart. The footer only displays rounds that occurred after the plugin was installed and where the API response carries usage.
Reference¶
Repository URL:
https://github.com/eurt-nano/dsh-cache-cost-monitor