Skip to content

fix(code): report total context after /offload - #5488

Merged
Mason Daugherty (mdrxy) merged 5 commits into
mainfrom
mdrxy/code/offload-context-report
Aug 18, 2026
Merged

fix(code): report total context after /offload#5488
Mason Daugherty (mdrxy) merged 5 commits into
mainfrom
mdrxy/code/offload-context-report

Conversation

@mdrxy

@mdrxy Mason Daugherty (mdrxy) commented Aug 13, 2026

Copy link
Copy Markdown
Member

/offload now reports the change in total context (system prompt, tool schemas, and conversation) instead of passing off a conversation-only estimate as "Context".


The previous report computed both sides of the before/after with count_tokens_approximately over conversation messages only, then labeled the result Context:. For a coding agent the fixed overhead (system prompt plus tool schema) is the dominant term, so the reported totals and the percentage reduction were both misleading — an offload that freed mostly conversation looked like it had freed the whole window.

  • When a provider-reported total is persisted from the last ordinary turn, the report subtracts only the conversation delta from it, keeping both figures on the provider's scale. The before figure prints exact; the after figure carries a ~ because the delta is estimated.
  • Subtracting the delta (rather than rebuilding the total as overhead + conversation_after) matters: the local estimator need only overshoot the provider count by a token for an overhead = max(0, reported - conversation_before) clamp to collapse the overhead to zero, which would silently report the entire system prompt and tool schema as freed context.
  • With no usable provider total (never set, or a stale checkpoint value), the report falls back to the conversation-only estimate and labels it Conversation: so the two metrics are not mistaken for one another — percentages are not comparable across the two labels.
  • The percentage is floored at zero: a summary can come out larger than the messages it replaced, and a negative "decrease" should never render.

@github-actions github-actions Bot added dcode Related to `deepagents-code` fix A bug fix (PATCH) internal User is a member of the `langchain-ai` GitHub organization size: S 50-199 LOC labels Aug 13, 2026
@open-swe open-swe Bot changed the title fix(code): report total context after /offload fix(code): report context and conversation turns Aug 13, 2026
@github-actions github-actions Bot added size: M 200-499 LOC and removed size: S 50-199 LOC labels Aug 13, 2026
@mdrxy
Mason Daugherty (mdrxy) marked this pull request as ready for review August 13, 2026 19:20
@mdrxy Mason Daugherty (mdrxy) changed the title fix(code): report context and conversation turns feat(code): report context and conversation turns Aug 18, 2026
@github-actions github-actions Bot added feature New feature/enhancement or request for one size: L 500-999 LOC and removed fix A bug fix (PATCH) size: M 200-499 LOC labels Aug 18, 2026

@open-swe open-swe Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open SWE Review found 1 potential issue.

Open in WebView Open SWE trace

Comment thread libs/code/deepagents_code/app.py
Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>
The reported branch rebuilt the post-offload figure as
`max(0, reported_before - conversation_before) + conversation_after`. When
the provider total fell below the local conversation estimate -- a stale
`_context_tokens`, or `count_tokens_approximately` overshooting by a single
token -- the clamp collapsed the overhead to zero, leaving `tokens_before` on
the provider scale (system prompt plus tool schemas included) while
`tokens_after` dropped to conversation-only. The whole fixed overhead was then
reported as freed context, which for a coding agent with a full tool schema is
the dominant term. The percentage could also render as a negative "decrease".

Subtract the conversation *delta* from the provider total instead. The two
forms are algebraically equal whenever the clamp does not fire, but only this
one keeps both figures on the provider's scale. Floor the percentage at zero,
since a summary can come out larger than the messages it replaced.

Also document what `MessageStore.turn_count` actually counts: rendered
user-authored rows, which include local-only flows (`!shell`, most slash
commands) that mount a `UserMessage` without invoking the server. That is a
broader population than the "conversation turns" the offload report derives
from graph state, and the two surfaces used the same word for it.

Restore the rationale for slicing the pre-seed message list rather than the
post-run one, now that two more counters depend on it, and cover the branches
that mutation-tested as unheld: the low-total path, the percentage floor,
`turns_offloaded`'s internal-message filter, the archive-failure wording, and
the singular labels whose assertions were prefixes of their own plurals.
@mdrxy
Mason Daugherty (mdrxy) force-pushed the mdrxy/code/offload-context-report branch from a3b88b5 to 78277ec Compare August 18, 2026 01:48
@github-actions github-actions Bot added size: M 200-499 LOC and removed size: L 500-999 LOC labels Aug 18, 2026
@mdrxy Mason Daugherty (mdrxy) changed the title feat(code): report context and conversation turns fix(code): report total context after /offload Aug 18, 2026
@github-actions github-actions Bot added fix A bug fix (PATCH) and removed feature New feature/enhancement or request for one labels Aug 18, 2026
…ntext-report

# Conflicts:
#	libs/code/deepagents_code/app.py
#	libs/code/tests/unit_tests/test_offload.py
When a generated summary is larger than the messages it replaces,
`tokens_after > tokens_before`, but the clamped `max(0, ...)` percentage
rendered the result as `0% decrease` alongside the claim "freeing up
context window space." Users were told context was freed even though
`/offload` increased it.

Render the signed outcome instead of clamping it away:

- When `tokens_after > tokens_before`, the stats line shows
  `(increase)` instead of a percentage, and the surrounding text says
  the summary was larger than the messages it replaced, so context
  increased — no "freed" claim.
- When the operation did reduce tokens, the existing `N% decrease` and
  "freeing up context window space" text is preserved.

The archive-failure path now shares the same `outcome` prefix, so the
error message no longer hardcodes "and freed context" either.
@mdrxy
Mason Daugherty (mdrxy) merged commit 5d806c8 into main Aug 18, 2026
57 checks passed
@mdrxy
Mason Daugherty (mdrxy) deleted the mdrxy/code/offload-context-report branch August 18, 2026 03:49
Mason Daugherty (mdrxy) pushed a commit that referenced this pull request Aug 18, 2026
> [!CAUTION]
> Merging this PR will automatically publish to **PyPI** and create a
**GitHub release**.

For the full release process, see
[`.github/RELEASING.md`](https://github.com/langchain-ai/deepagents/blob/main/.github/RELEASING.md).

---

_Release notes preview: keep this section in sync with the package
`CHANGELOG.md`. Publish reads the merged CHANGELOG via `release.yml`,
not this PR description — keep them aligned anyway so the PR stays an
accurate historical record for reviewers and anyone returning later._

---


##
[0.1.57](deepagents-code==0.1.56...deepagents-code==0.1.57)
(2026-08-18)

### Features

- Added warnings before expensive cold-cache turns and trust
user-declared endpoints for cold-cache policies
([#5439](#5439),
[#5462](#5462)).
- Made the chat input resizable by dragging its top border
([#5524](#5524)).
- Added a `multi_select` question type to `ask_user`
([#5097](#5097)).
- Added support for ACP approval modes
([#5394](#5394)).
- Added `DeepSeek-V4-Pro-0813` to the model picker
([#5512](#5512)).
- Show conversation turns alongside message counts
([#5571](#5571)).
- Include `TERM_PROGRAM` in the resume hint
([#5548](#5548)).

### Bug Fixes

- Report total context after `/offload`
([#5488](#5488)).
- Fixed transcript and thread restoration issues, including hydration
lag, scrolling resumed threads to the bottom, and hiding empty
previous-thread hints
([#5479](#5479),
[#5543](#5543),
[#5552](#5552)).
- Fixed Auto-mode approval handling by binding “yes” to the paired
`ask_user` question and avoiding duplicate Auto denial notices
([#5038](#5038),
[#5501](#5501)).
- Improved reload behavior by keeping the chat input responsive during
`/reload`, reporting MCP server changes, and avoiding plugin reload
prompt flashes or startup hints
([#5529](#5529),
[#5504](#5504),
[#5500](#5500),
[#5502](#5502)).
- Improved dependency update UI by preserving editable fields and hiding
dependency details after updates
([#5521](#5521),
[#5519](#5519)).
- Fixed chat UI polish issues, including detached spacer mount anchors,
the unfocused input cursor, and relative timestamp toggle display
([#5516](#5516),
[#5258](#5258),
[#5503](#5503)).
- Refresh the splash version after updates
([#5520](#5520)).

_End release notes preview._

---

> [!NOTE]
> A **community contributors** list and a **Special thanks** section
(crediting the users who filed the issues this release's PRs closed) are
appended to the GitHub release notes automatically at publish time (see
[Release
Pipeline](https://github.com/langchain-ai/deepagents/blob/main/.github/RELEASING.md#release-pipeline),
step 3).

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: langchain-oss-automated-triage[bot] <248757908+langchain-oss-automated-triage[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dcode Related to `deepagents-code` fix A bug fix (PATCH) internal User is a member of the `langchain-ai` GitHub organization size: M 200-499 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant