Skip to content

Package preview: exact payload bytes and a labelled input-token range - #31

Merged
razbenaharon merged 2 commits into
mainfrom
feature/token-estimate
Nov 21, 2026
Merged

razbenaharon merged 2 commits into
mainfrom
feature/token-estimate

Conversation

@razbenaharon

@razbenaharon razbenaharon commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

What

The package preview (review_package, modelquad review package) now says how big the payload is and, when the local record allows, roughly how many input tokens it will cost. Until now it said ModelQuad does not estimate tokens.

  • Exact size before a run. payload_bytes is the byte length of the rendering the run sends: the task text, ModelQuad's instruction, the files as JSON with their line-number prefixes, and the completion-envelope instruction. noshell.render_supplied records it in its report. The executor's file_audit carries it as a bounded integer (closed_file_audit), and it is kept in the package manifest in the ledger. The package id does not change.
  • Input-token range, labelled as an estimate. review_ledger.input_token_estimate() fits a straight line through the last 50 successful reviews in the ledger: task-text bytes plus file bytes against the input tokens Codex reported. It widens the line by the largest relative miss among those reviews and rounds outward to the thousand. It returns unknown, with the reason, when there are fewer than 20 such reviews, when the package falls outside the sizes they cover, or when the ledger cannot be read. It opens the ledger read-only (mode=ro) and never creates it. The range is removed before run() records the package, so the ledger never stores it. No price is estimated.
  • After a run. usage gains payload_bytes and uncached_input_tokens (input minus cached, only when Codex reported both). The reply lines add "the whole payload was N bytes" and "Of the input tokens, C came from Codex's cache and U did not."

Why this is honest (invariant 4)

The estimate is computed only from runs recorded on this computer, and it is never called a count. Evidence from the installed ledger on the maintainer's machine, read-only: 18 answered runs have both a package and token counts. A least-squares line gives about 17,700 tokens plus 0.33 tokens per byte, and no run is off the line by more than 4.3%. A near-empty package still costs about 18,300 input tokens: that part is Codex's own instructions, which ModelQuad cannot see. Older runs with no files, from earlier Codex releases, cost about 19,980, so that fixed part moves with the release. That is why the range uses only the latest 50 runs and needs at least 20 before it shows. With 18 runs, that machine reads "unknown" today. Cached tokens (0 / 7,296 / 15,744 seen) depend on timing and are not estimated.

The owner chose this approach (exact bytes plus a range from the record) over bytes alone.

Update after review (24ec29f)

  • Only the preview computes the estimate (package(..., estimate=True) from review_package and modelquad review package). The run that starts Codex skips it and the ledger read it needs; payload_bytes and Codex's reported counts are unchanged.
  • The ledger is opened with no busy wait (timeout=0): a ledger another process holds locked now reads as unknown (ledger_busy) at once, where it used to wait about five seconds.
  • Regression tests: the run never calls input_token_estimate and still reports the bytes and the counts; with the ledger held by BEGIN EXCLUSIVE, the preview returns its bytes in under a second with the estimate unknown.
  • check.py review PASS; check.py python PASS (3,276 passed, 39 skipped, 1 known xfail; serial timing 2 passed).

Checks

  • python scripts/check.py review: PASS
  • python scripts/check.py python: PASS (3,274 passed, 39 skipped, 1 known xfail; serial timing 2 passed)
  • New tests in tests/test_review_flow.py: exact payload size; unknown with no record (and the ledger file is not created); 19 vs 20 runs; range around the line, rounding, and width from the largest miss; outside the recorded sizes; failed runs and runs without counts are ignored; only the latest 50 runs count; an unreadable ledger is left untouched; the range is not in the reply or the record of a run; reply lines for the cached and uncached split; uncached stays unknown unless both counts are present. tests/test_attached_file_rules.py: the audit keeps payload_bytes only as a bounded integer.

Docs: README (not a cost estimator), SECURITY (where the range comes from and its limits), PRIVACY (the range is computed locally from the ledger; the manifest keeps the payload size), AGENTS.md current state, CHANGELOG Unreleased.

🤖 Generated with Claude Code

razbenaharon and others added 2 commits November 21, 2026 23:07
The preview gives the size of the whole payload as Codex gets it, measured
on the same rendering the run sends. Input tokens before a run are a range
from the last 50 successful reviews in the local ledger (a straight line,
widened by the largest miss among them), shown only from 20 such reviews
and within the sizes they cover; otherwise unknown, with the reason. The
ledger is opened read-only and the range is never recorded. After a run the
reply puts the bytes sent beside the input, cached and uncached tokens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… locked ledger

The run that starts Codex no longer computes the estimate or reads the
ledger for it; it still measures payload_bytes and reports Codex's own
counts after the run. The preview opens the ledger read-only with no busy
wait, so a ledger another process holds locked reads as unknown
(ledger_busy) at once instead of after about five seconds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@razbenaharon
razbenaharon marked this pull request as ready for review November 21, 2026 21:54
@razbenaharon
razbenaharon merged commit 7fe5e52 into main Nov 21, 2026
7 checks passed
@razbenaharon razbenaharon mentioned this pull request Nov 21, 2026
@razbenaharon
razbenaharon deleted the feature/token-estimate branch November 22, 2026 15:50
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.

1 participant