Skip to content

LLM

Agent 通过 LLMClient contract 与语言模型交互。

ts
import { anthropic, type LLMClient } from "duclaw-cli/sdk";

LLMClient

当你要接入自定义 model provider 时,实现这个 contract。

ts
type LLMClient = {
  chat(
    messages: LLMMessage[],
    system: string,
    tools?: Tool[],
    options?: {
      requestId?: string;
      signal?: AbortSignal;
    },
  ): Promise<LLMResponse>;
};

Anthropic Adapter

当模型端点兼容 Anthropic Messages API 时,可以使用 anthropic()

ts
const model = anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY!,
  baseURL: process.env.ANTHROPIC_BASE_URL,
  model: process.env.ANTHROPIC_MODEL,
});

baseURL 是可选项。当请求需要经过 Anthropic 兼容网关、代理服务或私有模型端点时传入它:

ts
const model = anthropic({
  apiKey: process.env.MODEL_API_KEY!,
  baseURL: "https://gateway.example.com/anthropic",
  model: "claude-sonnet-4-6",
});

如果端点需要 bearer 认证,而不是 Anthropic 标准的 x-api-key header,可以设置 authStyle

ts
const model = anthropic({
  apiKey: process.env.MODEL_API_KEY!,
  baseURL: "https://gateway.example.com/anthropic",
  model: "claude-sonnet-4-6",
  authStyle: "bearer",
});

支持的配置项:

配置项必填用途
apiKeyAPI key 或网关 token
model发送给端点的模型名称
baseURL自定义 Anthropic 兼容端点
maxTokens最大回复 token 数
authStyle默认使用 "x-api-key",网关需要 Authorization: Bearer 时用 "bearer"

如果应用自己负责 model routing、retries、observability 或 provider fallback,可以使用自定义 LLMClient

随 duclaw-cli package 一起发布。