@apicity/ 人类
Anthropic / Claude 消息、批量、模型、文件和管理员API的提供者.
运行时间依赖性 :
zod@^4.6.6- 请求在每一个 POST 端点上附加计划.schema
安装
npm install @apicity/anthropic
# or
pnpm add @apicity/anthropic
快速启动
import { createAnthropic } from "@apicity/anthropic";
const anthropic = createAnthropic({ apiKey: process.env.ANTHROPIC_API_KEY! });
真实世界实例: 多回合 TypeScript 审查, 系统提示 + 几发正弦
Anthropic Messages API给你的单个最大的把手是
messages 数组本身: 每个前转弯都是下一转的光滑 .
严格地说 system 触发以锁定输出格式并
一个旋转的交换来显示格式,然后得到一个
确定型单行臭虫审查员退出 claude-sonnet-4-6
没有工具 没有JSON模式 没有微调 每一个征兆,模型身份,
下面的内容块来自
tests/recordings/anthropic_2966493235/messages-code-review_891889396/, (中文(简体) ),
剧组重播
tests/integration/anthropic-messages-code-review.test.ts
- 直接记录与 https://api.anthropic.com/v1/messages 与
输入 @apicity/anthropic 客户端.
import { createAnthropic } from "@apicity/anthropic";
import type {
AnthropicMessageResponse,
AnthropicTextBlock,
} from "@apicity/anthropic";
const anthropic = createAnthropic({
apiKey: process.env.ANTHROPIC_API_KEY!,
});
// 1. The `system` field is a hard contract for response shape — Claude
// treats it as higher-priority instruction than anything in
// `messages`. Spelling out the exact line format here ("BUG: <one
// sentence>", no preamble, no Markdown) is what stops the model
// from wrapping the answer in "Sure! Here's the bug:" or fenced
// code, which it does by default.
//
// 2. The `messages` array is a *transcript*, not a single prompt. The
// one-shot user→assistant pair below primes the format on a known
// bug (off-by-one in a loop bound) so the third turn answers in
// the same shape on a code snippet the model has not seen before.
// This is few-shot prompting; no fine-tuning required.
const result: AnthropicMessageResponse = await anthropic.v1.messages({
model: "claude-sonnet-4-6",
max_tokens: 256,
system:
"You are a senior TypeScript reviewer. Reply with exactly one line " +
"in the form: 'BUG: <one-sentence summary>'. No preamble, no code, " +
"no Markdown.",
messages: [
{
role: "user",
content:
"Review this:\n```ts\nfunction firstNonEmpty(xs: string[]): string {\n" +
" for (let i = 0; i <= xs.length; i++) {\n" +
" if (xs[i]) return xs[i];\n" +
" }\n return '';\n}\n```",
},
{
role: "assistant",
content:
"BUG: The loop condition `i <= xs.length` reads one past the " +
"last index, so `xs[xs.length]` is dereferenced as undefined.",
},
{
role: "user",
content:
"Now review this one the same way:\n```ts\n" +
"async function readAll(stream: ReadableStream<Uint8Array>): " +
"Promise<Uint8Array> {\n" +
" const reader = stream.getReader();\n" +
" const chunks: Uint8Array[] = [];\n" +
" while (true) {\n" +
" const { done, value } = await reader.read();\n" +
" if (done) break;\n chunks.push(value);\n }\n" +
" return Buffer.concat(chunks);\n}\n```",
},
],
});
// 3. `content` is always an array of typed blocks, never a single
// string. This is the same shape Claude returns when there are
// `tool_use`, `thinking`, or `image` blocks in the response —
// treating it uniformly here means tool-using and thinking
// workflows drop in without restructuring the read path.
const text = result.content
.filter((b): b is AnthropicTextBlock => b.type === "text")
.map((b) => b.text)
.join("")
.trim();
console.log(text);
// → "BUG: The reader is never released via `reader.releaseLock()`
// (especially on error), leaving the stream permanently locked."
console.log(
`model=${result.model} stop=${result.stop_reason} ` +
`in=${result.usage.input_tokens} out=${result.usage.output_tokens}`,
);
// → "model=claude-sonnet-4-6 stop=end_turn in=268 out=33"
页:1
- 这个
system即时作为软但强的制约 而不是第一次 克劳德把它重新应用为 对话会延长, 格式不会在10个回合后漂移 单一用户信息指令的方式。 把它当作一串 短规则; 通过为[{ type: "text", text, cache_control: { type: "ephemeral" } }]以重用时标记为可缓存 同样的漫长的系统会引发许多请求. - 几枪打出假枪
assistant转身最便宜 将输出格式锁定的可靠方式tools或者说 结构化输出 API. 克劳德不区分转弯 它实际上生成和转动你写的—— 两者是平等的 权威上下文。 继续转弯 短而准确; 如果 您的系统规则字符换字符( 如 a) 系统说不该有 模型 将树篱. result.content是一个(AnthropicTextBlock | AnthropicToolUseBlock | AnthropicThinkingBlock | …)[]歧视工会。 打开过滤器b.type === "text"读取前.text打开; 打开b.type === "tool_use"读取前.input打开; 打开b.type === "thinking"读取前.thinking克劳德可以把它们分开 反应——例如主要文本块,然后是思维块,然后是 工具 使用- 所以总是太过紧张 永远不要假设content[0]。 。 。 .usage.input_tokens包括 整个 记录记录 转来转去 10个回合的谈话 给系统带来好处 每次都是历史的转折 将一个长的系统分期摊还 ( > 1024令牌) 通过许多呼叫,标记它cache_control: ephemeral看着usage.cache_read_input_tokens接到电话 2–N (英语).- 丢出错误
AnthropicError与status,解析body,以及 上游地区errorType("校正错误"), "速率 限制 错误","超载 错误",. 与withRetry从@apicity/anthropic页:1429财务报告和审定财务报表529 overloaded_error, (中文(简体) ), 克劳德在持续负载下返回 与withFallback翻转到claude-haiku-4-5对于非关键路径. - 在克劳德兼容的网关上发出同样的呼声
baseURL(和a)fetch如果后端使用不同的包装 认证信头比x-api-key改为:createAnthropic工厂的 要求的形状是上层的,所以任何说 Anthropic Messages 有线格式——Bedrock 代理,第三方 网关,本地取笑——工作时没有触摸到呼叫站点.
API 参考
24个端点跨出5个组. 每个方法都映射出上游的 URL 路径 .
文件
DELETE anthropic.v1.files.del
DELETE https://api.anthropic.com/v1/files/{fileId}
成本等级 : expensive
const res = await anthropic.delete.v1.files.del({ /* ... */ });
GET anthropic.v1.files.content
GET https://api.anthropic.com/v1/files/{fileId}/content
成本等级 : expensive
const res = await anthropic.get.v1.files.content({ /* ... */ });
GET anthropic.v1.files.list
GET https://api.anthropic.com/v1/files
成本等级 : expensive
const res = await anthropic.get.v1.files.list({ /* ... */ });
GET anthropic.v1.files.retrieve
GET https://api.anthropic.com/v1/files/{fileId}
成本等级 : expensive
const res = await anthropic.get.v1.files.retrieve({ /* ... */ });
POST anthropic.v1.files
POST https://api.anthropic.com/v1/files
成本等级 : expensive
const res = await anthropic.post.v1.files({ /* ... */ });
信件
DELETE anthropic.v1.messages.batches.del
DELETE https://api.anthropic.com/v1/messages/batches/{batchId}
成本等级 : expensive
const res = await anthropic.delete.v1.messages.batches.del({ /* ... */ });
GET anthropic.v1.messages.batches.list
GET https://api.anthropic.com/v1/messages/batches
成本等级 : expensive
const res = await anthropic.get.v1.messages.batches.list({ /* ... */ });
GET anthropic.v1.messages.batches.results
GET https://api.anthropic.com/v1/messages/batches/{batchId}/results
成本等级 : expensive
const res = await anthropic.get.v1.messages.batches.results({ /* ... */ });
GET anthropic.v1.messages.batches.retrieve
GET https://api.anthropic.com/v1/messages/batches/{batchId}
成本等级 : expensive
const res = await anthropic.get.v1.messages.batches.retrieve({ /* ... */ });
POST anthropic.post.stream.v1.messages
POST https://api.anthropic.com/v1/messages
成本等级 : expensive
const res = await anthropic.post.stream.v1.messages({ /* ... */ });
POST anthropic.v1.messages.batches.cancel
POST https://api.anthropic.com/v1/messages/batches/{batchId}/cancel
成本等级 : expensive
const res = await anthropic.post.v1.messages.batches.cancel({ /* ... */ });
POST anthropic.v1.messages
POST https://api.anthropic.com/v1/messages
成本等级 : expensive
const res = await anthropic.v1.messages({ /* ... */ });
POST anthropic.v1.messages.batches
POST https://api.anthropic.com/v1/messages/batches
成本等级 : expensive
const res = await anthropic.v1.messages.batches({ /* ... */ });
POST anthropic.v1.messages.countTokens
POST https://api.anthropic.com/v1/messages/count_tokens
成本等级 : expensive
const res = await anthropic.v1.messages.countTokens({ /* ... */ });
模型
GET anthropic.v1.models.list
GET https://api.anthropic.com/v1/models
成本等级 : expensive
const res = await anthropic.get.v1.models.list({ /* ... */ });
GET anthropic.v1.models.retrieve
GET https://api.anthropic.com/v1/models/{modelId}
成本等级 : expensive
const res = await anthropic.get.v1.models.retrieve({ /* ... */ });
欧斯
GET anthropic.api.oauth.usage
GET https://api.anthropic.com/api/oauth/usage
成本等级 : expensive
const res = await anthropic.api.oauth.usage();
技能
DELETE anthropic.v1.skills.del
DELETE https://api.anthropic.com/v1/skills/{skillId}
成本等级 : expensive
const res = await anthropic.delete.v1.skills.del({ /* ... */ });
DELETE anthropic.v1.skills.versions.del
DELETE https://api.anthropic.com/v1/skills/{skillId}/versions/{version}
成本等级 : expensive
const res = await anthropic.delete.v1.skills.versions.del({ /* ... */ });
GET anthropic.v1.skills.list
GET https://api.anthropic.com/v1/skills
成本等级 : expensive
const res = await anthropic.get.v1.skills.list({ /* ... */ });
GET anthropic.v1.skills.retrieve
GET https://api.anthropic.com/v1/skills/{skillId}
成本等级 : expensive
const res = await anthropic.get.v1.skills.retrieve({ /* ... */ });
GET anthropic.v1.skills.versions.list
GET https://api.anthropic.com/v1/skills/{skillId}/versions
成本等级 : expensive
const res = await anthropic.get.v1.skills.versions.list({ /* ... */ });
POST anthropic.v1.skills.create
POST https://api.anthropic.com/v1/skills
成本等级 : expensive
const res = await anthropic.post.v1.skills.create({ /* ... */ });
POST anthropic.v1.skills.versions.create
POST https://api.anthropic.com/v1/skills/{skillId}/versions
成本等级 : expensive
const res = await anthropic.post.v1.skills.versions.create({ /* ... */ });
中间软件
import { createAnthropic, withRetry } from "@apicity/anthropic";
const anthropic = createAnthropic({ apiKey: process.env.ANTHROPIC_API_KEY! });
const listModels = withRetry(anthropic.get.v1.models.list, { retries: 3 });
部分 辣点 单相机.
许可证
麻省理工学院——见 自由主义。 。 。 .