Lesson 8.6 · 6 of 9 · 6 min read · Reviewed 24 Nov 2026
Vision-language models and patch embeddings
By the end, you can
- Explain how a vision encoder turns an image into patch embeddings.
- Describe how a vision-language model feeds those embeddings to a language model.
- Estimate how image size drives token count and cost.
Lesson 8.1 said vision models “split an image into patches”. This lesson opens that box: what a patch embeddingEmbedding: A list of numbers (a vector) that represents the meaning of a piece of text, so similar meanings land close together. is, how it reaches the language model, and why it decides your tokenToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. bill.
Images as a sequence of patches
A transformerTransformer: The neural network architecture behind modern LLMs, built from stacked attention and feed-forward layers. reads a sequence of vectors. Text already is one. An image is a grid of pixels, so the visionVision: A model’s ability to read and reason about images, such as screenshots, charts and photos. transformer (ViT) turns it into a sequence:
224 x 224 image
-> cut into 16 x 16 pixel patches (14 across x 14 down = 196 patches)
-> flatten each patch (16 x 16 x 3 colours = 768 numbers)
-> multiply by a learned matrix (one patch embedding per patch)
-> add a position embedding (so the model knows where each patch sat)
-> transformer layers (patches attend to each other)
Each output vector is a patch embedding: a summary of that small square, informed by the rest of the image. The ViT paper’s title says it well: an image is worth 16x16 words.
From vision encoder to language model
A vision-language model (VLM) joins two pretrained parts with a small bridge:
| Part | Job | Often starts as |
|---|---|---|
| Vision encoder | Pixels to patch embeddings | A ViT, often trained CLIP-style on image and caption pairs (4.7) |
| Projector | Maps patch embeddings into the language model’s token space | A small learned layer or two |
| Language model | Reads image tokens and text tokens together, writes text | A pretrained LLM |
The LLaVA paper is a well-known open example: a CLIP vision encoder, a projector and an open-weight LLMLarge language model (LLM): A neural network trained on large amounts of text to predict the next token, which lets it generate and transform language., then instruction tuningInstruction tuning: Training a pretrained model on examples of instructions and good responses so it follows requests. on image conversations. Some other designs feed the image in through extra cross-attention layers instead, but the principle is the same: the language model only ever sees vectors, never pixels.
Why size decides cost
Patches grow with area. Double the width and height and you get four times the patches.
| Image | Patches at 16 px | Relative cost |
|---|---|---|
| 224 x 224 | 196 | 1x |
| 448 x 448 | 784 | 4x |
| 896 x 896 | 3,136 | 16x |
Real systems add details: they resize very large images down, tile them, or merge neighbouring patches. Providers publish their own rule of thumb. Claude’s vision docs, for example, estimate tokens as about width times height divided by 750. Either way, the lesson holds: resize to the smallest size that keeps the detail you need.
A worked example
A receipt photo from a phone is 3,000 by 4,000 pixels. The text is legible at 1,000 by 1,333.
- At full size, the provider will downscale it anyway, after you have paid to upload it.
- At 1,000 by 1,333 you send about 9x fewer pixels, and the patches still cover every line of text.
- For a 20-character serial number in one corner, crop to that corner instead. Fewer patches, each with more detail.
The same trick for audio
Speech-to-text models such as Whisper (8.2) treat sound the same way. The audio becomes a log-mel spectrogram, a picture of which frequencies are loud at each moment. An encoder turns short frames of it into vectors, and a decoder writes the transcript one text token at a time. Whisper works on 30-second windows, so long recordings are cut into pieces and stitched back together.
Common mistakes
- Sending the original photo. You pay for pixels the model throws away.
- Shrinking too far. Below a few pixels per character, small print turns into guesses. Test the smallest size that still reads correctly.
- Expecting exact positions. Patches blur fine geometry, which is one reason exact counting and measuring are weak spots (8.1).
Key points
- A vision transformer cuts an image into a grid of patches (for example 16 by 16 pixels) and embeds each patch as a vector.
- A vision-language model passes those patch vectors through a small projector so the language model can read them as tokens beside the text.
- More pixels means more patches, so image size drives tokens, latency and cost. Resize before you send.
- Speech models such as Whisper use the same idea on audio: slice a spectrogram into frames, encode them, then decode text.
Cut an image into patch embeddings
Do by hand what a vision encoder's first layer does: cut an image into patches, flatten them and count the tokens.
You will have: A patchify() function for a small pixel grid and a table of patch counts for real image sizes.
Steps
- Run the starter: it prints the 8 x 12 toy image and zero patches.
- Fill in patchify(): cut the grid into p x p squares, left to right, top to bottom, and flatten each.
- Pad the edge with zeros when the size isn't a multiple of p, and count the patches.
- Print the patch count for 224, 448 and 896 pixel squares at p = 16.
- Explain why the count grows fourfold each time the side doubles.
Done when
- The 8 x 12 grid at p = 4 gives 6 patches of 16 numbers.
- The 10 x 12 grid at p = 4 gives 9 patches after padding.
- 224, 448 and 896 squares print 196, 784 and 3,136 patches.
Open in the Playground No API key needed.
Starter code (Python)
IMAGE = [[(r * 12 + c) % 10 for c in range(12)] for r in range(8)] # 8 rows x 12 columns of pixel values
def patchify(img, p):
patches = []
# TODO: for each p x p square (pad with 0 beyond the edge), append the flattened list of its values
return patches
for row in IMAGE:
print(row)
ps = patchify(IMAGE, 4)
print("patches:", len(ps))
Show a solution
import math
IMAGE = [[(r * 12 + c) % 10 for c in range(12)] for r in range(8)]
def patchify(img, p):
h, w = len(img), len(img[0])
patches = []
for top in range(0, h, p):
for left in range(0, w, p):
flat = []
for r in range(top, top + p):
for c in range(left, left + p):
flat.append(img[r][c] if r < h and c < w else 0)
patches.append(flat)
return patches
ps = patchify(IMAGE, 4)
print(f"8x12 at p=4: {len(ps)} patches of {len(ps[0])} numbers")
print("first patch:", ps[0])
taller = IMAGE + [[1] * 12, [2] * 12]
print(f"10x12 at p=4: {len(patchify(taller, 4))} patches (edge padded)")
for side in [224, 448, 896]:
n = math.ceil(side / 16) ** 2
print(f"{side}x{side} at p=16: {n:,} patches")