How AI Agents Really Work: Build One in Plain Python
Build an AI Agent From Scratch in Python (live course) → https://realpython.com/workshops/agentic-loops/?utm_source=youtube&utm_medium=description&utm_campaign=agentic-loops&utm_content=oBbwZjcunDE You use AI agents every day, but an agent isn't an LLM. It's an LLM wrapped in a harness of plain code. In this video, Stephen Gruppetta builds that harness from scratch in Python with the OpenAI API and shows the single change that turns a chatbot into an agent. You'll start with one chat-completion call that can't see your files, then add a loop, a growing message history so the model remembers the conversation, and tool definitions so it can ask the harness to list and read files. The last step swaps an "if" for a "while", and the model starts chaining tool calls on its own until it has an answer. By the end, agents like Claude Code and Codex stop being a mystery: you'll know what they send to the model, why long conversations get expensive, and where permissions and memory fit in. Create Your Personal Python & AI Coding Learning Plan → https://realpython.com/plan?utm_source=youtube&utm_medium=description&utm_campaign=learning-plan&utm_content=oBbwZjcunDE 👉 Links from the video: Sample code for this video: https://realpython.com/bonus/agentic-loop-code/?utm_source=youtube&utm_medium=description&utm_campaign=agentic-loop-code&utm_content=oBbwZjcunDE OpenAI Python library: https://github.com/openai/openai-python OpenAI function calling guide: https://platform.openai.com/docs/guides/function-calling 00:00 Chatbot vs. agent: same model, different code 00:45 What a harness is 00:52 Talking to an LLM with the OpenAI API 02:40 Wrapping the model in a while loop 04:57 Why the model forgets: sending the whole conversation 06:37 Giving the model a tool 08:31 The model asks for a tool call 09:04 Running tools from the harness 11:50 Adding a read_file tool 13:02 Mapping tool names to functions 14:10 From if to while: the agent loop 15:49 What real agents add 16:18 Recap: how an agent works 17:05 Go further with the live course 🐍 Become a Python expert with real-world tutorials, on-demand courses, interactive quizzes, and 24/7 access to a community of experts at https://realpython.com?utm_source=youtube&utm_medium=description&utm_campaign=home&utm_content=oBbwZjcunDE ▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰ 🐍 Start Here → https://realpython.com/start?utm_source=youtube&utm_medium=description&utm_campaign=start&utm_content=oBbwZjcunDE 🗺️ Guided Learning Paths → https://realpython.com/learning-paths/?utm_source=youtube&utm_medium=description&utm_campaign=learning-paths&utm_content=oBbwZjcunDE 🎧 Real Python Podcast → https://realpython.com/podcast?utm_source=youtube&utm_medium=description&utm_campaign=podcast&utm_content=oBbwZjcunDE 📚 Python Books → https://realpython.com/books?utm_source=youtube&utm_medium=description&utm_campaign=books&utm_content=oBbwZjcunDE 📖 Python Reference → https://realpython.com/ref?utm_source=youtube&utm_medium=description&utm_campaign=ref&utm_content=oBbwZjcunDE 🧑💻 Quizzes & Exercises → https://realpython.com/quizzes/?utm_source=youtube&utm_medium=description&utm_campaign=quizzes&utm_content=oBbwZjcunDE 🎓 Live Courses: https://realpython.com/live/?utm_source=youtube&utm_medium=description&utm_campaign=live&utm_content=oBbwZjcunDE ⭐️ Reviews & Learner Stories: https://realpython.com/learner-stories/?utm_source=youtube&utm_medium=description&utm_campaign=learner-stories&utm_content=oBbwZjcunDE ▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰▰