Well, fancy seeing you again.
I realize for you, this is just like the very next video along from videos you're already watching.
But for me, this is me coming back like four months later to give you a latest update from the front
lines to show you something super interesting that fits perfectly at this point in the course.
So I'm slipping this extra in to show you something really cool.
During week one, I explained that there's no standard definition of the word agents, and it can really
mean whatever you want it to mean.
But there are some hallmarks of common agent solutions.
But I will say that over the course of the last few months, there has become a kind of evolving and
somewhat solidifying definition of agents that people are using more and more in the field.
And so I did want to get that across.
And then I wanted to show you something to make it feel real.
In the early days of agents, Sam Altman and OpenAI would sometimes describe them as AI systems that
can do work for you independently.
Thinking a bit about the GPT operator now called the GPT agent kind of paradigm.
And then in early 2025, there became this consensus that came from Anthropic's seminal post and from
Hugging face, that the right way to define a genetic AI was AI systems where an LLM controls the workflow.
And that's been the sort of solidifying definition for most of 2025.
But now there's a new evolving definition that has really taken hold in 2026.
And that's what I want to explain.
As of 2026, the most common definition of a genetic AI is it's where you have an LLM that's equipped
with tools, and it runs those tools in a loop to achieve a goal.
An LLM that runs tools in a loop to achieve a goal.
And it's one of these things that you know it when you see it.
And I highly recommend the tech writer Simon Willison, who has a terrific blog.
And this is one of the points that he makes very clearly there.
And when you're running such an agent system in a loop, this is what it feels like.
You write some software it calls the LM, and then the LM is off in its loop.
Calling, running, calling tools, calling tools until it's done and back comes the answer.
It's always important, as an AI engineer, to keep in the back of your mind that even though it feels
this way, this is the experience that you have as it's off doing its thing.
What's actually happening is, of course, this what's actually happening is that you've written some
software that is repeatedly calling an LM, and when they're stopping, reason is tool calls.
You're calling the tool, you're calling the LM.
Again, the LM is just something that generates likely tokens.
And you are interpreting those tokens and using them to make tool calls and giving this kind of illusion
that there is something out there that has the ability to be using these tools and keeping on going
until it's satisfied that it's done the job.
And so with that, let's see it in action.
Let's go to cursor and build ourselves an LLM with tools in a loop to achieve a goal.
Okay, here I am in cursor.
I'm going into week one foundations.
I'm going to the new five extra and extra addition to week one.
The unreasonable effectiveness of the agent loop a little recap on what an agent is and making it real.
I'm going to start with some imports and I'm loading Am and I trust at this point you are pros with
EMV.
I don't need to talk about that.
I'm also importing this thing called Rich console, which is just a nice little library that lets you
print to a terminal output and still have some colors in an easy way because that's going to look nice.
All right.
And in fact, so the way you do that is you call something called console dot print.
And just just so that I can handle if that ever throws an exception or anything, I've just made this
little utility show which tries to do a console dot print.
Otherwise it just prints just a little simple way to show anything.
Um, okay.
And then the usual OpenAI's OpenAI that you know.
Well.
And now let me tell you about to dos.
This is super simple.
I've got two lists.
One is called to dos and one is called completed to dos.
We'll just have a list of strings of things I need to do, and completed will be trues and falses.
If it's been done.
And I've got a little function here, get to do report and it just prints out what's in that to dos
list.
So if I run that, it prints nothing.
Okay.
Nothing clever so far.
Bear with me.
This is going to deliver.
Here is a function create to dos.
It takes a set of descriptions a list of strings, and it adds them to the to dos.
And it puts false in in the completed so that they're all not completed.
And then it gets the report okay.
And then another little function.
The last of these called Mark completed, which takes an index and some notes on how it's being completed.
And it just prints out what's happened.
So let's see this in action again.
We will empty to dos and completed and we will create some to dos.
Buy groceries, finish extra lab, eat banana.
I run that and look.
It prints out this nice little to do list and formatted text to do.
One is to buy groceries.
To do two is finish the extra lab.
And shockingly, three is to eat the banana.
Okay.
And now I am going to try marking complete the first item there.
As with the comment of bought.
Let's see what happens if I do that.
Well bam, it prints out and you can see I've been a bit fancy with my formatting.
It puts a strike a strike through the buy groceries and but the other two items are still there.
It works great.
Nothing about AI here.
This is just like vanilla python.
Just doing something to manage a to do list with two functions.
That's all we've done.
But we're going to give that as tools to an agent and put it in a loop.
Well, you know the scoop.
We have to come up with janky JSON.
Here is JSON for create to do's.
It simply describes that function.
Add to do's from a list of descriptions.
It describes the kind of data it needs.
It's the JSON that's same as we use before.
And this is the mark complete JSON.
Feel free to take a look at it, but it says Mark completed to do at the position and return the full
list.
That's the description of each of the different fields it needs to provide.
Done.
And now we put that into one list of tools.
Exactly as we did for the career conversation.
That's it.
Now time for running the tools in a loop.
Okay.
Handle tool calls.
This function is exactly the same as the function from from the career conversation.
And the reason I know it's exactly the same is I copied and pasted it in here.
I think I might have taken out one thing that wasn't needed.
And we probably don't need this this print statement here.
That's not going to we don't need that.
We can keep this as short and sharp as we can handle tool calls.
That just iterates through.
And you can see here it calls that that globals trick.
That makes this a nice short and sharp pythonic function.
And look at this function here.
This is a function whose name is a loop.
So we probably better look at it.
I am saying while not done, while you're not finished, it's a while loop just like the other while
loop.
It then calls OpenAI create.
I'm using what is for me right now the latest model, GPT 5.2.
You can pick whatever model you want, might have different behavior, but but as you wish, it's going
to pass in the messages.
I'm going to take the tools that we've just created up here.
And I'm saying reasoning effort is none for these latest reasoning models.
We want it to be nice and quick for this to work well.
So I don't want it to be going off and thinking and making extra tokens to describe a process.
And then this is something that you're familiar with.
We do finish reason is response reason, and if it wants to call a tool then we go ahead and call the
tool and record the output.
If it's decided it does not want to call a tool, then we say the loop is done because it's in a loop
calling tools until it's done, and then we will print the results using that show thing that I wrote
before.
That just shows it in a formatted way or just prints it.
Normally that is a loop.
It's a very simple loop, a simple while loop.
Let's try it.
Let me give you a system message.
You have a problem to solve using your to do tools to plan out the steps and then carrying out each
step in turn, blah blah blah blah blah blah.
Provide your solution in rich console markup.
Do not ask the user questions, blah blah blah.
Then the user message A train leaves Boston at 2 p.m. traveling 60 miles an hour.
Another train leaves New York at 3 p.m., traveling 80 miles an hour towards Boston.
When do they meet?
That is what we're putting in messages that the system message and the user message and the usual list
of dicts.
I've done that.
And now.
And now I'm emptying out my to do's and completed.
And I'm just gonna call this loop with those messages.
That's all I'm doing.
I'm just calling this loop with this set of messages right here.
And it's equipped with those very simple to do tools.
Let's see what happens.
Here we go.
Off it goes.
Oh what's that.
It's printed out a few things it wants to do.
Interpret the problem.
Set up variables.
Estimate any missing quantity.
Compute.
Oh look at that.
It's just doing things and I'm not touching it.
It's doing things.
It's crossing them off the list.
Things are printing out.
It's done all four to do's and bam!
There's the answer that that is an LM in a loop with tools to achieve a goal.
Now, of course, this isn't big news for you because you've seen things like Claude Code do this.
And in fact, Claude code, if you've seen it looks very like this.
It has to do's because I sort of cribbed the idea of having a to do list from Claud code.
The thing that might be be news to you.
The thing that you should recognize is how insanely easy it was to build this, how basic it was.
We just built this from first principles.
We just have an LLM that generates tokens, and we turned it into this thing that gives us the impression
that there's there's something out there that's busy working and thinking and planning and tracking
and crossing things off the list.
But in fact, it is simply calling an LLM repeatedly in a while loop and giving it tools and letting
it use the tools to make progress.
And so that that is hopefully really solidifies it really makes it real for you, makes it concrete
that you see that it's very easy to build an agent loop from first principles.
And the other point to make, of course, is that it turns out that using these kinds of techniques
results in better outcomes.
It's kind of obvious in some ways, but it still it needs to be thought about a bit, just simply getting
an LLM to generate tokens as the answer doesn't perform.
As well as using these sorts of agentic techniques that causes it to take longer to work through and
reason its way through a difficult problem.
And that's why it's so powerful.
But also, of course, if using these tools is actually doing something like creating a file or running
something or calculating something, and you can add those sorts of tools in here and see it get better
and better at what it's doing.
Okay.
But with that, that's a wrap on this great simple example.
And there is of course, an exercise for you.
And here's the exercise.
You know, I'm not a fan of typing things in from scratch, from nothing, or trying to memorize things
because it's just not the way that it works anymore.
Ides and Llms can help autocomplete code so fast, it's more important to understand what's happening
than to be able to memorize and type things out by rote.
But here's an exception.
Perhaps.
I do think that it's incredibly beneficial for everybody to have had one crack at writing a simple agent
loop from first principles, just using the OpenAI library and creating basically doing what I just
did there.
It's it's both very, very rewarding and satisfying, but it's also very instructive to see that coming
together and really understand the nuts and bolts.
So I challenge you now to start a new fresh Python notebook.
Go through it and try and create your own agent loop from scratch.
And of course refer back to this one.
And from time to time, I mean, I copied and pasted that some of this, like the handle tool calls.
I couldn't be bothered to write that again and nor should you, but there are parts of it like just
the to do's and the completed.
It's so interesting and fun to build that from scratch and then run it and see it come to life.
And then think of some interesting different problems that you can give your LLM to see it reason its
way through, and maybe add in some extra tools while you're doing it and have fun with it.
And I hope you found this both interesting and revealing and hopefully also very satisfying.