Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Build an AI Agent From Scratch in Python (Summary)

You’ve built an AI agent from scratch in Python, starting from a single call to an LLM. You wrapped the model in a harness, gave it short-term memory through the conversation context, described tools it could request, and closed the agentic loop so the model could chain tool calls until it had an answer. Along the way, the trace() helper showed you every message moving between you, the harness, the model, and the tools.

Many Python developers now work with agents every day, and so do more and more people who don’t write code at all. Knowing what’s happening behind the scenes helps you understand why an agent behaves the way it does, what it costs, and where it can go wrong.

In this video course, you’ve learned how to:

  • Call an LLM from Python with the OpenAI library
  • Wrap the model in a harness loop and trace the flow of information
  • Keep conversation context by resending the messages list
  • Describe tools with a JSON schema and run the tool calls the model requests
  • Close the agentic loop so the model can chain tool calls

Now, when you use an agent, you’ll know what’s going on inside it. It’s not a mystery anymore. You can extend your agent with a tool of your own or explore a framework such as Pydantic AI to build something bigger.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

00:00 In this course, you learned that an LLM isn’t an agent. You built the harness around it, a loop that sends the whole conversation to the model, runs tools when the model asks for them, and keeps going until the model has an answer for you.

00:15 Of course, there’s a lot more, right? There’s permissions. You might also want to limit how many turns you have, lots more tools, memory. We’ve been talking about the memory within the context, but you can also have external memory files and ways of extending them.

00:31 So there’s a lot more. What matters is that we have built from scratch what is an agent. The harness talks to the model, the model can either send information back for the user or ask the harness, can you run a tool for me because I’ve seen what tools you have available?

00:47 The harness can make some decisions about whether to run the tool or not, run the tool, get information from the tool, send it back to the model, the model may ask for more tools, back and forth, until the model says, I have an answer, send it back to the user.

01:02 That’s our AI agent built from scratch. A reminder, you have access to the file we’ve created here and the habits to have a play with this. Feel free to download it by looking at the link below as always.

01:16 If you want to see how frameworks build on these same ideas, have a look at the Real Python courses on Pydantic AI and on MCP servers and the LangGraph tutorial.

01:27 They’re all linked below this video. And I hope you have enjoyed this journey through creating your own agent. Now when you go and use agents, you sort of know what’s happening a bit behind the scenes.

01:38 It’s not a mystery anymore, hopefully. Thanks for joining me and I’ll see you next time.

Become a Member to join the conversation.