feat(agent): emit per-category context token breakdown#2352
Draft
k11kirky wants to merge 1 commit into
Draft
Conversation
Contributor
Author
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
This was referenced May 25, 2026
23ff881 to
d6105bb
Compare
7032aef to
78a3903
Compare
This was referenced May 25, 2026
Adds a `breakdown` field to the `_posthog/usage_update` ext-notification so the renderer can render per-source token splits in the upcoming context-breakdown popover. The agent estimates token counts for the stable pieces of the context (system prompt today; tools / MCP / rules / skills / subagents will follow as the agent gets at-rest access to their definitions) via a character-ratio heuristic. The `conversation` bucket is derived as `max(0, currentInputTokens - sum(stable))`, so the categories always sum to the input total. The renderer's `useContextUsage` now scans backwards for both the existing `session/update` aggregate and the new ext-notification's breakdown, surfacing the latter as a nullable field. Existing callers keep the aggregate; B4 wires the breakdown into the popover. Generated-By: PostHog Code Task-Id: bac06178-1ab1-4000-9a56-1901215bd4af Generated-By: PostHog Code Task-Id: bac06178-1ab1-4000-9a56-1901215bd4af
d6105bb to
1589fab
Compare
78a3903 to
00f8802
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Problem
The context usage indicator in the renderer showed only aggregate token counts (used / size / percentage) with no breakdown of where those tokens were coming from. Users had no way to see how much of the context window was consumed by the system prompt, tools, rules, conversation history, etc.
Changes
Agent-side (
packages/agent):context-breakdown.tswith a lightweight character-ratio token estimator (~3.5 chars/token) and helpers:estimateTokens/estimateJsonTokensfor cheap client-side estimationestimateSystemPrompthandles raw strings,{ type: "preset", append }objects, andundefined, adding a constantCLAUDE_PRESET_ESTIMATE_TOKENS(4000) when the opaque Claude preset is in usebuildBreakdownderives theconversationbucket as whatever input tokens remain after subtracting the stable pieces (system prompt, tools, rules, skills, MCP, subagents), floored at 0 to absorb estimation driftemptyBaseline/ContextBreakdownBaselinetypes for initializing and carrying per-source estimates across turnscontextBreakdownBaselineto theSessiontype, initialized at session start with the estimated system-prompt token count_posthog/usage_updatenotification after each model turn, using the result's own input token categories rather than the streamed delta to handle subagent turns correctlyRenderer-side (
apps/code):ContextUsagewith abreakdown: ContextBreakdown | nullfield and added theContextBreakdowninterface (mirroring the agent shape, kept local to avoid a cross-package dependency)extractContextUsageto scan events in a single reverse pass, independently finding the latest aggregate (session/update) and the latest breakdown (_posthog/usage_updateor__posthog/usage_update), then merging themHow did you test this?
context-breakdown.tscoveringestimateTokens,estimateJsonTokens,estimateSystemPrompt, andbuildBreakdown(edge cases: empty input, circular JSON, conversation floored at 0, preset vs. raw string)extractContextUsagecovering: no events, aggregate-only, merged breakdown, and the double-underscore method prefix variantPublish to changelog?
no