---
title: 'Build an AI Agent From Scratch With Python'
source: 'https://youtube.com/watch?v=jcpZU2vyrf4'
video_id: 'jcpZU2vyrf4'
date: 2026-06-14
duration_sec: 1296
---

# Build an AI Agent From Scratch With Python

> Source: [Build an AI Agent From Scratch With Python](https://youtube.com/watch?v=jcpZU2vyrf4)

## Summary

This video provides a hands-on tutorial on building an AI agent from scratch using Python and the open-source Strands Agent SDK. The presenter explains the core components of an agent—the model (brain), tools (hands), system prompt (personality), and the agentic loop—and demonstrates how to implement them step by step. The final project is a tabletop RPG game master agent that can roll dice, look up rules, and track player state.

### Key Points

- **Components of an Agent** [00:36] — An agent consists of a large language model (brain), tools (hands) to affect the outside world, a system prompt (personality), and an agentic loop that iteratively processes inputs, runs tools, and returns outputs.
- **Using Strands Agent SDK** [01:30] — The Strands Agent SDK is a free, open-source framework that works with any model provider and hosting provider. The tutorial uses Python and UV as a package manager.
- **Creating a Basic Agent** [03:09] — Import the Agent class from strands_agents, instantiate an agent, and call it with a prompt like 'tell me a joke'. This invokes the LLM but doesn't use tools yet.
- **Adding a Tool** [04:47] — Use the @tool decorator to turn a Python function into a tool. The docstring is passed to the LLM so it knows the tool's purpose. Example: a weather tool that returns a hardcoded string.
- **Adding a System Prompt** [07:17] — Pass a system prompt to the agent constructor to define its personality, e.g., 'You are a helpful assistant who always responds in the voice of a pirate.'
- **Building a Tabletop RPG Game Master** [08:25] — The presenter builds a game master agent using Amazon Bedrock models (Nova 2 Light). The agent includes tools for rolling dice, looking up rules, and tracking player state.
- **Adding a Chat Loop** [13:26] — A while-true loop allows continuous interaction with the agent, maintaining conversation history until the user types 'quit' or 'exit'.
- **Advanced Agent with Hooks** [15:41] — Hooks allow custom code to run at various points in the agentic loop. Example: a tool call hook that prints the name of the tool being used.

### Conclusion

Building an AI agent from scratch is straightforward with the Strands Agent SDK. By combining a model, tools, system prompt, and the agentic loop, you can create powerful, interactive agents for various applications, from simple assistants to complex game masters.

## Transcript

Everybody's talking about AI agents.
These are programs that can reason, make
decisions, and take actions all by
themselves given an input from us. And
most of us are talking about agents in
respect to Kiro or Kiro CLI or Claude
code, those agents that can help us to
write applications and write code. But
in this video, you're going to get
hands-on and make your very own agent.
But before we do that, what components
make up an agent?
We start off with the brain or the
model, the large language model that
gives us the natural language
intelligence to be able to process the
inputs that the agent gets. But how does
the agent do anything? Well, this is
with the hands or the tools. These tools
give our agent agency to be able to
affect change in the outside world. Then
we have the system prompt. The system
prompt is essentially the personality of
our agent. This describes what we want
the agent to do and how we want it to go
about it. Now, all of those components
are brought together in the agentic
loop. This is an iterative loop that the
agent is in control of. When we put an
input into the top of the agent, it then
processes through the loop, running
tools, thinking, running another tool
until it decides it has the output, and
then it provides the answer back to us.
Now, to build our first agent, we're
going to use a framework, and that
framework is Strands Agent SDK. This is
a completely free and open-source
agentic framework. It works with any
model provider. It works with any
hosting provider. And for us today,
we're going to get an agent running on
our laptop using a model from the cloud.
Let's jump into some code. And here is
my coding environment. This is Kiro, and
Kiro gives me all of the AI goodness I
need to assist me in writing code. But
in this case, we're going to write it
ourselves from first principles. So, let
me go and grab myself up a terminal, and
I'm going to kick off a Python project.
So, Strands Agent SDK is available in
both Python and in TypeScript. Python's
more my thing, so that's what I'm going
to do. And so, I'm going to start off
this project by saying UV init because
I'm using UV as a package manager for my
project. If you haven't used that
before, I'll put a link in the
description below of how you can install
UV inside of your environment. So, with
UV init, you can see on the side here my
Python project has started. I now need
to include the Strands Agent SDK. So,
I'm just going to say UV add, and then
I'm going to say Strands {hyphen}
Agents. Hit enter, and all of those
packages are now being downloaded. And
that's all I need to be able to then go
ahead and build my first agent. So, if I
look on the side here, I've got main.py.
This is just the boilerplate main
function from the uh initialization of
the UV project. So, I can go ahead and
select all of that and remove it. Now,
let's start writing our Strands Agent.
So, all I need to do is import the
Strands Agent SDK. So, I'm going to say
from Strands
import, and then I'm going to import um
agent.
Pretty much all I need to do. Now, I'm
going to go and get myself an agent
object. So, I'm going to say agent
equals
and then I'm going to say
agent
and that's all I need to do there.
And now I can prompt my agent. So, I can
just say agent uh tell me a joke.
And this is one of the very standard
things that we ask an LLM to do because
of course the LLM is the intelligence
behind our agent. So, essentially all
we're doing here is invoking the LLM
once. We don't have any tools or
anything like that at this stage. We
will though in just a moment. So, let me
save that, and let me just clear my
screen here, and let's go and run that.
So, UV run main, and it's almost
certainly going to ask me to or tell me
the joke about the scientist. You
probably heard it before um because
that's what LLMs do. So, let's have a
look after we've run that. Yes, of
course. Why don't scientists trust
atoms?
Because they make up everything.
Okay. So, a really, really simple agent
working there, but all it's done is
we've taken the input, it's gone into
its agentic loop, it's thrown this query
essentially over to the large language
model, and then it's spat the answer
back out. It's no different to just
prompting a large language model. In
order to make this properly into an
agent, well, we've got to start adding
these other components. So, let's go
ahead and give this a tool. So, I can
extend out the things that I'm importing
from Strands Agents to also import tool,
and tool for us here is actually a tool
decorator that we can now decorate a
Python function. It becomes a tool that
we can then pass to our agent. So, let's
just say um def, I'm going to define my
own thing. I'm going to say weather. Uh
Kiro's trying to be helpful for me under
the hood there, which is pretty cool. Um
and then I'm going to say city, um which
is a string, and the whole thing's going
to respond with a string.
Um and then I can put in a docstring
here. Now, this docstring is more than
just helpful to me to remember what this
function's for. This docstring is
actually something that will get passed
to the agent and therefore the large
language model so it knows what this
tool is for. So, let's just say this is
um get the weather for a given city.
Um and we're not actually going to
implement this code right now, but we
are going to just return
um
it's nice and hot. So, whatever city it
asks for, it's always going to say it's
nice and hot. This is just a piece of
sample code. Now, we can pass that to
our agent like this. So, we can say
inside of the agent constructor, we can
say tools, and we can give it a list of
the tools that it's got available to it.
In this case, we're going to pass it
weather because that's the only tool
we've got. And I can hit save. So, let's
just review that code just for one
moment. So, we're importing Strands
Agent SDK, and we're also importing the
tool decorator. We're defining a
function here as weather that returns a
string, so something that's natural
language that the agent will be able to
make sense out of. And we're decorating
that with tool. That means now that this
is ready to be loaded as a tool into our
agent. We pass that into the agent here
with tools equals and the list of tools
we have. And now, well, let's ask it
something different. What is the
weather like in London?
Okay, and now let's clear this out so we
can run this again
and run this agent again. This time,
we're actually using the agentic loop
because what happens is it goes in and
decides, "Okay, I'm going to check the
weather in London." It then calls a
tool, it calls the weather tool, which
is the only tool we have, and then it's
come back with, "The weather in London
is currently nice and hot." Which of
course it's not, or it might be, I don't
know. We just made up the answer here,
but now you see an agent using a tool.
So, there's only one more piece left,
and that is the system prompt. And we
can do that here um by adding in again
to this. We can say system prompt
because at the moment it's basically
been super generic. Um and we can say
you are a
helpful assistant
who always responds in the voice of a
pirate, which is always super useful.
So, let's do that and add a comma at the
end of that, and it should all be happy.
And then let's go ahead and clear this,
and then we're going to run this again.
So, we've set the personality of our
agent, and now of course it's even
thinking in pirate speak. Ah, yes. The
seas be calm, and the weather be fine in
London. I'm not going to say any more of
that. Okay, so now we've got the basics
of an agent up and running. Let's start
to build up a small project. I like
tabletop RPG games, so let's build
ourselves a games master that's going to
be the beginning of a project you could
take a long way.
Now, you might have noticed that in the
first demonstration that we ran, it
worked, but we didn't actually specify
the brain. We didn't specify the model
that we were going to use. And that's
because Strands Agents will actually
pick up a model by default for us if we
don't. But it's going to be super useful
for us to be able to define the model
that we actually want to use, and so
that's what I'm doing in this code here.
So, I'm now saying from Strands Models
import Bedrock Model, and then that
gives us the opportunity to actually
specify the Bedrock model that we want
to use. Now, again, Strands Agent SDK
doesn't have to use Bedrock models from
AWS. It can use models from any
provider. Indeed, in a blog post that
I've got linked in the description
beneath this video, I go through pretty
much everything that I'm going through
in this video, plus I'll show you how to
connect it and with Ollama, a locally
running model. But we're using Amazon
Bedrock models here. So, let me just go
over to the Amazon Bedrock console page
in the AWS console because if we go to
model catalog over here on the left-hand
side, I can see a a list of all of the
models which are available for us in the
particular region that we're working in
from all of the different model
providers. And you'll see that there's
all of these models from Anthropic here.
We can go and see them from places like
Meta and Cohere and Deep Seek, um and
also models from Amazon ourselves as
well. And so, if I go into here, I can
find the Nova 2 light model. This is a
small, cost-effective model for getting
started. It's a great place to start.
And in the code that we have here, I've
got a model that's actually specified
here as Amazon Nova 2 light. So if I go
back to the model catalog for a moment,
you can see that Nova 2 light there is a
model ID right there. Now if I come back
to the code, you'll notice I've got us.
dot and that's because this is a
regional endpoint for this model. So
it's going to do the best it can
throughout the whole of the US because
I'm using US East 1 for this particular
example.
Um so then I've got the rest of my model
defined as we did before, but this time
I'm creating a tabletop RPG games
master. So I've said you're you're a
dramatic and entertaining tabletop RPG
games master. Keep the story short and
to the point. Um one or two paragraphs
at most. We can adjust this. You can
adjust this to create a games master
which follows your particular style. And
so then I can pass in my first prompt
essentially, I kick open the tavern door
and stride up to the bar. Barkeeper,
your finest ale. Okay, let's run this
and see what happens.
Um so again, I've just pasted this into
main inside of my project code here. And
yeah, look, it does exactly what we
thought and it starts off a story. And
as you kick open the creaky tavern door,
the raucous din of laughter and clinking
mugs momentarily hushes. And you can
read the rest of that to see what's
going to go on. I'm not going to turn
this into a let's play session.
Let's build and let's continue to build
this agent cuz you notice that we don't
actually have any tools here. So let's
go and add the tool back in. So I'm
going to go and add in the tools import
one more time. Um and then we're going
to find ourselves
a tool to add.
And so let me paste that in. And here we
go. This is actually one of my favorite
tools. I love playing around with this.
So this is a dice rolling tool or roll
dice tool. And it takes as an input the
number of sides on the dice. So pretty
much anything from a D4 all the way up
to a D20
and the number of dice you want to roll
and then it gives some general
instructions here.
And it gives instructions to the large
language model or in other words, the
agent as to what these arguments mean.
So the number of sides on each die and
then how many dice to roll. And then
look, all it's doing is just calculating
some random numbers. But it's kind of
fun. The one thing I'm going to need to
do is import
random.
So let's just do that up top so that
we've got everything ready for the code.
And then the next thing I need to do is
I need to add this in as a tool. So I'm
going to say tools equals and then I'm
going to add in my dice
roll dice tool like that and save there.
Okay, so at this point I can run this
again and likely nothing's going to
happen because I haven't necessarily
done something that requires the dice.
But let's just have a quick look.
It might decide that it's going to do
it. This is part of the
non-deterministic nature of agents of
course. Yeah, look, it's decided to roll
the dice because it's got one.
And so what does it say this time?
I don't know what I rolled. We can find
that out in a second. The barkeep
grumbles, eyes narrowing as he slams the
tankard down in front of you with a
clank. Doesn't sound very happy. I
wonder whether I rolled something low.
Actually, it's difficult to tell because
I can't get back into this agentic
conversation. It's finished at this
point. Okay, let's fix that. So I'm
going to clear that up and I'm going to
expand down here. And what I've got here
is just a quick chat loop that I can
paste in.
So if I just grab my
code from here. So instead of having
this, I can insert above here this. I'm
going to borrow this
because maybe we can use this in the
next call anyway. So let's copy that and
delete that. Okay. So now what I've done
is I've added in this quick while true
loop here. And what it's basically going
to do is it's going to allow me to enter
some text. When I've entered the text,
it will then send that to the agent and
then keep going around unless I type in
quit or exit. So let's press save on
that and now let's run this code again.
Now I've got a prompt. And so I could
actually just have a general
conversation with the agent. So I could
just say hello
and see what it returns back with.
Greetings, intrepid adventurers, etc.
etc. Now let's just say that what I want
to do is I want to kick open the tavern
door, stride up to the bar and say
barkeeper, your finest ale. So let's
just press enter on that and see. Now
let me just expand that up.
This time it decided not to roll the
dice, but it's given us a bunch of
things that we could do. Um so pay for
the ale. I haven't read the entire
story. I just don't really feel like I
have time to do that. What if I say roll
a D20? Just throwing the games master a
curveball here I suppose.
Oh, it's decided it's going to do a
charisma check. So it's rolled the dice,
done a charisma check and got 17. I'm
doing well with my charisma today. So
you can see here how we are getting
started. And if I had the time and if I
wanted to, I could actually clear this
out, start again and actually start a
agentic run campaign all by myself with
my agent. Now you can actually do this
and you could actually start to play
this for quite some time. At some point,
the context window's going to fill up
and the whole thing's going to come
crashing down. And we can take a look at
that in a subsequent follow-up video.
But for now, it's going to keep that
conversation history, in other words,
the game history going and you can
follow on with responses about things
that have happened about the game that
you're playing. Now before we wrap this
up, let's go quite a few steps forward.
I've got a more established games master
code that I can paste into here and then
we can step through what we've got.
Okay, here it is. And in this case, I'm
hopefully going to make it a little bit
nicer to look at and a little bit better
to play. Let's step through the code
that I've got here and of course, all of
the code which I've had for the entire
video up until this point is going to be
linked in the description below. So
first of all, I define some colors
because I want to make something look a
little bit nicer in the command line
when we're playing this. Going to try
and separate out some of the tool calls
from what I'm typing and things like
that. We've got a tool call hook here.
We'll get back to what this is in just a
second, but we've got more tools to look
at. So we've got a tool which is the
look up rule tool. And this is going to
look up rules from the rule book. And I
have actually copied in a rule book
here. It's just a simple piece of text.
So this is a simple set of text
instructions that we can ask the agent
to dip into so it can come and have a
look at what are the combat rules? What
are the skills checks rules? Now look,
this is super simple and honestly, I
could put all of the things that are in
this rule book actually in the context
or in the system prompt for the agent.
But I'm just demonstrating here how we
can use a tool to go and query and get
data from another source. In this case,
it's a text file. But this is the basics
of retrieval augmented generation. We
can get much more complex with this and
we could search an entire enterprise or
corporate document repository. But for
now, we're just searching this
text-based rule book. That's the tool
there that we've got. We've got roll
dice here again, but we've also got some
other things. We've got player state. So
this is able to keep hold of what our
current player state is. Like what are
our current hit points? What's our
current strength? The agent can actually
update this as we go along because it's
got a couple of tools. It's got get
player state which will just return that
player state and update player state. So
the agent can keep track of our health
as we play this game. And we could be
able to query that as well. And here's
our agent. We're still using Nova 2. Um
here's our system prompt. It's a little
bit more comprehensive now. Just talks a
little bit more about what's going on.
It explains that there's a rule book
there and the different sections of the
rule book that it can get. Um and then
we've got all of those
tools all in that tool list there.
Including then, we've also got hooks. So
hooks is a powerful thing inside of
Strands Agents SDK. So at various points
in the life cycle of that agentic loop
that we looked at, different hooks will
fire and you can go and hook into that
with your code to go and do a certain
thing. And so we have tool indicator as
a hook that we're going to register
here. So if I just scroll back up to the
top where that hook was defined, um we
can see here that it actually extends
hook provider which is something that we
have called in from the Strands Agents
SDK. We're actually calling in all the
necessary things that we need to
register this hook. And so this hook
here is going to get called before and
it's basically just going to print out
the name of the tool which is being used
just to break up something in the user
interface. This is very simple example
of how to use hooks. They're super
powerful. If you want to dig into the
documentation, of course, a link is in
the description below.
So let's scroll down again back to our
main agent. And then we have a game loop
here. Just a little bit more flare if
you like inside of the chat agent
itself. Otherwise, everything is the
same. So let's jump in here. Let's
expand this out just a bit more so I've
got some more room to play. And let's
play for the last time our quest. So
I've got
an interesting start out here. It says
the sun dips low casting long shadows
across the cobbled road as you approach
approach the humble village of Everbrook
Hollow.
And on it goes with the weaving of the
story. Let's have a look.
You push open the creaky door of the
rusty tankard, the warm amber light
spilling out into the cooling evening
air. And then it's got all these other
stuff learned down there. Let's have a
look. And it says, "What would you like
to do? Speak with Innkeeper, approach
the lone figure." Do you know what? I
think it's always a good idea for roll
to roll for investigation. So, let's
roll for investigation.
Also, I want to have a go with the tool.
So, it's looking up some rules. It's
decided. And then it's rolling the dice.
Um and then well, it doesn't actually
say what number we got back for Oh, no,
it does. It We rolled a three. Oh,
that's not good. So, we probably didn't
manage to discover very much. But you
can see how this works. It's looking up
rules. It's uh rolling the dice. And
it's also remembering um the status of
us as a player. This is a much more
sophisticated agent. But it's just
scratching the surface of what we could
do. You can see how we can build in
pretty much any tool, anything you can
do programmatically, you could build in
a tool for your agent. And then beyond,
if you want to add memory, if you want
to add connections to other types of
document stores, if you want to add
connections to MCP servers, or if you
want to publish this out to the internet
and have a cloud-hosted
agent, you can do all of those things
with Amazon Bedrock Agent Core. That's
the next step beyond building an agent
with Strand's Agent SDK. And for Amazon
Bedrock Agent Core, there'll be a link
in the description below. Thank you so
much for taking the time to go through
on this quest and this journey with me
for building agents. I hope you have a
lot of fun building agents, maybe as
much [music] fun as I'm having telling
you about them. Thank you so much for
watching, and I will see you in another
video really soon.
