# bytewise

**See text the way a language model does: as bytes, as tokens, or as patches.**

Most language models never see letters. A tokenizer cuts text into pieces first, and the model only gets the
numbers of the pieces. That is why models miscount the r's in "strawberry", stumble when spelling a word backwards,
and spend more on Greek or Korean than on English. On 7 October 2026 a paper in *Nature* showed that an existing
model can be *byteified*, converted to read raw bytes, for under 1 % of its original training cost. `bytewise` is a
small toolkit for looking at the problem yourself.

One file, zero dependencies, Node 18+ and the browser. MIT licence.

```
npm test                                   # 13 tests
node bin/bytewise.js view "Byteification sees every letter"
node bin/bytewise.js hidden strawberry r
node bin/bytewise.js languages
node bin/bytewise.js demo
```

## What it does

- **Bytes.** `utf8()`, `profile()`: characters, bytes and bytes per character, per script (ASCII 1 byte, Greek 2,
  Han 3, emoji 4).
- **Tokens.** `train(corpus, { vocab })` trains a byte-level byte-pair-encoding tokenizer, the kind most large models
  use: start from 256 bytes, join the most frequent neighbouring pair, repeat. `encode()` gives each token's bytes,
  text and character span (a token that splits a character has no text); `decode()` is exact for any input.
- **Hidden letters.** `hidden(tok, text, letter)`: every place the letter appears and whether it sits inside a longer
  token, where a subword model cannot see it.
- **Character tasks.** `tasks({ n, seed, words })` makes seeded questions with exact answers (count a letter,
  reverse, nth letter, spell with hyphens, first position); `grade(tasks, replies)` scores a model's replies and
  forgives format ("three", quotes, spaced letters). `tokenGuess()` answers as a reader that sees only whole tokens
  would, a lower bound, not a real model.
- **Patches.** `ngram(corpus, { order })` counts which byte follows each run of up to 3 bytes; `patches()` starts a new
  patch where the next byte's entropy jumps (or exceeds a threshold), the rule entropy-patching byte models use.
- **Lengths.** `compare(text, tok, model)`: characters, bytes, tokens, patches, and the relative cost of attention
  (which grows with the square of the length).

## The same sentence in nine languages

With the tokenizer and byte model trained on the English text in `data/corpus.txt` (`node examples/languages.js`):

| Language | Bytes | Tokens | vs English |
|---|---|---|---|
| English | 170 | 75 | 1.0x |
| Spanish | 173 | 93 | 1.2x |
| German | 166 | 106 | 1.4x |
| Greek | 359 | 359 | 4.8x |
| Russian | 293 | 293 | 3.9x |
| Arabic | 212 | 212 | 2.8x |
| Chinese | 129 | 129 | 1.7x |
| Japanese | 255 | 255 | 3.4x |
| Korean | 219 | 219 | 2.9x |

A tokenizer that never saw a script falls back on bytes. Large tokenizers see far more languages, but the same
pattern holds in a milder form for languages that were rare in their training data.

## Use it

```js
const B = require("bytewise");               // or <script src="bytewise.js"> -> window.Bytewise

const tok = B.train(myText, { vocab: 768 });
B.encode(tok, "strawberry").map(t => t.text);  // [ 'st', 'rawber', 'ry' ] with the bundled corpus
B.hidden(tok, "strawberry", "r");              // { count: 3, hidden: 3, ... }

const tasks = B.tasks({ n: 20, seed: 1 });     // [{ id, kind, q, a }]
B.grade(tasks, repliesFromYourModel);          // { accuracy, byKind, rows }

const model = B.ngram(myText, { order: 3 });
B.patches(model, "a language model reads the text").patches.map(p => p.text);
B.compare(text, tok, model);                   // { bytes, tokens, patches, vsTokens: { bytes: { length, attention } } }
```

## Command line

```
bytewise view [text] [--vocab 768] [--corpus file] [--tokenizer tok.json]
bytewise hidden [word] [letter]
bytewise languages
bytewise tasks [--n 20] [--seed 1] [--words a,b,c] [--out tasks.json]
bytewise grade tasks.json answers.json        exit code 3 when any answer is wrong
bytewise train corpus.txt [--vocab 768] [--out tokenizer.json]
bytewise demo
```

## Limits

- The bundled corpus is about 6 KB of English, so the tokenizer is small (about 750 tokens) and English text comes
  out at about 2.3 bytes a token; the large tokenizers in production models reach about 4. Train on your own text
  for realistic numbers.
- The patcher is a 3-byte counting model, not the learned boundary predictor of a real byte-level model. It shows the
  shape of entropy patching, not what a production model would choose.
- `tokenGuess()` is a strict lower bound. Real subword models learn a good deal about the spelling of their tokens.
  Published results put a byteified 7B model at 78.6 on the CUTE character benchmark against 56.9 for the model
  it came from.

## Examples

`examples/hidden-letters.js`, `examples/languages.js`, `examples/grade-a-model.js`.
