AI Fundamentals · Guide 4 of 35

How Large Language Models Work

Explore tokens, learned representations, attention and next-token generation through a practical language-model workflow.

BeginnerAbout 5 min readPractical exercise

Before you start: Generative AI. Reading time is an estimate; allow extra time for practice.

What you’ll learn

  • Trace text through tokenization, contextual representations and next-token generation.
  • Explain what attention contributes and what it does not prove.
  • Distinguish training, request context and external tool results.
  • Identify the evidence needed to support a coding assistant’s claims.
On this page

From text to a continuation

A documentation assistant is asked which method a package supports. It returns a convincing method name. To judge that answer, first separate how the model produces text from how the application could verify it. Text generation alone has no requirement to consult the installed package.

A language model estimates probabilities over token sequences. In a common autoregressive setup, it predicts the next token from the available context, selects a token and repeats. Tokens can represent words, word fragments, punctuation or other units. The generated sequence is decoded into text that the application displays.

Training on a large amount of text can produce useful language and coding capabilities. Additional training can encourage instruction following and preferred response styles. This process does not create a reliable lookup table of verified facts. A convincing answer and a supported answer are different outcomes.

What a transformer contributes

Many current language models use transformer architectures. Tokens become numerical representations; attention layers combine information across token positions, and other layers transform those representations. In a causal text-generation model, a position attends to permitted earlier context rather than reading future tokens that have not been generated.

Consider “The function receives a list and returns its length.” Useful representations connect “its” to the list in context. Attention is a computational mechanism for mixing information, not a visible explanation of why the final answer is correct. Reading an attention score as a human reason would overstate what it demonstrates.

A simplified generation loop
context = tokenize(instructions + user_input)
repeat until a stopping condition:
    scores = model(context)
    next_token = select_from(scores)
    context.append(next_token)
return decode(generated_tokens)

This is explanatory pseudocode. Real systems add caching, message formatting, safety controls, tool protocols and limits. The loop alone does not browse the web or execute code.

Tools add capabilities outside the model

If an assistant checks a package version, a separate tool may query a registry and return data. The application supplies that result back to the model. The model can then explain it. Distinguish the tool’s actual result from the model’s proposed query and its interpretation of the result.

A response saying “I ran the tests” is not evidence of execution. A trustworthy coding workflow records the command, exit status and relevant output from the environment that ran it. Tool access should be explicitly scoped; knowing how to describe a file deletion does not grant permission to perform one.

What this means for your application

The model can only use the context made available to the request and information encoded in its parameters; external data requires an integration. Context has a finite capacity, and relevant facts may be omitted, truncated or overlooked. For a documentation helper, retrieve the correct version of the relevant page and display the evidence with the answer.

Evaluate the task you intend to ship. A model that explains short functions well may struggle with an entire repository or an unfamiliar framework. Include examples where the correct response is a question, an uncertainty statement or a refusal to guess. Keep correctness checks outside the model whenever possible.

Follow a tiny generation step

Suppose an illustrative model sees “The export format is” and assigns candidate next tokens the probabilities CSV 0.6, JSON 0.3 and XML 0.1. Choosing the highest-scoring token produces CSV; sampling can choose another token. These invented numbers describe continuation preferences in this example, not evidence that a real product supports CSV. After a token is chosen, it joins the context used for the next prediction.

The tokenizer maps text pieces to IDs. Learned embedding vectors turn those IDs into numerical representations, position information preserves ordering, and transformer layers update the representations using available context. Attention helps a token use information from other permitted positions. The output layer supplies scores for the next token. Multiple attention heads can capture different relationships, but no individual head is a guaranteed explanation of an answer.

Pretraining adjusts parameters over many examples; an ordinary generation request uses the current parameters. In many causal language models, predicting later tokens from earlier ones supplies training targets directly from text. Instruction tuning then uses additional examples or feedback to shape useful responses. Supplying a package manual in the current request gives the model evidence to condition on without, by itself, updating its parameters.

Return to the documentation question: the generated method name is a candidate answer. An actual package inspection or a version-matched manual can establish whether the method exists. A behavioral test checks whether it performs the required operation. Keeping these steps distinct lets you locate failures in generation, retrieval, execution or interpretation.

Put it into practice

Try it yourself

Map a coding assistant into four parts: input preparation, model generation, tool execution and output checking. Identify which part can prove a test passed.

Show hint

Only an actual execution result establishes the observed test outcome. The model may propose tests and interpret logs, but its prose cannot substitute for execution.

Optional practice

Check your understanding

5 questions to revisit the ideas. No score is saved, and you can retry. This is separate from Skill Quiz; it does not award XP or unlock guides.

1. An illustrative model gives CSV the highest next-token probability. What does that establish?
2. What does attention do in a transformer?
3. You supply a package manual in a normal request. What usually changes for that generation?
4. Which is evidence that a proposed test was executed?
5. A model explains a short function well. What should you conclude about a repository-wide task?

Sources & further reading

Primary references for the concepts in this guide. Examples and exercises are illustrative; verify provider-specific behaviour against the version you use.