This project implements a simple in-terminal AI coding agent inspired by tools like Cursor and Claude Code. The agent can inspect, edit, and run files inside a working directory in order to diagnose and fix bugs automatically.
The agent uses the Gemini API to reason about code and decide when to call tools that interact with the filesystem.
The agent works by looping between the language model and a set of tools:
- The user provides a prompt describing a task or bug.
- The model reads the prompt and decides whether it needs to call a tool.
- If a tool is needed (for example reading a file), the agent executes it.
- The result is sent back to the model.
- The model continues reasoning until it produces a final answer.
This is similar to the architecture used by modern AI coding assistants, where the model can:
- inspect files
- edit code
- run programs
- iterate until a fix is found
The main control loop for this process is implemented in main.py.
git clone https://github.com/ramzygirgis/cli-coding-agent.git
cd cli-coding-agentThis project requires Python 3.12 or newer.
python3.12 -m venv .venv
source .venv/bin/activateOn Windows:
.venv\Scripts\activateDependencies are defined in pyproject.toml, so install them with:
pip install .This will install the required packages:
google-genaipython-dotenv
-
Create a new API key.
-
Copy the generated key.
Create a .env file in the root of the repository:
GEMINI_API_KEY=your_api_key_hereThe application automatically loads this key using python-dotenv when the program starts.
Example:
python main.py "Fix the bug in calculator/main.py"
Optional verbose mode:
python main.py "Fix the bug in calculator/main.py" --verbose
Verbose mode prints token usage for debugging and monitoring API usage.
Gemini free tier APIs have small quotas.
Common issues:
429 RESOURCE_EXHAUSTED- quota exceeded errors
Possible solutions:
- reduce
MAX_ITERATIONS - switch to a cheaper model such as:
gemini-2.5-flash-lite
- wait for rate limits to reset
- enable billing for higher quotas
The agent typically follows this workflow:
- Explore the directory structure.
- Read relevant source files.
- Run the program to observe errors.
- Modify files.
- Re-run the program to test fixes.
This loop is repeated until the model believes the problem is solved.
Potential extensions for this project:
- add more tools (e.g. shell commands)
- add test running capabilities
- integrate with an editor
- implement memory or planning
- improve prompts and tool descriptions
A sample project used for testing the agent.
The agent can explore this directory, read files, modify them, and run Python code in order to debug issues.
This provides a safe environment to experiment with the agent’s capabilities.
This directory contains the tools available to the AI agent.
Each tool performs a specific action inside the working directory.
Reads the contents of a file.
Used when the model wants to inspect source code.
Lists files and metadata within a directory.
Useful for discovering project structure.
Writes or modifies files.
Allows the model to fix bugs or update code.
Executes Python files inside the working directory.
This lets the model:
- run programs
- observe runtime errors
- test potential fixes
Acts as the bridge between the model and the tools.
Responsibilities:
- registers the available tools (
available_functions) - interprets tool calls produced by the model
- executes the appropriate Python function
- returns the result to the model
This is what enables function calling / tool use.
Contains configuration variables used by the agent, such as:
- Gemini model name
- maximum number of agent iterations
- maxmimum number of characters readable in a file
Gemini APIs have fairly strict rate limits, especially on the free tier.
If you encounter rate limit errors, you may need to reduce MAX_ITERATIONS.
Each iteration sends a request to the model.
Contains the system prompt used by the agent.
This prompt defines:
- the agent’s role
- what tools it can use
- how it should reason about problems
Prompt design has a large impact on agent performance.
This file contains the core agent loop.
High-level flow:
-
Load the Gemini API key.
-
Send the user prompt to the model.
-
The model decides whether to call a tool.
-
If a tool is requested:
- execute it
- return the result to the model
-
Repeat until the model produces a final response.
The loop runs for at most:
MAX_ITERATIONS
to prevent runaway execution.
The repository includes several tests that verify the functionality of the tools:
test_get_file_content.py
test_get_files_info.py
test_run_python_file.py
test_write_file.py
These ensure that the tools behave correctly before they are exposed to the agent.
In the future these may be moved to a dedicated tests/ directory.