程式碼代理

GitHub Copilot CLI 設定指南

以自帶金鑰(BYOK)環境變數將 GitHub Copilot CLI 接上 RealRelay.ai:安裝、設定供應商、檢查連線並選擇模型。

協定
OpenAI Chat Completions · Anthropic Messages
基礎位址
https://www.realrelay.ai/v1
官方文件
docs.github.com
已對照工具官方文件核對
2026-10

概覽

GitHub Copilot CLI 是 GitHub 推出的終端機程式碼代理。它會讀取你的版本庫、編輯檔案,並在你核准後執行指令。使用自帶金鑰(BYOK)時,它會把請求送到你選擇的供應商。它的預設供應商類型使用 OpenAI Chat Completions,因此是連到 RealRelay.ai 的 /v1/chat/completions 端點。若使用 Claude 模型,也可以改用 Anthropic 類型與 /v1/messages。

  • 一組 API 金鑰,在主控台的 API 金鑰頁面取得。

  • openai 類型使用含 /v1 的基礎位址。anthropic 類型則使用不含 /v1 的網站位址。

  • 一個模型 ID,完全依照 GET /v1/models 列出的寫法,例如 z-ai/glm-5.2。

Copilot CLI 需要支援工具呼叫與串流的模型,GitHub 也建議上下文視窗至少 128K token。GitHub 的安裝指南另將有效的 Copilot 訂閱列為使用此 CLI 的前提。

安裝

請使用 GitHub 提供的任一種安裝方式。npm 套件需要 Node.js 22 或更新版本。

npm (Node.js 22+)
npm install -g @github/copilotcopilot --version
Homebrew (macOS · Linux)
brew install --cask copilot-clicopilot --version
Windows PowerShell
winget install GitHub.Copilotcopilot --version
macOS · Linux
curl -fsSL https://gh.io/copilot-install | bashcopilot --version

如果 copilot --version 印出版本號,就代表安裝成功。首次啟動時,Copilot CLI 可能會要求你執行 /login 登入 GitHub。

設定

Copilot CLI 只會從環境變數讀取供應商設定,所以請把它們寫進你的 shell profile。請把 sk-*** 換成你的金鑰。

~/.zshrc · ~/.bashrc
1export COPILOT_PROVIDER_TYPE="openai"2export COPILOT_PROVIDER_BASE_URL="https://www.realrelay.ai/v1"3export COPILOT_PROVIDER_API_KEY="sk-***"4export COPILOT_MODEL="z-ai/glm-5.2"5export COPILOT_PROVIDER_MAX_PROMPT_TOKENS=1280006export COPILOT_PROVIDER_MAX_OUTPUT_TOKENS=32000

啟動 Copilot CLI 前,先執行 source ~/.zshrc,或開啟新的終端機。

Windows PowerShell
1[Environment]::SetEnvironmentVariable("COPILOT_PROVIDER_TYPE", "openai", "User")2[Environment]::SetEnvironmentVariable("COPILOT_PROVIDER_BASE_URL", "https://www.realrelay.ai/v1", "User")3[Environment]::SetEnvironmentVariable("COPILOT_PROVIDER_API_KEY", "sk-***", "User")4[Environment]::SetEnvironmentVariable("COPILOT_MODEL", "z-ai/glm-5.2", "User")5[Environment]::SetEnvironmentVariable("COPILOT_PROVIDER_MAX_PROMPT_TOKENS", "128000", "User")6[Environment]::SetEnvironmentVariable("COPILOT_PROVIDER_MAX_OUTPUT_TOKENS", "32000", "User")

這些變數會儲存在你的使用者帳戶中。啟動 Copilot CLI 前,請關閉並重新開啟終端機。

~/.zshrc · ~/.bashrc
1export COPILOT_PROVIDER_TYPE="anthropic"2export COPILOT_PROVIDER_BASE_URL="https://www.realrelay.ai"3export COPILOT_PROVIDER_API_KEY="sk-***"4export COPILOT_MODEL="anthropic/claude-sonnet-4-6"5export COPILOT_PROVIDER_MAX_PROMPT_TOKENS=1280006export COPILOT_PROVIDER_MAX_OUTPUT_TOKENS=32000

使用 Claude 模型時,請以這組設定取代第一組;RealRelay.ai 在 /v1/messages 上原生提供這些模型。

  • COPILOT_PROVIDER_TYPE 的預設值是 openai;這裡明確寫出來,是為了讓你只改一行就能切換成 anthropic。

  • 除非每次都傳入 --model,否則必須設定 COPILOT_MODEL。

  • Copilot CLI 會依模型的知名名稱查詢其限制,而 RealRelay.ai 的 ID 並不是這類知名名稱。COPILOT_PROVIDER_MAX_PROMPT_TOKENS 與 COPILOT_PROVIDER_MAX_OUTPUT_TOKENS 會告訴它要送出多少、要求回傳多少。範例數值對這裡提到的模型都是安全的;你可以調高到該模型目錄頁面上標示的上限。

  • 不要提交含有金鑰的 profile 或 .env 檔案。

驗證

  1. 1

    直接呼叫端點

    用你的金鑰執行這段指令。若收到含 choices 陣列的 JSON 回覆,就代表位址、金鑰與模型都沒問題。

    Terminal
    1curl -sS "https://www.realrelay.ai/v1/chat/completions" \2  -H "Authorization: Bearer sk-***" \3  -H "Content-Type: application/json" \4  -d '{"model": "z-ai/glm-5.2", "max_tokens": 16, "messages": [{"role": "user", "content": "Reply with OK"}]}'
  2. 2

    執行單一提示詞

    在已設定變數的終端機中,執行一個提示詞。Copilot CLI 會印出回答後結束。

    Terminal
    copilot -p "Reply with OK"
  3. 3

    啟動工作階段

    在專案中啟動 copilot,送出一則簡短訊息,然後執行 /usage。token 總計中應列出你的 RealRelay.ai 模型,而這些請求也會出現在主控台的用量日誌中。

    GitHub Copilot CLI
    cd /path/to/your/projectcopilot/usage

模型

模型取自 COPILOT_MODEL。若只想在單次執行中改用其他模型,請傳入 --model;若要永久更換,請修改該變數並開啟新的終端機。

Terminal
copilot --model z-ai/glm-5.2

請挑選支援工具呼叫、上下文至少 128K token 的模型,並把兩個 token 上限設成與它相符。使用 anthropic 類型時,請使用 anthropic/* 模型。請參閱選擇模型。

疑難排解

  • 出現模型不支援工具或串流的錯誤。Copilot CLI 無法使用該模型。請改選支援工具呼叫的模型。

  • 401 或「Invalid token」。金鑰沒有傳到 CLI。請確認在你啟動 copilot 的終端機中已設定 COPILOT_PROVIDER_API_KEY。

  • 404。基礎位址形式與供應商類型不符:openai 需要 /v1,anthropic 則不能有。

  • 找不到模型。COPILOT_MODEL 必須與 RealRelay.ai 的 ID 完全相同,包括像 z-ai/ 這樣的前綴。

  • 回覆被截斷,或在長時間工作階段中出現上下文錯誤。token 上限沒有設定,或超出模型允許的範圍。請依模型目錄頁面設定。

  • RealRelay.ai 只在 Responses 上提供的模型(例如 openai/gpt-5.3-codex)在 Chat Completions 上會失敗。請為它切換 wire API:

    ~/.zshrc · ~/.bashrc
    export COPILOT_PROVIDER_WIRE_API="responses"export COPILOT_MODEL="openai/gpt-5.3-codex"
  • 你希望 CLI 只與 RealRelay.ai 通訊。GitHub 的文件說明了一種離線模式,可讓 CLI 不再連線到 GitHub 的伺服器。你的提示詞仍會送到 RealRelay.ai。

    ~/.zshrc · ~/.bashrc
    export COPILOT_OFFLINE=true