Anthropic SDK

Anthropic 官方库,可用于 Claude 及其他所有模型。

Anthropic SDK 使用 Messages 格式。把 Base URL 设为 https://binference.io/api:SDK 会自己加上 /v1/messages。

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  baseURL: "https://binference.io/api",
  apiKey: process.env.BINF_API_KEY,
});

const message = await client.messages.create({
  model: "anthropic/claude-sonnet-5.5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Explain gas fees to a new trader." }],
});

console.log(message.content);

须知

  • 模型名称: anthropic/claude-sonnet-5.5 和 Claude 自己的名称(如 claude-sonnet-5-5)都可以用。
  • 任意模型: Messages 格式也适用于非 Claude 模型,比如 google/gemini-3.8-flash。
  • 费用: Messages 的回答带有 Token 数,但不带费用。每次调用的扣费会在一分钟内出现在你的调用记录页面上。
  • 过载: 繁忙的模型会返回 529 overloaded_error,SDK 会在我们给出的等待时间之后重试。

本页目录