Sub topic 1
What a model call actually does
A coding agent looks like a collaborator that remembers your project. Underneath, it is a program that makes repeated calls to a language model. This chapter explains what one call does, because every later idea in this tutorial builds on it.
Prerequisites
This chapter assumes you have:
- Basic programming skill in Python or JavaScript.
- Familiarity with JSON and HTTP APIs.
- Some experience with a command line and a code editor.
- A general idea of what a large language model is.
Text in, text out
A language model takes a sequence of text and predicts what comes next. It does not read text as characters or words. It reads tokens, which are chunks of text such as a short word, part of a longer word, or a punctuation mark. A rough rule for English is about four characters per token. Code often uses more tokens per line than prose, because of symbols and unusual identifiers.
A model call has three parts:
- You send the input: a list of tokens, made from your text.
- The model produces output one token at a time. Each new token depends on the input and on the tokens it has already produced.
- Generation stops when the model emits an end marker or reaches a length limit you set.
You pay for, and are limited by, both the input tokens and the output tokens.
Calls are stateless
The key fact is that a call is stateless. The model keeps no memory between calls. When a call finishes, nothing about it stays inside the model. If you send a second request that contains only "Now fix the bug", the model has no idea which bug you mean.
The apparent memory comes from the program that calls the model. This program is the harness. On every call, the harness sends the whole conversation so far: the original task, each earlier reply, and anything else the model needs. The model reads all of it again from the start.
A worked example
This sketch shows the pattern in Python. The function call_model is a placeholder for your provider's API. It takes a list of messages and returns the reply text.
def call_model(messages):
# Placeholder: a real version sends `messages` to a provider over HTTP
# and returns the generated text.
return f"(reply based on {len(messages)} messages)"
history = []
def ask(user_text):
history.append({"role": "user", "content": user_text})
reply = call_model(history) # the entire history goes out every time
history.append({"role": "assistant", "content": reply})
return reply
print(ask("Write a function that adds two numbers."))
print(ask("Now add type hints."))The second call works only because history still holds the first exchange. If you passed just "Now add type hints." to call_model, the model would have nothing to add hints to.
The role field marks who wrote each message. The next chapter covers roles in detail.
Consequences you will meet again
Statelessness has practical effects:
- Cost and speed grow with history. Each call re-sends everything, so long conversations cost more per call.
- The input has a ceiling. The model can read only a fixed number of tokens at once. Chapter 3 covers this limit, the context window.
- Anything not in the input does not exist for the model. It cannot see your files, your terminal, or the web unless the harness puts that text into the messages.
- Output is not deterministic by default. Most providers sample tokens with some randomness, so the same input can give different replies. A low temperature setting reduces this variation.
Why this matters for agents
A coding agent is a harness that calls the model, acts on what comes back, and calls again with the result added. Everything the agent "knows" at any moment is whatever text the harness placed in the current input. Later chapters build on this: tools return text, errors return text, and managing the agent largely means managing that text.
Exercise
A learner builds a chat script that sends only the newest user message on each call. The first reply is good, but the second reply ignores the first exchange. What is wrong, and how do you fix it?
Show answer
The model is stateless, so it sees only what is in each request. The script must keep a list of all earlier messages, append each new user message and each model reply to it, and send the whole list on every call.
Exercise
A conversation has grown long, and each new call is slower and more expensive than the first. Why does this happen?
Show answer
The harness re-sends the entire history on every call, so the input grows with each turn. The model must process more input tokens each time, which increases both cost and latency.