<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 伟 徐</title>
    <description>The latest articles on DEV Community by 伟 徐 (@wesxu).</description>
    <link>https://dev.to/wesxu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4235122%2F3318ca75-4bab-443c-8e3c-c93575742a68.png</url>
      <title>DEV Community: 伟 徐</title>
      <link>https://dev.to/wesxu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wesxu"/>
    <language>en</language>
    <item>
      <title>How Does a Message Become an Answer? Inside the LLM Generation Loop</title>
      <dc:creator>伟 徐</dc:creator>
      <pubDate>Fri, 27 Nov 2026 14:23:31 +0000</pubDate>
      <link>https://dev.to/wesxu/how-does-a-message-become-an-answer-inside-the-llm-generation-loop-2gm</link>
      <guid>https://dev.to/wesxu/how-does-a-message-become-an-answer-inside-the-llm-generation-loop-2gm</guid>
      <description>&lt;p&gt;&lt;strong&gt;Part I: How LLMs Work · Lesson 01&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;LLMs &amp;amp; Agents, Explained | One question at a time&lt;/strong&gt;&lt;br&gt;
For software engineers who can write code but haven’t studied machine learning.&lt;br&gt;
Concrete examples, pseudocode, and data flows you can follow—so you can see how LLMs and agents actually work.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Imagine opening an AI assistant and typing:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Translate “我喜欢猫” into English.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A moment later, the answer appears:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;I like cats.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The translation makes sense. But how did it get there?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Did the model prepare the whole sentence before displaying it?&lt;/li&gt;
&lt;li&gt;Or, when it produced &lt;code&gt;I&lt;/code&gt;, had it yet to choose what came next?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, we’ll use a few snippets of pseudocode to trace what happens between hitting Send and seeing a response. First, we’ll follow the overall flow. Later, we’ll look inside the model.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We’ll focus on typical &lt;strong&gt;autoregressive text generation&lt;/strong&gt;: the model predicts the next token from the current context, the surrounding program selects one and appends it, and the process repeats. The name sounds more intimidating than the basic idea: keep writing from what’s already there. We’ll explain what a token is shortly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here’s the route we’ll follow:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdc3waaoy1fppgl3gng11.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdc3waaoy1fppgl3gng11.png" alt="The token generation loop" width="799" height="432"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In this article&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. The model receives a formatted conversation&lt;/li&gt;
&lt;li&gt;2. Text becomes IDs: What exactly is a token?&lt;/li&gt;
&lt;li&gt;3. What does one call to model() return?&lt;/li&gt;
&lt;li&gt;4. Why does the output go back into the input?&lt;/li&gt;
&lt;li&gt;5. When does generation stop—and how does the text appear?&lt;/li&gt;
&lt;li&gt;6. Putting the whole process into code&lt;/li&gt;
&lt;li&gt;7. Where do we go from here?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  1. The model receives a formatted conversation
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1ins9uzogh3fx4yf3fx5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1ins9uzogh3fx4yf3fx5.png" alt="Formatting a conversation with a chat template" width="800" height="473"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You send a sentence. The application also needs to tell the model &lt;strong&gt;who said it and whose turn it is to reply.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Translate “我喜欢猫” into English."&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;format_chat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;role&lt;/code&gt; identifies the speaker, and &lt;code&gt;content&lt;/code&gt; holds the message itself. &lt;code&gt;format_chat()&lt;/code&gt; combines them using the chat format expected by the model.&lt;/p&gt;

&lt;p&gt;Think of this as adding speaker labels and message boundaries, followed by a cue for the assistant to begin its reply. Schematically, the formatted input might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;user_message_start&amp;gt;
Translate “我喜欢猫” into English.
&amp;lt;user_message_end&amp;gt;
&amp;lt;assistant_reply_start&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the last line. It doesn’t contain an answer. It marks &lt;strong&gt;where the assistant’s answer should begin.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;format_chat()&lt;/code&gt; is a placeholder function for this article, and these markers are illustrative. Actual chat templates and special markers vary by model.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We’ll explore how models learn to respond to roles and instructions in Article 05. For now, remember this: &lt;strong&gt;the input includes conversation structure, not just the text of your message.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With the conversation formatted, the next step is to turn the text into IDs.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Text becomes IDs: What exactly is a token?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxvpcw4umizd944shnxsa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxvpcw4umizd944shnxsa.png" alt="Token vocabulary, encoding, and decoding" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The conversation is ready, but the model’s core is a network that performs numerical calculations. Before text can enter that network, it needs to become &lt;strong&gt;a sequence of numerical IDs.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The component responsible for this is the &lt;strong&gt;tokenizer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A model typically comes with a tokenizer that defines two key things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A vocabulary:&lt;/strong&gt; the tokens it uses and the numerical ID assigned to each one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tokenization rules:&lt;/strong&gt; how to split a piece of text into those tokens.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So what is a token?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Think of tokens as the tokenizer’s building blocks for text.&lt;/strong&gt; A token might be a character, part of a word, or a longer sequence of characters. Spaces and punctuation can be part of a token, too. A token is not necessarily a “word” in the everyday sense.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Suppose our tokenizer has the following vocabulary entries. We’ll keep the Chinese text in these examples so you can follow the same pieces through the diagrams: 我 means “I,” 喜欢 means “like,” 猫 means “cat,” and 狗 means “dog.”&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Token (text fragment)&lt;/th&gt;
&lt;th&gt;Token ID&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;我&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;喜欢&lt;/td&gt;
&lt;td&gt;357&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;猫&lt;/td&gt;
&lt;td&gt;89&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;狗&lt;/td&gt;
&lt;td&gt;90&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;。&lt;/td&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;This is an excerpt from an invented vocabulary, used only to illustrate the idea.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Rearrange and reuse those same building blocks, and you get different sentences:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;我喜欢猫。   → [12, 357, 89, 9]   # I like cats.
猫喜欢我。   → [89, 357, 12, 9]   # Cats like me.
我喜欢狗。   → [12, 357, 90, 9]   # I like dogs.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;A finite vocabulary does not limit the model to a fixed list of sentences.&lt;/strong&gt; A finite alphabet can form an enormous variety of words and sentences; tokens work in a similar way.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The tokenizer doesn’t need to register every sentence you might ever write. What happens when it encounters a word that isn’t in its vocabulary? It can usually split it into smaller units. Many tokenizers can fall back to basic units such as bytes, allowing unfamiliar names and made-up words to be represented without assigning them new IDs on the fly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To make the conversion concrete, here is our translation request written entirely in Chinese:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;请把“我喜欢猫”翻译成英文。
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means “Translate ‘我喜欢猫’ into English.” In practice, the tokenizer encodes the full formatted conversation from Section 1, including any role and message-boundary markers. We’re showing only the message body here to make the steps easier to see.&lt;/p&gt;

&lt;p&gt;Using an invented split and invented IDs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Text fragments:
["请", "把", "“", "我", "喜欢", "猫", "”", "翻译", "成", "英文", "。"]

Token IDs:
[7, 18, 23, 12, 357, 89, 24, 502, 31, 608, 9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is encoding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Split the text into tokens and return their IDs.&lt;/span&gt;
&lt;span class="n"&gt;prompt_ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tokenizer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tokenizer can also convert a sequence of IDs back into text:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tokenizer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token_ids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Encoding turns text into IDs the model can accept; decoding turns generated IDs back into text we can read.&lt;/strong&gt; Whether decoding reproduces the original input byte for byte depends on details such as whether the tokenizer normalizes the text.&lt;/p&gt;

&lt;p&gt;Two details are worth keeping in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;An ID is just an identifier.&lt;/strong&gt; In this vocabulary, &lt;code&gt;89&lt;/code&gt; identifies 猫. It isn’t a score for “cat,” and neighboring IDs don’t imply similar meanings.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The split and the IDs depend on the tokenizer.&lt;/strong&gt; Another tokenizer might give 猫 a different ID or split the entire sentence differently. Different models can also share the same tokenizer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We now have a list of integers. It’s time to pass them to &lt;code&gt;model()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. What does one call to model() return?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6vqpg3wj5ixpqcdxhdi0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6vqpg3wj5ixpqcdxhdi0.png" alt="Scoring candidates, converting scores, and selecting a token" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’ve converted the input into token IDs. Now the model finally enters the picture.&lt;/p&gt;

&lt;p&gt;To keep the focus on generation, we’ll treat &lt;code&gt;model()&lt;/code&gt; as a simple interface: &lt;strong&gt;pass in the current context as token IDs, and get back a score for every token in the vocabulary as a possible next token.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For this article, &lt;code&gt;model()&lt;/code&gt; is a teaching wrapper that returns only the scores used to predict the next token, from the final input position.&lt;/p&gt;

&lt;p&gt;On the first step, no answer has been generated yet, so the current context is just our prepared input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;context_ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prompt_ids&lt;/span&gt;
&lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context_ids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the request “Translate ‘我喜欢猫’ into English,” the scores might look like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Candidate token&lt;/th&gt;
&lt;th&gt;Score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;I&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;5.2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;The&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;3.1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Hello&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;0.4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;…&lt;/td&gt;
&lt;td&gt;…&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;These fragments and scores are illustrative. The actual output contains a score for every token in the vocabulary; only a few are shown here. The diagram above uses a separate, three-candidate toy example with scores of 3, 2, and 1 to make the probability conversion visible.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Notice what &lt;code&gt;model()&lt;/code&gt; has returned: &lt;strong&gt;a scorecard for the next token&lt;/strong&gt;, not a finished essay. A higher score means the model favors that token more strongly in the current context.&lt;/p&gt;

&lt;p&gt;Scoring the candidates doesn’t select one, though. The program still has two jobs to do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Convert candidate scores into probabilities.&lt;/span&gt;
&lt;span class="n"&gt;probabilities&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;softmax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Choose the next token ID using a selection strategy.&lt;/span&gt;
&lt;span class="n"&gt;next_token_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;select_token&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;probabilities&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;softmax()&lt;/code&gt; is a fixed mathematical transformation that turns the scores into probabilities that sum to 1. We can leave the formula for another day. Here, its job is simply “scores in, probabilities out.”&lt;/p&gt;

&lt;p&gt;&lt;code&gt;select_token()&lt;/code&gt; makes the actual choice. It might pick the most probable token, or it might sample according to the probabilities. Sampling isn’t a blind guess: &lt;strong&gt;a more probable candidate is more likely to be picked, but it isn’t guaranteed to win every time.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Put those steps together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# First step: no answer prefix yet, so use the original input.&lt;/span&gt;
&lt;span class="n"&gt;context_ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prompt_ids&lt;/span&gt;

&lt;span class="c1"&gt;# Score the candidates for the next token.&lt;/span&gt;
&lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context_ids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Convert the scores into probabilities.&lt;/span&gt;
&lt;span class="n"&gt;probabilities&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;softmax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Select one token ID.&lt;/span&gt;
&lt;span class="n"&gt;next_token_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;select_token&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;probabilities&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The division of labor is straightforward:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;model()&lt;/code&gt; scores the candidates. &lt;code&gt;softmax()&lt;/code&gt; converts the scores into probabilities. &lt;code&gt;select_token()&lt;/code&gt; picks one.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Why does the model assign these particular scores? That’s the black box we’ll open in the next article. For now, the important point is that we’re using &lt;strong&gt;a model that has already been trained.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Training&lt;/strong&gt; adjusts the model’s parameters so it gets better at predicting what comes next. Using those parameters to compute an output is called &lt;strong&gt;inference&lt;/strong&gt;. In machine learning, “inference” doesn’t necessarily mean complex logical reasoning—translating a short sentence counts, too. Ordinary inference generally does not update the model’s parameters.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Suppose we select the token ID for &lt;code&gt;I&lt;/code&gt;. We’ve got the first building block of the answer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But what comes after &lt;code&gt;I&lt;/code&gt;?&lt;/strong&gt; To keep going, we need to feed that new token back into the process.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Why does the output go back into the input?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqfdw5xb3fm5a8oelzblu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqfdw5xb3fm5a8oelzblu.png" alt="Original input and generated prefix form the current context" width="800" height="473"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’ve selected our first token: &lt;code&gt;I&lt;/code&gt;. It’s a start, but hardly a complete translation.&lt;/p&gt;

&lt;p&gt;The program records that token ID as part of the answer, then calls the model again with the text generated so far included in the context:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Add the selected ID to the answer.&lt;/span&gt;
&lt;span class="n"&gt;answer_ids&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next_token_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Next step's context = original input + answer generated so far.&lt;/span&gt;
&lt;span class="n"&gt;context_ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prompt_ids&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;answer_ids&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;+&lt;/code&gt; means &lt;strong&gt;list concatenation&lt;/strong&gt;, not arithmetic on the IDs. The original input stays the same; the growing answer is appended to it.&lt;/p&gt;

&lt;p&gt;Why feed the model its own output?&lt;/p&gt;

&lt;p&gt;Because the next step needs to know: &lt;strong&gt;“You’ve already written &lt;code&gt;I&lt;/code&gt;. Continue from there.”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this simplified program, which has no other state, passing in the same original input every time would be like repeatedly asking, “How should this answer begin?” If we also always picked the highest-scoring candidate, we’d get the same opening over and over. Excellent practice at starting a sentence. Not much progress toward finishing one.&lt;/p&gt;

&lt;p&gt;Here’s how the answer grows:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Step&lt;/th&gt;
&lt;th&gt;Answer before this step&lt;/th&gt;
&lt;th&gt;Text fragment selected&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Empty&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"I"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"I"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;" like"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"I like"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;" cats"&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"I like cats"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;"."&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;The table shows text for readability; the program stores token IDs. These token boundaries are assumed for teaching purposes. The leading spaces in &lt;code&gt;" like"&lt;/code&gt; and &lt;code&gt;" cats"&lt;/code&gt; are part of the example tokens.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The original input is still present at every step. What changes is the answer prefix—the part already written.&lt;/strong&gt; Together, they form the current context used to predict the next token.&lt;/p&gt;

&lt;p&gt;For example, after generating &lt;code&gt;I like&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Original request: Translate “我喜欢猫” into English.
Answer so far:    I like
                     ↓
          Score candidates and select
                     ↓
          Selected fragment: " cats"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One more ID is added to &lt;code&gt;answer_ids&lt;/code&gt;, and the answer grows from &lt;code&gt;I like&lt;/code&gt; to &lt;code&gt;I like cats&lt;/code&gt;. Then the loop runs again, until a stopping condition is met.&lt;/p&gt;

&lt;p&gt;You might be wondering: &lt;strong&gt;if the model generates such a small piece at a time, doesn’t it lose track of the whole sentence?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The distinction is this: &lt;strong&gt;a small output step does not mean a small context.&lt;/strong&gt; We select only one token at a time, but the original instructions and everything written so far are both available in the current context.&lt;/p&gt;

&lt;p&gt;Having access to context doesn’t guarantee the model will use it correctly, of course. Nor does it guarantee a correct answer. We’ll explore those limitations in Part II.&lt;/p&gt;

&lt;p&gt;Our generation loop is now connected: &lt;strong&gt;select a token, append it, and predict the next one using the updated context.&lt;/strong&gt; But it can’t go on forever. How does it stop, and how do those IDs become text on the screen?&lt;/p&gt;

&lt;h2&gt;
  
  
  5. When does generation stop—and how does the text appear?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faywh0900gru0gf7pedhz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faywh0900gru0gf7pedhz.png" alt="Stopping generation and displaying text" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The answer is growing one token at a time. How does the program know when to call it a day?&lt;/p&gt;

&lt;p&gt;Chat models typically use special markers to signal the end of a response. In our simplified code, selecting the end token causes the program to leave the loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;next_token_id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;end_token_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;break&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The end marker is meant for the program. It usually isn’t shown to the user as part of the answer.&lt;/p&gt;

&lt;p&gt;The program can also enforce a maximum generation length or other stopping conditions. So &lt;strong&gt;generation may stop because the model signals that it is done, or because the program’s limit has been reached.&lt;/strong&gt; Stopping doesn’t prove the answer is complete, and it certainly isn’t a correctness check.&lt;/p&gt;

&lt;p&gt;Next, the generated IDs need to become text. That’s where &lt;code&gt;decode()&lt;/code&gt;, which we met earlier, comes in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tokenizer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answer_ids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To keep our example simple, we’ll wait for generation to finish, then decode and display the answer all at once.&lt;/p&gt;

&lt;p&gt;But AI assistants often show their answers a little at a time. How does that fit?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The model generates; the application displays.&lt;/strong&gt; Those are separate jobs.&lt;/p&gt;

&lt;p&gt;The application can wait for the complete answer, or it can receive, decode, and display the output incrementally. The latter is usually called &lt;strong&gt;streaming&lt;/strong&gt;: users can begin reading before the whole response is finished.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The model generates tokens
            ↓
The application receives and decodes them
            ↓
The interface displays text in chunks
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Network delivery, buffering, and rendering all affect when the screen updates. &lt;strong&gt;One visible update doesn’t necessarily correspond to one token—or even one character.&lt;/strong&gt; Some tokens represent byte fragments that need to be combined before they form a complete character.&lt;/p&gt;

&lt;p&gt;So don’t use the screen’s refresh rhythm as a token counter. &lt;strong&gt;Token-by-token generation describes the generation mechanism. Incremental display describes what the user sees.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We now have the main pieces, from receiving a message to showing an answer. Let’s put them together.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Putting the whole process into code
&lt;/h2&gt;

&lt;p&gt;We’ve taken apart the steps between hitting Send and seeing a response. Time to put them back together.&lt;/p&gt;

&lt;p&gt;First, follow the complete flow:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzj6icvt54w72wnrtiy6d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fzj6icvt54w72wnrtiy6d.png" alt="Complete autoregressive generation flow" width="800" height="1067"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the pseudocode. Every line should connect to something we’ve already covered:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# 1. Format the conversation with roles and message boundaries.&lt;/span&gt;
&lt;span class="n"&gt;messages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Translate “我喜欢猫” into English."&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;format_chat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# 2. Convert the formatted input into token IDs.&lt;/span&gt;
&lt;span class="n"&gt;prompt_ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tokenizer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Track only the answer generated in this response.&lt;/span&gt;
&lt;span class="n"&gt;answer_ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_new_tokens&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# 3. Current context = original input + answer so far.&lt;/span&gt;
    &lt;span class="n"&gt;context_ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prompt_ids&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;answer_ids&lt;/span&gt;

    &lt;span class="c1"&gt;# 4. Score the candidates and select the next token.&lt;/span&gt;
    &lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context_ids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;probabilities&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;softmax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;next_token_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;select_token&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;probabilities&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# 5. Stop if the selected token is the end marker.&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;next_token_id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;end_token_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;

    &lt;span class="c1"&gt;# Otherwise, record the token and continue.&lt;/span&gt;
    &lt;span class="n"&gt;answer_ids&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next_token_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# 6. Decode the answer's IDs into text and display it.&lt;/span&gt;
&lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tokenizer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answer_ids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;answer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This pseudocode illustrates the logical flow. &lt;code&gt;model()&lt;/code&gt; wraps an already-trained model and returns only the scores used to predict the next token. &lt;code&gt;max_new_tokens&lt;/code&gt; limits the number of generation steps; &lt;code&gt;end_token_id&lt;/code&gt; is the end marker used in this example. We’ve omitted inference optimizations such as caching and chosen to display the answer only after generation finishes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A complete answer doesn’t simply fall out of a single call to &lt;code&gt;model()&lt;/code&gt;. &lt;strong&gt;The model scores possible next tokens using the current context. The surrounding program selects a token, appends it, checks whether to stop, and ultimately displays the result.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That leaves one very important function still waiting to be opened up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context_ids&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A list of integers goes in. Candidate scores come out. What happens in between?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In the next article, we’ll step inside &lt;code&gt;model()&lt;/code&gt;: how token IDs become vectors, how those vectors incorporate context, and how the result becomes scores for the next token.&lt;/strong&gt; We’ve traced the outer loop. Next, we’ll look at the machinery inside it.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Where do we go from here?
&lt;/h2&gt;

&lt;p&gt;In this article, we followed a message all the way through to a generated answer. Some of the most interesting work is still hidden inside &lt;code&gt;model()&lt;/code&gt;. That’s where we’re headed next.&lt;/p&gt;

&lt;p&gt;Why am I writing this series?&lt;/p&gt;

&lt;p&gt;Because I’ve been that engineer: plenty of experience shipping software, using AI every day, yet struggling to explain what actually happens under the hood. Try to learn the basics and you immediately meet a wall of terminology—tokens, embeddings, attention, Transformers. I came looking for an explanation and got handed a glossary.&lt;/p&gt;

&lt;p&gt;So I’m approaching these questions as &lt;strong&gt;a working software engineer without a machine learning background&lt;/strong&gt;. We’ll start with the input, follow the data, and see how the output is produced. When a detail is essential to understanding the mechanism, we’ll dig deeper. Along the way, we’ll distinguish what needs a high-level mental model from what deserves a closer look.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Feb45jxpkr571l5s3vgrv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Feb45jxpkr571l5s3vgrv.png" alt="Series roadmap: currently at Lesson 01" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The series is currently planned in three parts:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Part&lt;/th&gt;
&lt;th&gt;Focus&lt;/th&gt;
&lt;th&gt;Where we’re headed&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Part I · How LLMs Work&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The mechanics behind a response&lt;/td&gt;
&lt;td&gt;From using AI to understanding how it works&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Part II · Why LLMs Behave This Way&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Why they work well sometimes—and fail at others&lt;/td&gt;
&lt;td&gt;From understanding the principles to getting better results&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Part III · Connecting LLMs to Real-World Applications&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Putting models to work in actual products and workflows&lt;/td&gt;
&lt;td&gt;From a working demo to a dependable system&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;You’re at Lesson 01 of Part I.&lt;/strong&gt; The rest of Part I will cover the model’s internals, memory, where its knowledge comes from, and how it learns to answer, translate, or continue text according to instructions. The specific lessons in Parts II and III will take shape as the learning and practical work progress.&lt;/p&gt;

&lt;p&gt;I’ll learn, check, and write as I go. I’m not waiting until I know everything to start—otherwise, this series might never get past an empty project folder.&lt;/p&gt;

&lt;p&gt;But learning in public doesn’t mean presenting guesses as facts. I’ll check the key claims and make the limits of teaching simplifications explicit. This series is both my learning notebook and something I hope others can learn from. If you spot an error, a skipped step, or a passage where every word makes sense but the explanation still doesn’t, please say so. Let’s work out how to explain it better.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next up: inside &lt;code&gt;model()&lt;/code&gt;, following what happens to that list of token IDs.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;Explore the bilingual series on &lt;a href="https://github.com/xiaoxuxiansheng/llms-and-agents-explained" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, or read the &lt;a href="https://github.com/xiaoxuxiansheng/llms-and-agents-explained/blob/main/01-how-llms-work/01-from-message-to-answer/README.zh-CN.md" rel="noopener noreferrer"&gt;Simplified Chinese edition&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
