Skip to content

Latest commit

 

History

History
186 lines (125 loc) · 5.18 KB

File metadata and controls

186 lines (125 loc) · 5.18 KB

Rex Code

Go License: MIT LLM OpenRouter CLI AI Agent

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).


How it works

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

Tools

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)

Prerequisites

  • Go 1.21+ installed
  • An OpenRouter account (free to sign up)

Setup

1. Clone the repo

git clone https://github.com/aerex/Rex-Code.git
cd Rex-Code

2. Install dependencies

go mod download

3. Configure environment

Copy the example env file and fill in your credentials:

cp .env.example .env

Edit .env:

OPENROUTER_API_KEY=your-openrouter-api-key-here
OPENAI_BASE_URL=https://openrouter.ai/api/v1

Where 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.go to something like meta-llama/llama-3.3-70b-instruct:free or google/gemini-2.0-flash-exp:free.


Build & Run

Build

go build -o rex-code ./app/*.go

Run

./rex-code -p "Your prompt here"

Examples

# 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"

Project Structure

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

How OpenRouter works

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.


Environment Variables

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.


Dependencies

Package Purpose
openai/openai-go OpenAI-compatible API client
joho/godotenv Load .env files

License

This project is licensed under the MIT License — see the LICENSE file for details.