How to use web search with the Claude API as a server tool

Day 6, 25 minutes · beginner

Goal of this lessonTurn on the web search server tool, read the searches, results and citations in the response, and see what the searches cost.

Why it matters

Margriet's customers do not only ask about stock. They ask how to look after a plant once it is home, and the answers change: advice gets updated, and Claude's training data stops at a certain date. Writing your own search tool would mean a search service, a key and code to read web pages. The Claude API has a ready-made one that Anthropic runs for you.

Today you try a server tool: a tool that runs on Anthropic's servers instead of in your code. You switch on web search, ask a plant care question and read what came back: the searches, the pages found and the sources behind each sentence. At the end you have a script that answers a care question with sources, and you know what each search costs.

Server tools: you switch them on, Anthropic runs them

With your own tools you write the schema, run the code and send back the result. With a server tool you only add it to tools:

{ "type": "web_search_20250305", "name": "web_search", "max_uses": 3 }
  • type is the tool and its version. Server tools have a date in the name. Web search has three versions today: web_search_20250305 (basic search), web_search_20260209 (adds dynamic filtering) and web_search_20260318 (adds control over which result blocks you get back). This lesson uses the basic one.
  • name is web_search, as in every example in the docs.
  • max_uses is optional and caps the number of searches in one turn. The docs say a single fact usually takes 1 to 3 searches.

Then Claude decides when to search, the API runs the searches and gives Claude the results, and Claude writes its answer. This can repeat several times inside one request. You never build a tool_result for a server tool.

The other server tools, such as web_fetch and code_execution (a code sandbox: a closed-off place at Anthropic where code runs), are switched on the same way.

What comes back

The response holds more kinds of blocks than before:

  • server_tool_use: Claude's search, with the query in input. Its id starts with srvtoolu_, not toolu_.
  • web_search_tool_result: the pages found. Each result has a url, a title, a page_age (when the site was last updated) and an encrypted_content field that you must send back unchanged if you continue the conversation.
  • text blocks with citations: the sources behind a sentence. Each citation has the url, the title and a cited_text of up to 150 characters. Citations are always on for web search.

The docs add a rule for showing these answers to people: when you show the output to end users, include the citations to the original sources.

When something goes wrong

A failed search does not give you an HTTP error. The request still succeeds, and the result block holds an error with an error_code, such as max_uses_exceeded (the search cap was reached) or too_many_requests. Claude usually carries on with another approach or explains. Two exceptions come back as a 400 error: an organization where an admin has turned web search off in the Console, and a request with both allowed_domains and blocked_domains.

pause_turn

Because the search loop runs on Anthropic's side, it has a limit. When a long turn hits it, the response comes back with stop_reason set to pause_turn. The work is not done yet. Send the response back unchanged as an assistant message, with the same tools, and Claude continues where it stopped. Cap the number of times you do this, as with any retry. Today's script does that.

What it costs

On the Claude API, web search costs 10 US dollars per 1,000 searches, on top of the normal tokens. The search results count as input tokens. A search that fails is not billed. The response tells you how many searches ran, in usage.server_tool_use.web_search_requests.

Useful settings

  • allowed_domains or blocked_domains: only search certain sites, or never search certain sites. Use one of the two, not both.
  • user_location: makes results fit a place, with fields such as city, country (a two-letter code) and timezone.

You can also send web search and your own tools in one request. The rules for a turn that mixes both are on the Server tools page of the docs; this course keeps them apart.

The steps

  1. Check that web search is on for your organization.
  2. Create care_search.py: web search with at most three searches, a care question, and a loop that continues a paused turn.
  3. Run it and read the searches, the pages found, the answer and its sources.
  4. Check the cost: the number of searches in the output, and the request on the Usage page.

Follow the steps below.

Today's actions

Ticks are not saved without an account

Do it in Claude API

Claude API

  1. Open platform.claude.com/settings/capabilities in the Claude Console and check that web search is on. If it is off and you are not an admin, ask the admin of your organization.
  2. Open a terminal in your copperleaf-tools folder and turn on the virtual environment as in lesson 2.
  3. Create care_search.py with this code. It adds the web search tool with at most three searches, continues a paused turn up to three times and prints every block it gets back.
    from dotenv import load_dotenv
    import anthropic
    
    load_dotenv()
    client = anthropic.Anthropic()
    
    MODEL = "claude-opus-5-5"
    TOOLS = [{"type": "web_search_20250305", "name": "web_search", "max_uses": 3}]
    question = "How often should a peace lily be watered in winter? Answer in three short sentences."
    messages = [{"role": "user", "content": question}]
    blocks, searches = [], 0
    
    for _ in range(4):  # a long search turn can pause: send it back to continue
        response = client.messages.create(model=MODEL, max_tokens=4096, tools=TOOLS, messages=messages)
        blocks += response.content
        if response.usage.server_tool_use:
            searches += response.usage.server_tool_use.web_search_requests
        if response.stop_reason != "pause_turn":
            break
        messages.append({"role": "assistant", "content": response.content})
    
    for block in blocks:
        if block.type == "server_tool_use":
            print("Searched for:", block.input)
        elif block.type == "web_search_tool_result":
            if isinstance(block.content, list):
                for result in block.content:
                    print("  found:", result.title, result.url)
            else:
                print("  search error:", block.content.error_code)
        elif block.type == "text":
            print(block.text, end="")
            for citation in block.citations or []:
                print(f" [{citation.title}, {citation.url}]", end="")
    
    print("\nstop_reason:", response.stop_reason)
    print("Searches counted in usage:", searches)
  4. Run python care_search.py. You see what Claude searched for, the pages it found, the answer with a source after each cited sentence, and the number of searches counted in the usage data.
  5. Copy the answer, its sources and the number of searches into care-answer.txt. Before you show such an answer to a customer, keep the sources with it.
  6. Open the Usage page at platform.claude.com/spending/api-usage and find today's requests. Web search is billed per search, on top of the tokens.

Note: If the API answers with a 400 error that says web search is not enabled, an admin has turned it off in the Console. This lesson uses web_search_20250305, the basic version; the newer versions filter results with code and return more kinds of blocks.

Paid planPay as you go with prepaid credits in the Claude Console (minimum 5 US dollars), no free tier. Web search costs 10 US dollars per 1,000 searches, plus tokens.

Checked on 4 December 2026 · Source: Claude API docs: Web search tool (opens in a new tab)

Create a free account to save your progress. Sign in or sign up