如何在 OpenAI、Gemini 和 Claude API 啟用網路搜尋

三家 API 都只要在 tools 陣列加入一個項目,就能啟用網路搜尋:OpenAI 用 web_search,Gemini 用 google_search,Claude 用 web_search_20250305 或更新的版本。本文提供各家的完整程式碼、每 1,000 次搜尋的費用、網域與模型方面的限制,以及什麼情況下改用獨立的搜尋 API 會更合適。
一句話總結
- •OpenAI:在 Responses API 的 tools 加入 {"type": "web_search"}。每 1,000 次呼叫 $10,搜尋內容的 token 另依模型費率計費。
- •Gemini:加入 {"type": "google_search"}(Interactions API)或 {"google_search": {}}(generateContent)。Gemini 3 以後的模型每月前 5,000 次搜尋查詢免費,之後每 1,000 次 $14。
- •Claude:在 tools 加入 {"type": "web_search_20250305", "name": "web_search"}(或支援動態篩選的較新版本 20260209 / 20260318)。每 1,000 次搜尋 $10,搜尋結果的 token 另以輸入計費。
- •三家都由模型自行決定要不要搜尋、搜尋幾次,而且是按搜尋次數計費,不是按提問次數。我們 2026-11-24 測試 Gemini 時,一個問題就觸發了兩次計費查詢。
- •如果需要在多個模型間共用同一套搜尋、希望每個問題只呼叫一次且費用可預測,或是要把原始結果送進自己的處理流程,就該使用獨立的搜尋 API。apipick 每 1,000 次呼叫 $15,只對 HTTP 200 回應計費。
如何在 OpenAI、Gemini 和 Claude API 啟用網路搜尋?
在請求的 tools 陣列加入一個內建工具即可:OpenAI Responses API 用 {"type": "web_search"},Gemini Interactions API 用 {"type": "google_search"}(generateContent 則是 {"google_search": {}}),Claude Messages API 用 {"type": "web_search_20250305", "name": "web_search"} 或更新的版本。 接著由供應商執行搜尋、把結果交給模型,並回傳附有引用來源的答案。除了模型的 token 費用,每次搜尋還要另外付費。
以下內容皆於 2026-11-24 對照各家官方文件查核,Gemini 的範例也在當天實際執行過。
| OpenAI | Gemini | Claude | apipick 搜尋 API | |
|---|---|---|---|---|
| 要加入的內容 | {"type": "web_search"} | {"type": "google_search"} | {"type": "web_search_20250305", "name": "web_search"} | POST /api/search/web |
| 端點 | Responses API | Interactions API 或 generateContent | Messages API | 任何模型,自己的程式碼 |
| 每 1,000 次價格 | 每次呼叫 $10 | 每月免費 5,000 次後,每次查詢 $14 | 每次搜尋 $10 | 每次呼叫 $15 |
| 搜尋 token | 依模型費率計費 | 不以輸入計費 | 以輸入 token 計費 | 自行決定要送什麼 |
| 網域篩選 | 允許或封鎖,各 100 個 | 僅能封鎖,且僅限 Vertex AI | 允許或封鎖,不能並用 | 由你的程式碼篩選結果 |
| 引用來源 | url_citation 標註 | Grounding 中繼資料 | 一律開啟 | 每筆結果含標題、URL、摘要 |
| 由誰決定是否搜尋 | 模型 | 模型 | 模型 | 你自己 |
如何在 OpenAI API 啟用網路搜尋?
使用 Responses API,並在 tools 加入 {"type": "web_search"}。 是否搜尋由模型決定,回傳的答案會附上行內的 url_citation 標註,內含 URL、標題以及所佐證的字元範圍。
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-6.2-astra",
tools=[{"type": "web_search"}],
input="What was a positive news story from today?",
)
print(response.output_text)curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-6.2-astra",
"tools": [{"type": "web_search"}],
"input": "What was a positive news story from today?"
}'以上是 OpenAI 網路搜尋指南中的官方範例。實務上常用的選項如下:
| 選項 | 用途 |
|---|---|
filters.allowed_domains / blocked_domains | 限定或排除網域,各最多 100 個,不加 https://;包含子網域 |
user_location | 大略的 country(ISO 代碼)、city、region、timezone |
search_context_size | low、medium(預設)或 high |
external_web_access | 設為 false 會切換成離線、只使用快取的模式 |
include: ["web_search_call.action.sources"] | 回傳所有參考過的 URL,而不只是被引用的那些 |
tool_choice: "required" | 強制搜尋;設為 "auto" 時則由模型選擇 |
需要注意的限制。 即使模型支援更長的上下文,搜尋的上下文視窗上限仍是 128k token。使用 gpt-5 時,推理強度(reasoning effort)必須高於 minimal 才能進行網路搜尋。舊版的 web_search_preview 工具仍可供既有整合使用,但不支援 filters。Chat Completions 沒有可加入的工具,只能改用 gpt-5-search-api 這類搜尋模型。
價格。 每 1,000 次網路搜尋呼叫 $10,搜尋內容的 token 另依該模型費率計費。在非推理模型上使用舊版 preview 工具,則是每 1,000 次呼叫 $25,搜尋內容 token 免費(OpenAI 價格頁,2026-11-24)。
如何在 Gemini API 開啟 Google 搜尋 Grounding?
加入 google_search 工具:Interactions API 為 {"type": "google_search"},generateContent 請求則為 {"google_search": {}}。 Google 目前的文件以 Interactions API 為主;generateContent 仍接受這個工具,並會回傳 groundingMetadata。
from google import genai
client = genai.Client()
interaction = client.interactions.create(
model="gemini-4-hydrogen",
input="Who won the euro 2024?",
tools=[{"type": "google_search"}],
)
print(interaction.output_text)curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H "Api-Revision: 2026-05-20" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-4-hydrogen",
"input": "Who won the euro 2024?",
"tools": [{"type": "google_search"}]
}'如果你的程式碼還在呼叫 generateContent,工具放的位置也一樣:
{"contents": [{"role": "user", "parts": [{"text": "Which team won the 2026 World Series?"}]}],
"tools": [{"google_search": {}}]}我們在 2026-11-24 把這個請求原封不動送給 gemini-3.8-flash。模型回答「The Milwaukee Brewers won the 2026 World Series」,回應的 webSearchQueries 中列出了 兩筆 搜尋查詢,也就是一個問題產生了兩次計費查詢。groundingChunks 裡的來源都是 vertexaisearch.cloud.google.com/grounding-api-redirect/… 形式的轉址連結,標題只有網域名稱,也沒有頁面內文。
其他需要知道的事:
- 回應欄位。 generateContent 會回傳
groundingMetadata,其中包含webSearchQueries、groundingChunks、groundingSupports(每個來源佐證哪一段文字)和searchEntryPoint(用來顯示搜尋建議的已轉譯 HTML)。Interactions API 則改為回傳google_search_call、google_search_result和url_citation標註。在你的 UI 顯示搜尋建議時,須遵守 Google 服務條款的相關規範。 - 篩選。 Gemini Developer API 支援在工具上設定
timeRangeFilter。排除網域(exclude_domains)只有 Vertex AI 的文件有說明,我們 2026-11-24 在 Vertex AI 測試時確實有效。沒有允許清單。 - 模型。 Gemini 4、Gemini 3.x 及 Gemini 2.5 / 2.0 模型都支援這個工具。更早的模型使用的是
google_search_retrieval。 - 價格。 Gemini 3 以後的模型依搜尋查詢計費:這些模型共用每月 5,000 次免費額度,之後每 1,000 次 $14;Developer API 付費方案預設每天最多 1,500 次 grounding 請求。Gemini 2.5 依 grounding 提示計費:每天前 1,500 次免費,之後每 1,000 次 $35。
Gemini 多常搜尋、啟用與不啟用 grounding 時準確度如何,我們在 Google 搜尋 Grounding 與搜尋 API 的 40 題對照測試中有詳細分析。本文只說明設定方式。
如何在 Claude API 啟用網路搜尋工具?
在 Messages API 的 tools 加入 {"type": "web_search_20250305", "name": "web_search"};若使用 Claude 4.6 以後的模型,可改用 web_search_20260209 / web_search_20260318 來取得動態篩選功能。 搜尋在 Anthropic 端執行,單一回應中就會包含搜尋查詢、搜尋結果與引用文字。
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=4096,
messages=[{"role": "user", "content": "What's the weather in NYC?"}],
tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 5}],
)
for block in response.content:
if block.type == "text":
print(block.text)curl https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2026-10-21" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-5-5",
"max_tokens": 4096,
"messages": [{"role": "user", "content": "What is the weather in NYC?"}],
"tools": [{"type": "web_search_20250305", "name": "web_search", "max_uses": 5}]
}'兩個範例都依照 Anthropic 的網路搜尋工具文件撰寫。工具版本與選項如下:
| 設定 | 說明 |
|---|---|
web_search_20250305 | 基本搜尋,所有支援的模型與平台皆可使用 |
web_search_20260209 | 新增動態篩選:結果進入上下文之前,Claude 會先用程式碼篩選 |
web_search_20260318 | 新增 response_inclusion,可從回應中移除被篩掉的結果區塊 |
max_uses | 限制每輪的搜尋次數;失敗的搜尋也會計入 |
allowed_domains / blocked_domains | 只能擇一,不能並用(否則回傳 400 錯誤) |
user_location | type: "approximate",再加上 city、region、country、timezone |
需要注意的限制。 引用一律開啟,每筆引用都包含 url、title 以及最多 150 個字元的 cited_text。完整的結果文字會以 encrypted_content 回傳,之後的對話輪次必須原封不動地送回,但你無法讀取內容。max_uses_exceeded 之類的錯誤會以 HTTP 200 回傳,錯誤物件放在 web_search_tool_result 區塊裡。組織管理員可以在 Claude Console 關閉網路搜尋,此時請求會以 400 失敗。在 Google Cloud 上由 Google 代管的 Claude,以及託管於 Azure 的 Microsoft Foundry 部署中,只能使用基本版本。
價格。 每 1,000 次搜尋 $10,再加上標準 token 費用;搜尋結果在當輪與之後的輪次中都會計入輸入 token。發生錯誤的搜尋不計費。
每個問題的網路搜尋實際要花多少錢?
每個問題的成本 = 單次搜尋費用 × 模型執行的搜尋次數 + 搜尋結果增加的 token,而使用內建工具時,這兩者都由模型決定。 所以上面那些「每 1,000 次」的價格無法直接比較。
- 搜尋次數。 每個工具都允許模型針對一個問題搜尋多次。上面那個 Gemini 請求就搜尋了兩次。在我們 11 月 14 日的 grounding 測試中,Gemini 平均每題查詢 1.9 到 2.85 次,其中一題更高達 16 次。Claude 可以用
max_uses限制每輪次數;我們查閱的 OpenAI 與 Gemini 頁面都沒有列出類似的上限。 - 結果 token。 OpenAI 依模型費率對搜尋內容 token 計費,Claude 則把搜尋結果算進輸入 token,所以模型越貴,每次搜尋也就越貴。Gemini 不把 grounding 文字以輸入 token 計費。
- 免費額度。 Gemini 3 以後的模型每月有 5,000 次免費查詢。Amazon Bedrock AgentCore 的 Web Search 是給 AWS 上 AI 代理使用的獨立方案,每 1,000 次查詢 $7,自 2026-11-17 起每月提供 5,000 次免費查詢(AWS 公告)。
獨立的搜尋 API 剛好相反:每個問題要呼叫幾次(通常是一次)、要放多少筆結果進提示,都由你決定。在我們先前的測試中,5 筆 apipick 搜尋結果大約讓每個問題增加 1,400 個輸入 token。
什麼情況下內建搜尋工具不夠用?
內建搜尋是讓單一供應商的模型取得即時資訊最快的方法,但搜尋會被綁在該供應商身上,主導權也交給了模型。 以下五種情況,團隊通常會把搜尋移到自己的程式碼中:
- 你同時使用多個模型。 Google 搜尋 grounding 只能搭配 Gemini,Claude 的工具也只能搭配 Claude。如果用路由器把部分問題送給 GPT、其他送給 Claude,就得面對三套不同的搜尋引擎、價格和引用格式。
- 你需要每個問題的成本固定可預測。 使用內建工具時,搜尋幾次由模型決定。若先搜尋再把結果傳進去,每個問題都剛好只需一次呼叫。
- 你需要掌控資料來源。 各家的網域篩選方式不同,Gemini Developer API 也沒有允許清單。結果掌握在自己的程式碼中,就能在模型看到之前先剔除、重新排序、記錄或快取。
- 你需要原始結果。 Claude 回傳的結果文字是加密的,Gemini 回傳的則是轉址連結和網域名稱。自建的 RAG 流程、評測資料集或稽核紀錄,都需要可讀的標題、URL 和摘要。
- 你想公平地評比模型。 只有每個模型拿到相同的佐證資料,比較才算公平。另外呼叫一次搜尋,就能讓所有模型得到完全相同的輸入。
取捨確實存在:搜尋 API 代表多一次呼叫和幾行串接程式碼;而且在我們的 Gemini 測試中,Google 的 grounding 比先取得結果再傳入的做法更快。
如何為任何模型加入同一套網路搜尋?
在呼叫模型之前先呼叫搜尋 API,再把結果放進提示;或是把搜尋註冊成 function tool,讓模型自行呼叫。 使用 apipick 只需要一個 POST 請求:
curl -X POST https://www.apipick.com/api/search/web \
-H "x-api-key: $APIPICK_API_KEY" \
-H "Content-Type: application/json" \
-d '{"query": "2026 World Series winner", "max_num_results": 5}'2026-11-25 的實際回應(只保留前兩筆結果,摘要已截短):
{
"query": "2026 World Series winner",
"results": [
{
"title": "2026 World Series: Brewers vs. Rays Results, MVP & Recap",
"url": "https://swipesports.com/encyclopedia/2026-world-series/",
"snippet": "The Milwaukee Brewers won the 2026 World Series. They beat the Tampa Bay Rays four games to one and clinched with a 4-1 win in Game 5 at Tropicana Field on October 28, 2026.…",
"source_type": "website",
"published_at": null,
"score": 0.97,
"data_type": "unstructured"
},
{
"title": "World Series 2026: Milwaukee Brewers beat Tampa Bay Rays to become MLB champions - BBC Sport",
"url": "https://www.bbc.co.uk/sport/baseball/articles/c5x2v991l0lvo",
"snippet": "# Brewers win first World Series in 57-year history…",
"source_type": "website",
"published_at": null,
"score": 0.9550247754091665,
"data_type": "unstructured"
}
],
"result_count": 5,
"credits_used": 15
}每筆結果都包含 title、url、snippet、published_at 及相關性 score;也可以傳入 country_code、start_date 和 end_date。接著把同一批結果交給你要呼叫的任何模型:
import os, requests
def search(query: str) -> str:
r = requests.post(
"https://www.apipick.com/api/search/web",
headers={"x-api-key": os.environ["APIPICK_API_KEY"]},
json={"query": query, "max_num_results": 5},
timeout=60,
)
r.raise_for_status()
return "\n\n".join(
f"[{i + 1}] {x['title']} ({x['url']})\n{x['snippet']}"
for i, x in enumerate(r.json()["results"])
)
question = "Who won the 2026 World Series?"
prompt = f"Search results:\n{search(question)}\n\nAnswer with citations: {question}"
# 把 `prompt` 送給 OpenAI、Gemini、Claude 或本地模型都可以,佐證資料完全相同。一次網路搜尋消耗 15 點,以每 1,000 點 $1 計算,等於每 1,000 次呼叫 $15,而且只對 HTTP 200 回應計費(價格)。新帳號贈送 100 點免費點數,足夠搜尋六次。如果你希望由模型決定何時搜尋,可以參考我們的教學為 OpenAI 或 Claude AI 代理加入網路搜尋工具,裡面把同一個端點串成 function tool;工具 schema 請見網路搜尋 API 頁面。
該選擇哪一種方案?
max_uses,提示的寫法也別讓模型覺得可以略過搜尋。這份指南沒有涵蓋哪些內容?
- 價格與模型名稱變動很快。 本文所有價格與參數都在 2026-11-24 查核過;編列預算前,請再到各家官方頁面確認。
- 深度研究型 AI 代理。 OpenAI、Google 和 Anthropic 也提供多步驟的研究產品與 AI 代理,各有不同的計價方式。本文只涵蓋單次 API 請求中的搜尋工具。
- Azure OpenAI、Vertex AI 和 Bedrock。 雲端平台支援的工具版本與篩選選項,可能和原廠 API 不同;Claude 在部分平台上只能使用基本版搜尋就是一例。
- 搜尋品質。 本文沒有針對 OpenAI 與 Claude 的搜尋結果進行評比;Gemini 啟用與不啟用 grounding 的準確度,請參閱 40 題測試。
資料來源(2026-11-24 查核):OpenAI web search guide、OpenAI pricing、Grounding with Google Search、Gemini API pricing、Vertex AI grounding、Claude web search tool、AgentCore pricing。
常見問題
OpenAI API 有內建網路搜尋嗎?
有。在 Responses API 的 tools 陣列加入 {"type": "web_search"},模型就能搜尋網路並引用來源。費用是每 1,000 次搜尋呼叫 $10,搜尋內容的 token 另依模型費率計費(2026-11-24 查核)。在 Chat Completions 中,只能透過 gpt-5-search-api 這類專用搜尋模型使用網路搜尋。
Gemini API 的 Google 搜尋 Grounding 是免費的嗎?
部分免費。Gemini 3 以後的模型每月前 5,000 次搜尋查詢免費,額度由這些模型共用。超過之後,Google 每 1,000 次搜尋查詢收費 $14,而且模型執行的每一次查詢都會計費,所以一個問題可能產生兩次以上的查詢費用。Gemini 2.5 模型則是依 grounding 提示計費:每天前 1,500 次免費,之後每 1,000 次 $35。
Claude API 的網路搜尋怎麼收費?
每 1,000 次搜尋 $10,再加上標準 token 費用,因為 Claude 讀取的搜尋結果會計入輸入 token。發生錯誤而失敗的搜尋不計費。價格於 2026-11-24 在 Anthropic 官方文件查核。
可以把網路搜尋限定在特定網站嗎?
OpenAI 可以:使用 filters.allowed_domains 或 blocked_domains,各最多 100 個網域。Claude 也可以:使用 allowed_domains 或 blocked_domains,但同一個請求不能兩者並用。Gemini 的 Developer API 沒有網域允許清單;Vertex AI 則支援用 exclude_domains 排除網域。
可以在 Claude 或 GPT 使用 Google 搜尋 Grounding 嗎?
不行。各家供應商的內建搜尋只能為自家模型提供 grounding。想讓 Claude、GPT、Gemini 或本地模型拿到相同的即時結果,必須自己呼叫搜尋 API,再把結果放進提示,或是當作工具呼叫的回傳值交給模型。
明明啟用了工具,模型為什麼沒有搜尋?
因為這三家的內建工具都由模型自行決定何時搜尋。對於不太會變動的事實問題,模型常常不搜尋就直接回答。你可以在提問時明確要求最新資訊、在 OpenAI 把 tool_choice 設為 "required",或是在呼叫模型之前先在自己的程式碼裡完成搜尋。
Sarah Choy 是 apipick 的 CEO,專注於為 AI Agent 與 LLM 工作流打造可用於正式環境的 API。