AI Tools Guide

Claude API Beginner Guide

接 Anthropic Claude API 的正确做法:用 @anthropic-ai/sdk、Messages API(必须传 max_tokens、system 是顶层参数)、key 放服务端 Route Handler、流式返回、处理 401/429/overloaded。附可用代码。

What is the problem?

接 Anthropic Claude API 的正确做法:用 @anthropic-ai/sdk、Messages API(必须传 max_tokens、system 是顶层参数)、key 放服务端 Route Handler、流式返回、处理 401/429/overloaded。附可用代码。

Quick solution

Treat this as a AI tools tutorial issue. First confirm the environment, inputs, permissions, logs, and delivery boundary. Then use the linked deep guide for the full checklist before changing production code or promising a result.

Read the deep guide

Detailed steps

  1. **缺 max_tokens 报错**:Messages API 必须传 `max_tokens`,补上即可。
  2. **system 放错位置**:别在 `messages` 里放 `{ role: "system" }`,用顶层 `system` 字段。
  3. **401 authentication_error**:key 错或没读到 → 检查 `.env.local`、重启 dev。
  4. **429 / overloaded_error**:限流或服务繁忙 → 加指数退避重试;`overloaded` 通常稍后重试即可。
  5. **生产读不到**:Vercel 没配 `ANTHROPIC_API_KEY` → 补环境变量并重新部署。
  6. key 只在服务端,泄露立刻在 Anthropic 后台吊销换新。

Commands or code

npm install @anthropic-ai/sdk

Risk notes

Confirm the real project environment, account permissions, platform rules, and output quality before delivery. Do not ship AI-generated changes without human review, and do not claim indexing, income, deployment success, or ranking improvements without measured evidence.