Compact decision-history format: 12-21% fewer tokens#2
Open
xMKx wants to merge 1 commit into
Open
Conversation
Switch buildDecisionContext from a numbered prose list to a single-line "Prior decisions: Q=A; Q=A." form. Token deltas (cl100k_base proxy): depth=1 17 -> 15 tok (-12%) depth=3 34 -> 27 tok (-21%) depth=5 52 -> 41 tok (-21%) The "Prior decisions:" anchor is preserved so the LLM still recognises this as resolved state rather than negotiable constraints. The raw [Q=A; Q=A] form saved ~30% but lost that framing; this is the safer trade between size and behavioural risk. Note: an earlier experiment converting the history to a JSON block went the wrong way (+4-35% tokens) because of structural overhead from keys and quoting. Compact KV is the form that actually wins. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
Switches
ContextBuilder.buildDecisionContextfrom a numbered prose list to a single-linePrior decisions: Q=A; Q=A.form. Saves 12-21% tokens per fork on the decision-history slice of the user prompt.Token measurements
Tokenizer:
gpt-tokenizercl100k_base (OpenAI), used as an offline Claude proxy. Relative deltas should hold against the Anthropic tokenizer; absolute counts will vary slightly.These savings hit every non-fork child node (i.e. every fork on Anthropic/OpenAI providers, since neither natively resumes sessions). In a width=4 depth=3 tree that's 84 nodes each paying the delta.
Format choice
I tested four candidate formats before picking this one:
{"task":"...","resolved_decisions":[...]}): cost +4% to +35% tokens vs prose because of structural overhead ({,", key names, brackets). The "structured data is more efficient" intuition was wrong here.[Q=A; Q=A] task): saved ~30% but dropped the "Prior decisions:" semantic anchor, which I judged risky — the LLM might read those as constraints rather than resolved state.Prior decisions:\n- Q: A): only ~9% savings, didn't justify the format churn.Behavioural risk
Compact KV may be harder for the LLM to use than the numbered prose, especially if an answer text contains
=or;. None of the existing tests exercise such answers; real-world answers like "JWT-based authentication" or "PostgreSQL with JSONB" should also be safe. If anyone hits a regression, the fix is to escape=/;in answer text — happy to follow up.Test plan
npm test)npm run build)exploreagainst the Anthropic API provider to confirm projected savings translate to actual cost reduction (in progress, separate validation pass)Followups (separate PRs)
The other big token win is enabling prompt caching in the Anthropic API provider — the system prompt is currently sent without
cache_controlon every fork, paying full input price 85 times per tree. That PR is more involved because the current ~255-token system prompt is below the 1024-token caching minimum, so it needs to be restructured alongside the caching flag. Separate PR incoming.Co-Authored-By: Claude Opus 4.7 (1M context) noreply@anthropic.com