A basic template for building an AI agent.
This implements a simple agentic loop:
- User sends a query to the agent
- The model receives the query + available tools
- The model either:
- Returns a final answer, OR
- Calls one or more tools to gather information
- If tools were called, their results are sent back to the model
- Loop continues until the model provides a final answer or hits the max iteration limit.
agent.py: Main agentic loop and conversation managementtools.py: Tool definitions (each tool has a schema + execute method)
-
Create virtual environment:
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
Or similar Python setup on Windows machines!
-
Install dependencies:
pip install -r requirements.txt
-
Set your API key: Create a file called
.envand add this line:ANTHROPIC_API_KEY='your-key-here' -
Run the demo:
python agent.py
To create an agent that can answer questions about codebases, add these two tools:
1. ListDirectory Tool (ls)
- Use Python's
os.listdir()oros.walk() - Input:
path(directory to list) - Output: List of files/folders in that directory
- Tip: Return results as a formatted string with file names
2. ReadFile Tool (read_file)
- Use Python's
open().read() - Input:
file_path(path to file) - Output: The file's contents as a string
- Tip: Add error handling for files that don't exist or can't be read
- Bonus: Add
start_lineandmax_linesparameters to limit large files
With these tools, the agent can explore directories and read files to answer questions like "What does the WeatherTool class do?" or "How many Python files are in this project?"
Currently, the agent only works with the current directory. The simplest way to extend this:
- First use
input()to get the base path from the user - Provide this base path in the system prompt
- Modify the
lsandread_filetools to use absolute paths
For a quick hack, you can also copy your repo into this directory to give the agent access to it!
Refer to the codebase-qna branch for a complete implementation of this agent.
- Customize your system prompt! For example, add additional instructions to customize answer format.
- Use different models - try
claude-sonnet-4-5for smarter but slower responses.
- Add multi-turn conversation to reply to your agent's answers
- Add streaming to your agent to see the answer as it generates
- Experiment with more tools! (e.g.
grep,find)