A customizable, lightweight, and open-source coding CLI powered by OpenRouter - access any LLM model via OpenRouter
npm install openrouter-code
Overview ā¢
Installation ā¢
Usage ā¢
Development
The OpenRouter Code CLI gives you access to any LLM model available on OpenRouter, including Claude 3.5 Sonnet, GPT-4, Llama 3, Gemini, and many more. This is a customizable, lightweight, and open-source coding CLI that serves as a blueprint for developers looking to leverage, customize, and extend a CLI to be entirely their own.
This project is based on the original groq-code-cli-openrouter by rahulvrane, which was initially designed for Groq. This version has been adapted and enhanced to work exclusively with OpenRouter, providing access to a much wider range of AI models.
Unlike other coding CLIs that are locked to a single provider, OpenRouter Code CLI lets you choose the perfect model for each task. Want Claude's reasoning? GPT-4's creativity? Or the speed of smaller models? Simply use /model to switch between any available model on OpenRouter.
OpenRouter Code CLI is your chance to make a CLI truly your own while having access to the best AI models available. Equipped with all of the features, tools, commands, and UI/UX that's familiar, we make it simple to add new features you've always wanted. Simply activate the CLI by typing openrouter in your terminal. Use it in any directory just like you would with any other coding CLI.
A few customization ideas to get started:
- New slash commands (e.g. /mcp, /deadcode, /complexity, etc.)
- Additional tools (e.g. web search, merge conflict resolver, knowledge graph builder, etc.)
- Custom start-up ASCII art
- Change the start-up command
- Anything you can think of!
``bashopenrouter
git clone https://github.com/BaptisteDegryse/openrouter-code.git
cd openrouter-code
npm install
npm run build
npm link # Enables the command in any directory`
`bash`Run this in the background during development to automatically apply any changes to the source code
npm run dev
`bash`
npx openrouter-code@latest
`bash`Start chat session
openrouter
`bash
openrouter [options]
Options:
-t, --temperature
-s, --system
-d, --debug Enable debug logging to debug-agent.log in current directory
-h, --help Display help
-V, --version Display version number
`
On first use, start a chat:
`bash`
openrouter
And type the /login command:
> Get your API key from the OpenRouter Console here
This creates a .openrouter/ folder in your home directory that stores your API key, default model selection, and any other config you wish to add.
You can also set your API key for your current directory via environment variable:
`bash`
export OPENROUTER_API_KEY=your_api_key_here
Your API key is automatically protected from git commits:
- ā
The .openrouter/ folder is in .gitignore.env
- ā
Environment variables ( files) are ignored
- ā
API keys are masked in debug logs
- ā
No hardcoded keys exist in the source code
Never commit API keys to version control! The application safely stores your key locally in ~/.openrouter/ and supports environment variables for secure deployment.
- /help - Show help and available commands/login
- - Login with your OpenRouter API key/model
- - Select from any OpenRouter model (e.g., anthropic/claude-3.5-sonnet, openai/gpt-4-turbo, meta-llama/llama-3.1-70b-instruct)/clear
- - Clear chat history and context/reasoning
- - Toggle display of reasoning content in messages
- anthropic/claude-3.5-sonnet - Claude 3.5 Sonnet (excellent for coding)openai/gpt-4-turbo
- - GPT-4 Turbogoogle/gemini-pro-1.5
- - Gemini Pro 1.5meta-llama/llama-3.1-70b-instruct
- - Llama 3.1 70Bmistralai/mistral-large
- - Mistral Large
- And many more! See full list at openrouter.ai/models
`bash`Run this in the background during development to automatically apply any changes to the source code
npm run dev
`bash`
npm run build # Build TypeScript to dist/
npm run dev # Build in watch mode
``
openrouter-code/
āāā src/
ā āāā commands/
ā ā āāā definitions/ # Individual command implementations
ā ā ā āāā clear.ts # Clear chat history command
ā ā ā āāā help.ts # Help command
ā ā ā āāā login.ts # Authentication command
ā ā ā āāā model.ts # Model selection (tools supported)
ā ā ā āāā model-without-tools.ts # Model selection (no tools)
ā ā ā āāā reasoning.ts # Reasoning toggle command
ā ā āāā base.ts # Base command interface
ā ā āāā index.ts # Command exports
ā āāā core/
ā ā āāā agent.ts # AI agent implementation
ā ā āāā cli.ts # CLI entry point and setup
ā āāā tools/
ā ā āāā tool-schemas.ts # Tool schema definitions
ā ā āāā tools.ts # Tool implementations
ā ā āāā validators.ts # Input validation utilities
ā āāā ui/
ā ā āāā App.tsx # Main application component
ā ā āāā components/
ā ā ā āāā core/ # Core chat TUI components
ā ā ā āāā display/ # Auxiliary components for TUI display
ā ā ā āāā input-overlays/ # Input overlays and modals
ā ā āāā hooks/ # React hooks for state management
ā āāā utils/
ā āāā constants.ts # Application constants
ā āāā context-manager.ts # Context window management
ā āāā file-ops.ts # File system operations
ā āāā local-settings.ts # Local configuration management
ā āāā markdown.ts # Markdown processing utilities
ā āāā openrouter-models.ts # OpenRouter model utilities
āāā scripts/
ā āāā security-check.sh # Security validation script
āāā docs/ # Documentation and images
āāā .gitignore # Comprehensive git ignore rules
āāā .npmignore # npm publish exclusions
āāā package.json # Package configuration
āāā tsconfig.json # TypeScript configuration
āāā LICENSE # MIT license
TL;DR: Start with src/core/cli.ts (main entry point), src/core/agent.ts, and src/ui/hooks/useAgent.ts (bridge between TUI and the agent). Tools are in src/tools/, slash commands are in src/commands/definitions/, and customize the TUI in src/ui/components/.
#### Adding New Tools
Tools are AI-callable functions that extend the CLI's capabilities. To add a new tool:
1. Define the tool schema in src/tools/tool-schemas.ts:
`typescript`
export const YOUR_TOOL_SCHEMA: ToolSchema = {
type: 'function',
function: {
name: 'your_tool_name',
description: 'What your tool does',
parameters: {
type: 'object',
properties: {
param1: {type: 'string', description: 'Parameter description'},
},
required: ['param1'],
},
},
};
2. Implement the tool function in src/tools/tools.ts:
`typescript`
export async function yourToolName(param1: string): Promise
// Your implementation here
return createToolResponse(true, result, 'Success message');
}
3. Register the tool in the TOOL_REGISTRY object and executeTool switch statement in src/tools/tools.ts.
4. Add the schema to ALL_TOOL_SCHEMAS array in src/tools/tool-schemas.ts.
#### Adding New Slash Commands
Slash commands provide direct user interactions. To add a new command:
1. Create command definition in src/commands/definitions/your-command.ts:
`typescript
import {CommandDefinition, CommandContext} from '../base.js';
export const yourCommand: CommandDefinition = {
command: 'yourcommand',
description: 'What your command does',
handler: ({addMessage}: CommandContext) => {
// Your command logic here
addMessage({
role: 'system',
content: 'Command response',
});
},
};
`
2. Register the command in src/commands/index.ts by importing it and adding to the availableCommands array.
#### Changing Start Command
To change the start command from openrouter, change "openrouter" in "bin" of package.json to your global command of choice.
Re-run npm run build and npm link`.
Improvements through PRs are welcome!
For issues and feature requests, please open an issue on GitHub.
MIT