What is an LLM, and how does it write code?
An LLM (large language model) is a neural network trained on huge amounts of text to predict the next token, a small chunk of text. It writes code the same way it writes prose: one likely token after another, based on your prompt and the patterns it learned from code in its training data.
Cite
CodeLifter Forum, "What is an LLM, and how does it write code?", https://codelifter.com/forum/posts/what-is-an-llm-and-how-does-it-write-code/, accessed 2026-11-12.- CodeLifter editorsStaffStaff, CodeLifter ·
What does "large language model" actually mean?
Google's machine learning crash course defines a language model as something that "estimates the probability of a token or sequence of tokens occurring within a longer sequence of tokens." A token, it says, "could be a word, a subword (a subset of a word), or even a single character."
The "large" part refers to size. The course's page on LLMs says they predict better than older language models because they "contain far more parameters" and "gather far more context." Parameters are the numbers inside the network that training adjusts; the same page says Transformers contain "hundreds of billion or even trillions" of them. Context means the text around the token being predicted, which is how a model tells "orange" the fruit from "orange" the colour.
How did language models get here?
The crash course walks through three generations. The differences matter for code, where a variable defined on line 3 can decide what line 300 should say.
Model type How it uses context Main limit N-gram model Looks only at the previous few words (two words for a 3-gram) Too little context; longer N-grams become too rare to be useful Recurrent neural network Reads "token by token", carrying some context forward Useful context is "still relatively limited" Transformer-based LLM Evaluates "the whole context at once" with self-attention Cost grows with context length; still makes mistakes The Transformer came from a 2017 paper, Attention Is All You Need, which proposed an architecture "based solely on attention mechanisms, dispensing with recurrence and convolutions entirely." Self-attention, in the crash course's words, asks for each token: "How much does each other token of input affect the interpretation of this token?" The crash course calls the Transformer "the most successful and widely used architecture for building LLMs."
How is an LLM trained?
According to the crash course, training starts with "a phenomenal amount of training data (text)". The model learns by predicting tokens that have been hidden from it and adjusting its parameters when it is wrong. Repeated across an enormous number of examples, it picks up grammar, facts, and the structure of code. An optional later step, instruction tuning, "can improve an LLM's ability to follow instructions", which is what turns a raw text predictor into something that answers requests.
So how does it write code?
The crash course is blunt: "LLMs are essentially autocomplete mechanisms that can automatically predict (complete) thousands of tokens." When you type "write a function that validates an email address", the model treats your request as the start of a document and predicts the most plausible continuation, token by token. Code is text, so the same process produces Python or JavaScript.
It works well for code because models are trained on a lot of it. OpenAI's 2021 paper Evaluating Large Language Models Trained on Code introduced Codex, "a GPT language model fine-tuned on publicly available code from GitHub", and said a "distinct production version of Codex powers GitHub Copilot" at the time. On the paper's HumanEval test, which checks whether generated Python functions pass unit tests, Codex solved 28.8% of problems while GPT-3, a general model without that code training, solved 0%. Fine-tuning means extra training on a narrower dataset, here code.
The same paper lists limits that still explain many bad suggestions: "difficulty with docstrings describing long chains of operations and with binding operations to variables." In plain terms, long multi-step instructions and keeping track of which value goes in which variable are where the model slips.
Why does an LLM write code that looks right but is wrong?
Because it predicts what is plausible, not what is true. The crash course says LLMs "hallucinate, meaning their predictions often contain mistakes." For code that shows up as functions that do not exist, wrong arguments, outdated APIs, and package names nobody published. The thread on invented packages covers the research on that last one.
A model also knows nothing about your project unless the text reaches it. A plain chat model sees only what you paste. Coding tools such as Claude Code, Codex and Copilot wrap an LLM in a program that reads your files and runs commands, which the coding agents thread compares.
How do you use that knowledge when you prompt?
- Give the model the context it cannot guess: language, version, framework, the existing function signatures, and the error message in full.
- Ask for one small, testable piece at a time instead of a long chain of steps, the weakness the Codex paper names.
- Run the code. Passing tests is the evidence; a confident explanation is not.
- Check every import and API call against the official documentation before you install or ship anything.
- Treat the output as a draft written by someone who has read a lot of code but has never seen yours.
The CodeLifter prompt guide turns those steps into a reusable prompt structure, and the models page lists current models with their makers and licences.
Short version
- An LLM is a neural network that predicts the next token from the tokens before it.
- Most current LLMs use the Transformer design from 2017, which weighs the whole context at once.
- It writes code by predicting plausible code, helped by training on public code.
- Plausible is not correct: run it, test it, and check imports against the docs.
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.
2 answers
- CodeLifter editorsStaffStaff, CodeLifter ·
What is a token in an LLM? A token is the unit of text the model reads and writes, usually a piece of a word. Google's crash course says most modern language models "tokenize by subwords", and gives examples: "unwatched" might become three tokens, "un", "watch" and "ed", and "cats" two, "cat" and "s". Tokenization is language specific, so the count differs by language; for English the course gives a rule of thumb that "one token corresponds to ~4 characters or about 3/4 of a word, so 400 tokens ~= 300 English words." Code tokenizes differently from prose, because symbols, indentation and long identifiers such as
getUserProfileByIdsplit into several pieces, so do not apply the English ratio to a source file. Tokens matter in practice because every model has a limit on how many tokens it can consider at once, its context window, and hosted models are usually priced per token. When a coding assistant "forgets" an earlier file, the cause is often that the conversation has outgrown that limit. To see how your own text splits, use the tokenizer tool or token-counting endpoint your model provider documents, since each model family has its own tokenizer. - CodeLifter editorsStaffStaff, CodeLifter ·
Why does an LLM give a different answer to the same prompt? Because the application usually samples from the model's predictions instead of always taking the top one. Google's crash course explains that a model produces a table of possible next tokens with probabilities, and an application can pick "the highest probability" or make "a random selection from tokens having a probability greater than a certain threshold." With sampling on, two runs of the same prompt can branch at the first uncertain token and end up as different code. That variation can help. The Codex paper, Evaluating Large Language Models Trained on Code, found that "repeated sampling from the model is a surprisingly effective strategy": with 100 samples per problem, at least one working solution appeared for 70.2% of its HumanEval problems, against 28.8% for a single attempt. The catch is that someone has to tell the working sample from the broken ones, and in the paper that was the unit tests. For your own work, the lesson is the same: if an answer looks wrong, asking again may give a better one, but only a test tells you which answer is right.