How to build your own AI coding agent (in 218 lines)
By Dave Ebbelaar
Share:
Key Concepts:
- AI Coding Agent: An autonomous program that can understand and manipulate code files.
- Tools/Function Calling: A mechanism where the AI agent can use predefined functions (tools) to interact with the environment (e.g., file system).
- Entropic/Claude: A large language model provider (similar to OpenAI) used to power the AI agent.
- UV: A fast Python package and project manager, an alternative to pip.
- Pydantic: A Python library for data validation and settings management using type annotations.
- System Prompt: Instructions given to the AI model to define its behavior and personality.
- CLI: Command Line Interface, a text-based interface for interacting with the agent.
1. Introduction and Overview
- The video demonstrates building an AI coding agent from scratch using Python in approximately 200 lines of code.
- The agent can perform tasks such as listing files in a directory, reading file contents, and editing files.
- The agent is contained within a single
main.pyfile. - The video is aimed at individuals interested in AI agents who want to understand their inner workings.
2. Setup and Prerequisites
- GitHub Repository: A GitHub repository containing the code is provided for users to clone and follow along. Credit is given to Francis Pson, the original author.
- UV Installation: The video recommends using UV as a Python package manager for faster performance compared to pip. Instructions and a dedicated video on UV setup are provided.
- Entropic API Key: An API key from Entropic is required to use the Claude 4.5 Sonnet model. Users are instructed to obtain the key from console.entropic.com.
3. Code Structure and Runbook
- The code is divided into seven scripts (01 to 07) in a "runbook" directory, each building upon the previous one.
- The scripts progressively add complexity to the agent, starting with a basic setup and culminating in a fully functional agent with a CLI and personality.
4. Script 01: Basic Script
- The first script focuses on ensuring the code can run and plugging in the Entropic API key.
- The command
export ENTHROPIC_API_KEY=<your_api_key>is used to set the API key in the terminal session. - The script is executed using
uv run 01_basic_script.py.
5. Script 02: Agent Class
- This script defines the agent class and sets up the foundation for tools.
- A
__uv__.pyfile is introduced, which specifies the dependencies (entropic, pydantic) required to run the code without a virtual environment. - The
Toolclass is defined using Pydantic, specifying thename,description, andinput_schemafor each tool. - The
AIAgentclass is initialized with a client (Entropic), a list of messages (for conversation history), and a list of tools. - The script is executed using
uv run 02_agent_class.py, and "Agent initialized" should be printed to confirm successful execution.
6. Script 03: Define Tools
- This script defines the specific tools the agent will use:
read_file,list_files, andedit_file. - Each tool is defined using the
Toolclass, with a name, description, and input schema.read_file: Takes afile_pathas input.list_files: Takes apathas input.edit_file: Takes apath,old_text, andnew_textas input.
- The
setup_toolsfunction populates thetoolslist with these tool definitions. - The script is executed using
uv run 03_define_tools.py, and "Agent initialized with 3 tools" should be printed.
7. Script 04: Implement Tools
- This script implements the Python logic for each tool.
- Separate functions are created in the
toolsdirectory forread_file,list_files, andedit_file.read_file(path): Reads the content of a file at the given path usingwith open()and returns the content.list_files(path): Lists all files and directories in the specified path usingos.listdir()and returns a sorted list with indicators for directories vs. files.edit_file(path, old_text, new_text): Reads the file content, replacesold_textwithnew_textusingcontent.replace(), and writes the modified content back to the file.
- The
execute_toolfunction in theAIAgentclass takes the agent's output, identifies the tool being called, and executes the corresponding Python function. - The script is executed using
uv run 04_implement_tools.py, which directly calls thelist_filesmethod to test its functionality.
8. Script 05: Adding the Chat Method
- This script introduces the
chatmethod to theAIAgentclass, enabling interactive communication with the agent. - The
chatmethod takes user input, appends it to themessageslist, and sends the messages to the Claude 4.5 Sonnet model. - The tool schema is converted to the format expected by the Entropic Python SDK.
- A loop is implemented to handle different types of responses from the model:
- If the model returns text, it's added to the assistant message.
- If the model uses a tool, the
execute_toolfunction is called to execute the tool, and the results are appended to the messages.
- The script is executed using
uv run 05_add_chat_method.py, which asks the agent "What files are in the current directory?"
9. Script 06: Creating an Interactive CLI
- This script adds a command-line interface (CLI) to the agent using the
argparselibrary. - The CLI allows users to continuously interact with the agent until they type "exit" or "quit".
- The main function now includes a loop that captures user input and calls the
chatmethod. - The script is executed using
uv run 06_interactive_cli.py, which starts the AI coding assistant in the terminal.
10. Script 07: Adding a Personality
- This script adds a system prompt to the agent to define its personality and behavior.
- A
system_promptvariable is added to theAIAgentclass, instructing the agent to operate within a terminal environment and output plain markdown. - The script is executed using
uv run 07_add_personality.py, which starts the CLI with the defined personality. - The video demonstrates how to tweak the system prompt to improve the agent's output formatting.
11. Final Demonstration and Testing
- The video demonstrates the final AI coding agent by asking it to:
- Create a short
test.pyfile. - Add another function to the
test.pyfile. - Empty the
test.pyfile.
- Create a short
- The agent successfully performs these tasks, showcasing its ability to create, modify, and delete files.
12. Conclusion
- The video concludes by summarizing the process of building an AI coding agent from scratch in Python.
- It encourages viewers to like the video, subscribe to the channel, and check out another video on building reliable and effective agents.
Chat with this Video
AI-PoweredLoad the transcript when you're ready to chat so the initial page stays lighter.
Related Videos

Why Does This Guy Appear In Kids Videos?
sphynx

TIC en las Organizaciones - Electiva Complementaria II Unisimon
Julieth Güell S

How to Tame Your Advice Monster | Michael Bungay Stanier | TED
TED

Margaret Heffernan: Why it's time to forget the pecking order at work
TED

The importance of psychological safety: Amy Edmondson
The King's Fund

What Is Psychological Safety?
Harvard Business Review

13-Conflict Management: Listening in Conflict
Deliberate Development