教程
如何通过 hiapi API 使用 Claude Sonnet 4.6
你将构建的内容
一个可运行的集成示例:通过 hiapi 兼容 OpenAI 的 Chat Completions API 调用 Claude Sonnet 4.6——单次请求即可返回真实的补全结果,同时涵盖等演示跑通后你实际会用到的生产级模式(流式输出、工具调用、自适应思考)。
前置条件:一个 hiapi API 密钥。请从控制台获取——它就是一个sk-...字符串,同一个密钥可通用于你账户下所有已启用的模型,而不只是这一个。
下文全部内容均在撰写本文时对实时 API 实际运行验证过。
最小可运行示例
Claude Sonnet 4.6 是标准 Chat Completions 端点上的文本模型——而非 hiapi 用于图像/视频/音频模型的异步/v1/tasks流程。一次请求,一次响应,无需轮询。
curl https://api.hiapi.ai/v1/chat/completions \
-H "Authorization: Bearer sk-<your-api-key>" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"messages": [
{"role": "user", "content": "Explain how an HTTP cache works in three short paragraphs."}
]
}'
响应(已裁剪):
{
"id": "msg_bdrk_011Cf8Bo25qCVFFQF5ETk8Wf",
"model": "claude-sonnet-4-6",
"object": "chat.completion",
"choices": [
{
"index": 0,
"message": { "role": "assistant", "content": "..." },
"finish_reason": "stop"
}
],
"usage": { "prompt_tokens": 13, "completion_tokens": 4, "total_tokens": 17 }
}
同样的调用用 Python 仅依赖requests实现:
import requests
resp = requests.post(
"https://api.hiapi.ai/v1/chat/completions",
headers={"Authorization": "Bearer sk-<your-api-key>"},
json={
"model": "claude-sonnet-4-6",
"messages": [
{"role": "user", "content": "Explain how an HTTP cache works in three short paragraphs."}
],
},
timeout=60,
)
resp.raise_for_status()
data = resp.json()
print(data["choices"][0]["message"]["content"])
print(data["usage"]) # prompt_tokens / completion_tokens / total_tokens
由于结构兼容 OpenAI,这段代码无需修改也能配合官方openai Python/JS SDK 使用——只需将base_url指向https://api.hiapi.ai/v1并传入你的 hiapi 密钥即可。
生产级模式
流式输出
设置"stream": true并读取 Server-Sent Events。每个数据块携带一段delta.content片段;流在最后一个数据块以finish_reason: "stop"结束,随后是[DONE]哨兵:
curl https://api.hiapi.ai/v1/chat/completions \
-H "Authorization: Bearer sk-<your-api-key>" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"messages": [{"role": "user", "content": "Count 1 to 3"}],
"stream": true
}'
data: {"choices":[{"delta":{"content":"","role":"assistant"},"finish_reason":null,"index":0}]}
data: {"choices":[{"delta":{"content":"Here"},"finish_reason":null,"index":0}]}
data: {"choices":[{"delta":{"content":" you"},"finish_reason":null,"index":0}]}
data: {"choices":[{"delta":{},"finish_reason":"stop","index":0}]}
data: [DONE]
跨数据块累加choices[0].delta.content即可重建完整文本。
工具调用
按与 OpenAI 相同的方式声明函数,然后在下一轮重放助手的tool_calls消息以及一条tool结果消息:
curl https://api.hiapi.ai/v1/chat/completions \
-H "Authorization: Bearer sk-<your-api-key>" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"messages": [{"role": "user", "content": "What is the status of task demo-123?"}],
"tools": [{
"type": "function",
"function": {
"name": "get_task_status",
"description": "Look up a task by ID.",
"parameters": {
"type": "object",
"properties": {"task_id": {"type": "string"}},
"required": ["task_id"],
"additionalProperties": false
}
}
}],
"tool_choice": "auto"
}'
响应会返回finish_reason: "tool_calls",其中函数名和 JSON 编码的参数位于choices[0].message.tool_calls。在本地运行你的函数,然后在下一个请求中把结果作为带匹配tool_call_id的role: "tool"消息发回——模型会从此处继续。
自适应思考
Claude Sonnet 4.6 支持按需启用的推理环节。添加"thinking": {"type": "adaptive"},并通过output_config.effort选择努力等级(low、medium、high或max):
{
"model": "claude-sonnet-4-6",
"messages": [{"role": "user", "content": "Compare two approaches and state the trade-offs."}],
"thinking": {"type": "adaptive"},
"output_config": {"effort": "high"}
}
可读的推理轨迹会通过choices[0].message.reasoning_content返回;原始reasoning_details数组携带签名元数据,如果你要继续对话并进入工具调用,就必须原样保留它——不要解析、展示或臆造它,只需在下一个请求中直接透传即可。
错误处理
认证失败会返回 HTTP 401,并带有hiapi_error错误信封:
{
"error": {
"code": "permission_denied",
"message": "This API key is invalid. Check that it is correct or use another API key and try again.",
"type": "hiapi_error"
}
}
在访问response.json()["choices"]之前先检查response.status_code,如果需要升级处理,请记录error.request_id——支持团队可借此追踪具体的调用。
幂等性与重试
Chat Completions 调用不像/v1/tasks作业那样按请求 ID 幂等——重试的请求就是一次新的补全。对于生产流量,请用自己的带退避重试包裹调用以应对 5xx/超时,并设置max_tokens上限,避免重试风暴导致无上限的账单。
相关阅读
- Claude Sonnet 4.6 模型参考——完整参数表、响应 schema 以及更多直接来自 hiapi 文档的请求示例。
- 认证——hiapi API 密钥的工作方式以及如何轮换。
- hiapi 定价——Claude Sonnet 4.6 及所有其他已启用模型的实时每 token 费率。
常见问题
claude-sonnet-4-6是我应该发送的精确模型 ID 吗?
是的——在model字段中发送裸字符串claude-sonnet-4-6。hiapi 会在兼容 OpenAI 的接口背后将其路由到 Anthropic 的 Claude Sonnet 4.6,因此无需任何提供商特定前缀。
这是否使用与 hiapi 图像和视频模型相同的/v1/tasks流程?
不是。像 Claude Sonnet 4.6 这样的文本模型使用同步的POST /v1/chat/completions端点——一次请求,一次响应(或一个 SSE 流)。/v1/tasks仅用于媒体模型(图像、视频、音频),它们异步运行并返回可轮询的 URL 或回调。
我可以使用 OpenAI SDK 而不是原生curl/requests吗?
可以。由于端点兼容 OpenAI,只需将官方openai SDK 的base_url指向https://api.hiapi.ai/v1,并将你的 hiapi 密钥作为api_key——基本聊天、流式输出或工具调用无需其他代码改动。
Claude Sonnet 4.6 支持图像输入吗?
支持,通过内容块实现——将消息的content设置为一个数组,其中包含一个type: "image_url"的块,且image_url.url指向公共图像 URL,并与你的文本块并列。
如果我省略thinking会怎样?
根据当前版本,自适应思考是按需启用的——省略thinking字段(或将thinking.type设为disabled),模型会直接作答,响应中不含reasoning_content。
在运行大批量任务之前,我在哪里查看当前定价? hiapi 的定价页面列出了 Claude Sonnet 4.6 的实时每 token 费率——请在制定预算前查看,因为费率可能变化,而本文不会硬编码一个可能过时的数字。