Skip to content

feat: Price each thinking block at its own token count from the host - #81

Merged
boazdavid merged 3 commits into
mainfrom
feat/77-opaque-tokens
Nov 17, 2026
Merged

boazdavid merged 3 commits into
mainfrom
feat/77-opaque-tokens

Conversation

@boazdavid

Copy link
Copy Markdown
Contributor

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:

Model Blocks Input bill of a sent-back block
Opus 5.5 6 (60 to 1,047 thinking_tokens) thinking_tokens + 0 to 4, the same on every call
Sonnet 4.5 4 0 in all 8 comparisons: the model strips thinking
  • No fixed price can be right. Tokens per byte go from 0.075 to 0.35 as blocks get longer. The fit is about 680 bytes of overhead plus 2.3 bytes a token.
  • So the count from the host is the right input.

What changed

  • NewBodyCounted(tools, system, messages, count). This is NewBody with a lookup function. For each thinking.signature and redacted_thinking.data it finds, it calls count(OpaqueKey(s)). A known count goes to PosHash.OpaqueCounted and PosHash.OpaqueTokens. NewBody is unchanged.
  • OpaqueKey and SplitOpaqueTokens are 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.OpaqueTokens gives the count of a range. When it is 0, the library takes the counts of the body over the range.
  • Pricing. Counted bytes cost their count in R, upTo (the read extent), the write sizes, the payback and the sizes. Bytes with no count keep the fixed price.
  • Strip guard and check 14 (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 fails opaque_tokens. The check is not fatal and unprices nothing. This catches Sonnet 4.5 without a list of models.
  • Snapshot keeps the counts.
  • Docs: design D15 updated, a new D16, rule 7, the API listings, check 14 and Q16. Spec R8. issue_cases.md §9j.

How it was tested

  • gofmt -l ., go vet ./..., go test ./... and go mod tidy -diff are all clean.
  • New shared cases:
    • 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 fails opaque_tokens.
  • New unit tests in internal/engine/opaque_count_test.go: lookup, partial counts, the escaped-string key, the split, the ratio, the guard, clamping, Size.OpaqueTokens and the snapshot round trip.
  • No existing case changed its figures.

Open questions

  • Small counts on a model that strips thinking still fit the bill, so the guard does not catch them, and then R is too low by their size. The host contract is that a host passes no counts for a model that it knows strips thinking.
  • Thinking text on a model that strips thinking. Sonnet 4.5 sends back readable thinking text (569 to 4,879 bytes here) and bills 0 for it. The library counts that text as text bytes. This is in Q16 and needs its own issue.

Technical notes

  • PosHash grows from 24 to 32 bytes.
  • The JSON fields opaqueCounted, opaqueTokens (on a position) and opaqueTokens (on a size) are additive and omitempty, so the snapshot version does not change.
  • tokensOf now takes a parts value (bytes plus nonText), so all the call sites use the same opaque pricing.

Assisted-By: Claude (Anthropic AI) [email protected]

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 boazdavid left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

  1. 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):
      • newRatio sets r.counts only inside if all.counted > 0, and all is the forwarded body. Here all.counted is 0, so r.counts stays false.
      • rangeMedia for sizeBefore reads the client body and sets nt.counted = 3000, nt.ctok = 1000 (compute.go:519-523).
      • opaqueOf (body.go:305-316) ignores nt.counted and nt.ctok when r.counts is false. It prices 3,000 bytes at 1 token for 250 bytes = 12.
      • The conservation check (compute.go, ratio.upTo(c.client, ...)) also uses r.counts == false, so it agrees with the 12 and does not fail.
    • The same cause makes Size.OpaqueTokens do nothing when the host builds the forwarded body with NewBody, or when the forwarded body has no counted block. design.md §4.2 says: "A size takes its count from Size.OpaqueTokens when 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/5m keeps a second counted block (message 3) in the forwarded body. TestSizeOpaqueTokensWins builds a ratio by hand with counts: true, so it never goes through newRatio.
    • Fix: use the counts unless the guard drops them: r.counts = all.ctok+(opaque-all.counted)/opaqueBytesPerToken <= room when all.counted > 0, else r.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.
  2. internal/engine/opaque_count_test.go:141 — no test runs Size.OpaqueTokens through Compute

    • What fails: the new branch in rangeMedia (compute.go:530-532) has no test that reaches it. TestSizeOpaqueTokensWins calls sizeOf with a hand-made nonText and a hand-made ratio. It does not call rangeMedia or Compute. Finding 1 shows a defect on this path that the test does not see.
    • Proof: grep -n "OpaqueTokens" test/gen/gen_cases.py finds only PosHash fields (opaqueTokens in c77_body). No case sets sizeBefore.opaqueTokens or sizeAfter.opaqueTokens. The one Go test that builds Size{... OpaqueTokens ...} tests only invalid().
    • Fix: add a generated case (or a Compute unit test) with sizeBefore: {bytes, opaqueBytes, opaqueTokens}. Run it once with a counted block left in the forwarded body, and once with none.

Non-blocking

  • fingerprint.go:771: NewBodyCounted treats a negative lookup result as "no count" (fixed price). design.md §4.2 says "a negative count is 0". That sentence is true for PosHash.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.OpaqueTokens 0 means "no count", but a PosHash count 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.OpaqueTokens now makes the size invalid (the operation goes to Unpriced). 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 of docs/design.md, and R8 of docs/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) against newRatio and rangeMedia. 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, and api_test.go passes. CheckOpaqueTokens is in TestEveryCheckHasAFatalClass.
  • Checked Snapshot: wirePos keeps both counts, with omitempty, so old snapshots still load.
  • Checked the PosHash size in the doc: 8 + 6 x 4 = 32 bytes.
  • Ran gofmt -l . (no output), go vet ./... (clean) and go test ./... (all 3 packages pass). I did not run the live script.

@boazdavid boazdavid left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

  1. 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: newRatio sets r.counts only inside if all.counted > 0, and all is the forwarded body. opaqueOf (body.go:305-316) uses nt.ctok only when r.counts is 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.OpaqueTokens is ignored in the same way. No check fails, so the host does not see the error.
    • Proof (trace): client = m0 4,000 B, th1 400 B text + 3,000 B signature (counted at 1,000), m2 2,000 B. Forwarded = m0, th1f 400 B, m2. Bill 1,600.
      1. newRatio: all.counted = 0, so r.counts = false and r.countsDropped = false.
      2. rangeMedia for the "before" size (3,400 B) gives nt{opaque 3000, counted 3000, ctok 1000}.
      3. sizeOf = of(400) + opaqueOf = 100 + 3,000/250 = 112. After = 100. Saved = 12, not 1,000.
      4. Check 6 also passes: upTo(client) uses the same fixed price (1,612 - 1,600 = 12). Check 14 does not run, because countsDropped is false.
      5. 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-173 says that Size.OpaqueTokens sets 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 = true unless 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.py where the operator drops the only counted block.
  2. internal/engine/compute.go:480 — Size.OpaqueTokens and the share by proportion in rangeMedia have no test through Compute.
    • What fails: nothing proves the new branches of rangeMedia (compute.go:455-487). A wrong share or a wrong Size.OpaqueTokens override would pass every test.
    • Proof: grep -rln "OpaqueTokens\|rangeMedia" over the tests finds only opaque_count_test.go. TestSizeOpaqueTokensWins calls ratio.sizeOf with a nonText that it builds by hand, so it does not call rangeMedia. 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.

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, room is 0. Then any counted block with a count above 0 sets countsDropped, and check 14 fails beside media_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 when r.capped is true, or say in §6.5 check 14 that the two can fail together.
  • rangeMedia splits 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.py sends the key in two headers (x-api-key and authorization). This is fine for a proxy. A urllib.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 read test/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 to tokens, the remainder loop runs fewer than len(opaques) times, and the empty-strings path does not divide by 0.
  • R3: no new import. accountant.go exports NewBodyCounted, OpaqueKey, SplitOpaqueTokens, OpaqueCount and CheckOpaqueTokens. TestEveryCheckHasAFatalClass lists 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]>
@boazdavid

Copy link
Copy Markdown
Contributor Author

Replies to round 1 (both reviewers)

All are fixed in e367522, except the 2 notes at the end.

Finding 1 (both reviewers): the counts were ignored when the forwarded body held none. Fixed.

  • newRatio now uses the counts unless the forwarded body proves them wrong. A forwarded body with no counted block proves nothing, so the client body's counts apply.
  • The fixed price of the uncounted bytes is now cut to the room that the counts leave. TestOpaquePriceIsCutToTheBill found this, because the old cut ran only with the counts off.
  • New case opaque/counted-only-block-dropped/5m: the trace of reviewer 1. The drop saves 1,000, not 12.
  • D16, rule 7 and check 14 now say that the guard can act only when the forwarded body holds a counted block.

Finding 2 (both reviewers): no test of Size.OpaqueTokens or the share through Compute. Fixed with 2 cases.

  • opaque/counted-size-opaque-tokens/5m: one position holds 2 blocks (1,000 and 500 tokens). The size of the dropped block sets OpaqueTokens 1,000, and the drop saves 1,000.
  • opaque/counted-size-share/5m: the same drop with no OpaqueTokens, and equal counts (750 each). The share gives 750.
  • I first tried unequal counts and an expected conservation failure. Check 6 does not run on a request in phase unknown, so the case used equal counts. The limit for unequal counts is now in rule 7 and Q16.
  • opaque/counted-only-block-dropped/5m is the case with no counted block in the forwarded body (reviewer 2).

Non-blocking points.

  • newRatio comment, "counts alone": fixed. It now says the counts plus the fixed price of the uncounted bytes.
  • Check 14 beside media_tokens: fixed. Check 14 is not reported when the media is capped. §6.5 says why.
  • The share mixes the counts of the blocks in a range: added to rule 7 and Q16.
  • A negative lookup result: rule 7, D16 and the NewBodyCounted comment now say that it keeps the fixed price.
  • Size.OpaqueTokens 0 cannot say "0 tokens": documented in D16. A host that knows a model bills 0 passes no count.
  • The ping extent can drop the counts: documented in D16. The fallback is the D15 price.
  • A negative Size.OpaqueTokens: added to the malformed-operation row (negative_size).
  • The script now retries a URLError.

Not changed.

  • The script sends the key in 2 headers, so that one script works against the API directly and through a proxy. Reviewer 1 marked this as fine.

Assisted-By: Claude (Anthropic AI) [email protected]

@boazdavid boazdavid left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 sets r.counts = true and turns it off only when the forwarded body holds a counted block and the guard fails. Trace of my round-1 example: forwarded all.counted = 0, so r.counts = true. rangeMedia gives nt{opaque 3000, counted 3000, ctok 1000}. opaqueOf returns 1,000. The drop saves 1,000. Case opaque/counted-only-block-dropped/5m covers 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 Compute test of Size.OpaqueTokens): fixed. opaque/counted-size-opaque-tokens/5m goes through rangeMedia with opaqueTokens 1,000. opaque/counted-only-block-dropped/5m covers 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/5m runs 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 newRatio comment, check 14 skipped when capped, the share limit in rule 7 and Q16, the negative lookup (D16, rule 7, NewBodyCounted comment), Size.OpaqueTokens 0 and the ping extent in D16, the malformed-operation row, and the URLError retry. 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 to left runs only when the forwarded body has no counted block (then left = 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 (e367522 fix: 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..e367522 except test/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 of newRatio and opaqueOf, and rangeMedia and checks around 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 (the Size.OpaqueTokens override); 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: capped implies room = 0, and media_tokens then names the cause. When counts are dropped and capped is false, check 14 is still reported. So the skip hides no strip case that the media does not already explain.
  • Checked that r.counts is read only in opaqueOf, so the new default changes no other path.
  • The 3 new JSON files exist, and go test ./test/cases passes with them. I did not run go generate, because it writes files.
  • Ran gofmt -l . (no output), go vet ./... (clean) and go test ./... (3 packages pass). I did not run the live script.

@boazdavid boazdavid left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 sets r.counts = true and 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. Case opaque/counted-only-block-dropped/5m holds 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.OpaqueTokens and the share through Compute): fixed. The new cases reach both branches of rangeMedia, 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.
  • 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 newRatio comment;
    • check 14 when the media is capped;
    • the share limit in rule 7 and Q16;
    • a negative lookup in rule 7, D16 and the NewBodyCounted comment;
    • Size.OpaqueTokens 0 in D16;
    • the ping extent in D16;
    • a negative Size.OpaqueTokens in the malformed-operation row;
    • the URLError retry.
  • The author kept the two key headers in the script. I agree. I marked it as fine in round 1.

Findings

  1. internal/engine/compute.go:1992 — The new rule "check 14 is not reported when the media is capped" has no test.
    • What fails: e367522 adds !c.ratio.capped to the condition of check 14. This changes Result.Checks for 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/*.json finds 5 cases. None of them holds media.
      • The only test that uses capped near opaque bytes is TestOpaquePriceIsCutToTheBill (opaque_test.go:59). It calls newRatio and not checks, and its body has no counts.
      • Trace: capped is true only when room is 0. Then any counted block with a count above 0 sets countsDropped. So a body with media, a counted block and a bill below the media price now gives media_tokens and no opaque_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 no opaque_tokens.

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 (e367522 fix: 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 except test/cases/*.json, and I read c77_more in gen_cases.py in its place. I read the code around each change: newRatio, opaqueOf, rangeMedia, checks, conservation, and the PhaseUnknown path that sets c.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. conservation returns when c.unknown is true (compute.go:1953), and the PhaseUnknown case sets it (compute.go:1037).
  • 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, rangeMedia and docs/issue_cases.md §9j.
  • I checked that left in newRatio can never be negative. When the counts are used and the forwarded body holds a counted block, the guard ensures ctok + fixed/250 <= room.
  • I checked that capped implies room = 0. The opaque price is at most room, so a room above 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]>
@boazdavid

Copy link
Copy Markdown
Contributor Author

Replies to round 2

Fixed in a4f71ad.

Reviewer 1, finding 1: check 14 under a capped media bill had no test. Fixed.

  • New case opaque/counted-thinking-media-capped/5m. It has 4,000 text bytes, an image of 2,000,000 bytes and a counted thinking block, with a bill of 2,350 tokens, below the media price.
  • It expects media_tokens and no opaque_tokens. The cut of 2,000 text bytes saves 100 tokens.
  • Proof that it guards the rule: with !c.ratio.capped removed from the condition, this case fails. With it, all tests pass.

Non-blocking (both reviewers).

  • The newRatio comment: the sentence about the forwarded body now comes after "(check 14)", and the lines are wrapped.
  • The cut to left: the comment now says that it acts only when the forwarded body holds no count, or when the counts are dropped.
  • The trust rule on a model that strips thinking: no change. D16 states the cost, and both reviewers accept it.

Assisted-By: Claude (Anthropic AI) [email protected]

@boazdavid boazdavid left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 of newRatio for this case:
    1. Text = 4,000 bytes, so the floor is 200. The media price is 2,000,000 / 250 = 8,000. The bill is 2,350.
    2. room = max(2,350 - 8,000 - 200, 0) = 0. The forwarded body holds 1,000 counted tokens, and 1,000 > 0, so countsDropped = true.
    3. The fixed price is 3,000 / 250 = 12 > left 0, so the opaque price is cut to 0.
    4. r.tokens = 2,350 - 8,000 < 0, so capped = 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.
    5. The rewrite cuts m0 from 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-231 compares the full sorted list of failed checks. If someone removes !c.ratio.capped, the step also reports opaque_tokens. Then the list is media_tokens,opaque_tokens, and the case fails. This agrees with the author's reply.
  • Round-2 non-blocking points (both reviewers): fixed.
    • The newRatio comment now reads in order and is wrapped (body.go:250-258).
    • The comment on the cut to left now says when the cut acts (body.go:279-282).
  • 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 (a4f71ad test: 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 except test/cases/*.json: docs/issue_cases.md, internal/engine/body.go and test/gen/gen_cases.py (c77_capped). I read all of newRatio around 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 a4f71ad fixes both of them.
  • 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 boazdavid left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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/5m covers it. My trace is below.
  • Round-2 non-blocking points (both reviewers):
    • The newRatio comment 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 and all.counted > 0, the guard gives ctok + fixed/250 <= room, so fixed/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.

Non-blocking

  • None.

What I checked

  • Last review: round 2, e367522, 2026-11-17T06:45:17Z
  • New commits read: 1 (a4f71ad test: 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 except test/cases/*.json: docs/issue_cases.md, internal/engine/body.go (comments only), and c77_capped in test/gen/gen_cases.py. I read all of newRatio again after the change.
  • Traced opaque/counted-thinking-media-capped/5m by 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, so countsDropped is true.
    • The fixed price is 12 tokens and left is 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 m0 from 4,000 to 2,000 bytes. It covers no media, so mediaUnpriced is false. Saved = 2,000 x 0.05 = 100. The case expects cacheWrite5m=100 and checks=["media_tokens"].
    • Without !c.ratio.capped, check 14 would also run: countsDropped is true, there is usage, and c.invalid is "". 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.json exists, and go test ./test/cases passes. I did not run go generate, because it writes files.
  • Ran gofmt -l . (no output), go vet ./... (clean) and go test ./... (3 packages pass). I did not run the live script.

@boazdavid
boazdavid merged commit 7791ac7 into main Nov 17, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Give each thinking block its own token count from the host

1 participant