Claude の Web 検索
このページでは、web_search ツールを使って Claude に Web 検索をさせる方法を説明します。モデル ID、エンドポイントの URL、プロバイダーについては Claude シリーズの概要を参照してください。
ポイントWeb 検索には Anthropic プロバイダーの指定が必要です。対象は Fable 5、Opus 4.8、Opus 4.7、Sonnet 5 のみで、Anthropic ネイティブ API でのみ利用できます。
tools に公式のサーバーツール web_search を追加すると、モデルが自ら検索を行い、回答の中で出典を引用します。
Python
claude_web_search.py
import os
import anthropic
client = anthropic.Anthropic(
base_url="https://api.ofox.ai/anthropic",
api_key=os.environ["OFOX_API_KEY"],
default_headers={"X-OfoxAI-Provider-Type": "anthropic"},
)
message = client.messages.create(
model="anthropic/claude-sonnet-5",
max_tokens=1500,
tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 3}],
messages=[{"role": "user", "content": "Python の最新の安定版はいくつですか?"}],
)
print("".join(b.text for b in message.content if b.type == "text"))
print("検索回数:", message.usage.server_tool_use.web_search_requests)- レスポンスの
contentには、server_tool_use(検索キーワード)、web_search_tool_result(検索結果)、text(回答)の各ブロックが順に現れます。 usage.server_tool_use.web_search_requestsは実際の検索回数です。検索は回数単位で課金されます。単価はモデルページ を参照してください。max_usesで 1 回のリクエストあたりの最大検索回数を制限できます。
その他のエラーはよくあるエラーを参照してください。
最終更新日