Skip to content

Anthropic Messages 协议 ​

调用 Claude 全系模型(Opus / Sonnet / Haiku / Fable),与 Anthropic 官方 Messages API 完全兼容——把 SDK 的 base_url 指向 MoaCode 即可。

  • 端点:POST https://moacode.org/v1/messages
  • 认证:x-api-key: 你的APIKey(或 Authorization: Bearer)

基础调用 ​

python
import anthropic

client = anthropic.Anthropic(
    base_url="https://moacode.org",
    api_key="你的APIKey",
)

message = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "用一句话解释什么是递归"}],
)
print(message.content[0].text)
typescript
import Anthropic from '@anthropic-ai/sdk'

const client = new Anthropic({
  baseURL: 'https://moacode.org',
  apiKey: '你的APIKey',
})

const message = await client.messages.create({
  model: 'claude-sonnet-5',
  max_tokens: 1024,
  messages: [{ role: 'user', content: '用一句话解释什么是递归' }],
})
console.log(message.content)
bash
curl https://moacode.org/v1/messages \
  -H "x-api-key: 你的APIKey" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-5",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "用一句话解释什么是递归"}]
  }'

流式输出 ​

python
with client.messages.stream(
    model="claude-sonnet-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "写一首关于大海的短诗"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
typescript
const stream = client.messages.stream({
  model: 'claude-sonnet-5',
  max_tokens: 1024,
  messages: [{ role: 'user', content: '写一首关于大海的短诗' }],
})
for await (const event of stream) {
  if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
    process.stdout.write(event.delta.text)
  }
}
bash
curl https://moacode.org/v1/messages \
  -H "x-api-key: 你的APIKey" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-5",
    "max_tokens": 1024,
    "stream": true,
    "messages": [{"role": "user", "content": "写一首关于大海的短诗"}]
  }'

提示词缓存 ​

Claude 渠道支持 prompt caching,重复的系统提示与上下文按缓存价计费(通常便宜一个数量级)。用法与官方一致:

json
{
  "system": [
    {
      "type": "text",
      "text": "这里是很长的系统提示……",
      "cache_control": { "type": "ephemeral" }
    }
  ]
}

编码类会话的缓存命中率通常可达 90%,实际费用可在用量查询里看到缓存读写分项。

常用模型 ​

模型 ID说明
claude-opus-5最强推理
claude-sonnet-5均衡主力
claude-haiku-4-5快速轻量
claude-fable-5长程编码智能体

完整清单与价格见模型目录。

计费 ​

实际扣费 = 模型基础价 × 渠道倍率(如 ClaudeHybrid 1.7×)。渠道倍率见 API Key 管理。

MoaCode — One key. Every coding agent.