← Agentic AI Engineer
21 min

How LLMs Actually Work

Tokens, context windows, chat templates and sampling — the four things every agent engineer needs to understand correctly before anything else in this path makes sense.

Lesson 1 of 9
0 done
In plain English

An AI language model reads small pieces of text called tokens and predicts what comes next. It can only look at a limited amount of text at one time. Everything you send, such as instructions, tools and past messages, uses up that space and costs money.

Token
A small piece of text, roughly a short word or part of a word.
Context window
How much text the model can read in one request.
Temperature
A setting: low is steady and predictable, high is more varied.
Picture it
How a model writes text

What it can read at once is its context window. That space is limited, and it costs money.

1
Read the tokens so far
Tokens are small pieces of text
2
Predict the next token
The most likely next piece
3
Add it and continue
Until the answer is complete
Then it goes back to step 1 and repeats

A chat app hides a few details from you. The moment you build an agent, those details become your job. An agent is a program that calls the model in a loop and lets it use tools. Four things matter most: tokens, the context window, messages and sampling.

Tokens: the pieces a model reads

A model does not read letters or whole words. It reads tokens. A token is a small chunk of text. Common words are often one token. Rare words are split into pieces.

tokenizer.encode(...)
"unbelievable"      -> ["un", "believ", "able"]      (3 tokens)
"the"                -> ["the"]                        (1 token)
"gpt-5-mini"         -> ["gpt", "-", "5", "-", "mini"]  (5 tokens)
"🚀"                  -> ["🚀"] or multiple bytes         (varies by tokenizer)

Why this matters: everything you send is counted in tokens. That includes the tool descriptions, every earlier message and every tool result. As a rough rule for English, one token is about four characters, or three quarters of a word. Code, JSON and other languages usually take more tokens than plain English.

→If an agent is slow or costly, count the tokens before you guess. Most providers give you a way to count them. Use it on your real tool descriptions and your real message history.

Context window: one shared budget

The context window is how much text the model can look at in one request. Your input and the model's answer both count. Today's big models can handle from about a hundred thousand tokens to over a million. That sounds like a lot. An agent fills it faster than you expect.

Here is what shares the space:

  • The system prompt, which holds your agent's instructions
  • The name, description and fields of every tool
  • The whole conversation so far, including every earlier tool call and result
  • Any documents you add for RAG
  • The model's own answer for this turn

This is the trap that catches most beginners. A simple agent adds every message to the history and sends all of it again on each turn. So turn 10 sends the results of turns 1 to 9 again. The total grows fast. Big tool results, like a full web page, can use up the window in just a few turns.

⚠Models do not read a long text evenly. Tests show they often remember the start and the end best, and miss details in the middle. A bigger window does not mean the model remembers everything equally well.

Real agents manage this. They shrink old turns into short notes. They drop big tool results once they are used. They fetch only the history they need. You will build this later in the path. For now, remember one thing: the context is a budget. You manage it. You do not just fill it.

Messages and roles: how a chat becomes one prompt

You do not send plain text to a chat model. You send a list of messages. Each message has a role, such as system, user, assistant or tool. The provider joins them into one long sequence, using special marker tokens, in the form the model was trained on.

messages array
[
  { "role": "system", "content": "You are a support agent. Use tools to look up orders." },
  { "role": "user", "content": "Where's my order #4821?" },
  { "role": "assistant", "content": null, "tool_calls": [
      { "id": "call_1", "name": "get_order", "arguments": { "order_id": "4821" } }
  ]},
  { "role": "tool", "tool_call_id": "call_1", "content": "{ \"status\": \"shipped\", \"eta\": \"Thu\" }" },
  { "role": "assistant", "content": "Order #4821 shipped and should arrive Thursday." }
]

Look at the tool message. This is how nearly every agent works. The model does not run anything itself. It writes a request: please call this tool. Your code runs the tool. Then you send the result back as a new message with the same id. If the roles or ids are wrong, most APIs reject the request. This is a common first bug.

Sampling: how the model picks the next token

At each step the model scores every possible next token. Sampling settings decide how one gets picked.

  • temperature: A low value (0) means it nearly always picks the top choice. A higher value (0.7 to 1) allows more variety.
  • top_p: It picks only from the most likely tokens and ignores the unlikely ones.
  • max_tokens: This is a hard limit on the answer length. If it is reached, the answer stops half way. That can cut a tool call or a JSON block in half.
→Use a low temperature (0 to 0.2) when the model picks tools or fills a fixed format. You want the same correct choice every time. Use a higher setting only for writing that people will read, like a final summary.

Put it together. Each turn, your instructions, tool descriptions and message history are turned into tokens. They count against the window. They are arranged into the exact format the model expects. The model then picks its answer using your sampling settings. Every later lesson adds structure to this same loop.

→Before you move on, open a prompt in the library. Read it the way a model would: as a list of tokens, each with a role, inside a limited budget.
Key takeaways
  • ✓Models read tokens, not words. Tool descriptions, history and tool results all spend the same budget.
  • ✓The context is a budget you manage. A loop that re-sends everything grows very fast.
  • ✓Tool calling is a round trip: the model asks, your code runs it, and you send back a matching tool message.
  • ✓Use a low temperature for tool choice and fixed formats. Save higher values for writing.
Hands-on exercise

Measure where your tokens go

  1. Take a prompt with three or more tools. Count the tokens for the system prompt, the tool descriptions and one realistic tool result.
  2. Imagine a 10-turn loop where each turn adds a 2,000-token tool result. Work out the total tokens sent over all ten turns.
  3. Work it out again, but cut each tool result to 300 tokens once it has been used. Write down how much you saved.
  4. Write one sentence about what you would cut first in a real agent.
Check yourself
Q1Why can a 200,000-token window run out after only a few agent turns?show answer

Each turn sends the whole history again, including every earlier tool result. Big tool results pile up, so the total grows very quickly.

Q2What happens when max_tokens is reached in the middle of a tool call or JSON?show answer

The answer is cut off half way. The tool call or JSON is left unfinished and cannot be read.

Q3In the message list, what links a tool result to the request that caused it?show answer

The tool_call_id. It must match the id of the assistant's tool call. Most APIs reject a mismatch.

Prompting & Structured Outputs for Engineers →
0 / 9 finished0%