Skip to content

feat: add gpt-5 token counting - #12

Merged
rowan-stein merged 3 commits into
noa/issue-1from
noa/issue-11
Apr 21, 2026
Merged

rowan-stein merged 3 commits into
noa/issue-1from
noa/issue-11

Conversation

@casey-brooks

Copy link
Copy Markdown
Contributor

Summary

  • implement spec-compliant gpt-5 token counting for text, images, files, and overhead
  • add PDF text extraction plus image tile accounting
  • expand tokenizer coverage with unit tests and tighten server validation

Testing

  • go vet ./...
  • go test ./...

Refs #11

@casey-brooks

Copy link
Copy Markdown
Contributor Author

Test & Lint Summary

  • go vet ./...
  • go test ./...

Tests: passed 7, failed 0, skipped 0
Lint: go vet ./... (no issues)

@noa-lucent noa-lucent left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Implemented the core spec (structured parsing, o200k_base tokenization, image tiling, file/PDF handling, overhead) 👍

Requesting changes before merge due to a few correctness/maintainability blockers:

  • Tests: several expectations are computed via the same functions under test (geometry + PDF page image tokens), which won’t catch regressions. Please switch to fixed golden numbers for representative cases.
  • Tokenizer data loading: the BPE loader supports HTTP(S) downloads, which can introduce a runtime network dependency and non-determinism. Prefer bundling/embedding the ranks (or otherwise guaranteeing offline startup) per the “local counting” intent.
  • PDF text extraction: current custom content-stream scanner + newline joining is fragile and likely to diverge from real extraction behavior; please either use a supported extractor (if available) or add stronger golden coverage with real PDFs + fixed expected counts.

Minor notes also left inline (image detail validation strictness, pointer/value item handling).

Comment thread internal/tokenizer/tokenizer_test.go Outdated
Comment thread internal/tokenizer/tokenizer_test.go Outdated
Comment thread internal/tokenizer/parse.go Outdated
Comment thread internal/tokenizer/bpe_loader.go Outdated
Comment thread internal/tokenizer/pdf.go
Comment thread internal/tokenizer/count.go
@casey-brooks

Copy link
Copy Markdown
Contributor Author

Test & Lint Summary

  • go vet ./...
  • go test ./...

Tests: passed 11, failed 0, skipped 0
Lint: go vet ./... (no issues)

@noa-lucent noa-lucent left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-review: most of the prior feedback is addressed (golden numbers instead of code-under-test, strict image detail validation, pointer item handling, improved PDF whitespace handling + added tests). Thanks.

Remaining blocker:

  • The new local-only BPE loader rejects the HTTPS URL that tiktoken-go passes by default. Unless TOKEN_COUNTING_BPE_PATH/TOKEN_COUNTING_BPE_DIR is set to a real o200k_base.tiktoken at runtime (and the file is shipped), the service will error in CI/deployments. Please package/embed the full file and wire the env var in Dockerfile/Helm/devspace (and document it). The tiny test fixture is not sufficient for production accuracy.

Comment thread internal/tokenizer/bpe_loader.go
@casey-brooks

Copy link
Copy Markdown
Contributor Author

Test & Lint Summary

  • go vet ./...
  • go test ./...

Tests: passed 11, failed 0, skipped 0
Lint: go vet ./... (no issues)

@noa-lucent noa-lucent left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-review: BPE packaging issue is resolved.

  • Docker image now downloads and bundles the full o200k_base.tiktoken at build time and sets TOKEN_COUNTING_BPE_PATH by default.
  • Helm values and devspace pipelines also wire the env var / local encoding file.

LGTM.

@casey-brooks

Copy link
Copy Markdown
Contributor Author

BPE packaging fix is pushed (Dockerfile download + env wiring). Ready for re-review when you have a moment.

@rowan-stein
rowan-stein merged commit de589e5 into noa/issue-1 Apr 21, 2026
1 check 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.

3 participants