Skip to content

OpenCode 配置指南

安装

安装脚本

bash
curl -fsSL https://opencode.ai/install | bash

NPM

bash
npm i -g opencode-ai@latest

Homebrew

bash
brew install anomalyco/tap/opencode

Scoop(Windows)

powershell
scoop install opencode

Chocolatey(Windows)

powershell
choco install opencode

Arch Linux

bash
sudo pacman -S opencode

API 配置

配置文件

编辑 ~/.config/opencode.json

Anthropic 模型(Claude)

json
{
	"$schema": "https://opencode.ai/config.json",
	"provider": {
		"xcodeapi": {
			"models": {
				"claude-opus-4-6": {
					"name": "claude-opus-4-6"
				},
				"claude-sonnet-4-6": {
					"name": "claude-sonnet-4-6"
				}
			},
			"name": "X-Code API",
			"npm": "@ai-sdk/anthropic",
			"options": {
				"apiKey": "your-api-key",
				"baseURL": "https://api-cn.x-code.cc/v1"
			}
		}
	}
}

OpenAI 模型(GPT)

json
{
	"$schema": "https://opencode.ai/config.json",
	"provider": {
		"xcodeapi-openai": {
			"models": {
				"gpt-5.4": {
					"name": "gpt-5.4"
				}
			},
			"name": "X-Code API",
			"npm": "@ai-sdk/openai",
			"options": {
				"apiKey": "your-api-key",
				"baseURL": "https://api-cn.x-code.cc/v1"
			}
		}
	}
}

同时配置多个 Provider

json
{
	"$schema": "https://opencode.ai/config.json",
	"provider": {
		"xcodeapi-anthropic": {
			"models": {
				"claude-opus-4-6": {
					"name": "claude-opus-4-6",
					"options": {
						"thinking": {
							"type": "adaptive"
						},
						"effort": "max"
					},
					"variants": {
						"low": {
							"thinking": {
								"type": "adaptive"
							},
							"effort": "low"
						},
						"medium": {
							"thinking": {
								"type": "adaptive"
							},
							"effort": "medium"
						},
						"high": {
							"thinking": {
								"type": "adaptive"
							},
							"effort": "high"
						},
						"max": {
							"thinking": {
								"type": "adaptive"
							},
							"effort": "max"
						}
					}
				},
				"claude-sonnet-4-6": {
					"name": "claude-sonnet-4-6",
					"options": {
						"thinking": {
							"type": "adaptive"
						},
						"effort": "max"
					},
					"variants": {
						"low": {
							"thinking": {
								"type": "adaptive"
							},
							"effort": "low"
						},
						"medium": {
							"thinking": {
								"type": "adaptive"
							},
							"effort": "medium"
						},
						"high": {
							"thinking": {
								"type": "adaptive"
							},
							"effort": "high"
						},
						"max": {
							"thinking": {
								"type": "adaptive"
							},
							"effort": "max"
						}
					}
				}
			},
			"name": "X-Code API (Anthropic)",
			"npm": "@ai-sdk/anthropic",
			"options": {
				"apiKey": "your-api-key",
				"baseURL": "https://api-cn.x-code.cc/v1"
			}
		},
		"xcodeapi-openai": {
			"models": {
				"gpt-5.4": {
					"name": "gpt-5.4",
					"options": {
						"reasoningEffort": "xhigh"
					},
					"variants": {
						"low": {
							"reasoningEffort": "low"
						},
						"medium": {
							"reasoningEffort": "medium"
						},
						"high": {
							"reasoningEffort": "high"
						},
						"xhigh": {
							"reasoningEffort": "xhigh"
						}
					}
				}
			},
			"name": "X-Code API (OpenAI)",
			"npm": "@ai-sdk/openai",
			"options": {
				"apiKey": "your-api-key",
				"baseURL": "https://api-cn.x-code.cc/v1"
			}
		}
	}
}

配置文件位置

文件路径
配置文件~/.config/opencode.json

启动

配置完成后,在项目目录中运行:

bash
cd your-project
opencode