A lightweight agentic coding assistant powered by Google's Gemini API. This project is a toy implementation of tools like Cursor/Zed's Agentic Mode or Claude Code, demonstrating how AI agents can autonomously debug and fix code through function calling.
- Autonomous Debugging: AI agent analyzes bugs, reads relevant files, and applies fixes iteratively
- Sandboxed Execution: All operations are restricted to a specified working directory for safety
- Function Calling: Implements tool use for:
- Listing files and directories
- Reading file contents (with truncation protection)
- Executing Python files with arguments
- Writing/overwriting files
- Iterative Agent Loop: Up to 20 iterations to solve problems autonomously
- Verbose Mode: Optional detailed logging of function calls and token usage
The agent follows a strict workflow:
- Understand the bug from the user's description
- Reproduce the bug by running the program
- Inspect relevant files to locate the root cause
- Make minimal changes to fix the bug
- Re-run the program to verify the fix
- Continue until the output matches expected behavior
- Python 3.12 or higher
- Google Gemini API key
- Clone the repository:
git clone https://github.com/yourusername/coding-agent.git
cd coding-agent- Install dependencies using uv:
uv syncOr using pip:
pip install -r requirements.txt- Create a
.envfile in the project root:
GEMINI_API_KEY=your_gemini_api_key_here
Basic usage:
python main.py "Fix the bug in the calculator"With verbose output:
python main.py "Fix the bug in the calculator" --verbosepython main.py "The calculator crashes when dividing by zero. Fix it."The agent will:
- List files to understand the codebase structure
- Read the calculator implementation
- Run the program to reproduce the bug
- Identify the division by zero issue
- Add error handling
- Re-run to verify the fix
- Report success
coding-agent/
├── main.py # Entry point and agent loop
├── config.py # Configuration (file size limits, etc.)
├── prompts.py # System prompt for the agent
├── functions/ # Function implementations
│ ├── call_function.py # Function dispatcher
│ ├── get_files_info.py # List directory contents
│ ├── get_file_content.py # Read file contents
│ ├── run_python_file.py # Execute Python scripts
│ └── write_file.py # Write/overwrite files
├── calculator/ # Example project for debugging
│ ├── main.py
│ └── tests.py
└── .env # API keys (not committed)
The agent operates within a sandboxed directory specified in functions/call_function.py:
args["working_directory"] = "./calculator"Change this to point to your target codebase.
Maximum file read size is configurable in config.py:
MAX_FILE_CHARS = 10_000 # Truncates files larger than thisMaximum agent iterations can be adjusted in main.py:
for iteration in range(20): # Change to desired max iterationsThe agent has access to these tools:
Lists files in the specified directory with size and type information.
Reads and returns file contents (with truncation for large files).
Executes a Python file with optional command-line arguments, returns stdout/stderr.
Writes content to a file, creating parent directories if needed.
- Path Traversal Protection: All file operations validate that paths remain within the working directory
- Execution Timeout: Python file execution times out after 30 seconds
- File Size Limits: Files are truncated when reading to prevent memory issues
- No Shell Injection: Uses
subprocess.run()with argument lists instead of shell strings
- Python-only execution (can be extended to other languages)
- Limited to text files (no binary file support)
- No git operations or version control integration
- Maximum 20 iterations per request
- Single-threaded operation
To add new capabilities:
- Create a new function in
functions/with:- Implementation function
- Schema definition using
types.FunctionDeclaration
- Add to
call_function.py:- Import the function and schema
- Add schema to
available_functions - Add function to
function_map
Example:
# functions/search_code.py
def search_code(working_directory, query):
# Implementation
pass
schema_search_code = types.FunctionDeclaration(
name="search_code",
description="Search for code patterns",
parameters=...
)See the calculator/ directory for an example project the agent can debug:
# Example bug fixes the agent can handle:
python main.py "The calculator doesn't handle division by zero"
python main.py "Tests are failing, fix them"
python main.py "Add input validation to prevent negative numbers"Contributions welcome! Areas for improvement:
- Add support for more programming languages
- Implement code search/grep functionality
- Add git integration for committing fixes
- Support for reading/writing binary files
- Multi-file refactoring capabilities
- Integration with other LLM providers
MIT License - see LICENSE file for details
Inspired by:
- Cursor - AI-powered code editor
- Zed - Collaborative code editor with AI features
- Claude Code - Anthropic's coding assistant
Built with:
- Google Gemini API - Powerful LLM with function calling
- uv - Fast Python package manager
"GEMINI_API_KEY not found"
- Ensure
.envfile exists with valid API key
"Maximum iterations reached"
- Problem may be too complex
- Try breaking into smaller requests
- Increase iteration limit in
main.py
"Cannot read/write file outside working directory"
- All paths must be relative to working directory
- Update
working_directoryincall_function.py
Function call errors
- Enable
--verboseflag to see detailed function calls - Check that file paths are relative, not absolute