DEV Community

Cover image for How to Power OpenCode CLI Agent with Decentralized 0G Router
Emmanuel Haankwenda
Emmanuel Haankwenda Subscriber

Posted on

How to Power OpenCode CLI Agent with Decentralized 0G Router

Agentic coding tools are becoming the default way many developers write software. While most people connect them to centralised APIs, 0G Network lets you run the same OpenAI-compatible workflows on decentralised compute using your 0G credits.

Because the 0G Router API is OpenAI-compatible, OpenCode can use it without any code changes—just point it at a different endpoint.

In this guide you'll learn how to:

  • Install OpenCode
  • Configure it to use the 0G Router
  • Connect your API key
  • Run GPT-5.6 Sol, DeepSeek V4 Pro, or GLM-5.2 from your terminal

Prerequisites

Before getting started, you'll need:

  • Node.js installed
  • A 0G account with compute credits
  • An API key from the 0G Console

Step 1 - Install OpenCode

Install OpenCode globally:

npm install -g opencode
Enter fullscreen mode Exit fullscreen mode

Step 2 - Generate a 0G API Key

Head over to the 0G Console.

From there you can:

  • View your available credits
  • Browse available models
  • Generate an API key

Useful links:

Your generated API key will look similar to:

sk-xxxxxxxxxxxxxxxxxxxxxxxx
Enter fullscreen mode Exit fullscreen mode

Step 3 - Configure OpenCode

Open your configuration file:

nano ~/.config/opencode/opencode.json
Enter fullscreen mode Exit fullscreen mode

Replace the contents with:

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "0g": {
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "name": "0G Router",
        "baseURL": "https://router-api.0g.ai/v1",
        "apiKey": "{env:OG_API_KEY}"
      },
      "models": {
        "gpt-5.6-sol": {
          "id": "gpt-5.6-sol",
          "name": "GPT-5.6 Sol",
          "limit": {
            "context": 1048576,
            "output": 128000
          }
        },
        "deepseek-v4-pro": {
          "id": "deepseek-v4-pro",
          "name": "DeepSeek V4 Pro",
          "limit": {
            "context": 1048576,
            "output": 393216
          }
        },
        "glm-5.2": {
          "id": "zai-org/GLM-5-FP8",
          "name": "GLM-5.2",
          "limit": {
            "context": 1048576,
            "output": 131072
          }
        }
      }
    }
  },
  "model": "0g/gpt-5.6-sol"
}
Enter fullscreen mode Exit fullscreen mode

Save the file and exit your editor.


Step 4 - Export Your API Key

Export your key into your terminal session:

export OG_API_KEY="your_0g_api_key_here"
Enter fullscreen mode Exit fullscreen mode

Launch OpenCode:

opencode
Enter fullscreen mode Exit fullscreen mode

Step 5 - Switch Between Models

Inside OpenCode run:

/models
Enter fullscreen mode Exit fullscreen mode

You'll be able to choose between:

Model Best For
GPT-5.6 Sol Long reasoning, planning, complex coding tasks
DeepSeek V4 Pro Fast editing, large refactors, repositories
GLM-5.2 General-purpose coding and analysis

Step 6 - Test Agentic Coding

Give OpenCode a task that requires planning, file creation and execution.

Example:

Create a zero-dependency HTML/JavaScript Flappy Bird game inside ./flappy-bird. Launch a local preview and verify that it works.

OpenCode will:

  • Create the directory
  • Generate the game
  • Write the HTML, CSS and JavaScript
  • Launch a preview server (when available)
  • Verify the output

All inference requests are routed through the 0G Router.


How It Works

OpenCode CLI
      │
      ▼
0G Router API
      │
      ▼
Decentralised GPU Compute
      │
      ▼
Model Execution
      │
      ▼
Tool Calls + Generated Code
Enter fullscreen mode Exit fullscreen mode

Because the Router follows the OpenAI API specification, most existing AI SDKs and tools work by simply changing the baseURL.


Why Use 0G Router?

  • OpenAI-compatible API — works with existing tooling.
  • Decentralised compute — inference runs across the 0G Network.
  • Pay with 0G credits — use the credits in your 0G account to access powerful models.
  • Large context windows — models support up to 1M tokens.
  • Drop-in compatibility — point existing OpenAI-compatible tools at the Router with minimal configuration.

Resources


Final Thoughts

The best part about the 0G Router is that you don't have to learn a new API. If your application, SDK, or coding agent already supports the OpenAI API, switching to decentralised inference is often as simple as changing the baseURL and supplying a 0G API key.

That makes it easy to experiment with flagship open-weight models like GPT-5.6 Sol, DeepSeek V4 Pro, and GLM-5.2 while taking advantage of decentralised GPU infrastructure.

Have you tried OpenCode with the 0G Router yet? If so, I'd love to hear what you're building in the comments.

Top comments (0)