Beginner · about 15 minutes

How a chatbot writes an answer

Text cut into pieces, the next piece predicted one at a time, why the answer reads as confident whatever its accuracy, and how the hidden instructions and searched documents a chatbot adds shape its answer.

TopicsAI basicsHow models workUsing AI in practice

Helpful to read first, if the subject is new to you: Neural networks without the mathematics, a Beginner module.

Text cut into pieces

The chatbots lawyers now use are built on a large language model, which is a neural network trained on a very large amount of text to predict what comes next. The network itself is a very large set of simple calculations, each controlled by settings that work like volume dials, and a large language model has billions of those dials. Training sets the dials: the network guesses the next word in passages of published text, and after each guess every dial is nudged a fraction in the direction that would have made the guess better. Both the network and its training are explained in more depth in Neural networks without the mathematics, a Beginner module.

The network works only with numbers, so before the model reads a sentence, software cuts the text into pieces called tokens and swaps each piece for its number in a fixed list. Common words become single tokens, while rarer words are cut into two or more pieces; punctuation marks and spaces are turned into tokens as well. The full list of pieces a model knows is called its vocabulary, and it runs to tens of thousands of entries or more.

A token is on average shorter than a word, so a text holds more tokens than words. As a rule of thumb, OpenAI’s documentation puts a token of English text at about four characters, or three quarters of a word, which makes about four tokens for every three words. A page of a judgment of around 500 words therefore comes to roughly 670 tokens. The limit on how much text a model can take in at once is measured in tokens, as is the price providers charge for each use, so a long bundle’s token count bears on how much of it a chatbot can read in one go.

The model sees only the numbers, which explains some odd failures. Asked how many letters a word contains, a model may get the count wrong, since the word arrived as one or two numbers and the letters inside it were not shown to the model. The way text becomes tokens is covered in detail in Words as numbers, an Intermediate module.

The text The claimant’s solicitors filed the appeal. Cut into tokens The claim ant ’s solicitors filed the appeal . Swapped for numbers 791 3802 519 753 31240 12820 279 14736 13 The cuts and numbers are illustrative; each model has its own list.
A sentence cut into tokens, with common words kept whole, a rarer word split in two and each token swapped for its number in the model’s list; the cuts and numbers are illustrative.

One piece at a time

The model takes the text so far and gives each piece in its vocabulary a likelihood of coming next, expressed as a percentage, with the percentages for all the pieces adding up to 100. The software around the model then picks one piece, adds it to the text and runs the model again on the longer text to choose the piece after that. Every answer a chatbot gives is written in this loop, one piece at a time, which is why the answer appears on screen a word or so at a time.

The simplest way for the software to pick is to take the likeliest piece every time, and some systems do just that. Chatbots often use a weighted draw, like a lottery in which each piece holds tickets in proportion to its likelihood, so a piece with a likelihood of 30 per cent wins about three draws in ten.

The same question asked a second time gets fresh draws, which can land on a different piece early in the answer. Every later piece is then predicted from text that includes the different piece, so the rest of the answer can take a different path and end up worded differently. A lawyer who has checked one answer has therefore checked that answer alone, since asking again can produce a different one.

A phone keyboard that suggests the next word works on a related idea with far less text behind it. A large language model has learned from a very large amount of published text, so its guesses take account of grammar, of facts that are often repeated, of the style of a legal opinion and of the shape of an argument. The lab below lets a reader play the software’s part, choosing each next word or letting the software draw one by its likelihood.

Try it: one piece at a time

The court dismissed the

The percentages are made up for this example and cover only the pieces listed, whereas a real model scores every piece in its vocabulary, which runs to tens of thousands of entries or more.

Pick each next word from the list, or use the button below the list to have the software draw one at random, weighted by the percentages, and watch a sentence form one piece at a time.

Why it sounds sure of itself

Each piece is chosen for being likely to come next, which is a different test from whether the sentence it completes is true. Fluent, confident prose in the register of a legal opinion is the kind of text the model has learned to expect, so the model writes in that register whether its statement is right or wrong. Checking a statement against a source is a separate step, and the model on its own does not take it. The name for a false statement written in that fluent, confident style is a hallucination.

The Divisional Court described the same link between prediction and error in R (Ayinde) v London Borough of Haringey, a judgment of June 2025 on lawyers who had cited cases that do not exist. At paragraph 6 it said that freely available generative AI tools “generate textual responses by predicting what words or phrases come next in a particular context, based on patterns identified from a vast quantity of training data”, and that their “coherent and plausible responses may turn out to be entirely incorrect”.

The way models are trained and tested may add to the confident tone, by rewarding a model for giving an answer where it could admit doubt. Developers test a model by putting sets of questions with known answers to it and counting how many it gets right. A later stage of training, after the model has learned from published text, works in a similar way, nudging the model’s dials towards answers that score well. Scoring that counts only right answers works like a multiple-choice exam that does not deduct marks for wrong answers, in which a student who guesses scores more on average than one who leaves a question blank. A 2025 paper by researchers at OpenAI and the Georgia Institute of Technology argues that language models produce false statements because “the training and evaluation procedures reward guessing over acknowledging uncertainty”. On the researchers’ argument, a model trained and tested that way learns that a confident answer scores better than an admission of doubt.

Instructions and documents added

A chatbot product adds text of its own to what the user types, before the model reads any of it. The business running the product writes a system prompt, which is a set of instructions placed ahead of the conversation. One example is a law firm that runs its own chatbot and tells it, in the system prompt, to answer only questions about employment law, in a formal tone, citing the firm’s own guidance. The user is not shown the system prompt, so a lawyer using the product may read an answer shaped by instructions the lawyer has not seen. The model gives the system prompt priority over the user’s own messages where the two conflict, because in the later stage of training, after it has learned from published text, answers that follow the system prompt score better.

Many products also search a collection of documents, such as a database of judgments or a firm’s own precedents, before the model answers the user’s question. The software finds the passages that seem most relevant to the question, pastes them into the text just before the question itself and asks the model to answer from them and to cite them. The technique is called retrieval-augmented generation, a name given to it in a 2020 paper, because the passages retrieved augment the text from which the model generates its answer. The model predicts each piece from all the text in front of it, so once that text includes a passage stating the answer, the likeliest continuation is one that draws on the passage. With the right passage in front of it, the model is therefore less likely to invent an answer.

Retrieval helps without removing the risk, since the search can miss the passage a lawyer most needs and the model can still misread or misquote the passages it was given. A study of legal research tools by researchers at Stanford, published in 2024, found that tools from LexisNexis and Thomson Reuters which use retrieval produced fewer hallucinations than a general-purpose chatbot. Each of those tools still hallucinated “between 17% and 33% of the time” on the questions the researchers set, with every tool’s overall rate falling somewhere in that band.

System prompts and retrieval are covered in more depth in The system around the model, an Advanced module. Models that write out their reasoning before they answer, and the reasons models invent authorities, are the subject of Does AI reason?, a Beginner module.

Check your understanding

  1. Question 1 Why might a chatbot give the wrong count of the letters in a word?

    Choose one answer to question 1
    Show the answer

    Answer: It reads the word as one or two numbered tokens, without seeing the letters inside. Text is cut into tokens and each token is swapped for a number before the model reads it, so the letters inside a common word come to the model only as part of a single number.

  2. Question 2 Why can the same question get two differently worded answers from the same chatbot?

    Choose one answer to question 2
    Show the answer

    Answer: The software often draws each next piece at random in proportion to its likelihood, so the answers can branch. Drawing pieces at random in proportion to their likelihoods means a less likely piece is sometimes chosen, and one different piece early on sends the rest of the answer down a different path.

  3. Question 3 Why does a chatbot’s answer read as confident whatever its accuracy?

    Choose one answer to question 3
    Show the answer

    Answer: Each piece is chosen for being likely to come next, and a confident, fluent style is likely text. Being likely to come next is a different test from being true, and the Divisional Court in Ayinde described such tools as predicting what words come next from patterns in their training data.

  4. Question 4 What does retrieval add to a chatbot that answers legal research questions?

    Choose one answer to question 4
    Show the answer

    Answer: Relevant passages found by a search and placed before the question, which makes invented answers less likely. Retrieval puts source material in front of the model, which reduces invention without removing it: a 2024 Stanford study found that legal research tools using retrieval still hallucinated between 17 and 33 per cent of the time.

Sources

  1. OpenAI, Key concepts (API documentation), on tokenschecked 28 October 2026
  2. R (Ayinde) v London Borough of Haringey, heard with Al-Haroun v Qatar National Bank, [2025] EWHC 1383 (Admin), paragraph 66 June 2025 · checked 28 October 2026
  3. Kalai, Nachum, Vempala and Zhang, Why Language Models Hallucinate4 September 2025 · checked 28 October 2026
  4. Lewis and others, Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks22 May 2020 · checked 28 October 2026
  5. Magesh, Surani, Dahl, Suzgun, Manning and Ho, Hallucination-Free? Assessing the Reliability of Leading AI Legal Research Tools30 May 2024 · checked 28 October 2026

Your progress

Switched on, it keeps a short record of the modules you open and the quizzes you finish in this browser’s own storage, and the record stays on this device.

This module is general information, checked against the sources above on 28 October 2026. It is not legal advice, and the law it describes may have changed since then. Read the disclaimer.