@coder/ai-sdk-provider

中文文档
0.4.54发布于 2026/11/01Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 5033

Vercel AI SDK provider for Coder AI Gateway. Point the AI SDK at your Coder deployment with a Coder API token and use any model your deployment proxies — across AI Gateway's OpenAI- and Anthropic-compatible surfaces.

原始使用文档
文档目录9 节
  1. Install
  2. Named providers and the two wire protocols
  3. Custom-named providers
  4. Embeddings are not supported yet
  5. Authentication
  6. Configuration
  7. Enterprise governance & security
  8. Examples
  9. License

@coder/ai-sdk-provider

CI npm License

A Vercel AI SDK provider that routes requests through your Coder deployment's AI Gateway (formerly "AI Bridge"). Use any model your deployment proxies, the same way you'd use OpenRouter or any other provider.

import { generateText } from "ai";
import { createCoder } from "@coder/ai-sdk-provider";

const coder = createCoder({
  baseURL: "https://coder.example.com",
  apiKey: process.env.CODER_API_TOKEN!,
});

const { text } = await generateText({
  model: coder("claude-sonnet-4-6"),
  prompt: "What is Coder?",
});

Why: in centralized mode (the default), developers authenticate with their Coder token and never handle raw provider keys. AI Gateway authenticates each request against a Coder identity, injects the centrally managed keys for the upstream providers (Anthropic, OpenAI, Bedrock, Copilot, …), and audits usage per user. In bring-your-own-key mode, developers supply their own upstream key instead (see Authentication). The deployment decides which models and providers are available.

[!TIP] This package is for plain model calls: generateText, streamText, and generateObject (schema-constrained structured output). For Coder's server-side agent (multi-step tool loop, built-in tools, MCP servers, workspace file/shell tools), use @coder/ai-sdk-agent. They compose: provider for pure text/JSON steps, Agent for tool-driven ones.

Install

pnpm add @coder/ai-sdk-provider ai zod

Requirements:

  • Node ≥ 22 and ai v7.
  • A Coder deployment with AI Gateway enabled: stable since Coder v2.29, GA in v2.30, on by default in v2.34. Requires the AI Governance Add-On.

Named providers and the two wire protocols

Coder admins define AI Gateway providers: named routes on the deployment (/api/v2/aibridge/<name>/v1/…). Each speaks one of two wire protocols, set by its admin-configured type:

Wire protocolProvider types behind itDefault name
OpenAI-compatibleopenai, azure, google, copilot, openai-compat, openrouter, vercelopenai
Anthropic-compatibleanthropic (native Claude), bedrock (Bedrock-hosted Claude)anthropic

Routing is by URL path, not by model id: the provider name in the URL decides which upstream handles the request.

createCoder fronts the default openai / anthropic pair. The bare call coder(modelId) picks one by heuristic: ids starting with claude or anthropic go to the Anthropic-protocol provider, everything else to the OpenAI-protocol one. Explicit accessors override the heuristic, e.g. to reach Claude through a Copilot-typed provider on the OpenAI protocol:

coder("gpt-4o"); // → `openai` provider
coder("claude-sonnet-4-6"); // → `anthropic` provider (heuristic)
coder.openai("claude-sonnet-4"); // → `openai` provider (e.g. Copilot)
coder.anthropic("claude-opus-4-5"); // → `anthropic` provider (explicit)

Model ids pass through unchanged to the upstream provider (no vendor/model namespacing). Use whatever ids your deployment's providers accept.

Custom-named providers

Provider names are admin-chosen and match ^[a-z0-9]+(-[a-z0-9]+)*$. A deployment may expose, say, an Azure-backed azure-openai next to a Bedrock-backed anthropic-bedrock. There are two ways to reach them.

Sub-provider accessors. openaiProvider(name) / anthropicProvider(name) return a full sub-provider bound to that gateway provider, so one createCoder instance can target any number of providers. Pick the accessor that matches the provider's wire protocol:

const azure = coder.openaiProvider("azure-openai"); // OpenAI-compatible type
const bedrock = coder.anthropicProvider("anthropic-bedrock"); // Anthropic-compatible type

await generateText({ model: azure("gpt-4o"), prompt: "Hi" });
await generateText({ model: bedrock("claude-sonnet-4-6"), prompt: "Hi" });
NameFails with
Outside the gateway's grammarAI SDK InvalidArgumentError at accessor time
Well-formed but not configured on deploymentThe gateway's 404 at request time

A name outside the grammar can never be registered, so it fails early.

Re-pointing the defaults. If your deployment just names its one OpenAI/Anthropic pair differently, override the names once and keep using the bare call and the openai / anthropic accessors:

const renamed = createCoder({
  baseURL: "https://coder.example.com",
  apiKey: process.env.CODER_API_TOKEN!,
  providers: { openai: "azure-openai", anthropic: "anthropic-bedrock" },
});

Ask your Coder admins for provider names. Discovery is admin-only server-side: GET /api/v2/ai/providers returns 403 for regular users, and the models endpoint does not attribute models to providers.

Embeddings are not supported yet

coder.textEmbeddingModel(id) and the embedding accessors on coder.openai throw the AI SDK's NoSuchModelError immediately.

Why embeddings throw instead of sending a request

AI Gateway does not intercept /v1/embeddings, so a request would be rejected with a 404. Throwing up front avoids emitting it. The accessors stay so they can light up without a breaking change once the gateway adds an embeddings route. See coder/ai-sdk#69.

Authentication

ModeSetUpstream provider key
Centralized (default)apiKey: your Coder API tokenHeld by AI Gateway, which brokers the call
Bring-your-own-keycoderToken: Coder token; apiKey: your upstream keyYours, forwarded to the upstream

Centralized mode is all most apps need:

createCoder({ baseURL: "https://coder.example.com", apiKey: coderToken });

BYOK sends coderToken in the X-Coder-AI-Governance-Token header to authenticate you to the gateway, and forwards apiKey to the upstream:

createCoder({
  baseURL: "https://coder.example.com",
  coderToken, // authenticates you to AI Gateway
  apiKey: upstreamKey, // your own OpenAI/Anthropic key
});

Configuration

OptionTypeDefaultDescription
baseURLstring— (required)Your Coder deployment URL, e.g. https://coder.example.com. The AI Gateway path is appended for you.
apiKeystring—Coder API token (centralized) or upstream key (BYOK).
coderTokenstring—Enables BYOK mode; sent in X-Coder-AI-Governance-Token.
headersRecord<string,string>—Extra headers merged into every request.
aiGatewayPathstring/api/v2/aibridgeOverride if your deployment uses a different mount path. Use the canonical /api/v2/ai-gateway with Coder v2.35.0+.
providers{ openai?, anthropic? }openai / anthropicRe-point the default pair at differently-named providers.
fetchtypeof fetchglobal fetchCustom fetch (testing / middleware).

Enterprise governance & security

The full reference for security reviewers is in docs/security.md: data flow, credential isolation, audit capture, required permissions, and a security FAQ.

Key points:

  • Client vs. gateway. What this package puts on the wire is verifiable in src/provider.ts. Key custody, audit capture, and retention are enforced server-side by your Coder deployment.
  • One destination. The package only initiates requests to your baseURL. It never contacts upstream vendors directly and adds no telemetry.
  • Use HTTPS outside trusted local environments. The client does not enforce https://; an http:// URL sends tokens, keys, and prompts in plaintext.
  • Redirects are followed by default. A cross-origin redirect would resend the prompt body and non-Authorization headers (in BYOK mode, including x-api-key and the governance token) to the redirect target. Pass fetch: (url, init) => fetch(url, { ...init, redirect: "error" }) to forbid it.
  • Coder tokens are not AI-only. A token grants the user's full Coder API permissions. Treat a leak as a Coder account compromise (revoke the token) and prefer short-lived, dedicated tokens for AI workloads.
  • Audit. Every request is attributed to the authenticating Coder user. The Gateway stores the last user prompt, tool calls (name and arguments), reasoning content when present, token usage, and metadata. Response text is discarded. Retention defaults to 60 days and is configurable.

Examples

Runnable scripts live in examples/; see its README for setup.

pnpm example:generate    # non-streaming generateText
pnpm example:stream      # streaming streamText
pnpm example:anthropic   # the Anthropic surface (native Claude)

License

Apache-2.0 © Coder Technologies, Inc.

版本与发布记录

@coder/ai-sdk-provider 共发布 63 个版本,首次发布于 2026/06/18;过去 12 个月发布了 63 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.4.54latest2026/11/019 天前
0.4.532026/11/019 天前
0.4.522026/10/3110 天前
0.4.512026/10/3011 天前
0.4.502026/10/3011 天前
0.4.492026/10/2912 天前

关于 @coder/ai-sdk-provider 的常见问题

@coder/ai-sdk-provider 是什么?

Vercel AI SDK provider for Coder AI Gateway. Point the AI SDK at your Coder deployment with a Coder API token and use any model your deployment proxies — across AI Gateway's OpenAI- and Anthropic-compatible surfaces.

如何安装 @coder/ai-sdk-provider?

在项目目录运行 npm install @coder/ai-sdk-provider,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:ai(^7.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@coder/ai-sdk-provider 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@coder/ai-sdk-provider 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@coder/ai-sdk-provider 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@coder/ai-sdk-provider 可以商用吗?

@coder/ai-sdk-provider 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@coder/ai-sdk-provider 的下载量有多少?

根据 npm 官方下载统计,@coder/ai-sdk-provider 在 2026/10/10 – 2026/11/08 这 30 天内被下载 5,033 次,日均约 168 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@coder/ai-sdk-provider 还在维护吗?

最新版本 0.4.54 发布于 2026/11/01(9 天前);至今共发布 63 个版本,其中过去 12 个月发布了 63 个;首次发布于 2026/06/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。