Repository navigation
feat: Price each thinking block at its own token count from the host - #81
Conversation
A host now gives each thinking block the thinking_tokens of the response that made it. NewBodyCounted looks up each signature by OpaqueKey. The library prices counted bytes at the count, in the text ratio and in every range. Bytes with no count keep the fixed price of 1 token for 250 bytes. - Measured on Opus 5.5: a sent-back block bills its thinking_tokens plus 0 to 4 tokens. On Sonnet 4.5 it bills 0. - If the counts do not fit the bill, the library drops them for the request and fails the new check opaque_tokens. This catches a model that strips thinking. - Size.OpaqueTokens gives the count of a range. SplitOpaqueTokens splits one response's count between its blocks. Snapshot keeps the counts. Closes #77. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: DAVID BOAZ <[email protected]>
boazdavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (claude, user of @boazdavid) | Claude Code 2.1.241 | offload-review skill, @boazdavid's session | fresh | 2 of 2 | 1 of 2 | 6480c66897b9 |
blocking |
Verdict: blocking
Findings
-
internal/engine/body.go:270-273 — the library uses counts only if the forwarded body holds a counted block, so a drop of the last counted block saves the fixed price
- What fails: the client body has one counted thinking block (3,000 signature bytes, count 1,000). The operator drops that signature. The forwarded body then has no counted block. The saving is 100 text + 12 tokens, not 100 + 1,000. No check fails. This is the main use case of D16 (a thinking operator that drops old blocks), and it gives the same error that D16 is meant to remove.
- Proof (trace):
newRatiosetsr.countsonly insideif all.counted > 0, andallis the forwarded body. Hereall.countedis 0, sor.countsstaysfalse.rangeMediaforsizeBeforereads the client body and setsnt.counted = 3000,nt.ctok = 1000(compute.go:519-523).opaqueOf(body.go:305-316) ignoresnt.countedandnt.ctokwhenr.countsis false. It prices 3,000 bytes at 1 token for 250 bytes = 12.- The conservation check (compute.go,
ratio.upTo(c.client, ...)) also usesr.counts == false, so it agrees with the 12 and does not fail.
- The same cause makes
Size.OpaqueTokensdo nothing when the host builds the forwarded body withNewBody, or when the forwarded body has no counted block. design.md §4.2 says: "A size takes its count fromSize.OpaqueTokenswhen the host sets it". The code does not do that in these cases. So the code and the doc disagree. - Why the tests pass: case
opaque/counted-thinking-blocks/5mkeeps a second counted block (message 3) in the forwarded body.TestSizeOpaqueTokensWinsbuilds aratioby hand withcounts: true, so it never goes throughnewRatio. - Fix: use the counts unless the guard drops them:
r.counts = all.ctok+(opaque-all.counted)/opaqueBytesPerToken <= roomwhenall.counted > 0, elser.counts = true. Then say in D16 and check 14 that the strip guard can act only when the forwarded body holds a counted block. Add a generated case where the operator drops the only counted block.
-
internal/engine/opaque_count_test.go:141 — no test runs
Size.OpaqueTokensthroughCompute- What fails: the new branch in
rangeMedia(compute.go:530-532) has no test that reaches it.TestSizeOpaqueTokensWinscallssizeOfwith a hand-madenonTextand a hand-maderatio. It does not callrangeMediaorCompute. Finding 1 shows a defect on this path that the test does not see. - Proof:
grep -n "OpaqueTokens" test/gen/gen_cases.pyfinds onlyPosHashfields (opaqueTokensinc77_body). No case setssizeBefore.opaqueTokensorsizeAfter.opaqueTokens. The one Go test that buildsSize{... OpaqueTokens ...}tests onlyinvalid(). - Fix: add a generated case (or a
Computeunit test) withsizeBefore: {bytes, opaqueBytes, opaqueTokens}. Run it once with a counted block left in the forwarded body, and once with none.
- What fails: the new branch in
Non-blocking
- fingerprint.go:771:
NewBodyCountedtreats a negative lookup result as "no count" (fixed price). design.md §4.2 says "a negative count is 0". That sentence is true forPosHash.OpaqueTokens(opqTokens), but a reader can apply it to the lookup. Say in the doc that a negative lookup result keeps the fixed price. - compute.go:519-523: the split in proportion mixes counted and uncounted blocks in one range. Example: a range holds a counted block (3,000 bytes, 1,000 tokens) and an uncounted block (3,000 bytes). A size that holds only the counted block's 3,000 opaque bytes gets 500 + 6 tokens, not 1,000. The design says "in proportion", so this is documented. It can be worth a note in §11 question 16.
- types.go:1141:
Size.OpaqueTokens0 means "no count", but aPosHashcount of 0 is a count. A host cannot say on a size that its signature bills 0 tokens (a model that strips thinking). This is a choice, but D16 can say it. - compute.go:917: the ping extent calls
newRatio(u.CacheRead+u.CacheWrite, prev.Forwarded). The bill there covers only the prefix the ping cached, but the guard compares it with the counts of the whole body. Large counts after the prefix can drop the counts for the extent. The fallback is the fixed price, so the effect is small. - types.go:1148: a negative
Size.OpaqueTokensnow makes the size invalid (the operation goes toUnpriced). The design table of malformed operations names "negative size". It can name this field too.
What I checked
- Read
CLAUDE.md, and the D15, D16, §4.2 rule 7, §5.1, §6.5 (checks 9, 11, 14) and §11 question 16 parts ofdocs/design.md, and R8 ofdocs/spec.md. - Read the full diff of all files except
test/cases/*.json:accountant.go,body.go,compute.go,fingerprint.go,persist.go,types.go,result.go,doc.go, the tests,cases.go,cases_test.go,gen_cases.py(c77),scripts/measure-thinking-bill.py, and the doc changes. - Read the code around each change:
rangeBody,sizeTokens,plausible, the rewrite pricing loop,mediaUnpriced,leaveOutOfConservation, the conservation check,checks,checkUsage,badRatio,pingRead,premiums,markPayback. - Traced the arithmetic of both new cases (
counted-thinking-blocks/5m,counted-thinking-not-billed/5m) againstnewRatioandrangeMedia. Both agree with the code. - Checked the guard: when the counts are used, the opaque price is at most the room, so check 9 and check 11 cannot fail because of the counts.
- Checked R3: no new import. Checked the API: the new names are in
accountant.go, andapi_test.gopasses.CheckOpaqueTokensis inTestEveryCheckHasAFatalClass. - Checked
Snapshot:wirePoskeeps both counts, withomitempty, so old snapshots still load. - Checked the
PosHashsize in the doc: 8 + 6 x 4 = 32 bytes. - Ran
gofmt -l .(no output),go vet ./...(clean) andgo test ./...(all 3 packages pass). I did not run the live script.
boazdavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (claude, user of @boazdavid) | Claude Code 2.1.241 | offload-review skill, @boazdavid's session | fresh | 1 of 2 | 1 of 2 | 6480c66897b9 |
blocking |
Verdict: blocking
Findings
- internal/engine/body.go:271 — The library ignores every count when the forwarded body holds no counted block. A drop of the last counted thinking block then saves only the fixed price.
- What fails:
newRatiosetsr.countsonly insideif all.counted > 0, andallis the forwarded body.opaqueOf(body.go:305-316) usesnt.ctokonly whenr.countsis true. So if the operator drops every counted block, or the blocks that stay have no count, the dropped blocks in the client body cost 1 token for 250 bytes.Size.OpaqueTokensis ignored in the same way. No check fails, so the host does not see the error. - Proof (trace): client =
m04,000 B,th1400 B text + 3,000 B signature (counted at 1,000),m22,000 B. Forwarded =m0,th1f400 B,m2. Bill 1,600.newRatio:all.counted= 0, sor.counts= false andr.countsDropped= false.rangeMediafor the "before" size (3,400 B) givesnt{opaque 3000, counted 3000, ctok 1000}.sizeOf=of(400)+opaqueOf= 100 + 3,000/250 = 112. After = 100. Saved = 12, not 1,000.- Check 6 also passes:
upTo(client)uses the same fixed price (1,612 - 1,600 = 12). Check 14 does not run, becausecountsDroppedis false. - The same result occurs if the host sets
Size.OpaqueTokens: 1000.
- Why it is a defect: D16 says "The library prices the counted bytes at the count, in the text ratio and in every range". §4.2 says "every range (a size, the read extent, a write) prices its counted bytes at their count".
types.go:170-173says thatSize.OpaqueTokenssets the count. No doc says that the counts need a counted block in the forwarded body. The two new cases both keep one counted block (th2) in the forwarded body, so they do not show this path. - Fix: choose one rule and write it in D16 and §4.2.
- (a) Use the counts when the forwarded body has none (set
r.counts = trueunless the guard drops them). Trust the host, as D16 already does for models that strip thinking. - (b) Keep the code. Then document that a request with no counted forwarded block uses the fixed price, and report it (for example, a check detail), so that the host knows the saving is low.
- In both cases, add a case in
gen_cases.pywhere the operator drops the only counted block.
- (a) Use the counts when the forwarded body has none (set
- What fails:
- internal/engine/compute.go:480 —
Size.OpaqueTokensand the share by proportion inrangeMediahave no test throughCompute.- What fails: nothing proves the new branches of
rangeMedia(compute.go:455-487). A wrong share or a wrongSize.OpaqueTokensoverride would pass every test. - Proof:
grep -rln "OpaqueTokens\|rangeMedia"over the tests finds onlyopaque_count_test.go.TestSizeOpaqueTokensWinscallsratio.sizeOfwith anonTextthat it builds by hand, so it does not callrangeMedia. Both JSON cases use sizes with bytes only (c77_drop), and each range holds one whole block, so the share is always 1. - Fix: add (a) a case or unit test with a size that sets
OpaqueTokens, and (b) a size whose opaque bytes are fewer than the range's opaque bytes, so that the share by proportion runs. The CLAUDE.md rule is "a missing test for new behavior" is a defect.
- What fails: nothing proves the new branches of
Non-blocking
- body.go:252 (comment of
newRatio): it says "If the counts alone do not fit in the room". The code (body.go:272) tests the counts plus the fixed price of the uncounted opaque bytes. Check 14 in design §6.5 describes the code correctly. Change the comment to agree. - When the bill is below the media price,
roomis 0. Then any counted block with a count above 0 setscountsDropped, and check 14 fails besidemedia_tokens. Its detail says "the counts are above what the bill leaves". The cause is the media price, not a model that strips thinking. You can skip check 14 whenr.cappedis true, or say in §6.5 check 14 that the two can fail together. rangeMediasplits a position's count by bytes when a size holds part of the opaque bytes of a range. If the blocks in that range have different counts, the share is wrong. D16 already lists the split by length as open. Name this case there too.scripts/measure-thinking-bill.pysends the key in two headers (x-api-keyandauthorization). This is fine for a proxy. Aurllib.error.URLError(for example, a timeout) is not retried and stops the run with a traceback.
What I checked
- I read the full diff of all files except
test/cases/*.json. I readtest/gen/gen_cases.py(c77_body,c77_drop,c77) in its place. - I read the code around each change:
newRatio,opaqueOf,tokensOf,extentWith,pingRead,rangeMedia,rangeBody,sizeTokens,price,conservation,checks,checkUsage,premiums,markPayback,persist.go. - I checked the figures of both new cases by hand: ratio (3,900 - 300) / 14,400 = 0.25; room 3,648 - 720 = 2,928 < 4,000; drop 4,200 / 250 = 16. They agree with
docs/issue_cases.md§9j. - I checked the D15 and D16 numbers: 0.075 / 0.004 ≈ 19 and 0.35 / 0.004 ≈ 87. The "680 B + 2.3 B a token" model gives 0.073 tokens a byte at 60 tokens and 0.34 at 1,047 tokens.
- I checked that the strip guard does not fail when the provider bills the counts: real counts are at most the bill less the media and the real text cost, and the real text cost is above 0.05 tokens a byte.
- I checked
SplitOpaqueTokens: the parts add up totokens, the remainder loop runs fewer thanlen(opaques)times, and the empty-strings path does not divide by 0. - R3: no new import.
accountant.goexportsNewBodyCounted,OpaqueKey,SplitOpaqueTokens,OpaqueCountandCheckOpaqueTokens.TestEveryCheckHasAFatalClasslists the new check. gofmt -l .prints nothing.go vet ./...is clean.go test ./...passes (3 packages).- I did not run the measurement script. Live tests are not approved.
Review of #81. The library used the counts only when the forwarded body held a counted block. A drop of the last counted block then saved the fixed price (12 tokens), not its count (1,000). - The counts are used unless the forwarded body proves them wrong. - The fixed price of the uncounted bytes is cut to the room the counts leave. - Check 14 is not reported when the media is cut to the bill. - New cases: the only counted block dropped, Size.OpaqueTokens through Compute, and the share by proportion through Compute. - Docs: the guard's reach, a negative lookup, Size.OpaqueTokens 0, the ping extent, and the share limit in Q16. - The measurement script retries a timeout. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: DAVID BOAZ <[email protected]>
Replies to round 1 (both reviewers)All are fixed in Finding 1 (both reviewers): the counts were ignored when the forwarded body held none. Fixed.
Finding 2 (both reviewers): no test of
Non-blocking points.
Not changed.
Assisted-By: Claude (Anthropic AI) [email protected] |
boazdavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (claude, user of @boazdavid) | Claude Code 2.1.241 | offload-review skill, @boazdavid's session | resumed | 2 of 2 | 2 of 2 | e3675222d404 |
approve |
Verdict: approve
Findings
None.
Status of round-1 findings
- Reviewer 2, finding 1 (counts ignored when the forwarded body has none): fixed.
newRatio(body.go:274-277) now setsr.counts = trueand turns it off only when the forwarded body holds a counted block and the guard fails. Trace of my round-1 example: forwardedall.counted= 0, sor.counts= true.rangeMediagivesnt{opaque 3000, counted 3000, ctok 1000}.opaqueOfreturns 1,000. The drop saves 1,000. Caseopaque/counted-only-block-dropped/5mcovers it. D16, §4.2 rule 7 and check 14 say that the guard acts only on a counted block in the forwarded body. - Reviewer 2, finding 2 (no
Computetest ofSize.OpaqueTokens): fixed.opaque/counted-size-opaque-tokens/5mgoes throughrangeMediawithopaqueTokens1,000.opaque/counted-only-block-dropped/5mcovers the forwarded body with no count. - Reviewer 1, finding 1: the same defect as my finding 1. Fixed with option (a).
- Reviewer 1, finding 2 (share by proportion not tested): fixed.
opaque/counted-size-share/5mruns the share: 1,500 x 3,000 / 6,000 = 750. - Non-blocking points of both reviewers: all are done as the author's reply says. I checked each one in the diff: the
newRatiocomment, check 14 skipped whencapped, the share limit in rule 7 and Q16, the negative lookup (D16, rule 7,NewBodyCountedcomment),Size.OpaqueTokens0 and the ping extent in D16, the malformed-operation row, and theURLErrorretry. The 2-header point is not changed. Reviewer 1 marked it as fine, and I agree.
Non-blocking
- body.go:255: the comment now has the sentence "Only counts in the forwarded body can be tested: with none there, the counts of the client body are used as they are." between the guard and "The library then drops the counts". So "then" seems to follow "with none there". Move that sentence after "(check 14)". The line is also much longer than the lines around it.
- body.go:281-286: when the counts are used and the forwarded body holds a counted block, the guard already makes
fixed/250 <= room - ctok. So the cut toleftruns only when the forwarded body has no counted block (thenleft=room, as before). The code is correct. The comment "cut to the room the counts leave" describes a case that cannot occur. - The trust rule has a known cost. On a model that strips thinking, a forwarded body with no counted block cannot fail the guard. A drop of a counted block then books its full count, and the provider billed 0. D16 says this, and tells the host not to pass counts for such a model. I accept this.
What I checked
- Last review: round 1, 6480c66, 2026-11-17T06:39:15Z
- New commits read: 1 (
e367522fix: Use the client body's counts when the forwarded body has none) - Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files)
- New items since my last review:
- Review by reviewer 1, round 1 (06:39:44Z): read it. I checked each finding against the new commit (see "Status of round-1 findings").
- Comment by @boazdavid, "Replies to round 1" (06:43:33Z): checked each reply against the diff. All agree with the code. The note "check 6 does not run in phase
unknown" explains why the share case uses equal counts. The share limit is documented, so I accept it.
- Read the full diff
6480c66..e367522excepttest/cases/*.json:design.md,issue_cases.md,body.go,compute.go,fingerprint.go,measure-thinking-bill.py,gen_cases.py(c77_more). I read all ofnewRatioandopaqueOf, andrangeMediaandchecksaround the change. - Traced the 3 new cases by hand:
counted-only-block-dropped: ratio 1,600 / 6,400 = 0.25; before 100 + 1,000; after 100; saved 1,000; base 2,600.counted-size-opaque-tokens: forwarded room 2,100 - 320 = 1,780 >= 500, so the counts are used; ratio 1,600 / 6,400 = 0.25; before = 0 text + 1,000 (theSize.OpaqueTokensoverride); after = 0 bytes; saved 1,000; the sum check holds (6,400 - 3,400 = 3,000).counted-size-share: share 1,500 x 3,000 / 6,000 = 750; saved 750.
- Checked the change to check 14:
cappedimpliesroom= 0, andmedia_tokensthen names the cause. When counts are dropped andcappedis false, check 14 is still reported. So the skip hides no strip case that the media does not already explain. - Checked that
r.countsis read only inopaqueOf, so the new default changes no other path. - The 3 new JSON files exist, and
go test ./test/casespasses with them. I did not rungo generate, because it writes files. - Ran
gofmt -l .(no output),go vet ./...(clean) andgo test ./...(3 packages pass). I did not run the live script.
boazdavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (claude, user of @boazdavid) | Claude Code 2.1.241 | offload-review skill, @boazdavid's session | resumed | 1 of 2 | 2 of 2 | e3675222d404 |
blocking |
Verdict: blocking
Both round-1 findings of both reviewers are fixed. One new finding is small: a new rule in checks has no test.
Status of round-1 findings
- Reviewer 1, finding 1 (counts ignored with no counted forwarded block): fixed.
newRatio(body.go:274-277) now setsr.counts = trueand drops the counts only when the forwarded body holds a counted block that does not fit. I traced my round-1 example again: before = 100 + 1,000, after = 100, saved = 1,000. Caseopaque/counted-only-block-dropped/5mholds this trace. D16, rule 7 and check 14 now say that the guard acts only when the forwarded body holds a counted block. - Reviewer 1, finding 2 (no test of
Size.OpaqueTokensand the share throughCompute): fixed. The new cases reach both branches ofrangeMedia, and each case tells the branches apart:- In
counted-size-opaque-tokens/5m, the share alone would give 3,000 / 6,000 x 1,500 = 750. The override gives 1,000. The case expects 1,000. - In
counted-size-share/5m, the whole range would give 1,500. The share gives 750. The case expects 750.
- In
- Reviewer 2, findings 1 and 2: these are the same as my findings 1 and 2. Both are fixed, with the same proof as above.
- Non-blocking points (both reviewers): all are fixed or documented, as the author's reply says. I checked each one in the diff:
- the
newRatiocomment; - check 14 when the media is capped;
- the share limit in rule 7 and Q16;
- a negative lookup in rule 7, D16 and the
NewBodyCountedcomment; Size.OpaqueTokens0 in D16;- the ping extent in D16;
- a negative
Size.OpaqueTokensin the malformed-operation row; - the
URLErrorretry.
- the
- The author kept the two key headers in the script. I agree. I marked it as fine in round 1.
Findings
- internal/engine/compute.go:1992 — The new rule "check 14 is not reported when the media is capped" has no test.
- What fails:
e367522adds!c.ratio.cappedto the condition of check 14. This changesResult.Checksfor a request whose bill is below the media price and whose forwarded body holds a counted block. If the condition is removed or inverted, every test still passes. - Proof:
grep -ln opaqueCounted test/cases/*.jsonfinds 5 cases. None of them holds media.- The only test that uses
cappednear opaque bytes isTestOpaquePriceIsCutToTheBill(opaque_test.go:59). It callsnewRatioand notchecks, and its body has no counts. - Trace:
cappedis true only whenroomis 0. Then any counted block with a count above 0 setscountsDropped. So a body with media, a counted block and a bill below the media price now givesmedia_tokensand noopaque_tokens. No test shows this.
- Fix: add one generated case: a forwarded body with a base64 image and a counted thinking block, and a bill below the media price. Expect
checks=["media_tokens"]and noopaque_tokens.
- What fails:
Non-blocking
- body.go:255: the new sentence "Only counts in the forwarded body can be tested: ..." sits between "the provider did not bill them" and "The library then drops the counts". So "then" now seems to follow the client-body sentence. The line is also about 190 characters long. Move the new sentence after "(check 14)", and wrap the line.
- body.go:280-286: when the counts are used and the forwarded body holds a counted block, the guard already ensures
fixed/250 <= left. So the new cut acts only when the forwarded body holds no counted block, or when the counts are dropped. This is correct. A short comment that says so would help a reader.
What I checked
- Last review: round 1, 6480c66, 2026-11-17T06:39:44Z
- New commits read: 1 (
e367522fix: Use the client body's counts when the forwarded body has none) - Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files)
- New items since my last review:
- Commit
e367522: I read the full diff excepttest/cases/*.json, and I readc77_moreingen_cases.pyin its place. I read the code around each change:newRatio,opaqueOf,rangeMedia,checks,conservation, and thePhaseUnknownpath that setsc.unknown. - Author's reply comment (2026-11-17T06:43:33Z): I checked each point against the diff. See "Status of round-1 findings".
- The claim "Check 6 does not run on a request in phase unknown" is true.
conservationreturns whenc.unknownis true (compute.go:1953), and thePhaseUnknowncase sets it (compute.go:1037).
- Commit
- Reviewer 2's round-1 review (06:39:15Z) is older than my last review. I read it in full for this round.
- I traced the three new cases by hand:
only-block-dropped: ratio 1,600 / 6,400 = 0.25, saved 1,000, base 2,600.size-opaque-tokens: ratio (2,100 - 500) / 6,400 = 0.25, saved 1,000, base 3,100.size-share: ratio (2,350 - 750) / 6,400 = 0.25, saved 750.- All three agree with
newRatio,rangeMediaanddocs/issue_cases.md§9j.
- I checked that
leftinnewRatiocan never be negative. When the counts are used and the forwarded body holds a counted block, the guard ensuresctok + fixed/250 <= room. - I checked that
cappedimpliesroom= 0. The opaque price is at mostroom, so aroomabove 0 leaves the text at least its floor. So the comment on the check-14 condition is right. - The "always trust the counts" rule has a risk: a model that strips thinking, and a drop of every counted block, books the whole count. D16 says that a host must not pass counts for such a model, so I accept this rule.
gofmt -l .prints nothing.go vet ./...is clean.go test ./...passes (3 packages).- I did not run the measurement script. Live tests are not approved.
Review of #81, round 2. The rule that check 14 is not reported when the media is cut to the bill had no test. The new case fails without it. The newRatio comment now reads in order and is wrapped. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: DAVID BOAZ <[email protected]>
Replies to round 2Fixed in Reviewer 1, finding 1: check 14 under a capped media bill had no test. Fixed.
Non-blocking (both reviewers).
Assisted-By: Claude (Anthropic AI) [email protected] |
boazdavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (claude, user of @boazdavid) | Claude Code 2.1.241 | offload-review skill, @boazdavid's session | resumed | 1 of 2 | 3 of 3 | a4f71add7e85 |
approve |
Verdict: approve
Findings
None.
Status of earlier findings
- Reviewer 1, round 2, finding 1 (no test for "check 14 is not reported when the media is capped"): fixed. The new case is
opaque/counted-thinking-media-capped/5m. My trace ofnewRatiofor this case:- Text = 4,000 bytes, so the floor is 200. The media price is 2,000,000 / 250 = 8,000. The bill is 2,350.
room= max(2,350 - 8,000 - 200, 0) = 0. The forwarded body holds 1,000 counted tokens, and 1,000 > 0, socountsDropped= true.- The fixed price is 3,000 / 250 = 12 >
left0, so the opaque price is cut to 0. r.tokens= 2,350 - 8,000 < 0, socapped= true. The media gets 2,350 - 200 = 2,150 tokens, and the text gets 200 tokens over 4,000 bytes = 0.05. Check 9 passes.- The rewrite cuts
m0from 4,000 to 2,000 text bytes, with no media in the range. It saves 2,000 x 0.05 = 100 tokens, as the case expects.
- The case guards the rule. The step compare in
test/cases/cases_test.go:224-231compares the full sorted list of failed checks. If someone removes!c.ratio.capped, the step also reportsopaque_tokens. Then the list ismedia_tokens,opaque_tokens, and the case fails. This agrees with the author's reply. - Round-2 non-blocking points (both reviewers): fixed.
- The
newRatiocomment now reads in order and is wrapped (body.go:250-258). - The comment on the cut to
leftnow says when the cut acts (body.go:279-282).
- The
- All round-1 findings of both reviewers were fixed in
e367522. My round-2 review confirmed this, and the new commit does not change it. - The trust rule on a model that strips thinking: not changed. D16 states the cost, and both reviewers accept it.
Non-blocking
- None.
What I checked
- Last review: round 2, e367522, 2026-11-17T06:45:33Z
- New commits read: 1 (
a4f71adtest: Show that a capped media bill does not report opaque_tokens) - Read in full: 4 reviews, 2 comments, 0 inline comments (from the three files)
- New items since my last review:
- Commit
a4f71ad: I read the full diff excepttest/cases/*.json:docs/issue_cases.md,internal/engine/body.goandtest/gen/gen_cases.py(c77_capped). I read all ofnewRatioaround the comment change. The code did not change, only the comments. - Comment "Replies to round 2" (06:47:31Z): I checked each point against the diff and the test harness. All points agree with the code.
- Review by reviewer 2, round 2 (06:45:17Z, approve). It is older than my round-2 review by 16 s, so I read it in full for this round. Its two non-blocking points are the same as mine, and
a4f71adfixes both of them.
- Commit
- I checked the new row in
docs/issue_cases.md§9j. It agrees with the case and with check 14 in design §6.5. gofmt -l .prints nothing.go vet ./...is clean.go test ./...passes (3 packages).- I did not run the measurement script. Live tests are not approved.
boazdavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (claude, user of @boazdavid) | Claude Code 2.1.241 | offload-review skill, @boazdavid's session | resumed | 2 of 2 | 3 of 3 | a4f71add7e85 |
approve |
Verdict: approve
Findings
None.
Status of earlier findings
- Reviewer 2, round 1, findings 1 and 2: fixed in
e367522(see my round-2 review). The new commit does not change that code. - Reviewer 1, round 1, findings 1 and 2: fixed in
e367522, as both reviewers said in round 2. - Reviewer 1, round 2, finding 1 (no test for "check 14 is not reported when the media is capped"): fixed. The new case
opaque/counted-thinking-media-capped/5mcovers it. My trace is below. - Round-2 non-blocking points (both reviewers):
- The
newRatiocomment order and the long line: fixed. "The library then drops the counts" now follows the guard. The sentence about the client body comes after "(check 14)". The lines are wrapped. - The comment on the cut to
left: fixed. It now says that the cut acts only when the forwarded body holds no count, or when the counts are dropped. This agrees with the code: with counts used andall.counted > 0, the guard givesctok + fixed/250 <= room, sofixed/250 <= left. - The trust rule on a model that strips thinking: not changed, on purpose. D16 states the cost. I accept it, as in round 2.
- The
Non-blocking
- None.
What I checked
- Last review: round 2, e367522, 2026-11-17T06:45:17Z
- New commits read: 1 (
a4f71adtest: Show that a capped media bill does not report opaque_tokens) - Read in full: 4 reviews, 2 comments, 0 inline comments (from the three files)
- New items since my last review:
- Review by reviewer 1, round 2 (06:45:33Z): read it. Its one finding (no test for the capped rule) is fixed. Its 2 non-blocking points are the same as mine, and both are fixed.
- Comment by @boazdavid, "Replies to round 2" (06:47:31Z): I checked each point against the diff. All agree with the code.
- Commit
a4f71ad: I read the full diff excepttest/cases/*.json:docs/issue_cases.md,internal/engine/body.go(comments only), andc77_cappedintest/gen/gen_cases.py. I read all ofnewRatioagain after the change.
- Traced
opaque/counted-thinking-media-capped/5mby hand:- Forwarded body: text 2,000 + 2,000 = 4,000 bytes, media 2,000,000 bytes (8,000 tokens), and a 3,000-byte signature counted at 1,000.
floor= 200.room= max(2,350 - 8,000 - 200, 0) = 0. The counts (1,000) are above 0, socountsDroppedis true.- The fixed price is 12 tokens and
leftis 0, so the price is cut to 0. r.tokens= 2,350 - 8,000 < 0, so the ratio is capped. The media gets 2,150 tokens, and the text gets 200 tokens over 4,000 bytes = 0.05. Check 9 passes, because 0.05 is not below the limit.- The rewrite cuts
m0from 4,000 to 2,000 bytes. It covers no media, somediaUnpricedis false. Saved = 2,000 x 0.05 = 100. The case expectscacheWrite5m=100andchecks=["media_tokens"]. - Without
!c.ratio.capped, check 14 would also run:countsDroppedis true, there is usage, andc.invalidis "". So the case guards the rule, as the author says. I did not change the code to run this, because the rules forbid it.
- The JSON file
opaque-counted-thinking-media-capped-5m.jsonexists, andgo test ./test/casespasses. I did not rungo generate, because it writes files. - Ran
gofmt -l .(no output),go vet ./...(clean) andgo test ./...(3 packages pass). I did not run the live script.
Summary
A host can now give each thinking block its own token count. The library prices the block at that count, in the text ratio and in every range. Before this, every signature had a fixed price of 1 token for 250 bytes. On Opus 5.5 that price is 19 to 87 times too low. Closes #77.
What we found
The measurement that #77 asked for comes first. It used
scripts/measure-thinking-bill.py, 28 calls, with no cache:thinking_tokens)thinking_tokens+ 0 to 4, the same on every callWhat changed
NewBodyCounted(tools, system, messages, count). This isNewBodywith a lookup function. For eachthinking.signatureandredacted_thinking.datait finds, it callscount(OpaqueKey(s)). A known count goes toPosHash.OpaqueCountedandPosHash.OpaqueTokens.NewBodyis unchanged.OpaqueKeyandSplitOpaqueTokensare for the response side. Every host keys and splits the same way. The split is by signature length, and the parts add up exactly.Size.OpaqueTokensgives the count of a range. When it is 0, the library takes the counts of the body over the range.upTo(the read extent), the write sizes, the payback and the sizes. Bytes with no count keep the fixed price.opaque_tokens). Sometimes the counts in the forwarded body are more than the bill leaves: the bill less the media price and the least the text can cost. Then the library drops the counts for that request and uses the fixed price. It also failsopaque_tokens. The check is not fatal and unprices nothing. This catches Sonnet 4.5 without a list of models.Snapshotkeeps the counts.issue_cases.md§9j.How it was tested
gofmt -l .,go vet ./...,go test ./...andgo mod tidy -diffare all clean.opaque/counted-thinking-blocks/5m: R = (3,900 − 300) / 14,400 = 0.25. The drop saves the block's own 1,000 tokens; the fixed price would give 12. Request 2 finds the read extent with the counts.opaque/counted-thinking-not-billed/5m: the strip guard drops the counts and failsopaque_tokens.internal/engine/opaque_count_test.go: lookup, partial counts, the escaped-string key, the split, the ratio, the guard, clamping,Size.OpaqueTokensand the snapshot round trip.Open questions
Technical notes
PosHashgrows from 24 to 32 bytes.opaqueCounted,opaqueTokens(on a position) andopaqueTokens(on a size) are additive andomitempty, so the snapshot version does not change.tokensOfnow takes apartsvalue (bytes plusnonText), so all the call sites use the same opaque pricing.Assisted-By: Claude (Anthropic AI) [email protected]