opencode 报错“This API key has expired“怎么办:三步换掉旧 Key,顺带看懂四种常见原因

本文详解opencode报错“This API key has expired”的根本原因与修复方案:Key在服务端已失效,无法通过重新登录恢复,必须在OpenCode Console吊销旧Key、创建新Key,并在终端通过opencode命令替换凭证。同时分析四种常见过期原因(账单欠费、手动轮换、安全事件批量失效、设置有效期),并介绍接入兼容OpenAI协议的自定义Provider以规避单点失效风险。

发布日期:2026-10-22 | 数据截至 2026-10-21,来源为 opencode 官方文档与 GitHub issue #58032

opencode 报错"This API key has expired. Create a new API key in OpenCode Console and replace it in your client or secret store. Signing in again will not renew this key.“的根本原因是:旧 Key 已在服务端标记失效,重新登录无法续期,必须在 OpenCode Console 手动生成新 Key 并替换到本地。完整修复分三步:进入 opencode.ai 控制台吊销旧 Key → 创建新 Key → 在终端执行 /connect 粘贴新 Key;凭证写入 ~/.local/share/opencode/auth.json,不需要改任何配置文件。对于同时出现 ECONNREFUSED 127.0.0.1:8080(本地代理未启动)和"model not available in your country”(地区限制)的情况,三条报错来源各不相同,需逐一排查。如果不想再被单一 Provider 的 Key 失效打断工作,可以在 opencode.json 里接入一个兼容 OpenAI 协议的统一 API 端点,把多家模型的调用收归一个 Key 管理。


报错原文和它真正在说什么

完整错误提示:

This API key has expired.
Create a new API key in OpenCode Console and replace it in your client or secret store.
Signing in again will not renew this key.

这三句话说了三件重要的事:

  1. 已过期:Key 在服务端被标记为失效,不是网络问题,不是密码错误
  2. 需要新建:必须去控制台重新生成,没有"续期"操作
  3. 重新登录没用:OAuth 重新授权走的是不同通道,不会刷新这个 Key

三步修复

第一步:吊销旧 Key

打开浏览器访问 opencode.ai,登录后进入 OpenCode Console,找到已过期的 Key,点击吊销(Revoke)。这一步不是必须的,但能防止旧 Key 被别人拿去继续尝试。

第二步:创建新 Key

在同一个控制台页面点击 Create new API key,复制生成的 Key 字符串,不要关掉页面,因为大多数平台只显示一次完整 Key。

第三步:在 opencode 里替换

打开终端,在任意目录启动 opencode,执行:

/connect

在弹出的提供商列表里选择 OpenCode(即 OpenCode Zen),粘贴新 Key,回车确认。

opencode 会把凭证写入:

~/.local/share/opencode/auth.json   # Linux / macOS
%APPDATA%\opencode\auth.json        # Windows

写入成功后可以用 /models 命令验证是否能正常列出可用模型。

如果同时出现其他报错

在 GitHub issue #58032(opencode v2.1.2,Windows 11)里,用户在同一时间遇到了三条报错:

报错真正原因对应操作
This API key has expiredKey 服务端失效按上文三步换 Key
This model is not available in your country所选模型有地区限制用 /models 切换其他模型
ECONNREFUSED 127.0.0.1:8080本地代理/VPN 客户端没开,或配置文件里写了错误的 baseURL检查本地代理状态,或清理 opencode.json 里的 baseURL 配置

这三条报错来源各不相同,混在一起很容易让人以为是同一个问题,逐一排查效率更高。

为什么 Key 会过期

opencode 的 Key 失效有几种常见来源:

1. 账单欠费自动吊销:OpenCode Zen 是后付费模式,账单逾期后平台会自动让 Key 失效。检查 opencode.ai 后台是否有未付账单。

2. 手动轮换:安全规范要求定期轮换凭证时,旧 Key 被主动吊销。

3. 安全事件后批量失效:opencode 近期发生过一次安全事件(参考 GitHub issue 中的讨论),平台对受影响账户的 Key 做了批量吊销。如果你没有手动操作过,但 Key 突然失效,可以去控制台检查是否有安全提醒。

4. Key 本身有有效期:部分平台在创建 Key 时可以设置过期时间,到期自动失效。

换成自定义 Provider,以后不怕单点失效

如果你同时使用多家模型,或者不想每次都通过 OpenCode Zen 中转,可以在 opencode.json 里直接接入兼容 OpenAI 协议的第三方端点。

配置文件位置:

~/.config/opencode/opencode.json   # Linux / macOS
%APPDATA%\opencode\opencode.json   # Windows

添加一个自定义提供商的最小配置:

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "myprovider": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "我的统一端点",
      "options": {
        "baseURL": "https://api.example.com/v1"
      },
      "models": {
        "model-id": {
          "name": "模型显示名"
        }
      }
    }
  }
}

baseURL 填入支持 /v1/chat/completions 的端点地址,api_key 通过 /connect 命令写入 auth.json,不写在配置文件里,避免 Key 明文泄露。

国内可以直接调用的统一 API 端点(同时覆盖 DeepSeek、Kimi、GLM 等多款主流模型),可以参考七牛云 Token Plan 的接入文档,一个 Key 管理多家模型调用,Key 过期只需要换一处。

换完还是报错怎么办

替换 Key 之后如果仍然报相同错误,按以下顺序排查:

  1. 确认 auth.json 已更新:用文本编辑器打开 auth.json,检查 Key 字段是否是新值
  2. 检查是否有多个 opencode 进程:旧进程可能仍在使用内存中缓存的旧 Key,重启 opencode
  3. 确认新 Key 在控制台状态为 Active:Console 里 Key 列表显示 Active 才算有效
  4. 检查网络:curl -s https://opencode.ai 返回正常再排除网络问题

总结

"This API key has expired"报错的唯一出路是:去 OpenCode Console 生成新 Key,回到终端用 /connect 替换,重新登录解决不了这个问题。如果同时出现 ECONNREFUSED 或地区限制报错,说明是三条独立问题叠在一起,分开处理。本文操作步骤来自 opencode 官方文档(opencode.ai/docs/providers)及 GitHub issue #58032,如果 Console 入口有变动以官网实际页面为准。

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值