DSH long-session cost and compaction
DeepSeek Harness Long Sessions: Built-in vs Proxy Compaction
- Author
- DeepSeekAgent.io Editorial Team
- Published
- Updated
Long-session cost is not determined only by the final answer. Every Agent turn can resend system prompts, history, tool definitions, and tool results. When automatic compaction does not complete in time, per-request context keeps growing and cumulative billing can greatly exceed the token count visible for one turn.
Recent community reports on 0.1.7-rc.2 and 0.2.0-rc.2 describe compaction not triggering, higher late-session cost, and interactions among Goal rounds and truncated tool results. These are reproducible cases in specific environments, not a claim that every Session is affected, but they expose a failure chain worth monitoring.
The DSH compaction path
Compaction is an optional capability outside the Agent Loop core. The automatic path evaluates two triggers before request construction:
pressure: estimated context approaches the configured threshold;context-overflow: the provider confirms that the request exceeds its window.
The engine selects a Surface span with balanced tool calls and results, generates a summary, and replaces the old range with a new user message. compaction/start, summary, and end events record the transaction. A failed attempt remains observable instead of being represented as completed.
Why a full window can prevent recovery
Summarization itself requires a model request. If history already fills the context and the configured maximum output reserves a large region, the provider can reject the summary before compaction can save the Session.
With an 80K context and 32K maximum output, the safely usable history is far below 80K. Waiting for a meter to reach 100% is too late; the system needs room for the summary request, tool declarations, and the next output.
How 0.2.1-alpha.1 reserves headroom earlier
v0.2.1-alpha.1 makes the active LLM Adapter route authoritative for model capacity and lets proactive compaction combine context window W, current request-output reservation O, and additional headroom:
floor(min(W × thresholdRatio, W − O − headroomTokens))
The defaults are thresholdRatio: 0.8, headroomTokens: 65,536, and retainRatio: 0.16. The summary call's maxTokens follows headroomTokens by default and includes reasoning tokens charged by the Provider.
This change is about timing, not a more optimistic context meter. Even before history reaches 80% of the window, DSH uses the earlier threshold when reserving one full output and summary room leaves a smaller budget. Capacity is resolved again after a Provider or model switch, and identical model IDs under different Providers remain separate routes.
A custom model must declare its real contextWindow or inherit an accurate defaultContextWindow. Inflating the local value only delays compaction until the Provider rejects the request. For a small-window model, reduce headroomTokens or lower thresholdRatio, while preserving contextWindow > O + headroomTokens.
Why dsh-compact takes a different path
The community project dsh-compact, published on October 19, 2026, does not modify the local DSH Session or replace built-in compaction. It runs a transparent proxy on 127.0.0.1 between DSH and an OpenAI-compatible provider and changes only the request sent upstream.
The proxy first performs a model-free pass that strips Base64 images and shrinks old tool results. If the request still exceeds its threshold, it calls a compaction model to summarize older history while retaining recent turns verbatim. The author reports that a plain-chat fixture fell from about 195,000 tokens to 97,965 while retaining five of five facts and all seven summary headings. This is one project-provided fixture, useful for understanding the mechanism rather than predicting a universal compression rate.
Built-in compaction versus a local proxy
| Dimension | DSH built-in compaction | dsh-compact local proxy |
|---|---|---|
| Modification point | Old spans in Session history | Only the provider-bound request |
| Local DSH record | Stores summary and events | Preserves the original transcript |
| Trigger | DSH route, window, and pressure | Proxy threshold |
| Provider path | Native DSH model chain | OpenAI Chat Completions-compatible endpoint |
| Primary value | Native Session shrinkage reused by later turns | Independent request-size control without rewriting local history |
Do not stack both paths without understanding their thresholds. A proxy may summarize a request immediately after built-in compaction, losing more detail and making cost or failure attribution harder. Evaluate the Base URL, logs, data boundary, and recovery procedure in an isolated Profile first.
Recovering a Session after context overflow
A community case describes a roughly 190K-token Session in which token underestimation triggered compaction too late, the summary request overflowed again, and failed replay left the Session unusable. Segmenting the history compacted 227 items—about 123,163 tokens—to roughly 9K tokens and restored the Session.
A safer recovery sequence is:
- Stop automatic continuation so history cannot grow further.
- Copy the Session or export its record.
- Split old history on tool-call/result boundaries.
- Summarize the oldest spans incrementally and recalculate the remaining window after each step.
- Record input size, finish reason, and failure reason per segment.
- Resume with read-only checks before repeating side-effecting work.
A local proxy can reduce the next upstream request, but it does not automatically repair invalid or oversized state already persisted in the DSH Session. Data recovery still belongs at the DSH history and compaction-transaction layer.
How truncated tool results amplify loops
Before summarization, DSH can deterministically prune oversized tool results while preserving their beginning and end. If evidence the Agent needs is removed from the middle, it may reread files, rerun commands, or repeat verification. An unfinished Goal can then continue automatic rounds and multiply the cumulative request volume.
Investigate more than “the model reasoned too much”:
- the first turn containing
[…]or a spill file; - repeated reads of the same path or command;
- compaction starts without useful summaries;
- a Goal driver continuing without new progress;
- Sub-agents copying large context concurrently.
Monitoring signals
| Metric | Warning sign |
|---|---|
| Input tokens per turn | Rapid growth across consecutive turns |
| Cache-miss input | Frequent changes to schemas, prompts, or prefixes |
| Tool repetition | The same resources are read repeatedly |
| Compaction state | Repeated failures, weak shrinkage, or a stale lock |
| Goal rounds | Automatic continuation without new progress |
| Cost per minute | Continued growth disconnected from result quality |
Safer configuration and recovery
- Set thresholds below the model's hard limit and reserve summary/output room.
- Give long tasks independent turn, time, and cost budgets.
- Read large files by range instead of reloading them every turn.
- Watch pruning markers and preserve critical evidence in a short note.
- If automatic compaction fails, stop the Agent, temporarily enlarge the window, run one manual compaction, and restore the normal setting.
- Verify possible side effects before resuming or retrying operations.
A reproducible evaluation
Hold the model, Profile, task, and tools constant. Compare average input tokens, cache hits, compaction count, tool calls, and cost over the first and last ten turns. Repeat with a more conservative pressure threshold. Cost differences are meaningful only when both runs meet the same task-success criteria.
Related reading
- DSH long Sessions: token budgets, message caps, and HTTP 400
- Agent Harness Components: Tools, Compression, and Subagents
- Why did a simple task take 50 turns?
- DSH Sub-agent usage and billing
- DeepSeek Harness dynamic tools and KV Cache
References
- DeepSeek Harness compaction subsystem
- DeepSeek Harness v0.2.1-alpha.1 Release
- Routed model context and compaction policy
- compaction-basic configuration reference
- Community reproduction: automatic compaction does not trigger
- Community test: 0.2.0-rc.2 long-session cost
- Community case: Goal rounds, compaction, and high token use
- dsh-compact local context proxy
- Segmented recovery after context overflow