Skip to content

✨ feat(mq-lang,mq-run): add token_count(text, model) builtin#2008

Open
harehare wants to merge 2 commits into
mainfrom
feat/token-count-builtin
Open

✨ feat(mq-lang,mq-run): add token_count(text, model) builtin#2008
harehare wants to merge 2 commits into
mainfrom
feat/token-count-builtin

Conversation

@harehare

Copy link
Copy Markdown
Owner

Summary

Add a native token_count(text, model) builtin for LLM context-window budgeting (e.g. select(token_count(.) < N)), pairing with the existing toon.mq module for minimizing token usage. Two-tier: a dependency-free chars-per-token heuristic (adjusted by detected script — CJK tokenizes much denser than Latin) ships by default and costs nothing in binary size, including for mq-wasm. Exact counting via tiktoken-rs is opt-in behind a new tiktoken Cargo feature (mq-lang and, forwarded, mq-run), since resolving model at runtime makes every vendored encoding reachable and unremovable by dead-code elimination.

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • ♻️ Refactor
  • 📝 Documentation
  • ⚡ Performance
  • ✅ Test
  • 📦 Build / dependencies
  • 👷 CI

Checklist

  • I ran cargo fmt and cargo clippy and addressed any warnings
  • I ran just test-all and all tests pass
  • I added or updated tests covering this change
  • I updated relevant documentation (/docs, crate README.md) if needed
  • I added a changelog entry if this is a user-facing change

Additional Context

harehare added 2 commits July 13, 2026 21:25
Add a native token_count(text, model) builtin for LLM context-window
budgeting (e.g. select(token_count(.) < N)), pairing with the existing
toon.mq module for minimizing token usage. Two-tier: a dependency-free
chars-per-token heuristic (adjusted by detected script — CJK tokenizes
much denser than Latin) ships by default and costs nothing in binary
size, including for mq-wasm. Exact counting via tiktoken-rs is opt-in
behind a new `tiktoken` Cargo feature (mq-lang and, forwarded, mq-run),
since resolving `model` at runtime makes every vendored encoding
reachable and unremovable by dead-code elimination.
Support token_count(text) alongside token_count(text, model), so
callers who only want the heuristic estimate aren't forced to name a
model just to satisfy arity. Since builtin dispatch resolves pipe
injection purely by argument count, this collides with the previous
text | token_count(model) idiom (1 explicit arg); that form is
replaced by explicit two-arg calls, e.g. token_count(md, "gpt-4").
@codspeed-hq

codspeed-hq Bot commented Jul 13, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing feat/token-count-builtin (8f901b7) with main (7176a29)

Open in CodSpeed

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