Skip to main content
Kliff

How language models work

What a language model does when it answers you, and why that shapes how you should talk to it.

Video coming soon

A coding agent is built around a language model. Everything the agent does, from reading a file to writing a fix, starts as text the model produces. You do not need the math to use one well, but a working picture of what the model is doing helps you predict when it will be useful and when it will struggle.

A model predicts the next piece of text

A language model is trained on a very large amount of text, including code. During training it learns one skill over and over: given some text, predict what comes next.

When you send a prompt, the model reads it and produces a likely next piece of text. It adds that piece to the end, reads everything again, and produces the next one. An answer is built this way, one small piece at a time, until the model produces a signal that it is done.

Take this prompt:

Promptts
// Return true when the year is a leap year.
function isLeapYear(year: number): boolean {

A model that has seen many leap year functions will very likely continue with the familiar rule: divisible by 4, except centuries, unless divisible by 400. It is not looking the rule up. It is continuing the pattern that fits best.

That one idea explains a lot. The model is strongest where its training data was rich and consistent, such as popular languages, common libraries, and well known algorithms. It is weaker where the pattern is rare, new, or specific to your codebase.

Training gives knowledge, instructions give direction

Most models you use have gone through two broad stages. First they learn from raw text, which gives them general knowledge of language and code. Then they are tuned to follow instructions and hold a conversation, which is why you can ask for something in plain words and get a useful reply instead of a random continuation.

The knowledge from training has a cutoff. A model knows nothing that happened after its training data was collected, and it has never seen your private repositories. Anything it needs to know about your project has to reach it in the prompt, or through a tool it can use to read it. Later lessons cover both.

Answers are sampled, not fixed

At each step the model does not pick one certain next piece. It weighs many possibilities and one is chosen, with some randomness. That is why the same prompt can give different answers on different tries.

This has practical effects:

  • Two runs of the same task can take different paths. Neither is necessarily wrong.
  • A fluent answer is not proof of a correct one. The model is choosing what reads well, and reading well and being right usually line up, but not always.
  • If an answer looks off, asking again with more detail often works better than asking again with the same words.

The model does not remember you

A model has no memory between requests. Each time you send a message in a chat, the application sends the whole conversation so far back to the model, and the model reads it fresh.

So when a chat or an agent seems to remember what you said earlier, it is because that text is still in what gets sent. When a conversation is cleared or summarized, the details that were dropped are gone for the model. Tools that offer memory across conversations do it by saving notes and putting them back into the prompt later.

Why this changes how you prompt

If the model continues the most fitting pattern, the best way to steer it is to give it a clear pattern to fit. Compare two requests:

Vaguetext
Fix the date bug.
Specifictext
The invoice page shows yesterday's date for users in UTC+7.
The date is formatted in formatInvoiceDate in billing/format.ts.
Make it use the user's timezone, and add a test for UTC+7.

The second one tells the model what is wrong, where to look, and what done means. It narrows the set of likely continuations to the ones you want.

Key points

  • A language model writes by predicting the next piece of text, over and over.
  • It is strongest on patterns that were common in its training, and knows nothing about your private code unless you show it.
  • Answers involve randomness, so the same prompt can give different results, and fluent does not mean correct.
  • The model keeps no memory between requests. Whatever it should know has to be in what is sent.
  • Specific prompts work better because they leave fewer wrong ways to continue.