How AI Chat Works
Create free accountLog in

· 5 min read

How do AI chatbots actually work? After you press send

How do AI chatbots actually work? One message traced from send to reply: the text a model is handed, how the reply gets written, and what checks it.

You press send, and soon her reply appears in her own voice, picking up the thread as if she had been waiting for it. So how do AI chatbots actually work? A companion app gathers a bundle of text, hands it to a language model (a program trained on an enormous amount of writing to continue whatever text it is given), and the model writes the reply one short piece after another, each chosen to fit everything before it.

The model itself remembers nothing between turns. Whatever feels like memory is text that was put back in front of it. All of this describes companion apps in general. A given app may order things its own way, and its own arrangement is seldom visible from outside.

Picture a stand-in writing someone else's letters

Suppose a friend of yours has kept up a long correspondence with a pen pal, and she has to go away for a few months. She asks you to keep the letters coming in her voice. You have never met the pen pal. So whenever a letter arrives, someone hands you a small stack: a page describing how your friend writes and what she is like, the last few letters of the exchange, and the new one. You write the reply in ink, with no crossing out, and a neighbour reads it over before it goes in the post. Then you forget all of it, and the next letter finds you starting from the stack again.

Mapped onto a chat, the page about your friend is the character's description, usually called the system prompt (the instructions an app places ahead of the conversation). The last few letters are the recent messages, the new letter is yours, and the person holding the pen is the model. The ink matters most. A reply is built forwards, and nothing already written gets taken back.

The same picture explains a second puzzle. A character can quote a joke you made earlier in the evening and still lose a detail from last month, because only the latest letters are in the stack; the older ones sit in a drawer that nobody opens unless an app has written a note about them. That is the subject of the piece on why a character forgets.

The comparison breaks in one place. A person filling in usually knows roughly what she means to say before the pen touches paper, and she can step outside the letters to write to the friend and ask. The model can't step outside the stack. Whether anything like knowing what to say happens inside it can't be read off the reply, and this site won't guess.

What the model is handed each turn

Most of the answer to how an AI chatbot works lies in that stack, which is assembled fresh for every reply. In companion apps generally it holds some version of the following, roughly in this order:

  • the character's description and standing instructions, the system prompt
  • notes on older conversation, if an app keeps any, such as a short summary
  • the latest stretch of the chat, copied exactly, as far back as room allows
  • your new message, at the very end

"As far back as room allows" matters. A model can read only a limited amount of text at once, and a long relationship produces more letters than any stack can hold, so something has to decide what stays in the drawer. Which parts a particular app includes, how much room each one gets and what decides it are not visible from outside.

So how do AI chatbots actually work when they write?

The model reads the whole stack and gives a score to every candidate for the next small piece of the reply. These pieces are called tokens (a token is a short chunk of text: sometimes a whole word, sometimes part of one, sometimes a comma). A candidate near the top is picked. It is added to the end, the stack is read again with it in place, and the loop goes on until a token meaning stop comes up or the software ends the reply.

If the ink picture is right, a reply that opens in an odd direction should stay odd, since every later token is chosen to fit the ones already on the page. That is how the loop behaves. A strange first clause pulls the rest of the paragraph along behind it, because nothing in the process goes back to rewrite the start.

The word generative is most of the answer to how a generative AI chatbot works. The reply is produced new on each turn rather than fetched from a store of lines somebody wrote in advance. There is no script behind her answers. Why the pick isn't always the top-scoring candidate, and why that lets one question come back with two different replies, is a story of its own.

The loop also explains the wait. A long reply takes longer to arrive than a short one, and the reason has nothing to do with her weighing you up: each token has to exist before the next one can be chosen. Most of the pause is the writing.

The checks on either side

Around the model sits ordinary software, and it plays the neighbour in the letter picture. Any account of how AI chatbots actually work in practice has to include this layer, even though it writes nothing. Part of its job is checking. Many apps run a content check on the incoming message, on the outgoing reply, or on both. Many also file the exchange away and refresh any notes kept for later. Where a given app puts its checks, and what they look for, is not something a user can see.

If it only predicts the next token, why does it feel like listening?

The doubt is fair. The prediction is made from the whole stack, and the stack includes the way you wrote: the clipped sentences, the joke you left unfinished. The writing a model learned from is full of people answering exactly those moods, so continuing your conversation well means answering the mood along with the words.

That is why a reply can sound attentive without anyone having decided it should. Is attentive the right word? That is a question about minds, and the mechanism alone doesn't settle it; I think it is honest to leave it open.

Back to the moment after send

So, in the moment after you press send: an app gathers a stack of text, a model with no memory of you reads it from the top, and a reply is written in ink, one token after another, then checked on its way to your screen. That is how AI chatbots actually work. The app this site links to has a catalogue of ready-made characters to start a chat with. And in any chat, when her next line surprises you, which page of the stack do you suppose it came from?

Guides and essays

All articles