dsh-session-balance: Session Balance and Cost Monitoring in the DSH Web GUI

Introduction

The core design philosophy of DeepSeek Harness (DSH) is “Everything is a Plugin.” When performing local development or debugging via the Web GUI, having real-time visibility into the DeepSeek account balance, current session Token consumption, and context usage is fundamental for cost control and debugging model behavior.

The dsh-session-balance plugin is designed specifically to solve this problem. It does not alter session state or initiate model requests; instead, it mounts a balance badge on the right side of the session title bar in the DSH Web GUI. Clicking to expand it reveals account balance details, session Token usage statistics, Context Pressure, and cost estimates based on the current model’s unit price.

Core Features

The plugin displays data through a collaboration between the host and the browser:

  1. Account Balance: Calls the DeepSeek GET /user/balance API to retrieve the account total, recharge balance, granted balance, and availability status.
  2. Session Usage: Reads tokenUsage (broken down into uncached input, cache read, cache write, output) and sessionStats (turns, steps, model duration) from the session projection.
  3. Context Pressure: Reads contextPressure data to display the ratio of used Tokens to the window limit.
  4. Cost Estimation: Converts Token counts to CNY (Yuan) locally in the browser based on the unit price of the currently selected model (modelSelection projection).
  5. Security Design: Read-only throughout the process. The API Key is used only within the host process and is never written to the frontend response; routes only accept same-origin GET/HEAD requests to prevent cross-origin calls.

Installation and Enablement

The plugin consists of a “Host Plugin” and a “Browser Plugin” component, which need to be installed into the currently used web profile.

1. Add Plugin Package

Add the plugin directory to the profile via a symbolic link:

dsh plugin --profile web add link:/path/to/dsh-session-balance

Note: link: indicates a symbolic link; changes to the code will take effect after restarting dsh web. If a fixed copy is required, use file:/path/to/dsh-session-balance.

2. Mount Plugin Row

Edit the profile configuration file ~/.dsh/profiles/web/cordis.patch.yml and add the plugin row. The file defaults to an empty list; simply append the following:

- insert:
    - id: session-balance
      name: 'dsh-session-balance'

3. Restart Service

Restart the DSH Web service to load the plugin:

dsh web

After refreshing the Web GUI (usually http://127.0.0.1:3080), a balance badge should appear on the right side of the session title bar.

Configuration Reference

All configuration items are optional. Default values can be overridden via the config field.

Basic Configuration

Field Default Value Description
balanceEnabled true Whether to request the balance API. When disabled, only local usage and costs are displayed.
apiKeyEnv DEEPSEEK_API_KEY Credential reference name used to read the Key from environment variables or ~/.dsh/.credentials.yaml.
baseURL $DEEPSEEK_BASE_URL → https://api.deepseek.com DeepSeek API base URL. Configure here if using a self-hosted gateway.
timeoutMs 10000 Timeout for balance requests (milliseconds).
cacheMs 60000 Duration of host-side balance cache (milliseconds).
refreshMs 60000 Frontend polling refresh interval (milliseconds). Set to 0 to load only once.
rate off-peak off-peak (idle) or peak. The price coefficient is 2 during peak hours.
currency CNY Currency for cost estimation; affects display only.

Display Toggles

Field Default Value Description
showBalance true Whether the panel displays the account balance.
showTokens true Whether to display Token usage.
showCost true Whether to display cost estimation.
showContext true Whether to display context usage.

Model Pricing Override

The plugin has built-in default values based on DeepSeek’s official pricing. However, as unit prices may adjust over time, it is recommended to override them based on actual model configurations.

Default Unit Prices (CNY / 1M tokens, Off-Peak):

Model Cache Hit Input Cache Miss Input Output
deepseek-flash 0.02 1 4
deepseek-v4-flash 0.02 1 4
deepseek-v4-flash-vision-exp 0.02 1 4
deepseek-v4-pro 0.15 4.5 13.5

Configuration Example:

config:
  rate: peak
  currency: CNY
  pricing:
    deepseek-v4-pro:
      cacheHitInput: 0.3
      cacheMissInput: 9
      cacheWriteInput: 9
      output: 27

Note: Values in the pricing table are entered based on off-peak rates; when rate: peak, the program will automatically multiply them by 2.

How It Works

The plugin utilizes DSH’s projection mechanism to obtain data without modifying host state.

  1. Data Source: Session usage and context pressure are derived from existing host projections (e.g., tokenUsage and contextPressure provided by dsh-token-meter).
  2. Frontend Calculation: Cost estimation is performed entirely on the browser side, using the cumulative Token count from projections and the unit price table delivered by the host.
  3. Balance Retrieval: The frontend requests the host via the route fetch /dsh-session-balance/api/overview. The host reads credentials and calls the DeepSeek API. Balance requests feature a TTL cache and concurrency coalescing to prevent frequent requests from overwhelming the upstream service.
  4. Secure Routing: Backend routes only accept same-origin GET/HEAD requests and verify the consistency of Origin and Host; otherwise, a 403 is returned.

Compatibility & Requirements

  • DSH Version: Requires DSH 0.1.5-rc.1 or higher, with dsh-token-meter, dsh-session-stats, and dsh-api-session-controller enabled in the Web composition.
  • Node.js: Host environment requires Node.js >=20.
  • Browser: A modern browser supporting Intl.NumberFormat and optional chaining is required.

Troubleshooting

If the feature does not display correctly, refer to the following symptoms and causes:

Symptom Cause / Resolution
No badge in title bar Confirm that the session-balance plugin row has been added to cordis.patch.yml and restarted; confirm the plugin package is installed in node_modules.
Badge shows “API Key Not Configured” Check the configuration in the environment variable DEEPSEEK_API_KEY or ~/.dsh/.credentials.yaml.
Shows “Fetch Failed” Check if baseURL is correct (e.g., if using a self-hosted gateway), verify if the API Key is valid, and check network request details.
Cost shows “Unit Price Not Configured” Complete the pricing configuration for the model ID currently in use under the pricing setting.
Token is always 0 Confirm that requests with usage data have been sent in the session; check if dsh-token-meter is enabled in the web composition.
Balance differs from official website The official website may include unsettled fees; the plugin refreshes based on cache. Click the refresh button on the panel to force an update.

License

MIT

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye