Part 5 of 20 in Tools we built and why. Previous: wavewait: seeing what a graphics card waits for · Next: mojo-port: checking that a rewrite does the same thing
What this is about
How big is this code? The usual answer is a count of lines. A line counter reads your files, sorts them by language, and tells you how many lines are code, how many are comments, and how many are blank. It is the quickest way to get a feel for a new project.
mokei is our line counter. It does what a well-known counter called tokei does, for 40 languages in version 0.1.0. Then it goes further. It can tell you how big a change is, how many tokens your code will cost in a prompt for an AI model, which blocks are copied from each other, and which function names no test mentions.
Why it matters
Line counts are only the first question. When you review a change, a better question is: is this change small enough to read in one sitting? When you hand code to an AI model, the question is: will this fit, and what will it cost? When you maintain code, the question is: where did we copy and paste?
A person can answer these by hand on a small project. On a large one, a tool answers them in a fraction of a second. We built mokei because tokei answers the first question very well and the others needed a second tool, or a script for each one.
How it works
Mokei reads a table called languages.toml. Each row says how one language marks comments, how it quotes strings, and which other languages it can hold inside itself, such as code fences inside Markdown. The scanner reads every file with its row. With no arguments inside a git repository, it reads the list of files git already tracks. Anywhere else it walks the folder and obeys .gitignore. The scan runs in parallel.
Mokei does not parse code the way a compiler does. It reads tokens by rules. That is fast, and it is why it can be wrong on odd constructs. The known cases are listed in a file called docs/known-diffs.md, each with a tiny sample file that shows the difference.
Two modes go beyond counting lines. --semantic counts statements, functions, nesting depth and a complexity score for each file in Python, Mojo, JavaScript, Rust and C. --tokens estimates how many tokens a model would read, with a byte-level model fitted to the Qwen2.5 tokenizer. If you pass a real tokenizer.json, it counts exactly.
A five-minute tour
The README shows this path.
- Clone the repository and run
pixi install. - Run
pixi run gate. It builds the tool, runs the tests, and runs a negative control, a check that must fail. It needs no GPU. - Run
.bin/mokei --versionto see it work. - Go to any project of your own and run
mokei. You get a table with files, lines, code, comments and blanks for each language. - Try
mokei --filesfor one row per file, and--jsonfor output a script can read. - Try
mokei --tokensto see tokens per language. - In a git repository, try
mokei --diff A..Bwith two commits or branches. It shows lines added and removed, per language. - Add
--budget Sto the diff. It exits with status 1 if the added code lines reach the limit. The limits follow our size labels: XS is 20 lines, S is 60, M is 150, and L is 400.
That last step is the useful one for teams. A size budget turns "this change feels big" into a number that a script can check.
How fast is it?
On one checkout with 11.5 thousand files, measured on 2026-11-07, mokei took 67.4 ms of wall time and tokei took 100.1 ms. That is 1.49 times as fast. In CPU time it was 351 ms against 989 ms. A later change for embedded languages added about 5%, bringing mokei to 70.9 ms. Treat those numbers as one machine and one checkout. They do not promise the same ratio on yours.
What it does not do
- It runs on Linux only.
- It has no parser. A construct the token rules do not model is miscounted.
- The untested-names check is a name match. A name that appears in any test file counts as tested, so it can say "tested" when it is not.
--tokensis an estimate unless you pass a tokenizer file.- Only
--sort,--filesand--jsonof tokei's flags exist. - Parity with tokei was last run on our corpora and has not been re-run since tokei was removed from our machine. The README says so.
Words you may not know
- Line of code: a line that holds code, not just a comment or a blank.
- Token: a small piece of text that an AI model reads and writes.
- Diff: the list of changes between two versions.
- Budget: a limit that fails a check when it is exceeded.
- Duplicate block: a stretch of code copied from somewhere else.
- Complexity: a score for how many paths a function can take.
- Oracle: a trusted program we compare our results with. Here it is tokei.
Try it
- Read the mokei project page.
- Clone github.com/amarbarosys/mokei and run the gate.
- Point mokei at the biggest repository you have. Compare its table with what you expected.
- Run a diff with
--budgeton your last pull request and see which size label it fits.
Read next
- Why we build our own tools: why a second line counter was worth writing.
- mojo-port: checking that a rewrite does the same thing: the next post in this series.

Comentarii
Niciun comentariu încă.
Conectare pentru a comenta.