OpenCode + OpenRouter 免费运行 AI 编码模型教程
🤖 Run AI Coding Models for FREE with OpenCode + OpenRouter
作者分享在终端用 OpenCode 搭配 OpenRouter 免费模型运行 AI 编码智能体的完整教程。
Hey guys, while everyone else keeps paying for ChatGPT's monthly subscription, we'll be getting AI to write code for us right here—for free. 😎 Today's plan is to set up OpenCode on our system and run it using OpenRouter's free models. Just have a little patience and keep your tea ready. ☕
📚 Today's Menu
- 🧐 OpenCode: What & Why
- 💻 Install OpenCode
- 🔑 Configure OpenCode with OpenRouter
- 🔌 Connect it on your local system
- 🚀 Run the app and pray
- 🎬 Wrapping up
🧐 1. OpenCode: What & Why
📖 Introduction
OpenCode is an open-source AI coding agent that lives in your terminal. A chatbot answers your question and leaves the copy-pasting to you. An agent actually opens your project, reads the files, edits them, and runs commands. Pehle aap code copy-paste karte the, ab AI khud mehnat karta hai, aur aap credit lete ho. 🤝
🎯 Why use OpenCode?
- 🔓 Open-source: You can read the code, tweak it, and nobody locks you in. Transparency ka full dose.
- 🧠 Model freedom: OpenAI, Anthropic, Google, OpenRouter, local models, whatever you like. Sab ke saath dosti. 🤗
- 🌐 OpenRouter support: One account, one API key, and a whole buffet of models.
- 🚫 No vendor lock-in: If one provider gets expensive or moody, switch models and carry on. Workflow wahi rahega.
- 🕵️ Agentic coding: It reads, edits, debugs, and works across your whole codebase, not just one pasted snippet.
- ⌨️ Terminal-first: Developers who love the command line will feel at home. Mouse? Kaun sa mouse? 🐭
- 🧩 Extensible: It supports MCP, plugins, custom commands, and agents.
- 🏠 Local-model support: You can run models on your own machine if your laptop can take the heat. 🔥
- 💸 Cost control: Use cheap or free models for easy jobs and save the fancy ones for serious work.
- 🎛️ Customizable: Agents, models, permissions, and per-project behaviour are all configurable.
💻 2. Install OpenCode
Since you're on Windows, there are two routes. One is easy, and the other is what the developers recommend. 😏
🅰️ Option 1: Windows + npm
- 📦 Install Node.js first if you don't have it. Without it, the next step will just throw an error at you.
- 🔵 Open PowerShell.
- Run:
npm install -g opencode-ai
- ✅ Check that it installed:
opencode --version
- 🎬 Start it:
opencode
If you see a version number, you're good. If you see a red error, check that Node.js is installed and that you opened a fresh PowerShell window afterwards. 🔄
🅱️ Option 2: Windows + WSL ⭐
The OpenCode team recommends WSL (Windows Subsystem for Linux) for the smoothest experience on Windows. Basically, they are saying Linux is the happy place, and honestly, same. 🐧
Inside WSL, run:
curl -fsSL https://opencode.ai/install | bash
Then verify and start it the same way:
opencode --version
opencode
💡 Which one should you pick? If you just want to get going, use npm. If you spend most of your day in a terminal anyway, WSL is the better long-term home.
🔑 3. Configure OpenCode with OpenRouter
🌐 What is OpenRouter, anyway?
Think of OpenRouter as the Swiggy of AI models. 🛵 You don't visit every restaurant (provider) separately. You order from one app with one account, and it delivers from many kitchens. Some of those dishes are even free.
🆕 Create an OpenRouter account
- Go to the OpenRouter sign-up page and register.
✨ Bonus point: People use tempmail.app for sign-up, but most temp mail services get rejected. A normal email is safer, and you won't lose your account later. 😅
- 🏠 Open your Dashboard and go to Keys.
- ➕ Click Create API Key.
- 🏷️ Name it something sensible, like
OpenCode. - 📋 Copy the key immediately. OpenRouter shows it only once, so if you lose it, you create a new one. Bhool gaye toh bhugto. 😬
⚠️ Warning: Never share this API key publicly, and above all never push it to GitHub. Bots scan public repos all day, and they will find your key faster than your relatives find your salary. 💰
🔌 4. Configure OpenCode on your local system
Once this step is done, you can run free-tier models straight from your machine. 🎉
- 🔗 Inside OpenCode, type /connect. A provider selection screen will appear.
- 🎯 Select OpenRouter from the list.
- 🔐 When it asks for an API key, paste the one you generated earlier. Check that no extra spaces sneaked in. 👀
- 🧠 Type /models and pick the models you want to use.
💡 You can browse every free model on OpenRouter through this link. Window shopping is free. 🛍️
📌 Note: Add at least one model first, such as
nvidia/nemotron-3-nano-omni-30b-a3b-reasoning:freeoropenrouter/free. After that, you can ask OpenCode to add more models for you.🎁 Lucky dip:
openrouter/freeautomatically picks an available free model based on what your request needs. Decision lene ka jhanjhat khatam. 🙌
🧱 Your final setup
┌─────────────────┐
│ You │
└────────┬────────┘
↓
┌─────────────────┐
│ OpenCode │
│ Coding Agent │
└────────┬────────┘
↓
┌─────────────────┐
│ OpenRouter │
│ API Gateway │
└────────┬────────┘
↓
┌─────────────────────────┐
│ Free AI Models │
│ Nemotron / Gemma / etc. │
└─────────────────────────┘
🚨 Warning: OpenRouter lists 25+ free models, but "free" comes with limits, including a stated 50 requests per day. So don't treat it like an all-you-can-eat thali. 🍛 Use your requests wisely.
🚀 5. Run the OpenCode application
🛠️ Optional: make the openrouter shortcut
If typing openrouter gives a "not recognized" error, create the shortcut yourself. Open your PowerShell profile:
notepad $PROFILE
Add this line, save, and restart PowerShell:
function openrouter { opencode }
🧪 The test
- Open PowerShell and type openrouter.
- Give it the easiest prompt in the world:
Create a simple Python Hello World program
- Wait. The model is thinking, so don't smash Enter like it's a lift button. 🛗
- If it asks permission to create a file, say yes. It's not a scam. 🙏
✅ It works?
Mubarak ho! 🎊 Now add more models and compare them. Some are fast and shallow, others are slow and thoughtful, just like your friends. 😄
❌ It doesn't work?
Don't panic. 😤 Troubleshoot like a pro:
- 🔍
openrouternot recognized → shortcut or PATH missing, or PowerShell not restarted - 🔑 Authentication error → wrong or expired API key, so paste it again
- 🐢 Model not responding → free models get overloaded, so try another one
- 📉 Requests failing suddenly → you may have hit the daily free limit, so come back tomorrow
🎬 Wrapping up
You now have an AI coding agent in your terminal, powered by free models, with no subscription needed. 🥳 Is it as sharp as the premium models? No. Does it cost ₹0? Haan. Toh bas, kaam chalao. 😎
Hope you liked what you saw above, do like if you've gained some knowledge. Do comment out if you're facing any issues in any step, would love to help troubleshoot.
来源:Google AI:DEV 作者专属(RSS) · dev.to