How AI coding tools actually work
Vibe Coding · Start Here · Basics · 14 min
What is really happening when an AI writes your code: prediction, the context window, agents in a loop, and what that means for how you should work.
It predicts code, one piece at a time
Every AI coding tool, from a chat window to a full coding agent, is built on a large language model (LLM). An LLM does one thing: given some text, it predicts what text most likely comes next. It writes a small piece (a token, roughly a word or part of a word), adds it, then predicts the next piece, over and over.
Code is just text, so the same trick works. If the model sees this:
// Add 18% GST to a price in rupees
function addGST(amount) {
the most likely continuation is return amount * 1.18; followed by }. Nobody stored that function anywhere. The model learned, from reading huge amounts of public code, what usually follows a comment and a function name like that.
This is why AI is so fast at code that looks like code it has seen many times. It is also the root of most of its mistakes, as the next pages show.
The context window: what the tool can see
The model has no memory of your project. Each time it answers, the tool builds one big bundle of text and sends it to the model. That bundle is the context window: everything the model can "see" for this answer. It has a size limit, measured in tokens.
What usually goes into it:
- Your message and the earlier messages in this chat
- Files that are open, attached, or that an agent chose to read
- Rules files such as
CLAUDE.md, Cursor rules orcopilot-instructions.md(covered in Context and rules) - Docs or examples you pasted
- Tool output: error messages, test results, terminal logs
What is not in it, unless you put it there: the plan in your head, the other 40 files in your project, your database, the decision you made in yesterday's chat, and how your users actually behave.
So when the AI writes code that ignores your existing formatPrice() helper and makes a new one, it usually is not being careless. It simply could not see the helper.
Why it invents APIs and packages
Because it predicts likely text, the model can produce a function, a settings option or a whole package that sounds right but does not exist. This is called a hallucination. Common versions in code:
- A package name that fits the pattern (
indian-money-format) but was never published - A method that existed in an older version of a library, or in a different library
- A configuration option that would be sensible, so the model assumes it is there
- An API endpoint guessed from the shape of other endpoints
Three things make it worse: rare libraries (less training data), recent changes (the model learned from older code), and missing docs in context (it has nothing real to copy from).
There is a security side too. Attackers register package names that AI tools tend to invent and fill them with malware. This is sometimes called slopsquatting. Installing an invented package is not just a broken build; it can be a break-in.
Agents: a model in a loop with tools
A plain chat model can only write text. A coding agent (for example Claude Code, Cursor's agent, or Copilot agent mode) is the same kind of model, given tools and allowed to use them in a loop. A tool is just an action the tool's software performs for the model: read a file, search the project, edit a file, run a command.
You: "Add a 'mark as paid' button to each invoice"
|
v
1. THINK model decides what it needs to know
2. ACT tool call: search project for "Invoice"
3. SEE tool result goes back into the context
4. THINK "InvoiceRow.jsx renders each row"
5. ACT tool call: edit InvoiceRow.jsx
6. ACT tool call: run "npm test"
7. SEE "1 test failed: paid status not saved"
8. THINK "the API route ignores the new field"
9. ACT edit the API route, run tests again
10. SEE "all tests passed"
11. STOP summarise the change for you
Each loop, the result of the last action is added to the context, so the model "sees" real evidence instead of guessing. That is the big upgrade: an agent can notice its own failing test and fix it.
It is also the big risk. The same loop that runs npm test can run a command that deletes files or pushes broken code. That is why agents ask for permission, and why you set those permissions carefully (see Working with coding agents).
Why the same prompt gives different results
Ask the same question twice and you may get two different, both reasonable, answers. Several things cause this:
- Sampling. At each step the model picks from several likely next tokens, with a little randomness. Small differences early grow into different designs later.
- Different context. A new chat, a different open file, or a longer history changes what the model sees.
- Different model or settings. Tools let you switch models, and providers update models over time.
- Agent paths. An agent that reads files in a different order may reach a different plan.
What this means for you: one answer is a draft, not the truth. If a design choice matters, ask for two or three options and pick. If the result must be exactly right, the thing that decides "right" is a test, not the AI's confidence.
Great at boilerplate, weak at your unwritten rules
Boilerplate is code that looks almost the same in every app: a sign-up form, a table with edit and delete buttons, a settings page, an API route that saves a record. The model has seen thousands of these, so it writes them quickly and usually well.
Your business rules are different. They live in your head, in a WhatsApp chat with your co-founder, or in how your coaching institute has always done things. For example:
- "Refund the fee only if the batch has not started, and only within 7 days."
- "A student can join a maximum of two batches at once."
- "Teachers can see marks for their own batch, not other batches."
The model has never seen these. If you do not write them down, it will fill the gap with the most common pattern, which is often wrong for you, and it will do it confidently.
What this means for how you work
Once you see the AI as "a fast predictor that only knows what is in front of it", good habits follow naturally:
- Write down what is in your head. A short spec and a rules file turn your unwritten rules into context.
- Control what it sees. Point it at the right files and the right docs. Remove noise.
- Work in small steps. Short tasks keep the context focused and mistakes small.
- Verify, don't trust. Run it, test it, read the diff. Confidence is not evidence.
- Save working states. Commit to Git after every step that works, so a bad answer costs minutes, not days.
The rest of this track teaches each of these in depth. You can start feeling the difference right now in the Vibe Lab.
More in Start Here
Previous: Vibe coding vs software engineering · Next: Pick your tools and set up