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.py file.
  • 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__.py file is introduced, which specifies the dependencies (entropic, pydantic) required to run the code without a virtual environment.
  • The Tool class is defined using Pydantic, specifying the name, description, and input_schema for each tool.
  • The AIAgent class 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, and edit_file.
  • Each tool is defined using the Tool class, with a name, description, and input schema.
    • read_file: Takes a file_path as input.
    • list_files: Takes a path as input.
    • edit_file: Takes a path, old_text, and new_text as input.
  • The setup_tools function populates the tools list 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 tools directory for read_file, list_files, and edit_file.
    • read_file(path): Reads the content of a file at the given path using with open() and returns the content.
    • list_files(path): Lists all files and directories in the specified path using os.listdir() and returns a sorted list with indicators for directories vs. files.
    • edit_file(path, old_text, new_text): Reads the file content, replaces old_text with new_text using content.replace(), and writes the modified content back to the file.
  • The execute_tool function in the AIAgent class 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 the list_files method to test its functionality.

8. Script 05: Adding the Chat Method

  • This script introduces the chat method to the AIAgent class, enabling interactive communication with the agent.
  • The chat method takes user input, appends it to the messages list, 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_tool function 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 argparse library.
  • 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 chat method.
  • 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_prompt variable is added to the AIAgent class, 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.py file.
    • Add another function to the test.py file.
    • Empty the test.py file.
  • 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-Powered

Load the transcript when you're ready to chat so the initial page stays lighter.

Ready to summarize another video?

Summarize YouTube Video