主要パラメータ
temperature (温度)
ランダム性 を制御します。
- 0.0: ほぼ決定論的 (毎回同じ出力)。
- 1.0: 多様で創造的。
- 推奨:
- 分類・抽出: 0.0
- 文章生成: 0.7
- 創作: 1.0
top_p (Nucleus Sampling)
確率の累積が p になるまでのトークン候補からサンプリング。
通常は temperature と併用せず、どちらか一方で十分です (Anthropic 公式は temperature 派)。
max_tokens
出力 の上限トークン数 (入力ではない)。
不足するとレスポンスが途中で切れて stop_reason="max_tokens" になります。
⚠️ 入力との合算で課金 されるので、入力 + 出力 が極端に大きくならないよう注意。
stop_sequences
ここに含まれる文字列が出力に現れたら停止します。 区切り文字を強制したいときに有効。
# 確実に決定論的な分類
client.messages.create(
model="claude-sonnet-4-6",
max_tokens=20,
temperature=0.0,
messages=[{"role": "user", "content": "次の感情を pos/neg/neu で答えてください: 最高でした"}],
)
# 創作タスク
client.messages.create(
model="claude-opus-4-7",
max_tokens=2048,
temperature=1.0,
messages=[{"role": "user", "content": "猫が世界征服する短編小説を書いて"}],
)
metadata.user_id
ユーザー単位のレートリミットや乱用検知に使われる識別子。 個人特定情報 (PII = Personally Identifiable Information) ではなく、 ハッシュ化した ID を入れるのが推奨です。
tools = [{
"name": "extract_meeting",
"description": "テキストから会議情報を抽出",
"input_schema": {
"type": "object",
"properties": {
"title": {"type": "string"},
"date": {"type": "string", "format": "date"},
"attendees": {"type": "array", "items": {"type": "string"}},
},
"required": ["title", "date"],
},
}]
res = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=512,
tools=tools,
tool_choice={"type": "tool", "name": "extract_meeting"},
messages=[{"role": "user", "content": "5/8 14:00 から 田中・佐藤と週次定例"}],
)
data = next(b.input for b in res.content if b.type == "tool_use")
# data: {"title": "週次定例", "date": "2026-05-08", "attendees": ["田中", "佐藤"]}
実例: 決定論的 vs 創造的
# 確実に決定論的な分類
client.messages.create(
model="claude-sonnet-4-6",
max_tokens=20,
temperature=0.0,
messages=[{"role": "user", "content": "次の感情を pos/neg/neu で答えてください: 最高でした"}],
)
# 創作タスク
client.messages.create(
model="claude-opus-4-7",
max_tokens=2048,
temperature=1.0,
messages=[{"role": "user", "content": "猫が世界征服する短編小説を書いて"}],
)
試して比較
同じプロンプトで temperature を変えると応答がどう変わるか体感しましょう。
temperature=0.0 想定で「春の雨」をテーマに俳句を 1 句書いてください。temperature=1.0 で創造的に「春の雨」をテーマに俳句を 1 句書いてください。意外な角度を歓迎します。