Sub topic 2
LLM foundations an agent builds on
This chapter assumes you can write Python, call web APIs, handle JSON, and have a working idea of what a large language model (LLM) is. It covers the three ideas every later chapter relies on: prompts, tokens, and the context window.
The model is a stateless function
An LLM takes text in and returns text out. It remembers nothing between calls. If you want it to "remember" an earlier turn, you send that turn again.
An agent is built on this fact. Every step of the agent loop sends the whole conversation so far and receives one new reply. The list of messages you keep is the agent's only memory.
Prompts are lists of messages
Most chat APIs take a list of messages. Each message has a role:
| Role | Purpose |
|---|---|
| system | Standing instructions for the model |
| user | The request or goal from the person |
| assistant | Earlier replies from the model |
| tool | Results of actions the program ran |
The model reads the list from top to bottom and predicts the next message. Wording, order, and examples in that list change what it writes.
Tokens and the context window
A token is a chunk of text, often a short word or part of a word. Models read and write tokens, not characters. English text averages roughly four characters per token, but code, JSON, and non-English text often use more.
The context window is the maximum number of tokens the model can handle in one call. Input and output share it. Your system prompt, the full message history, tool results, and the reply must all fit.
Agents hit this limit faster than chatbots. Each loop step adds an assistant message and a tool result, so the history grows with every step. A single large tool result, such as a full web page, can use most of the window.
Counting tokens before you send
Estimate size before each call so you can act before the API rejects the request. This sketch uses a rough rule of thumb. For exact counts, use your provider's tokenizer.
def estimate_tokens(text: str) -> int:
# Rough rule: about 4 characters per token for English text.
return max(1, len(text) // 4)
def history_tokens(messages: list[dict]) -> int:
return sum(estimate_tokens(str(m.get("content") or "")) for m in messages)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Summarize the report."},
]
print(history_tokens(messages))Sampling and determinism
The model picks each next token from a probability distribution. The temperature setting controls how much randomness that choice has. A low temperature, near 0, gives more repeatable output. Agents that call tools usually do better with low temperature, because a malformed tool call breaks the loop. Even at 0, outputs are not guaranteed to be identical across runs.
Common errors
- Assuming the model remembers. If a fact is not in the messages you send, the model does not have it.
- Ignoring output budget. If the history fills the window, the reply is cut short or the call fails.
- Trusting fluent text. The model can state false things confidently. Later chapters cover grounding and checks.
- Pasting raw tool output. Large results waste tokens. Trim or summarize them first.
Exercise
An agent has run five steps and its next call fails with a context-length error. The system prompt is short. Name two likely causes and one fix.
Show answer
Likely causes: the message history has grown with every assistant message and tool result, and one or more tool results were very large, such as whole documents. A fix is to truncate or summarize tool results before adding them to the history, or to drop or condense older messages.