TokenLab

程式工具

GitHub Copilot CLI

使用 BYOK 將 GitHub Copilot CLI 連接到 TokenLab

概覽

GitHub Copilot CLI 可以連接到任何與 OpenAI Chat Completions 相容的端點。TokenLab 是透過 Copilot CLI 的 BYOK 環境變數來運作的。

安裝

若使用 Node.js 22 或更高版本,請透過 npm 安裝:

npm install -g @github/copilot@1.0.85

在 macOS 或 Linux 上,您也可以使用 Homebrew:

brew install --cask copilot-cli

驗證安裝:

copilot --version

設定 TokenLab

在啟動 Copilot CLI 之前,請先設定提供者變數:

請使用 macOS、Linux、WSL 或 Windows PowerShell 的 快速入門 終端機說明來設定 TOKENLAB_API_KEY,以避免將金鑰寫入指令歷史記錄中。從該終端機啟動 Copilot CLI。若需持久使用,請使用您當地的密碼管理工具;請勿將明文金鑰附加到您的 shell 設定檔中。

這些變數僅適用於本次啟動;您已儲存的帳戶、預設模型和權限設定將保持不變。若要使用先前的設定,請在不帶這些變數的情況下正常啟動。請勿將它們持久化在您的 shell 設定檔中。

COPILOT_PROVIDER_TYPE=openai COPILOT_PROVIDER_BASE_URL=https://api.tokenlab.sh/v1 COPILOT_PROVIDER_API_KEY="${TOKENLAB_API_KEY:?Set TOKENLAB_API_KEY first}" copilot --model claude-sonnet-5-5
變數TokenLab 數值
COPILOT_PROVIDER_TYPEopenai
COPILOT_PROVIDER_BASE_URLhttps://api.tokenlab.sh/v1
COPILOT_PROVIDER_API_KEY您的 TokenLab API key
COPILOT_MODEL即時模型 ID,例如 claude-sonnet-5-5

GitHub 要求所選模型必須同時支援串流 (streaming) 與工具呼叫 (tool calling)。claude-sonnet-5-5 支援工具呼叫,且其上下文視窗大於 GitHub 建議的 128K 最低要求。

驗證

在 Copilot CLI 中,先要求執行唯讀操作,例如:

Inspect this repository and summarize its top-level structure. Do not modify files.

儲存設定或列出模型並不會驗證金鑰或完成請求。提示詞 (prompt) 會消耗 TokenLab 餘額。在嘗試使用工具之前,請檢查回覆及其對應的 請求記錄;輔助請求也可能會被計費。

若要查看 Copilot CLI 記錄的提供者設定,請執行:

copilot help providers

在同一個 Agent 中生成圖像與影片

Copilot CLI 也可以透過 TokenLab MCP server 建立圖像與短片。每次生成都會從相同的 TokenLab 餘額中扣除。將此項目合併到 ~/.copilot/mcp-config.json 中。Copilot CLI 僅將 PATH 傳遞給伺服器,因此這兩個變數都應放在 env 中:

{
  "mcpServers": {
    "tokenlab": {
      "type": "local",
      "command": "npx",
      "args": ["-y", "@tokenlabai/mcp-server@0.6.48"],
      "env": {
        "TOKENLAB_MCP_TOOL_PROFILE": "core",
        "TOKENLAB_API_KEY": "<TOKENLAB_API_KEY>"
      },
      "tools": ["*"]
    }
  }
}

僅在此本機檔案中將 <TOKENLAB_API_KEY> 替換為您的金鑰。啟動 Copilot CLI 並執行 /mcp show tokenlab 以檢查連線。然後嘗試:

Call get_model_pricing for nano-banana-2, then use create_image with nano-banana-2 at 1k to make a photo of a matte black coffee mug on a white desk.

create_video 會回傳一個任務 ID。Agent 會呼叫 get_task_status 直到影片準備就緒。請參閱 將 MCP 伺服器新增至 Copilot CLI 以了解其他設定。

疑難排解

請勿將 API 金鑰提交到 Git 追蹤的 shell 指令碼或 dotfiles 中。若金鑰曾暴露在終端機歷史記錄、螢幕截圖或日誌中,請務必進行輪替 (rotate)。

本頁內容