An AI coding agent built with Ruby and the RubyLLM gem. This command-line tool lets you converse with an AI assistant that can list directories, read files, and edit code in your project.
- 🤖 Interactive Chat Interface - Conversational interface for natural language interactions
- 📁 Directory Listing - Browse and explore project structure
- 📄 File Reading - Read and analyze file contents
- ✏️ File Editing - Modify files with search and replace operations
- 💰 Cost Tracking - Monitor token usage and estimated API costs per request
- 🔒 Security - Restricted file access (no dotfiles) and directory sandboxing
.
├── Gemfile # Ruby dependencies
├── Gemfile.lock # Locked gem versions
├── run_agent.rb # Main executable entry point
├── simple_agent.rb # Core agent implementation
├── directory_listing_tool.rb # Tool for listing directory contents
├── read_file_tool.rb # Tool for reading file contents
├── edit_file_tool.rb # Tool for editing files
├── rubyllm-coding-agent-plan.md # Detailed implementation guide
└── ruby_llm.log # API interaction logs
- Ruby (2.7 or higher recommended)
- Bundler
- An API key from one of the supported providers:
- OpenRouter (recommended)
- OpenAI
- Anthropic
-
Clone or download this project
-
Install dependencies:
bundle install
-
Configure environment variables:
Create a
.env.localfile in the project root:WELCOME_MESSAGE="Hello! I'm your coding assistant." OPENROUTER_KEY=your_openrouter_api_key_hereAlternatively, you can use other providers by setting:
OPENAI_API_KEYfor OpenAIANTHROPIC_API_KEYfor Anthropic
-
Make the script executable:
chmod +x run_agent.rb
Run the agent from the command line:
./run_agent.rbList project files:
>> What files are in this directory?
Read a specific file:
>> Show me the contents of simple_agent.rb
Analyze dependencies:
>> What gems does this project use?
Edit a file:
>> Change the welcome message in simple_agent.rb to say "Welcome to the AI Coding Assistant"
Exit the conversation:
>> exit
(or type quit or bye)
Lists files and directories at a given path, returning name, type, and size information.
Reads the contents of files within the project directory. Prevents reading dotfiles for security.
Performs search-and-replace operations on files. Creates new files if they don't exist. Only allows editing files in the current directory or subdirectories.
The agent is configured in run_agent.rb with the following settings:
- Model:
anthropic/claude-sonnet-4.5(via OpenRouter) - API Base:
https://openrouter.ai/api.v1 - Logging: Enabled with debug level to
ruby_llm.log
You can modify these settings in the RubyLLM.configure block.
After each interaction, the agent displays:
- Estimated cost for the request
- Input and output token counts
- Total tokens used in the conversation
Example output:
---
Estimated cost: $0.001234
Conversation total tokens: 1523
- Dotfile Protection: Cannot read or edit files starting with
.(like.env.local) - Directory Sandboxing: File operations restricted to current directory and subdirectories
- Error Handling: Graceful error messages for invalid operations
- ruby_llm - Core LLM framework for Ruby
- dotenv - Environment variable management
- json - JSON parsing and generation
- pastel - Terminal text coloring
- rspec - Testing framework
To add a new tool, create a class that inherits from RubyLLM::Tool:
class MyTool < RubyLLM::Tool
description "What this tool does"
param :my_param, type: :string, desc: "Parameter description"
def execute(my_param:)
# Tool implementation
end
endThen register it in simple_agent.rb:
@chat.with_tool(MyTool)This project includes a comprehensive tutorial in rubyllm-coding-agent-plan.md that walks through building the agent from scratch, covering:
- Basic Ruby environment setup
- RubyLLM configuration
- Building interactive conversation loops
- Creating custom tools
- Implementing cost tracking
- Security considerations
This is a tutorial project. Feel free to use and modify as needed.
This is an educational project based on the RubyLLM coding agent tutorial. Feel free to fork and experiment with your own enhancements!
API Key Issues:
- Ensure your
.env.localfile exists and contains valid API keys - Check that the API key has sufficient credits
Tool Execution Errors:
- Verify file paths are relative to the project directory
- Check file permissions for read/write operations
Unexpected Behavior:
- Check
ruby_llm.logfor detailed API interaction logs - Ensure you're using compatible Ruby version (2.7+)