Goose API Key 配置

Goose API Key 怎么填:config.yaml 配置

把下面这段贴进 config.yaml,再把你自己的 API Key 写进 ~/.bashrc,Goose 就能用 Claude 模型了,按量付费。

~/.config/goose/config.yamlyaml
config
GOOSE_PROVIDER: openai
GOOSE_MODEL: claude-haiku-4-5-20251001
OPENAI_HOST: https://api.aicodewith.ai
OPENAI_BASE_PATH: v1/chat/completions

配置步骤

一键长期设置 Key

登录后会填入你本人的 Key。复制并在终端执行一次,写入这个工具实测过的配置位置;换终端也还在。

bash
for rc in "$HOME/.zshrc" "$HOME/.bashrc"; do
  touch "$rc"
  echo 'export OPENAI_API_KEY=sk-***' >> "$rc"
done

没登录时只显示占位符。登录后会自动填你的 Key。 去控制台取 Key

复制成功只表示命令已复制到剪贴板。还要在终端执行,或把 Key 贴进工具设置。 获取 API Key 教程

以下步骤在 Linux(bash)实测。macOS 和 Windows 的写法还没实测,暂不列出。

  1. 还没装 Goose?先安装(装过可跳过)

    运行 curl -fsSL https://github.com/aaif-goose/goose/releases/download/stable/download_cli.sh | CONFIGURE=false bash,再运行 goose --version,打印出版本号就装好了。

    安装 Goose:curl -fsSL https://github.com/aaif-goose/goose/releases/download/stable/download_cli.sh | CONFIGURE=false bash(CONFIGURE=false 跳过交互配置),装到 ~/.local/bin/goose。goose --version 输出 1.54.0
  2. 把配置贴进 config.yaml

    打开 ~/.config/goose/config.yaml,贴入页首那段配置并保存。以后不用再 export 这些变量。

    把接口写进 ~/.config/goose/config.yaml
  3. 把你的 API Key 写进 ~/.bashrc

    运行 echo 'export OPENAI_API_KEY=sk-***' >> ~/.bashrc,sk-*** 换成你的 API Key(获取 API Key)。~/.bashrc 最后一行是这句就对了。

    确认 ~/.bashrc 最后一行是 export OPENAI_API_KEY=sk-***(图中已打码)
  4. 新开终端,测试 Goose

    新开一个终端,运行 goose run --no-session --quiet --max-turns 1 -t "Reply with exactly: OK",看到 OK 就说明 Goose 能用你的 API Key 了。

    验证:关掉终端、新开一个,运行 goose run --no-session --quiet --max-turns 1 -t "Reply with exactly: OK",回复 OK 即配置好
  5. 没看到 OK?对着报错找

    把终端里的报错和下面的常见报错对一下,每条都写了原因和改法。

  6. Goose 换模型:改 GOOSE_MODEL

    把 config.yaml 里 GOOSE_MODEL 后面的模型名换成新的 Model ID,名字要一字不差。Model ID 见模型与价格。

常见报错

没设 Key:Authentication failed for https://api.aicodewith.ai/v1/chat/completions. Status: 401 Unauthorized. Response: {"error":"请在请求中提供有效的 API Key。"}

出现什么

error: Ran into this error: Authentication error: Authentication failed for https://api.aicodewith.ai/v1/chat/completions. Status: 401 Unauthorized. Response: {"error":"请在请求中提供有效的 API Key。"}.
原因
终端里没有 OPENAI_API_KEY:没写进 ~/.bashrc,或者写了但没新开终端
怎么改
按第 3 步把 Key 写进 ~/.bashrc,然后新开终端

回到第 3 步:把你的 API Key 写进 ~/.bashrc →

没设 Key:新终端里没有 OPENAI_API_KEY。运行 goose run --no-session --quiet --max-turns 1 -t "Reply with exactly: OK",原始报错(打印两次):error: Ran into this error: Authentication error: Authentication failed for https://api.aicodewith.ai/v1/chat/completions. Status: 401 Unauthorized. Response: {"error":"请在请求中提供有效的 API Key。"}. 下面附 Please retry if you think this is a transient or recoverable error.

Key 填错:Authentication failed for https://api.aicodewith.ai/v1/chat/completions. Status: 401 Unauthorized. Response: {"error":"请在请求中提供有效的 API Key。"}

出现什么

error: Ran into this error: Authentication error: Authentication failed for https://api.aicodewith.ai/v1/chat/completions. Status: 401 Unauthorized. Response: {"error":"请在请求中提供有效的 API Key。"}.
原因
~/.bashrc 里的 OPENAI_API_KEY 填错了,报错和没设 Key 一样
怎么改
从控制台重新复制 Key,改好 ~/.bashrc 最后一行,然后新开终端

回到第 3 步:把你的 API Key 写进 ~/.bashrc →

Key 填错:tail -n 1 ~/.bashrc 显示 export OPENAI_API_KEY=sk-wrong;运行 goose run --no-session --quiet --max-turns 1 -t "Reply with exactly: OK",原始报错(打印两次):error: Ran into this error: Authentication error: Authentication failed for https://api.aicodewith.ai/v1/chat/completions. Status: 401 Unauthorized. Response: {"error":"请在请求中提供有效的 API Key。"}.(和没设 Key 时相同)

404「不支持的请求路径」

出现什么

404「不支持的请求路径」
原因
OPENAI_BASE_PATH 写错了(例如 nope/chat/completions),请求发到了 https://api.aicodewith.ai/nope/chat/completions
怎么改
改回 v1/chat/completions

回到第 2 步:把配置贴进 config.yaml →

常见报错:OPENAI_BASE_PATH 写错(这里写成 nope/chat/completions)时,请求打到 https://api.aicodewith.ai/nope/chat/completions,返回 404「不支持的请求路径」。改回 v1/chat/completions 即可。注意:这个中转对 HOST 多写一层 /v1 实测仍能通,不能靠它判断配错

常见问题

Goose 怎么用自己的 API Key?

在 ~/.config/goose/config.yaml 里写 GOOSE_PROVIDER: openai、OPENAI_HOST: https://api.aicodewith.ai 和 OPENAI_BASE_PATH: v1/chat/completions,再把 export OPENAI_API_KEY=你的 Key 写进 ~/.bashrc。

Goose 的 OPENAI_HOST 要不要带 /v1?

不用。OPENAI_HOST 填 https://api.aicodewith.ai,/v1 写在 OPENAI_BASE_PATH 里。

Goose 提示 401 怎么办?

终端里没有 OPENAI_API_KEY,或者 Key 填错了,改法见上面的常见报错。

Goose 找不到模型怎么办?

确认 GOOSE_MODEL 和 AI Code With 的 Model ID 一字不差。

参考来源

相关链接