4 步給 OpenAI / Claude Agent 接入即時網頁搜尋

想讓 Agent 的回答基於最新資訊?只要兩樣東西:一個回傳 LLM 友善 snippet 的搜尋 API,加一個模型能呼叫的 tool 定義。本文把這兩件事拆成 4 步。
一句話總結
- •Step 1:建立免費 API key,先用 curl 跑通端點。
- •Step 2:直接從 API 拉 OpenAI / Claude 的 tool schema —— 不要手寫 JSON。
- •Step 3:把 tool 註冊到 Agent(OpenAI Assistants、Anthropic Messages、LangChain 或 n8n)。
- •Step 4:寫 tool handler 呼叫 /api/search/web,把結果回寫給模型。
為什麼這篇短
大多數「給 LLM 加網頁搜尋」的教學之所以長,是因為它們其實在教某個搜尋 SDK 或某個重型 Agent 框架。本文不是。整件事就是:選一個端點 → 註冊成 tool → 寫一個 5 行 handler。
1拿到免費 API key 並跑通端點
到 apipick.com/dashboard/api-keys 註冊,新帳號送 100 credits,不需信用卡。建一個 key,然後:
curl -X POST https://www.apipick.com/api/search/web \
-H "x-api-key: pk_yourkey" \
-H "Content-Type: application/json" \
-d '{"query": "what is retrieval augmented generation"}'會拿到一個精簡的 ranked 清單:title、URL、snippet。先在 curl 裡跑通能省掉很多後續 debug 時間。
2直接拉 tool schema(別手寫)
API Pick 在 GET /api/search/web/tool-schema 上提供「貼上即用」的 tool schema,裡面同時給了 OpenAI function 定義與 Claude tool use 定義。
curl https://www.apipick.com/api/search/web/tool-schema把結果快取住 —— 它不會經常變。手寫 function schema 是 Agent 程式碼裡最容易踩坑的地方(型別錯、required 錯、description 缺)。
3把 tool 註冊到 Agent
OpenAI Assistants
from openai import OpenAI
import requests
client = OpenAI()
schema = requests.get("https://www.apipick.com/api/search/web/tool-schema").json()
assistant = client.beta.assistants.create(
name="Research Agent",
model="gpt-4o",
instructions="Use web_search whenever the user asks about current events or anything that changes after your training cutoff.",
tools=[{"type": "function", "function": schema["openai"]}],
)Anthropic Claude
import anthropic
import requests
schema = requests.get("https://www.apipick.com/api/search/web/tool-schema").json()
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=[schema["claude"]],
messages=[{"role": "user", "content": "Summarise this week's RAG research."}],
)LangChain
from langchain.tools import tool
import requests
@tool
def web_search(query: str) -> dict:
"""Real-time web search. Use for any question that needs current information."""
return requests.post(
"https://www.apipick.com/api/search/web",
headers={"x-api-key": "pk_yourkey"},
json={"query": query},
).json()n8n
加一個 HTTP Request 節點:方法 POST、URL https://www.apipick.com/api/search/web、Header x-api-key: pk_yourkey、JSON Body {"query": "{{ $json.query }}"}。把它接到 Agent 節點或 LLM Chain 節點。不需要 SDK。
4處理 tool 呼叫
OpenAI Assistants 的 run 在 requires_action 狀態停下時,跑搜尋並提交結果:
import json, time, requests
from openai import OpenAI
client = OpenAI()
KEY = "pk_yourkey"
def run_with_tool(thread_id: str, assistant_id: str):
run = client.beta.threads.runs.create(thread_id=thread_id, assistant_id=assistant_id)
while True:
run = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
if run.status == "requires_action":
outputs = []
for call in run.required_action.submit_tool_outputs.tool_calls:
if call.function.name == "web_search":
args = json.loads(call.function.arguments)
result = requests.post(
"https://www.apipick.com/api/search/web",
headers={"x-api-key": KEY},
json=args,
).json()
outputs.append({"tool_call_id": call.id, "output": json.dumps(result)})
client.beta.threads.runs.submit_tool_outputs(
thread_id=thread_id, run_id=run.id, tool_outputs=outputs,
)
elif run.status in ("completed", "failed", "cancelled", "expired"):
return run
time.sleep(0.5)Anthropic Messages 的迴圈類似 —— 當 response 裡出現 tool_use 區塊,呼叫 API,把結果作為下一輪的 tool_result 內容區塊發回去。
把迴圈跑得更穩
- tool description 要從模型的視角寫:「Use this when the user asks about current events, breaking news, or anything that may have changed after your training cutoff.」別只寫「這是一個網頁搜尋 API」。
- 在 handler 裡對同一會話裡重複出現的相同 query 做快取。搜尋結果在分鐘級是穩定的,沒必要在同一對話裡付兩次 15 credits。
- 對新鮮度敏感時加
start_date。「這週的新聞」如果不加日期過濾,常常回傳去年的標題。
下一步
Agent 會搜尋之後,自然該會讀連結。把 URL 內容擷取 API 註冊成第二個 tool —— 同樣的 auth、同樣的 JSON 結構、一次最多 25 個 URL。「搜尋 + 擷取」就是最小可用的研究 Agent。
常見問題
我用 OpenAI Responses API 而不是 Assistants,可以嗎?
可以。同一份 tool schema 直接在 client.responses.create() 裡透過 tools=[{...}] 傳入即可。處理姿勢完全一致:解析 tool_calls,呼叫 POST /api/search/web,把 JSON 回傳。
結果需要再格式化一次才回給模型嗎?
不需要。POST /api/search/web 回傳的就是 ranked 的 title + URL + LLM 友善 snippet。原樣回給模型即可,模型會引用 snippet、連結 URL。
每個 Agent turn 大約多少錢?
每次 web 搜尋 15 credits(按 $5/5,000 折算約 $0.015)。一個典型 Agent turn 用到一次搜尋遠低於 1 美分 —— 真正花錢的是 LLM token。
如果模型從來不呼叫工具怎麼辦?
把 tool description 寫得具體。「Use this for any question that requires current or post-training information」遠比「web search」更管用。模型會根據「這個 tool 是做什麼的」來決定走不走它。
可以限定國家或日期範圍嗎?
可以。傳 country_code(ISO 3166-1 alpha-2)和/或 start_date / end_date(YYYY-MM-DD)。要讓模型自行選擇,就在 tool schema 裡把它們當參數;如果你的 Agent 有固定的 locale 或新鮮度要求,直接在 handler 裡寫死即可。
本文使用的 API
Sarah Choy 是 API Pick 的 CEO,專注於為 AI Agent 與 LLM 工作流打造可用於正式環境的 API。