Applied AI School
v0 · 規劃中
Anthropic

內建 tools

Anthropic 直營的 web_search、text_editor、fine-grained tool calling——你不用接 Bing、不用寫 file API,schema 一行就跑。

TL;DR

  • 不是所有 tool 都要自己寫——Anthropic 提供 web_searchtext_editor 等內建版
  • web_search 在 server 端就跑(你不用接 Bing/Google),response 帶 citations 可以直接 render
  • text_editor schema 內建、實作要你自己寫(一個半成品);fine-grained tool calling 是 streaming 的進階用法

一個情境:「Claude,幫我查一下 React 19 release notes」

你的 chatbot 要回最近的技術新聞,Claude 訓練資料 cut off 在去年,沒看過 React 19 release notes。

照前幾篇的做法,你要:

  1. 註冊 Bing / Google / Brave search API
  2. search_web 函式去打那個 API
  3. 寫 schema、處理 result、塞回 messages

繁瑣。Anthropic 看到大家都在做同一件事,乾脆做一個內建 web_search tool。整個 search 在 Anthropic 那邊就跑完了,你只要在 tools 加一行:

tools = [{
    "type": "web_search_20250305",
    "name": "web_search",
    "max_uses": 5,
}]

不用接外部 API、不用寫 function、不用 routing。

內建 tool 跟自寫 tool 的差別

自寫 toolAnthropic 內建 tool
Schema你寫內建(你只給 type + name
Function 實作你 server 寫Anthropic server 跑(multi tool 例外,下面講)
Tool result你包 tool_result 回傳自動 stream 進 response
計費看你 API 用量算 tool use 計費(官方價目
Block typetool_use / tool_resultserver_tool_use / web_search_tool_result

關鍵差別:有的內建 tool 是「server-side」,Anthropic 直接幫你跑,你只看到結果;有的是「client-side」,schema 內建但實作要你寫(text_editor 屬於這一類)。

Web search:最常用的內建 tool

加在 tools array:

res = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=2048,
    tools=[{
        "type": "web_search_20250305",
        "name": "web_search",
        "max_uses": 5,
        "allowed_domains": ["nih.gov", "developer.mozilla.org"],  # 選填
    }],
    messages=[{"role": "user", "content": "React 19 有哪些主要的 breaking changes?"}],
)

max_uses 限制這次 request 最多搜幾次(避免無窮亂打);allowed_domains 把搜尋限在你信的網域——醫療類問題鎖 nih.gov、技術類鎖 mdn.io,比叫 Claude「請只引用權威來源」可靠太多。

response content 會多出幾種 block type:

Block type內容
textClaude 的回答
server_tool_useClaude 實際送出的搜尋 query
web_search_tool_result搜到的結果列表(標題 + URL)
citations 的 text block答案中的句子標註來自哪個 URL

UI 端可以分開 render:頁尾列 source、句子旁邊浮 citation。比叫 model「請引用」結構化得多。

Text editor:schema 內建、function 你寫

Claude Code 那種「打開檔案、改幾行、寫回去」的能力 Anthropic 也包了一個半成品:text_editor

def get_text_editor_schema(model):
    if model.startswith("claude-sonnet-4"):
        return {"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}
    if model.startswith("claude-3-7-sonnet"):
        return {"type": "text_editor_20250124", "name": "str_replace_editor"}
    # 版本字串隨 model 換,最新請查 docs

它只給你 schema,沒給你實作——Claude 會生 tool_use 說「我要 view ./main.py」、「我要 str_replace 這段」,你要自己寫一個 class 處理這些 command:

Command你要做什麼
viewopen file / list dir
str_replace找 string、replace
create寫新檔
insert在某行插入
undo_edit還原(要自己存 history)

意義不在「省你寫實作」,而是省你設計 schema。Claude 訓練時就看過這個 schema,呼叫格式不會錯、edit 失敗 retry 也會自動。你只要把 file system 那層接上去,就有一個迷你 Claude Code。

什麼時候用?

  • IDE-like 應用(在自己 product 裡讓 Claude 改檔)
  • 後端跑 batch 修檔(CI 上自動 fix lint、生 docs)
  • 跟自家 sandbox 整合(教學平台讓 Claude 改學生程式)

不是用來取代 Cursor / Claude Code 的,而是你自己在做 product 時的內建素材。

其他內建 tool(一覽)

Tool跑在哪用途
web_searchAnthropic server即時資訊查詢、citations
web_fetchAnthropic server抓特定 URL 的內容
text_editor你 server檔案 view / edit / create
bash你 server(sandboxed 建議)跑 shell 命令
computer你 serverscreen + mouse + keyboard 操作(Section 6 會講)
code_executionAnthropic server跑 Python,回結果 + 圖(也算內建系,Section 6

computercode_execution 也都是 Anthropic 直營的「特殊 tool」——computer use 讓 Claude 操作畫面,code execution 在 sandbox 跑 Python 回結果跟圖。這兩個能力大、坑也多,留到 Claude 進階能力 section 細講。

版本號(type 結尾的日期)會變——這是 Anthropic 給內建 tool 的「version pin」,model upgrade 時舊版會繼續支援一段時間,正式 production 記得 pin 住版本字串再上線。

Fine-grained tool calling:streaming 的進階用法

預設情況下,streaming + tool use 看起來不太 stream:tool input 的 JSON 會等一個完整 top-level key value pair 出來才一次吐給你(API 會 buffer + 驗證)。

如果你想要更細粒度的 stream(例如即時顯示 tool 參數打到一半的樣子):

res = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=2048,
    tools=tools,
    messages=messages,
    stream=True,
    extra_headers={"anthropic-beta": "fine-grained-tool-streaming-2025-05-14"},
)

開啟後:

  • chunk 一生成就吐——沒 buffer 等驗證
  • 你會收到 input_json 事件,每個帶 partial_json(這次的片段)跟 snapshot(累積到目前的 JSON)
  • 代價:JSON 可能無效——你的 code 要能 handle parse error

什麼時候要:tool 參數很大(一篇文章 / 程式碼)、要邊收邊處理、UI 要顯示 typing effect。80% 場景用預設就好,不要為了「比較炫」開這個。

接下來

到這邊 Tool Use section 收尾——你已經有完整的:寫 schema、處理 multi-block response、跑 multi-turn agent loop、用內建 tool。

下一個問題:當 tool 不夠用、或要丟一份 50 頁 PDF 給 Claude 問——直接塞進 prompt 又貴又散,model 也會被無關內容稀釋。這就是 RAG 要解的——下一篇先講 為什麼需要 RAG,再進到 embeddings、chunking、完整 pipeline。