[Feature]: Per-item size in ContextManifest to attribute what fills the context window
Is your feature request related to a problem? Please describe.
In a multi-tenant assistant product we need to show end users what is consuming each request's context window: how much comes from the system prompt, long-term memory, conversation history, tool descriptions, and skill instructions. The harness assembles the outgoing request from these sources, but the framework only reports a single whole-request figure: ContextManifest.estimatedInputTokens (ContextManifest.java:26 at main@30dc84b59). ContextManifest.Item (lines 55–66) already records sourceType / sourceId / contentHash / revision / placement / required / priority, yet no size. Callers therefore recompute per-source sizes at the application layer, and those numbers drift from what is actually sent: skill instructions are appended to the system prompt on every call (DynamicSkillMiddleware.java:183, concatenation at 218), and by the time Model.stream(List
Describe the solution you'd like
While building each manifest item the harness already holds the item's content (HarnessContextBuilder.java:340/347/355). Please record a per-item size there — characters or tokens, whichever counting method you prefer, as long as it is documented — on ContextManifest.Item, or expose an equivalent read-only per-source report. This stays within the manifest's stated "metadata only, no prompt bodies" contract (ContextManifest.java:20), since only a number would be added.
Describe alternatives you've considered
Tagging segments via Msg.metadata and aggregating — Msg.equals/hashCode include metadata (Msg.java:218–238), so tagging changes message equality/dedup semantics. Wrapping the Model interface and measuring before stream — tool schemas are visible there, but the system prompt has already been merged into one message, so per-source attribution is lost (ChatModelBase.stream is final, so this requires a delegating Model implementation). Recomputing per-source sizes at the application layer (current workaround) — duplicates framework logic and drifts from the real request. Additional context
Anchors verified against main@30dc84b59; we consume v2.1.0-BETA1. We have a minimal local patch ready (per-item size filled at construction + unit tests) and will open a PR after this issue.
An implementation is up for review in #4215. It adds ContextManifest.Item.contentChars(), disjoint manifest segments whose sizes sum to the whole request, ContextUsage.breakdown / itemBreakdown / systemBreakdown / summary / systemChannelIsSingleBlock, and ItemShare.contentHash() so a row can be correlated across calls even when sourceId is regenerated. EN/ZH documentation is included. Feedback welcome.
Claiming. Verified on current main: ContextManifest (with estimatedInputTokens and Item at the cited lines) is intact.
The design as described is the right one — Item already carries the structural identity, so adding per-item estimated size turns the existing manifest into an attribution breakdown without new plumbing. Implementation notes: reuse whatever estimator produced the whole-request figure so the parts stay consistent with the total (and document that they're estimates from the same model, not provider-reported usage — related to the separate discussion in #4253); sum semantics for sources assembled per-request (system prompt, memory, history, tool schemas, skill instructions) need one pass over where the manifest items are currently recorded. Will keep the wire/API surface additive (new optional fields).
A heads-up to avoid two parallel implementations: #4215, opened on Nov 16, also implements this issue. I linked it here the same day, but that may have been missed, so I am restating it next to #4282.
What each one adds:
- #4215 adds
ContextManifest.Item.contentChars(), the length in characters of the exact content thatcontentHashis computed over. It also makes the manifest segments disjoint, so the per-item sizes overitems()add up to the whole request, and adds a smallContextUsagehelper class (per-source rows, six display categories, a summary) with EN/ZH docs. - #4282 adds
ContextManifest.Item.estimatedTokens(), measured with the same estimator asestimatedInputTokens(), and documents the per-item figures as independent estimates that need not add up to the whole-request number.
The practical differences are the unit (characters vs estimated tokens), whether the parts add up to the whole (yes vs not required), and the surface (one field vs a field plus helper APIs). Both keep the existing 7-argument constructor. As written, each adds a different eighth component to Item, so they need reconciling before either one merges.
I am happy to go whichever way you prefer: rebase #4215 onto #4282 and keep only the disjoint segments and ContextUsage, change the design to whatever you would rather have, or close #4215 if #4282 is the version you want. Just tell me which.