Skip to content

ikwukao/ai_agent

Repository files navigation

AI Coding Agent

A command-line AI coding assistant built with Python, the OpenAI Python SDK, and OpenRouter. This project demonstrates how modern AI agents such as Claude Code and Cursor work under the hood by combining large language models with secure tool calling.

The agent can inspect project files, read source code, modify files, execute Python programs, and iteratively solve coding tasks while operating within a restricted working directory for safety.


Features

  • πŸ€– AI-powered command-line coding assistant
  • πŸ“‚ List files and directories
  • πŸ“– Read file contents safely
  • ✍️ Create and overwrite project files
  • ▢️ Execute Python programs with optional arguments
  • πŸ”’ Secure sandboxed file operations
  • 🧠 Multi-step reasoning using iterative tool calls
  • πŸ“ Verbose mode for debugging and development
  • πŸ”‘ Environment-based API key management

Tech Stack

  • Python 3.10+
  • OpenAI Python SDK
  • OpenRouter API
  • python-dotenv
  • argparse
  • JSON Schema (Tool Definitions)
  • subprocess
  • uv (Python package manager)

Project Structure

ai_agent/
β”œβ”€β”€ calculator
β”‚   β”œβ”€β”€ lorem.txt
β”‚   β”œβ”€β”€ main.py
β”‚   β”œβ”€β”€ pkg
β”‚   β”‚   β”œβ”€β”€ calculator.py
β”‚   β”‚   β”œβ”€β”€ morelorem.txt
β”‚   β”‚   β”œβ”€β”€ __pycache__
β”‚   β”‚   β”‚   β”œβ”€β”€ calculator.cpython-313.pyc
β”‚   β”‚   β”‚   └── render.cpython-313.pyc
β”‚   β”‚   └── render.py
β”‚   β”œβ”€β”€ README.md
β”‚   └── tests.py
β”œβ”€β”€ call_function.py
β”œβ”€β”€ config.py
β”œβ”€β”€ functions
β”‚   β”œβ”€β”€ get_file_content.py
β”‚   β”œβ”€β”€ get_files_info.py
β”‚   β”œβ”€β”€ __pycache__
β”‚   β”‚   β”œβ”€β”€ get_file_content.cpython-313.pyc
β”‚   β”‚   β”œβ”€β”€ get_files_info.cpython-313.pyc
β”‚   β”‚   β”œβ”€β”€ run_python_file.cpython-313.pyc
β”‚   β”‚   └── write_file.cpython-313.pyc
β”‚   β”œβ”€β”€ run_python_file.py
β”‚   └── write_file.py
β”œβ”€β”€ LICENSE
β”œβ”€β”€ main.py
β”œβ”€β”€ prompts.py
β”œβ”€β”€ __pycache__
β”‚   β”œβ”€β”€ call_function.cpython-313.pyc
β”‚   β”œβ”€β”€ config.cpython-313.pyc
β”‚   └── prompts.cpython-313.pyc
β”œβ”€β”€ pyproject.toml
β”œβ”€β”€ README.md
β”œβ”€β”€ test_get_file_content.py
β”œβ”€β”€ test_get_files_info.py
β”œβ”€β”€ test_run_python_file.py
β”œβ”€β”€ test_write_file.py
└── uv.lock

7 directories, 32 files

How It Works

The agent follows an iterative workflow:

  1. Receives a coding request from the user.
  2. Sends the request to an LLM through OpenRouter.
  3. The model decides which tool(s) to use.
  4. The application securely executes the requested function.
  5. The function result is returned to the model.
  6. The process repeats until the model produces a final answer.

This tool-calling loop enables the agent to inspect code, modify files, execute programs, and verify fixes before responding.


Available Tools

List Files

Displays the contents of directories inside the working directory.

get_files_info()

Read Files

Reads the contents of project files with a configurable maximum character limit.

get_file_content()

Write Files

Creates or overwrites files while preventing writes outside the allowed project directory.

write_file()

Execute Python Files

Runs Python scripts with optional command-line arguments and captures both standard output and error output.

run_python_file()

Installation

Clone the repository.

git clone https://github.com/ikwukao/ai_agent.git
cd ai_agent

Create a virtual environment.

uv venv

Activate it.

Linux/macOS

source .venv/bin/activate

Windows

.venv\Scripts\activate

Install the dependencies.

uv sync

Environment Variables

Create a .env file in the project root.

OPENROUTER_API_KEY="your_api_key_here"

Never commit this file to version control.


Usage

Run the agent with a prompt.

uv run main.py "Explain how the calculator renders results."

Enable verbose mode.

uv run main.py "Run the calculator tests." --verbose

Example requests:

List all files in the project.

Read calculator/main.py.

Run the calculator tests.

Create a README file.

Fix the bug where 3 + 7 * 2 returns 20.

Safety

The agent is intentionally sandboxed.

It cannot:

  • Read files outside the configured working directory.
  • Write files outside the project.
  • Execute non-Python files.
  • Access arbitrary locations on the host system through tool calls.

These safeguards reduce the risk of accidental or malicious file access while allowing the LLM to operate effectively within the project.


Learning Outcomes

This project demonstrates practical experience with:

  • Large Language Model integration
  • Function (tool) calling
  • OpenAI-compatible APIs
  • Prompt engineering
  • Secure filesystem operations
  • Python subprocess management
  • Multi-module Python applications
  • Command-line application development
  • Environment variable management
  • Iterative AI agent workflows

Future Improvements

  • Conversation memory
  • Streaming responses
  • Additional developer tools
  • Git integration
  • Shell command execution
  • Unit and integration testing
  • Syntax-aware code editing
  • Support for multiple LLM providers
  • Conversation logging

Acknowledgements

This project was built as part of the Boot.dev Backend Developer learning path and is inspired by modern AI coding assistants such as Claude Code, Cursor, and GitHub Copilot.


License

This project is intended for educational and portfolio purposes.


πŸŽ₯ Demo

Watch the AI Coding Agent in action:

ai_agent_demo.mp4

About

An AI-powered coding assistant built with Python, OpenRouter, and the OpenAI SDK. Demonstrates secure tool calling, file system operations, Python execution, and iterative agent workflows inspired by modern AI coding tools like Claude Code and Cursor.

Topics

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages