Skip to content

01 · The LLM Call, Revisited as an Agent Primitive

We’ll start with an ordinary model call and look at the boundary between the model and the application:

An agent is built out of ordinary model calls.

There is no special “agent intelligence” hiding somewhere underneath.

At the bottom of everything we’re going to build over the next nine lessons is essentially this:

Python prepares context
↓
Python calls model
↓
Model returns something
↓
Python decides what happens next

Today we’ll isolate that primitive and make sure the boundaries are completely clear.


Using the OpenAI Python SDK:

The SDK is a Python library that prepares HTTP requests and gives us response objects. OpenAI() creates a client. If you choose to execute the API examples, it expects configured credentials, usually an OPENAI_API_KEY environment variable, and model usage has a cost. You need neither an account nor an API key to read the lessons.

The reviewed examples use Python 3.13.18, openai==3.55.1, and the documented gpt-5.5-2026-04-23 snapshot as of November 19, 2026. Pydantic 2.14.4 appears in the next lesson. Verification uses synthetic responses; the sample answers are illustrations, not live measurements. Model reference

from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5.5-2026-04-23",
instructions="""
You are a software assistant.
Give concise, practical answers.
""",
input="Explain what a database transaction is."
)
print(response.output_text)

instructions provides guidance for this request. input supplies the task or conversation items. response is a Python object representing what came back; response.output_text collects its text output. The raw response can also contain non-text items, which we’ll inspect in Lesson 3.

This opening example follows a successful response. A response can instead be refused, incomplete, or empty, and an API request can raise an exception. Later examples add explicit checks; a printed sentence alone never establishes that an external action happened.

Ignore agents for a moment.

What actually happened?

Your Python program created an HTTP request containing roughly:

MODEL:
gpt-5.5-2026-04-23
INSTRUCTIONS:
You are a software assistant.
Give concise, practical answers.
INPUT:
Explain what a database transaction is.

The OpenAI server ran the model.

The server returned a response.

Your Python program received it.

That’s it.


This distinction becomes critical once we start building agents.

The model can reason about what should happen.

Your Python application controls what actually happens.

For example, imagine asking:

response = client.responses.create(
model="gpt-5.5-2026-04-23",
input="""
A customer ordered an item that is out of stock.
Decide what we should do.
"""
)

The model might answer:

Issue a refund and notify the customer.

But nothing happened.

No refund occurred.

No email was sent.

No database changed.

The model produced information.

Your program would need to perform the actions.

Conceptually:

MODEL:
"Issue refund."
PYTHON:
refund_customer()

That boundary is the foundation of tool calling, which we’ll reach in Lesson 3.


This sounds obvious, but many confusing agent diagrams blur this distinction.

Suppose we write:

task = """
Check whether invoice 4812 has been paid.
If not, send the customer a reminder.
"""

And send that to a model.

The model cannot magically inspect your database.

It cannot magically send the email.

It only knows what you provided in the request plus whatever capabilities the API explicitly exposes.

So this:

"Check invoice 4812"

is merely language.

For it to become an actual software operation, something has to connect language to code.

Eventually we’ll build:

Model decides:
check_invoice(4812)
Python executes:
check_invoice(4812)
Python sends result back:
unpaid
Model decides:
send_reminder(4812)
Python executes:
send_reminder(4812)

That is where “agentic” behavior begins.

But every model interaction is still just another ordinary API call.


A useful mental model is:

CONTEXT
↓
┌──────────────┐
│ MODEL │
└──────────────┘
↓
OUTPUT

Context includes things such as:

instructions
user request
conversation history
tool definitions
tool results
retrieved documents
application state

The model receives the context available for that call.

It produces an output based on that context.

This matters because the model does not somehow possess your application’s live state.

If your database says:

document.status = "approved"

but you never tell the model that, the model does not know it.


5. Models do not automatically remember previous Python calls

Section titled “5. Models do not automatically remember previous Python calls”

Consider this:

response1 = client.responses.create(
model="gpt-5.5-2026-04-23",
input="My favorite database is SQLite."
)
response2 = client.responses.create(
model="gpt-5.5-2026-04-23",
input="What is my favorite database?"
)
print(response2.output_text)

You should not design your application assuming that response2 inherently knows what happened in response1.

From the application’s point of view, those are separate calls.

You need to provide continuity.

One simple approach is explicitly carrying the earlier information:

response = client.responses.create(
model="gpt-5.5-2026-04-23",
input="""
Earlier the user said:
My favorite database is SQLite.
Current question:
What is my favorite database?
"""
)

Later we’ll handle state much more cleanly.

But the important architectural principle is:

Your application owns state.

Not the model.

That’s Lesson 5 in this series, and it becomes one of the biggest differences between a demo and a reliable agent.


6. Your first tiny “agent-shaped” program

Section titled “6. Your first tiny “agent-shaped” program”

Let’s move one inch toward agents without actually building one yet.

Suppose we want a program that converts a vague request into a suggested next action.

from openai import OpenAI
client = OpenAI()
def decide_next_step(task: str) -> str:
response = client.responses.create(
model="gpt-5.5-2026-04-23",
instructions="""
You help operate a software system.
Given a task, identify the single next action
that should happen.
Do not perform the action.
Only describe it.
""",
input=task,
)
if response.status != "completed" or not response.output_text.strip():
raise RuntimeError("No completed text decision returned.")
return response.output_text
task = """
A user uploaded a PDF.
The file has already passed validation.
It has not yet been analyzed.
"""
next_step = decide_next_step(task)
print(next_step)

Possible output:

Run the document analysis process on the uploaded PDF.

Notice the architecture:

Python
owns task
Python
sends task to model
Model
decides next step
Python
receives decision

The small guard rejects an incomplete response or an absent text decision. It is not an assessment of whether the proposed action is correct. Lesson 4 handles response items explicitly, and Lesson 6 develops failure handling.

Right now Python just prints the decision.

Eventually Python could act on it.

That transition is the heart of agent engineering.


7. The model is the nondeterministic component

Section titled “7. The model is the nondeterministic component”

Compare these two functions.

def add(a, b):
return a + b

If we run:

add(2, 3)

we expect:

5

Every time.

Now:

def decide_next_step(task):
return call_model(task)

Given the same input, the model might phrase or even reason about the answer differently.

For example:

Analyze the PDF.

or:

Send the validated PDF to the document-analysis service.

or:

Begin document analysis.

All may mean the same thing.

This becomes a problem when Python needs to react to the result.

Python would hate this:

if response == "Analyze the PDF":
...

because the model could respond with 500 semantically equivalent sentences.

Which brings us directly to our next lesson:

Structured outputs as contracts.

Instead of asking the model for arbitrary prose, we’ll eventually want something like:

{
"action": "analyze_document"
}

Now Python has something reliable to work with.


From here forward, I want you to mentally separate three things.

Layer 1 — Deterministic application logic

Section titled “Layer 1 — Deterministic application logic”

Your Python.

if document.status == "uploaded":
...

Reliable, testable, explicit.

The LLM.

Given this situation, which action makes sense?

Flexible and probabilistic.

Things your system can interact with:

database
email
web APIs
filesystem
browser
payment system
search

Eventually our architecture becomes:

┌──────────────┐
│ MODEL │
│ reasoning │
└──────┬───────┘
│
decision
│
↓
┌──────────────┐
│ PYTHON │
│ control │
└──────┬───────┘
│
execution
│
↓
┌────────────────────────────────┐
│ database / APIs / files / web │
└────────────────────────────────┘

This picture will become increasingly important.

The model reasons.

Python controls.

Tools act.


Suppose Python does this once:

ask model
print answer

That’s an AI application.

Suppose Python does this:

ask model what to do
↓
perform action
↓
give result to model
↓
ask what to do next
↓
perform action
↓
repeat until finished

Now we have something agent-shaped.

In pseudocode:

RECEIVE task
WHILE task is not finished
ASK model what to do
IF model requests an action
EXECUTE action
GIVE result back to model
ELSE
RETURN answer

That WHILE loop is eventually going to become the center of this entire curriculum.

But notice what it contains:

ordinary model calls.

That’s the demystification I want you to keep.


Traditional program:

Developer determines sequence beforehand.
Step A
Step B
Step C
Step D

Agentic program:

Developer determines boundaries and capabilities.
Model decides which permitted step should happen next.
Step ?
Step ?
Step ?

The developer hasn’t disappeared.

The developer has moved up one level.

Instead of specifying every execution path, you specify:

what the model knows
what the model may do
what tools exist
what limits exist
what counts as finished
what happens when things fail

That’s why agent engineering is still software engineering.


No coding necessary.

Imagine we’re building an agent that handles incoming support requests.

Requirement:

Read the customer’s message, look up their account if necessary, and either answer the question or escalate it to a human.

Try to separate the responsibilities mentally.

A good decomposition would be something like:

PYTHON:
receive support request
MODEL:
understand customer's intent
MODEL:
decide whether account information is required
PYTHON:
if requested, retrieve account information
MODEL:
examine account information
MODEL:
decide whether it can answer safely
PYTHON:
if answer:
return answer
PYTHON:
if escalation:
create human-support ticket

The important question isn’t:

How would I code the agent?

It’s:

Which decisions belong to the model, and which actions/control belong to normal software?

That’s the habit we’re developing.


This tiny example:

response = client.responses.create(...)

looks almost ridiculously easy.

Real systems fail around it.

You expected:

Analyze document.

It returns three paragraphs instead.

Later we’ll fix this with structured outputs.


Network timeout.

Rate limit.

Temporary service failure.

Your application needs retry/error behavior.

That’s Lesson 6.


The model might misunderstand the situation.

You cannot simply assume:

model said it
therefore execute it

Some actions need validation or human approval.

That’s Lesson 7.


If you keep sending everything that ever happened:

message 1
message 2
message 3
...
message 10,000

cost and latency increase, and useful context gets buried.

That’s part of Lesson 5.


Eventually we’ll build:

while not finished:
...

What if the model never decides it’s finished?

You need:

MAX_STEPS = 10

Agent autonomy should always have boundaries.


6. It looks great in one demo but fails unpredictably

Section titled “6. It looks great in one demo but fails unpredictably”

This is one of the biggest traps.

You run the agent once:

perfect result

and think:

It works.

Then users give it 500 different inputs.

That’s why we’ll eventually build evals rather than trusting impressive demos.

Lesson 8.


Today the most important thing to internalize is:

An agent is not a magical new kind of model.
It is software that repeatedly uses a model
as one component in a controlled system.

Or even more compactly:

MODEL = reasoning
PYTHON = control
TOOLS = actions
STATE = memory
LOOP = agency

We haven’t built any of those abstractions yet.

Good.

Over the next lessons, we’ll construct them one at a time with normal Python so you can see exactly where each piece comes from.

Our current model can respond with essentially arbitrary text:

"Analyze the PDF."
"Begin analysis."
"I recommend analyzing the document next."

Humans understand that.

Python does not.

Next time we’ll turn fuzzy model output into reliable application data:

{
"action": "analyze_document",
"reason": "The document has passed validation."
}

That seemingly small change is one of the foundations of reliable AI systems.