Lesson 37.3 · 3 of 5 · 5 min read · Reviewed 24 Nov 2026
Server tools: web search and code execution
By the end, you can
- Tell server tools from client tools.
- Use web search and code execution through the API.
Client tools vs server tools
- Client tools are the ones you define. Claude asks; your code runs them and sends back a
tool_result. - Server toolsBuilt-in tools: Tools provided by the platform rather than by you, such as web search, code execution or a text editor tool. are provided by Anthropic. You add them to the request, and the platform runs them. Their results appear in the same response, with no round trip through your code.
Web search
Add the web search toolWeb search tool: A built-in tool that lets the model search the web and cite results. and Claude can search the web during a response, citing its sources.
- Limit how many searches it may run per request.
- Allow or block domains, such as only your docs site.
- Costs include a per-search fee on top of tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id..
Code execution
Lets Claude write and run code in a secure sandboxSandbox: An isolated environment that limits what code or an agent can reach, such as files, network or credentials. to analyse data, do maths or make charts, then use the results in its answer.
When to use your own tool instead
If search must cover private data, or you need full control over sources, write a client tool that queries your own index (Part 4, RAGRetrieval-augmented generation (RAG): Retrieving relevant documents at request time and adding them to the prompt so the answer is grounded in them.).
On other providers
Hosted tools are now common. OpenAI’s Responses API offers web search, file search, a code interpreter, computer use, image generation and remote MCPModel Context Protocol (MCP): An open protocol that standardises how AI applications connect to tools, data and prompts through servers. servers as built-in tools. Gemini offers Google Search grounding, code executionCode execution tool: A built-in tool that lets the model run code in a sandbox and use the results. and URL context. Names, prices and data-handling terms differ, so treat each as a separate vendor decision.
Key points
- Client tools run in your code; server tools run on Anthropic's side and their results come back in the same response.
- Web search lets Claude search the web and cite sources; you can limit domains and the number of searches.
- Code execution runs code in a sandbox for analysis and charts.
Sort a response's client and server tool calls
Read a mixed response and decide which tool calls your code must run and which the platform already ran.
You will have: A function that splits a response's content blocks into calls to run locally and server results to pass through, with a printed summary.
Steps
- Read the simulated response: it mixes text, a server-side web search with its result, and a client tool_use.
- Write split_blocks(content) that collects tool_use blocks for your code to run.
- Collect server_tool_use blocks separately and pair each with its result block by id.
- Run the client tool and build the tool_result you would send back.
- Print what you ran, what the platform ran, and the cited sources.
Done when
- Only get_order_status is run by your code.
- The web search is reported as already run, with its result paired by id.
- Exactly one tool_result is built, matching the client tool_use id.
Open in the Playground No API key needed.
Starter code (Python)
response_content = [
{"type": "text", "text": "Let me check the courier's published delays and your order."},
{"type": "server_tool_use", "id": "srvtoolu_01", "name": "web_search", "input": {"query": "courier delays this week"}},
{"type": "web_search_tool_result", "tool_use_id": "srvtoolu_01",
"content": [{"type": "web_search_result", "url": "https://courier.example.com/status", "title": "Service status"}]},
{"type": "tool_use", "id": "toolu_02", "name": "get_order_status", "input": {"order_id": "A-1042"}},
]
def get_order_status(order_id):
return {"order_id": order_id, "status": "in transit"}
def split_blocks(content):
run_locally, server_ran = [], []
# TODO: tool_use -> run_locally; server_tool_use -> server_ran (pair with its *_tool_result by id)
return run_locally, server_ran
local, server = split_blocks(response_content)
print("run locally:", local)
print("platform ran:", server)
Show a solution
response_content = [
{"type": "text", "text": "Let me check the courier's published delays and your order."},
{"type": "server_tool_use", "id": "srvtoolu_01", "name": "web_search", "input": {"query": "courier delays this week"}},
{"type": "web_search_tool_result", "tool_use_id": "srvtoolu_01",
"content": [{"type": "web_search_result", "url": "https://courier.example.com/status", "title": "Service status"}]},
{"type": "tool_use", "id": "toolu_02", "name": "get_order_status", "input": {"order_id": "A-1042"}},
]
def get_order_status(order_id):
return {"order_id": order_id, "status": "in transit"}
def split_blocks(content):
run_locally, server_ran = [], []
results = {b["tool_use_id"]: b for b in content if b["type"].endswith("_tool_result")}
for b in content:
if b["type"] == "tool_use":
run_locally.append(b)
elif b["type"] == "server_tool_use":
server_ran.append((b, results.get(b["id"])))
return run_locally, server_ran
local, server = split_blocks(response_content)
tool_results = []
for b in local:
out = {"get_order_status": get_order_status}[b["name"]](**b["input"])
tool_results.append({"type": "tool_result", "tool_use_id": b["id"], "content": str(out)})
print("run locally:", [b["name"] for b in local])
for call, result in server:
print("platform ran:", call["name"], "paired:", result is not None)
print(" sources:", [r["url"] for r in result["content"]])
print("tool_results to send:", [(r["tool_use_id"], r["content"]) for r in tool_results])