Claude3.7 Sonnet Thinking対応API PERL版

claude  

Claude3.7 Thinking対応 API メモ。

Anthropic API Reference

 

エンドポイント

エンドポイントは、通常と同じ。

https://api.anthropic.com/v1/messages

 

ドキュメントにあるCURLのリクエストとレスポンス

curl https://api.anthropic.com/v1/messages \
     --header "x-api-key: $ANTHROPIC_API_KEY" \
     --header "anthropic-version: 2023-06-01" \
     --header "anthropic-beta: output-128k-2025-02-19" \
     --header "content-type: application/json" \
     --data \
'{
    "model": "claude-3-7-sonnet-20250219",
    "max_tokens": 128000,
    "thinking": {
        "type": "enabled",
        "budget_tokens": 32000
    },
    "messages": [
        {
            "role": "user",
            "content": "Generate a comprehensive analysis of..."
        }
    ],
    "stream": true
}'

 

ヘッダーに、anthropic-beta: output-128k-2025-02-19 を含める必要がある。

メッセージボディーは、通常と少し異なる。

レスポンス

 

{
  "content": [
    {
      "type": "thinking",
      "thinking": "Let me analyze this step by step...",
      "signature": "WaUjzkypQ2mUEVM36O2TxuC06KN8xyfbJwyem2dw3URve/op91XWHOEBLLqIOMfFG/UvLEczmEsUjavL...."
    },
    {
      "type": "redacted_thinking",
      "data": "EmwKAhgBEgy3va3pzix/LafPsn4aDFIT2Xlxh0L5L8rLVyIwxtE3rAFBa8cr3qpP..."
    },
    {
      "type": "text",
      "text": "Based on my analysis..."
    }
  ]
}

 

公式ではこうなっているが実際には以下のようになっている模様。
ドキュメントが色々と間違っている感じ。

 

{
  "content": [
    {
      "type": "thinking",
      "thinking": "Let me analyze this step by step...",
      "signature": "WaUjzkypQ2mUEVM36O2TxuC06KN8xyfbJwyem2dw3URve/op91XWHOEBLLqIOMfFG/UvLEczmEsUjavL...."
    },
    {
      "type": "text",
      "text": "Based on my analysis..."
    }
  ]
}

 

 model,input_token,output_token,total_tokenなどの他のデータは通常と同じに取得可能。

 

続きを読むにはパスワードを入力してください。

この記事はパスワードを入力することで閲覧可能になります。