How AI Coding Works
Understanding how LLMs generate code, explained with analogies
AI can write code not because it "understands" programming, but because it has read virtually all code ever written by humans.
Understanding this principle helps you make better decisions in Vibe Coding — knowing when to trust it and when to question it.
Super Autocomplete

You know the predictive text on your phone? You type "how are" and it suggests "you doing."
AI writing code works on the same principle, just at a massively larger scale.
Your phone's suggestions are based on a few thousand common phrases. AI code models (like Claude, GPT) were trained on:
- Billions of lines of open-source code on GitHub
- Millions of Q&A posts on Stack Overflow
- Technical documentation, tutorials, blog posts
- Official docs for various programming languages
It learned patterns in code: when you say "build a login page," it knows this typically means a form, input fields, a submit button, validation logic — because it has seen thousands of login page implementations.
Token-by-Token Prediction
The core mechanism of AI code generation is called next token prediction.
A token is the smallest unit AI processes text in, roughly equivalent to a word or half a word. AI does one thing at a time: based on everything before it, predict the most likely next token.
Your input: "Build a button that when clicked shows"
AI predicts: "a" → "modal" → "dialog" → "with" → ...It's like a chef who has read every recipe ever written — you say "tomato and," they immediately say "eggs," because that's the most common pairing. But they're not "thinking" about the chemistry of tomatoes and eggs — they're pattern matching.
Context Window: AI's "Working Memory"

AI has a key limitation: the context window.
This is the amount of text AI can "see" at once, measured in tokens. Think of it as AI's desk — the bigger the desk, the more documents it can spread out simultaneously.
| Analogy | Description |
|---|---|
| Small desk (4K tokens) | Can only see part of one file |
| Medium desk (32K tokens) | Can see several complete files |
| Large desk (128K+ tokens) | Can see most of a small project's code |
This means:
- Short conversations work well — AI remembers everything you've said
- Long conversations cause "forgetting" — content beyond the window gets dropped
- Large projects need strategy — you can't dump an entire codebase at AI at once
Practical tip: When conversations get long and AI starts "forgetting" earlier requests, start a new conversation and re-provide the key context.
Does It "Understand" Code?
Strictly speaking, no.
AI doesn't understand "why this code is written this way" like a human programmer does. What it does is extremely sophisticated pattern matching — after seeing enough examples, it can generate code that looks correct and usually is correct.
It's like someone who has never been to China but has watched ten thousand Chinese movies — they can deliver authentic Chinese dialogue, but don't truly understand the cultural meaning behind it.
This explains several typical AI coding behaviors:
- Common tasks go well — login pages, form validation, data display — it has seen these countless times
- Niche requirements are error-prone — the more obscure the library or unusual the scenario, the less training data available
- It can "confidently make things up" — it might fabricate non-existent function names or APIs, because it's "completing" rather than "looking up"
- Same question, different answers — prediction has randomness, each generation may produce different results
What This Means for Vibe Coding
Understanding the principles helps you use AI more effectively:
- Clearer descriptions, better results — AI relies on your input to "locate" the right patterns; vague descriptions lead to vague results
- Step by step beats all at once — Break big tasks into small steps, each within the context window, and AI performs more reliably
- Verification is always your job — AI-generated code may look right but have bugs; running and testing can't be skipped
- When stuck, rephrase — When AI keeps giving wrong results, try a different description for potentially completely different output
What's Next
- Tools Landscape — Understand the differences between mainstream AI coding tools
- The Vibe Coder Mindset — The mental shift needed for Vibe Coding