Get Gemini 3.1 Pro running in OpenCode for free via Google Vertex AI — $300 trial credit + $10/month developer benefit, with a proxy fix that actually works in mainland China.
The Goal
Quick Start (for AI Agents)
Markdown
I want to set up Google Vertex AI in OpenCode to use Gemini 3.1 Pro for free.
Reference: https://mjshao.fun/blog/opencode-vertex-gemini-pro
My setup:
- OS: [macOS / Linux]
- OpenCode config path: [~/.config/opencode/opencode.json]
- GCP project ID: [ask me]
- Service account JSON key path: [ask me]
- In mainland China (need proxy fix): [yes / no]
- Proxy address if yes: [http://127.0.0.1:7890]
- OpenCode launched as launchctl service: [yes / no]
Steps:
1. Install @ai-sdk/google-vertex in OpenCode's node_modules
2. Add "vertex" provider block to opencode.json with SA JSON key auth
3. If in China: add NODE_OPTIONS=--use-env-proxy to ~/.zshenv
4. If launchctl service: create ~/.bin/opencode-server wrapper script with proxy env vars, update plist ProgramArguments
5. Verify connectivity: node -e https.get test against oauth2.googleapis.com
6. Restart OpenCode server and confirm vertex provider appears
Two API Systems — What's the Difference
| AI Studio API | Vertex AI API | |
|---|---|---|
| Endpoint | generativelanguage.googleapis.com | aiplatform.googleapis.com |
| Auth | API Key (AIzaSy...) | GCP project + Service Account |
| Billing | AI Studio billing | GCP Billing Account |
| Free credits | Free tier only | $300 trial + $10/month |
| Quota | Shared, rate-limited | Independent quota |
Step 1: Get the Free Credits
$300 Trial (New GCP Account)
- Go to console.cloud.google.com
- Create a Billing Account → select "Start free trial"
- Credit applies automatically to Vertex AI API usage
$10/Month (Google Developer Program)
- Not automatic — you must claim it manually each month
- URL: developers.google.com/program/my-benefits
- $10 credit is consumed first, before the $300 trial credit
Step 2: GCP Project Setup
Use a Personal Gmail Account (No-Org)
Bash
gcloud projects describe YOUR_PROJECT_ID --format='value(parent.type)'
# empty output = no-org ✅
# "organization" = might cause issues ⚠️
Create the Project
Bash
# Or do it in GCP Console
gcloud projects create YOUR_PROJECT_ID --name="my-vertex-project"
gcloud config set project YOUR_PROJECT_ID
Enable Vertex AI API
Bash
gcloud services enable aiplatform.googleapis.com
Create a Service Account + JSON Key
Bash
# Create service account
gcloud iam service-accounts create vertex-sa \
--display-name="Vertex AI SA"
# Grant Vertex AI User role
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:vertex-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"
# Create JSON key
gcloud iam service-accounts keys create ~/vertex-key.json \
--iam-account="vertex-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com"
Set a Spend Cap (Important)
- Amount: $0
- Actions: ✅ "Disable billing on this project when budget is exceeded"
Step 3: OpenCode Configuration
Install the SDK
Bash
cd ~/.local/share/opencode && bun add @ai-sdk/google-vertex
Bash
ls ~/.local/share/opencode/node_modules/@ai-sdk/google-vertex/package.json && echo "OK"
Add to opencode.json
Json
{
"providers": {
"vertex": {
"name": "🪐 Vertex AI",
"npm": "@ai-sdk/google-vertex",
"options": {
"project": "YOUR_PROJECT_ID",
"location": "global",
"googleAuthOptions": {
"keyFile": "/absolute/path/to/vertex-key.json"
}
},
"whitelist": [
"gemini-3.1-pro-preview-customtools",
"gemini-3.1-pro-preview",
"gemini-3-flash-preview",
"gemini-3.1-flash-lite-preview"
]
}
}
}
Why the Custom npm Provider (Not Built-in Vertex)
Step 4: Proxy Fix for Mainland China
The Problem
The Fix
Bash
export NODE_OPTIONS="--use-env-proxy"
Bash
export HTTP_PROXY="http://127.0.0.1:7890"
export HTTPS_PROXY="http://127.0.0.1:7890"
export NO_PROXY="localhost,127.0.0.1,10.0.0.0/8"
The Launchctl Gotcha
Bash
# ~/.bin/opencode-server
#!/bin/bash
export NODE_OPTIONS="--use-env-proxy"
export HTTP_PROXY="http://127.0.0.1:7890"
export HTTPS_PROXY="http://127.0.0.1:7890"
export NO_PROXY="10.0.0.0/8,127.0.0.1,localhost"
exec /path/to/opencode serve --port 4096 --hostname 0.0.0.0 "$@"
Bash
chmod +x ~/.bin/opencode-server
Xml
<key>ProgramArguments</key>
<array>
<string>/Users/YOUR_USERNAME/.bin/opencode-server</string>
</array>
Bash
launchctl unload ~/Library/LaunchAgents/ai.opencode.server.plist
launchctl load ~/Library/LaunchAgents/ai.opencode.server.plist
Verify It's Working
Bash
# Test Node.js can reach Google through proxy
NODE_OPTIONS="--use-env-proxy" HTTP_PROXY="http://127.0.0.1:7890" \
node -e "const https=require('https'); https.get('https://oauth2.googleapis.com/', (res)=>{console.log('HTTP', res.statusCode); res.destroy()})"
# HTTP 404 = reachable ✅ (404 just means no handler for /, that's fine)
# Confirm running server has the env vars
ps eww $(pgrep -f "opencode serve") | tr ' ' '\n' | grep -E "NODE_OPTIONS|HTTP_PROXY"
Step 5: Fallback Chain
Text
vertex/gemini-3.1-pro-preview-customtools ← primary (Vertex project 1)
↓ 429 / fail
vertex2/gemini-3.1-pro-preview-customtools ← secondary (Vertex project 2)
↓ 429 / fail
google/gemini-3.1-pro-preview ← AI Studio (API key)
↓ 429 / fail
qwen-coding-plan/qwen3-max ← final fallback
Text
vertex/gemini-3.1-flash-lite-preview
↓ 429 / fail
vertex2/gemini-3.1-flash-lite-preview
↓ 429 / fail
google/gemini-3.1-flash-lite-preview
Model IDs (global region)
| Model ID | Type | Notes |
|---|---|---|
| gemini-3.1-pro-preview-customtools | Pro | Recommended — stronger tool use |
| gemini-3.1-pro-preview | Pro | Standard |
| gemini-3-flash-preview | Flash | Fast |
| gemini-3.1-flash-lite-preview | Flash Lite | Lightest |
Diagnostic Commands
Bash
# Confirm no ADC file (triggers built-in provider)
ls ~/.config/gcloud/application_default_credentials.json 2>/dev/null && echo "WARNING" || echo "OK"
# Confirm plist has no GCP env vars that trigger built-in provider
/usr/libexec/PlistBuddy -c "Print :EnvironmentVariables" \
~/Library/LaunchAgents/ai.opencode.server.plist | grep -i "GOOGLE_APPLICATION\|GOOGLE_CLOUD"
# Check SDK installed
ls ~/.local/share/opencode/node_modules/@ai-sdk/google-vertex/package.json && echo "OK"
# Restart server
launchctl stop ai.opencode.server && sleep 1 && launchctl start ai.opencode.server
Let AI Do It
Bash
I want to set up Google Vertex AI in OpenCode to use free Gemini 3.1 Pro.
Reference: https://mjshao.fun/blog/opencode-vertex-gemini-pro
My setup:
- GCP project ID: [ask me]
- Service account JSON key path: [ask me]
- OpenCode config: ~/.config/opencode/opencode.json
- In mainland China (need proxy fix): [yes / no]
- Proxy address if yes: [http://127.0.0.1:7890]
- OpenCode runs as launchctl service: [yes / no]
Steps:
1. Install @ai-sdk/google-vertex in ~/.local/share/opencode via bun
2. Add vertex provider block to opencode.json with SA JSON key
3. If in China: add NODE_OPTIONS=--use-env-proxy to ~/.zshenv
4. If launchctl: create ~/.bin/opencode-server wrapper, update plist ProgramArguments
5. Verify: node https.get test to oauth2.googleapis.com → expect HTTP 404
6. Reload launchctl service and confirm vertex provider appears in OpenCode
通过 Google Vertex AI 免费运行 Gemini 3.1 Pro — $300 试用额度 + 每月 $10 开发者福利,附中国大陆代理修复方案。
目标
快速配置(给 AI Agent)
Markdown
我想在 OpenCode 里配置 Google Vertex AI,免费使用 Gemini 3.1 Pro。
参考文章:https://mjshao.fun/blog/opencode-vertex-gemini-pro
我的环境:
- OS:[macOS / Linux]
- OpenCode 配置路径:[~/.config/opencode/opencode.json]
- GCP 项目 ID:[告诉我]
- 服务账号 JSON Key 路径:[告诉我]
- 在中国大陆(需要代理修复):[是 / 否]
- 代理地址(如果是):[http://127.0.0.1:7890]
- OpenCode 以 launchctl 服务方式运行:[是 / 否]
步骤:
1. 在 OpenCode 的 node_modules 里安装 @ai-sdk/google-vertex
2. 在 opencode.json 里添加 vertex provider,使用 SA JSON Key 认证
3. 如果在中国:在 ~/.zshenv 添加 NODE_OPTIONS=--use-env-proxy
4. 如果是 launchctl 服务:创建 ~/.bin/opencode-server wrapper 脚本,更新 plist ProgramArguments
5. 验证:node https.get 测试 oauth2.googleapis.com → 期望 HTTP 404
6. 重启 OpenCode server,确认 vertex provider 出现
两套 API 系统
| AI Studio API | Vertex AI API | |
|---|---|---|
| 接入点 | generativelanguage.googleapis.com | aiplatform.googleapis.com |
| 认证 | API Key(AIzaSy...) | GCP 项目 + 服务账号 |
| 计费 | AI Studio 独立计费 | GCP Billing Account |
| 免费额度 | 仅 Free Tier 限速 | $300 试用 + 每月 $10 |
| Quota | 共享,限速较严 | 独立 quota |
第一步:获取免费额度
$300 试用额度(新账号)
- 进入 console.cloud.google.com
- 创建 Billing Account → 选择"Start free trial"
- 自动计入 Vertex AI API 用量
每月 $10(Google Developer Program)
- 不自动发放 — 每月要去手动 claim
- 地址:developers.google.com/program/my-benefits
- $10 额度优先消耗,再消耗 $300 试用额度
第二步:GCP 项目设置
必须用个人 Gmail(No-Org)
Bash
gcloud projects describe YOUR_PROJECT_ID --format='value(parent.type)'
# 空输出 = no-org ✅
# 输出 "organization" = 有 org,可能有问题 ⚠️
创建步骤
Bash
# 启用 Vertex AI API
gcloud services enable aiplatform.googleapis.com
# 创建服务账号
gcloud iam service-accounts create vertex-sa \
--display-name="Vertex AI SA"
# 授予 Vertex AI User 角色
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:vertex-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"
# 创建 JSON Key
gcloud iam service-accounts keys create ~/vertex-key.json \
--iam-account="vertex-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com"
设置 Spend Cap(防止超额)
- Amount:$0
- Actions:✅ "Disable billing on this project when budget is exceeded"
第三步:OpenCode 配置
安装 SDK
Bash
cd ~/.local/share/opencode && bun add @ai-sdk/google-vertex
添加到 opencode.json
Json
{
"providers": {
"vertex": {
"name": "🪐 Vertex AI",
"npm": "@ai-sdk/google-vertex",
"options": {
"project": "YOUR_PROJECT_ID",
"location": "global",
"googleAuthOptions": {
"keyFile": "/绝对路径/vertex-key.json"
}
},
"whitelist": [
"gemini-3.1-pro-preview-customtools",
"gemini-3.1-pro-preview",
"gemini-3-flash-preview",
"gemini-3.1-flash-lite-preview"
]
}
}
}
第四步:中国大陆代理修复
问题根源
解决方案
Bash
export NODE_OPTIONS="--use-env-proxy"
export HTTP_PROXY="http://127.0.0.1:7890"
export HTTPS_PROXY="http://127.0.0.1:7890"
export NO_PROXY="localhost,127.0.0.1,10.0.0.0/8"
Launchctl 的坑
Bash
# ~/.bin/opencode-server
#!/bin/bash
export NODE_OPTIONS="--use-env-proxy"
export HTTP_PROXY="http://127.0.0.1:7890"
export HTTPS_PROXY="http://127.0.0.1:7890"
export NO_PROXY="10.0.0.0/8,127.0.0.1,localhost"
exec /path/to/opencode serve --port 4096 --hostname 0.0.0.0 "$@"
Xml
<key>ProgramArguments</key>
<array>
<string>/Users/YOUR_USERNAME/.bin/opencode-server</string>
</array>
验证
Bash
# 测试 Node.js 能通过代理访问 Google
NODE_OPTIONS="--use-env-proxy" HTTP_PROXY="http://127.0.0.1:7890" \
node -e "const https=require('https'); https.get('https://oauth2.googleapis.com/', (res)=>{console.log('HTTP', res.statusCode); res.destroy()})"
# HTTP 404 = 可达 ✅
# 确认运行中的 server 有代理变量
ps eww $(pgrep -f "opencode serve") | tr ' ' '\n' | grep -E "NODE_OPTIONS|HTTP_PROXY"
第五步:Fallback 链
Text
vertex/gemini-3.1-pro-preview-customtools ← 主力(Vertex 项目 1)
↓ 429 / 失败
vertex2/gemini-3.1-pro-preview-customtools ← 备用(Vertex 项目 2)
↓ 429 / 失败
google/gemini-3.1-pro-preview ← AI Studio(API Key)
↓ 429 / 失败
qwen-coding-plan/qwen3-max ← 最终兜底
Text
vertex/gemini-3.1-flash-lite-preview
↓ 429 / 失败
vertex2/gemini-3.1-flash-lite-preview
↓ 429 / 失败
google/gemini-3.1-flash-lite-preview
可用 Model ID(global region)
| Model ID | 类型 | 说明 |
|---|---|---|
| gemini-3.1-pro-preview-customtools | Pro | 推荐,工具调用更强 |
| gemini-3.1-pro-preview | Pro | 标准版 |
| gemini-3-flash-preview | Flash | 快速 |
| gemini-3.1-flash-lite-preview | Flash Lite | 最轻量 |
诊断命令
Bash
# 确认无 ADC 文件(会触发内置 provider)
ls ~/.config/gcloud/application_default_credentials.json 2>/dev/null && echo "WARNING" || echo "OK"
# 确认 plist 无 GCP 环境变量
/usr/libexec/PlistBuddy -c "Print :EnvironmentVariables" \
~/Library/LaunchAgents/ai.opencode.server.plist | grep -i "GOOGLE_APPLICATION\|GOOGLE_CLOUD"
# 检查 SDK 已安装
ls ~/.local/share/opencode/node_modules/@ai-sdk/google-vertex/package.json && echo "OK"
# 重启 server
launchctl stop ai.opencode.server && sleep 1 && launchctl start ai.opencode.server