fix(codex): restore o200k token estimates - #90
Merged
Conversation
Owner
|
Seems to make sense. Thanks again |
bp0lr
pushed a commit
to bp0lr/claude-code-proxy
that referenced
this pull request
Aug 29, 2026
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
o200k_basetext tokenization for Codex/v1/messages/count_tokensProblem
The Rust implementation currently treats each uninterrupted run of letters, digits,
_, or-as one token. Long identifiers, CJK text without whitespace, and base64-like text can therefore collapse to a single estimated token. The endpoint then severely under-reports request size.This is also a regression from the proxy's original TypeScript implementation, which encoded text with
gpt-tokenizer/model/gpt-4o. The current documentation continued to describe the Codex path aso200k_base-backed even though the Rust implementation had become a word-run heuristic.Measured end to end against Codex terminal
usagefor the same translated requests:/count_tokensWith this change:
/count_tokensThe remaining six-token offset is the proxy's protocol-framing estimate.
/count_tokensremains a local preflight estimate, not an upstream billing total.Implementation
tiktoken-rswith default features disabledo200k_basetokenizerWhy this is a bug
o200k_baseestimate for the Codex path.POST /v1/messages/count_tokensas a preflight estimate over the message input, including structured content such as tools and images, and notes that the estimate can differ slightly from final usage in its token-counting guide.gpt-5family too200k_base. The OpenAI Cookbook token-counting example also liststiktoken-rsamong Rust implementations.Validation
cargo fmt --all -- --checkcargo check --all-targetscargo test --all-targetscargo clippy --all-targets -- -D warnings/count_tokenscalls and matching terminal Codex requests across all fixtures above/count_tokensremains local and does not make an upstream request