Can gzip be a language model?
TL;DR Highlight
Scoring candidates by gzip length in a beam search yields Shakespeare-ish text
Who Should Read
A backend developer who uses LLMs but finds the compression-equals-prediction idea unfamiliar while building text generation or classification logic
Core Mechanics
- A compressor hides a probability model
- It spends few bytes on data it expects and many on data it doesn't.
- Bits needed for a symbol is -log2 p, where p is the probability the model assigns.
- gzip length is the score
- DEFLATE turns repeats within a 32 KiB sliding window into cheap back-references.
- Score is len(gzip(context + candidate)); smaller means more predicted.
- Putting a corpus in the window makes corpus-like continuations compress small.
- Greedy byte picking fails
- gzip only gives integer byte lengths, so adding one byte often leaves the length unchanged.
- Many candidates tie and the signal drowns in quantization noise.
- Beam search fixes it by looking ahead
- Extend each candidate by every byte in the corpus and keep the beam_width most compressible.
- Repeat for horizon bytes, then commit the best span.
- With positive temperature, sample among the finalists.
- Only a recent tail stays in context
- DEFLATE codes nearby matches more cheaply than far ones.
- With full history, generation tends to fall into verbatim copy loops.
- Output is rough but Shakespeare-flavored
- Primed on tiny Shakespeare, the text isn't coherent but clearly reflects the corpus.
- It's one pure standard-library Python file using zlib rather than spawning gzip.
Community Reactions
- Swapping in bzip2 and zstd gave gibberish, per a commenter
- They had MiMo-V2.6-Flash fork the code; bzip2 produced alternating-symbol runs like yxyxyx.
- zstd produced mostly whitespace, since repeated-byte runs are nearly free.
- Skeptics say the search quality is unknown
- The search space is far larger than what beam search covers, so results are only a lower bound on gzip as a plausibility test.
- They note the post doesn't discuss how close beam search gets to the global optimum.
- Commenters pointed to existing gzip uses
- One described classifying text by appending a test file to topic documents and picking the smallest gzip -9 output.
- Others linked an earlier gzip-for-ML paper (arxiv 2212.09410) and a tsoding implementation video.
- Some pushed back on overclaiming
- One opinion says n-gram-style models have historically been oversold as close to large neural nets.
- Others disliked that the article seems AI-written and uses zlib rather than actual gzip.
How to Apply
- Try gzip -9 topic classification
- Today, prepare 2-3 topic sample documents and the file to classify.
- Append the test file to each, run gzip -9, and pick the topic with the smallest output.
- Keep the topic documents similar in size.
- Build a zlib candidate-scoring function
- Compute len(zlib.compress(context + candidate)) for each continuation.
- If many scores tie, compare multi-byte spans instead of single bytes.
- Clone gzipt and swap in your corpus
- Clone github.com/nathanrs/gzipt and pass your own domain text via --corpus.
- Vary beam_width and horizon and compare outputs.
Try It
# Run in a Python shell (simplified version of the article's method: score candidates by zlib length)
import zlib
corpus = open("{{corpus.txt}}", "rb").read()[-30000:]
prompt = b"{{prompt}}"
cands = [b"{{candidate1}}", b"{{candidate2}}", b"{{candidate3}}"]
# Shorter means the corpus 'expected' that continuation more
for c in cands:
print(len(zlib.compress(corpus + prompt + c, 9)), c)
# Next: extend to multi-byte spans and keep only the top few, i.e. a small beam searchTerminology
Related Papers
Gemini 4 Neon is the new #1 on the Models Engineering Index
Gemini 4 Neon ranks first on the Models Engineering Index with 68.0, combining multiple benchmarks.
LLMs can read and write executables byte by byte from a hex dump
Given only a hex dump and no tools, an LLM analyzed, patched and wrote working executables.
Claude was trained on protein sequences at scale
Next-amino-acid tests on obscure proteins show only the latest Claude models predict them
Remembered as Brief: Stored Preferences SilentlyRemove Decision-Critical Information
A stored 'keep it short' preference silently drops warnings; listing must-know points first restores content.
All AI-generated code should be Open Source
A blog argues AI-generated code should be open source; most HN commenters disagreed.
What thousands of conversations look like inside a language model
Mapping Qwen3-1.7B states showed Chinese sitting apart from English, while German and Croatian overlapped it.
AI, activity, and outcomes in software engineering
AI sped up writing code, but shipping speed did not rise as much, says a talk
LLMs Play Zachtronics' TIS-100
16 LLMs played TIS-100 and set 133 new Pareto frontier points across 18 puzzles
Related Resources
- https://nathan.rs/posts/gzip-lm/
- https://github.com/nathanrs/gzipt
- https://arxiv.org/abs/2309.10668
- https://arxiv.org/abs/2212.09410
- https://bellard.org/ts_zip/
- https://arxiv.org/abs/2506.01084
- https://www.youtube.com/watch?v=l6DKRf-fAAM
- https://www.youtube.com/watch?v=9n39SbRPXKQ
- https://jakobs.dev/solving-mnist-with-gzip/