附录 A:Claude API 快速参考

本手册涵盖了 Python SDK 和 TypeScript SDK 的高频用法。 完整的 API 文档请参考 docs.anthropic.comarrow-up-right

API Quick Reference

12.1.1 基础消息

所有交互的核心。

Python

TypeScript

关键参数说明

  • model: 模型 ID (如 claude-4-5-sonnet-20250929, claude-4-opus-20250929)。

  • max_tokens: 最多生成多少 Token。必须指定

  • temperature: 0.0 - 1.0。0 为确定性(编程/提取),1 为创造性(写作)。

  • system: 系统提示词。

12.1.2 流式输出

提高首字响应速度 (TTFT)。

Python

12.1.3 工具使用

Tool Definition

Handling Tool Calls

12.1.4 图像理解

支持 JPEG, PNG, GIF, WEBP。单张图片建议压缩在 5MB 以内。

12.1.5 提示缓存

需要在 Headers 中启用 anthropic-beta: prompt-caching-2024-07-31

12.1.6 错误代码对照表

Error Code
含义
解决方案

400 invalid_request_error

参数错误

检查 JSON Schema 或 Token 限制。

401 authentication_error

鉴权失败

检查 API Key 是否正确/过期。

403 permission_error

权限不足

检查账号是否被封禁或地区限制。

429 rate_limit_error

速率限制

实现指数退避重试 (Exponential Backoff)。

500 api_error

服务器错误

Anthropic 侧故障,稍后重试。

529 overloaded_error

负载过高

临时繁忙,稍后重试。

最后更新于