Rex Code — a minimal AI coding agent in Go. Give it a prompt; it reads files, writes code, and runs commands. Works with any LLM via OpenRouter (Claude, Gemini, GPT, Llama, and more).
Rex Code runs an agentic loop: it sends your prompt to an LLM, which can autonomously call tools to interact with your filesystem and shell. This repeats until the LLM has no more tool calls and returns a final answer.
You (prompt)
│
▼
LLM (via OpenRouter)
│
├── Tool call: Read → reads a file → result sent back to LLM
├── Tool call: Write → writes a file → result sent back to LLM
├── Tool call: Run → runs a shell command → output sent back to LLM
│
└── No more tool calls → prints final answer → done
The LLM has access to three tools:
| Tool | Description | Parameters |
|---|---|---|
Read |
Read and return the contents of a file | file_path (string) |
Write |
Write content to a file (overwrites if exists) | file_path, content |
Run |
Run a shell command and return its output | command (string) |
- Go 1.21+ installed
- An OpenRouter account (free to sign up)
git clone https://github.com/aerex/Rex-Code.git
cd Rex-Codego mod downloadCopy the example env file and fill in your credentials:
cp .env.example .envEdit .env:
OPENROUTER_API_KEY=your-openrouter-api-key-here
OPENAI_BASE_URL=https://openrouter.ai/api/v1Where to get your API key: Sign up at openrouter.ai, go to Keys in your account settings, and create a new key.
Want free usage? OpenRouter has free models (rate-limited). Change the model in
app/main.goto something likemeta-llama/llama-3.3-70b-instruct:freeorgoogle/gemini-2.0-flash-exp:free.
go build -o rex-code ./app/*.go./rex-code -p "Your prompt here"# Ask it to read a file and explain it
./rex-code -p "Read app/main.go and explain what it does"
# Ask it to create a new file
./rex-code -p "Create a file called hello.go that prints Hello, World!"
# Ask it to run a command and summarise the output
./rex-code -p "Run 'ls -la' and tell me what files are in this directory"
# A more complex task
./rex-code -p "Read app/main.go, find any bugs, and fix them"rex-code/
├── app/
│ └── main.go # Entry point — agent loop + tool implementations
├── .env # Your secrets (gitignored)
├── .env.example # Template — safe to commit
├── .gitignore
├── go.mod
└── go.sum
Rex Code uses the official openai-go SDK but points it at OpenRouter instead of OpenAI directly. OpenRouter implements the same API format as OpenAI, then routes your request to whichever model you specify.
openai-go SDK → openrouter.ai/api/v1 → Anthropic / Google / Meta / OpenAI / etc.
This means you can switch models by just changing the Model field in app/main.go:
// Claude (Anthropic)
Model: "anthropic/claude-haiku-4.5"
// GPT-4o (OpenAI)
Model: "openai/gpt-4o"
// Gemini (Google) — free tier
Model: "google/gemini-2.0-flash-exp:free"
// Llama (Meta) — free tier
Model: "meta-llama/llama-3.3-70b-instruct:free"Browse all available models at openrouter.ai/models.
| Variable | Required | Default | Description |
|---|---|---|---|
OPENROUTER_API_KEY |
✅ Yes | — | Your OpenRouter API key |
OPENAI_BASE_URL |
❌ No | https://openrouter.ai/api/v1 |
Override to use a different endpoint |
Variables are loaded from .env automatically if the file exists. Shell environment variables always take precedence.
| Package | Purpose |
|---|---|
openai/openai-go |
OpenAI-compatible API client |
joho/godotenv |
Load .env files |
This project is licensed under the MIT License — see the LICENSE file for details.