Why 21 characters, and why the alphabet has exactly 64
The standard Nano ID is 21 characters from a 64-character alphabet. Both numbers are chosen on purpose, and you can check the arithmetic behind them in a few minutes. It also tells you what happens when you change either one.
Start with the alphabet. A computer’s random source gives bytes, and a byte has 256 possible values. To turn a byte into a character, you divide it by the alphabet size and keep the remainder. With 64 characters this is perfect: 256 is exactly 4 × 64, so byte values 0, 64, 128 and 192 all map to the first character, and every character gets exactly four byte values. Each one has the same 1 in 64 chance, and no byte is ever wasted. A 64-character alphabet is also the largest you can build from letters, digits and the two symbols, - and _, that need no escaping in a URL.
Now try 62 characters, letters and digits only, which many people prefer because - and _ look odd in an ID. 256 divided by 62 is 4 with 8 left over. If you simply used the remainder, the byte values 248 to 255 would land on the first 8 characters, giving each of them 5 chances in 256 while the rest get 4. Those 8 characters would turn up 25% more often than the others, which quietly makes the IDs easier to guess. This generator avoids that by throwing away any byte of 248 or more and drawing again. That wastes 8 bytes in every 256, about 3%, and keeps every character exactly equally likely.
Next, the length. Each character from 64 choices carries log2(64) = 6 bits, so 21 characters carry 126 bits. With 62 characters each one carries about 5.95 bits, and 21 of them give about 125. The question that matters is how many IDs you can make before two match by chance. Because of the birthday effect, the answer is roughly the square root of the number of possible IDs, not the number itself. For 126 bits, the count at which there is a 1% chance of a single collision anywhere in your data is about 1.3 × 10^18. Making 1,000 IDs every second, you would reach it in about 41 million years.
That is why the page shows the collision line instead of asking you to trust a default. If you shorten the ID to 8 characters, you have 48 bits, and the 1% point drops to about 2.4 million IDs, which a busy table can reach. Switch the alphabet to digits only and keep 21 characters, and you have about 70 bits, with the 1% point near 4.5 billion. Pick the length from the most IDs you will ever hold, with a wide margin.
The limits are worth knowing. The estimate assumes a good random source, which crypto.getRandomValues is in every current browser. It does not make an ID a secret: once an ID appears in a URL or a log, anyone who sees it has it. And Nano IDs carry no time or order, so if you need IDs that sort by creation time, a version 7 UUID is the better fit.