Introduction¶
When building agent trees in DeepSeek Harness (DSH), the root agent and its ephemeral, resumable subagents often consume tokens in parallel, easily exceeding per-conversation or global budget limits. The dsh-plugin-agent-budget plugin provides shared token budget management for these agent trees. It treats the entire agent tree as a single budget account, or accounts for each session independently, helping prevent uncontrolled token consumption.
Core Features¶
This plugin is maintained by winter-street and is an MIT-licensed open source project. Its core functionality focuses on token accounting and control for agent trees:
- Scope management: Supports treating the entire agent tree as one budget account (
scope: tree), or allowing each session to use its own budget independently (scope: session). - Separate storage: The ledger is stored in a plugin-owned sidecar directory
~/.dsh/agent-budget/and is not written into session logs. This means uninstalling the plugin will not corrupt session data. - Ledger behavior: Maintains an append-only, replayable, and recoverable log structure, ensuring data persistence and auditability.
- Model awareness: Provides a read-only
budget_statustool so the model itself can proactively perceive remaining quota. - No external dependencies: Runs as a pure local Bundle and does not rely on external services.
Installation and Configuration¶
The package is not currently published to npm, so it needs to be installed as a local package or Git dependency.
- Install the plugin:
dsh plugin --profile <name> add dsh-plugin-agent-budget
Or use pnpm:
pnpm add dsh-plugin-agent-budget
- Basic configuration:
The plugin depends on thedsh.profile.bundleslayer and is usually configured throughcordis.patch.yml.maxTokens(a positive integer) is required.
- insert:
- id: agent-budget
name: dsh-plugin-agent-budget
config:
maxTokens: 200000
missingUsage: exhaust
scope: tree
* `maxTokens`: Limits the maximum number of tokens in the scope.
* `missingUsage`: Defaults to `exhaust` (exhausted); if the provider does not return usage and you intentionally want to ignore it, set it to `ignore`.
* `scope`: Defaults to `tree`. If set to `session`, each session is accounted for independently.
Scope and Semantics¶
The plugin determines the granularity and rules for token accounting based on its configuration:
- Tree scope (
scope: tree):- Prefers resolving the root node through DSH runtime agent ownership.
- Falls back to the persisted
parentSessionchain if the runtime is unavailable. - If neither is available, it creates a standalone budget account with a warning.
- The plugin does not merge unrelated sessions into the same account.
- Session scope (
scope: session):- Each session has its own independent budget, including subagents.
- Accounting rules:
- Includes all
llm/streamcalls with asessionId(conversations, subagents, workflows, compression, and title generation). - Uncached input, cache reads, cache writes, and output are divided into four separate buckets.
- Reasoning tokens are already included in output and are not counted again.
- Includes all
Control and Degradation¶
The plugin provides an optional control layer that intervenes proactively when budget limits are approaching, helping prevent overspending:
degradeRatio(0–1):
When the remaining ratio in a scope falls below this threshold, subsequent calls are degraded.maxTokensis clamped to the remaining budget (minimum value is 1), and can be combined withmaxOutputTokensto set a hard output cap.pressurePrompt(defaults totrue):
When usage exceeds 50% and reaches 80%, a budget-pressure prompt is injected into the system prompt. This leverages the model’s own throttling ability to self-constrain before hitting hard limits.maxConcurrentCalls:
Limits the maximum number of concurrent provider calls allowed per scope. Calls exceeding the limit fail before dispatch with aTOKEN_BUDGET_CONCURRENT_LIMITerror.
API and Settings¶
The plugin includes a built-in local HTTP API and settings panel.
- Settings panel:
A “Token Budget” section is registered in the DSH settings page. It can list all open scopes, display progress bars, and allow adjusting limits or resetting usage without directly manipulating log files. - HTTP API (local):
The server runs at/agent-budget/apiand is limited to local access. Requests must include theX-Agent-Budget-Request: 1header.GET /scopes: Lists scopes and current usage.POST /adjust-limit: Adjusts scope limits.POST /reset: Clears usage for the specified scope.
Notes¶
- Experimental status: The plugin is currently experimental and has been validated against DSH
0.1.0-rc.6. - Dependencies and builds:
- Specific peer dependencies are required, such as
@deepseek-ai/cordisand@deepseek-ai/dsh-agent. - When installing Git dependencies with pnpm >= 10, build scripts are blocked by default. Add
allowBuilds: dsh-plugin-agent-budget: truetopnpm-workspace.yaml.
- Specific peer dependencies are required, such as
- Concurrent races:
- Calls allowed concurrent admits may eventually result in usage exceeding the limit.
- Once settled usage reaches the limit, subsequent calls fail before dispatch with
TOKEN_BUDGET_EXHAUSTED.
- Data storage and uninstallation:
- Data is stored in
~/.dsh/agent-budget/. - Uninstalling the plugin will not corrupt session logs, but to fully remove budget data, manually delete that directory.
- Concurrent writes are rejected by the lock; corrupt logs or indexes will cause startup failure.
- Data is stored in
Summary¶
dsh-plugin-agent-budget provides fine-grained, persistent token budget management for agent trees in DeepSeek Harness. By configuring scope, enabling degradation strategies, or using model-side prompt intervention, you can build cost-controlled agent workflows. For more details, refer to the plugin directory or GitHub repository.