Lộ trình học›Claude API›Building with the Claude API Mở bản đồ

6.44 · The web search tool — Claude tra cứu internet

Bài viếtTrung cấp15 phút đọc

Claude's training data có cutoff (ví dụ 1/2026). Hỏi:

Sau bài này, bạn sẽ
  1. Enable built-in web search tool
  2. Giới hạn search domain (whitelist authoritative sources)
  3. Handle multi-block response với citations
  4. Case study: research assistant, fact-checker

Mở đầu: Real-time knowledge

  • "Giá vàng hôm nay?" → không biết
  • "Kết quả bầu cử tuần trước?" → không biết
  • "Claude model mới nhất?" → biết đến lúc train

Web search tool giải quyết — Claude search Google-like, đọc kết quả, reply với citations.

Enable

Pre-requisite: Bật web search tool trong Console → Privacy settings.

web_search_schema = {
    "type": "web_search_20250305",
    "name": "web_search",
    "max_uses": 5  # Max searches Claude can do per request
}

response = client.messages.create(
    model="claude-sonnet-4-6-20260205",
    max_tokens=2000,
    messages=[{
        "role": "user",
        "content": "What is the latest Anthropic model released in 2026?"
    }],
    tools=[web_search_schema]
)

Claude tự decide có search hay không. Nếu có → full flow diễn ra server-side.

Response structure

Multi-block:

- TextBlock: "Let me search for that..."
- ServerToolUseBlock: query="Anthropic Claude model 2026"
- WebSearchToolResultBlock:
    - WebSearchResult: {title, url, snippet}
    - WebSearchResult: {...}
    - ...
- TextBlock with citations: "Claude Opus 4.7 was released April 2026 [1]..."

Extract

for block in response.content:
    if block.type == "text":
        print(block.text)
    elif block.type == "server_tool_use":
        print(f"Searched: {block.input['query']}")
    elif block.type == "web_search_tool_result":
        for result in block.content:
            print(f"Source: {result.url} — {result.title}")

Allowed domains — restrict to authoritative

Whitelist domains cho reliable sources:

web_search_schema = {
    "type": "web_search_20250305",
    "name": "web_search",
    "max_uses": 5,
    "allowed_domains": ["nih.gov", "who.int", "nature.com"]
}

Use cases:

  • Medical chatbot → limit PubMed (nih.gov)
  • Legal → official gov sites
  • Financial → SEC filings only

Improves quality drastically — no random blog contamination.

Blocked domains

Inverse: block specific domains:

"blocked_domains": ["reddit.com", "quora.com"]

Chặn user-generated content không reliable.

Citations inline

Khi Claude response với source backing, có citation blocks embedded:

"Anthropic released Claude Opus 4.7 in April 2026, featuring improvements
in reasoning and coding [1]. The model supports up to 200K context window [2]."

Citation metadata:

{
    "type": "citation",
    "cited_text": "...",  # quoted text from source
    "url": "...",
    "title": "..."
}

UI render: hover [1] → show source.

Ví dụ: Research assistant

response = client.messages.create(
    model="claude-sonnet-4-6-20260205",
    max_tokens=3000,
    messages=[{
        "role": "user",
        "content": """Research: what are the latest advances in battery technology in 2026?
Focus on lithium alternatives. Summary in 300 words with citations."""
    }],
    tools=[{
        "type": "web_search_20250305",
        "name": "web_search",
        "max_uses": 10,
        "allowed_domains": ["nature.com", "sciencedirect.com", "ieee.org", "spectrum.ieee.org"]
    }]
)

Claude search multiple academic sources, synthesize, cite.

Case studies

Content writer với fact-check

Writer prompts: "Write article about X, fact-check all claims with web search."

Claude search → verify → cite. Reduce factual errors in articles 80%.

Financial news digest

"Today's top 5 news affecting AAPL stock" → web search → summarize.

Medical info chatbot

allowed_domains=["nih.gov", "mayoclinic.org"] → reliable medical info only. Include disclaimer "consult doctor".

Competitive intelligence

"What did company X launch in last 30 days?" → search news + blog.

vs Extended thinking

Web searchExtended thinking
PurposeFresh external infoDeep reasoning on existing info
Latency5-15s (search time)5-10s
Cost$ (search + content tokens)$$ (thinking tokens)
Use whenNeed current factsNeed careful reasoning

Can combine both: Search để get facts → thinking để synthesize.

Anti-patterns

max_uses unlimited

Claude can run 20+ searches → expensive.

Fix: Set max_uses=3-5 typical.

No allowed_domains cho sensitive domain

Medical chatbot search random blog → misinformation.

Fix: Whitelist authoritative sources.

Forget to enable in console

API returns error if not enabled.

Fix: Check Console → Privacy → enable.

Dùng cho info static

"Who's president of US?" — Claude biết. Search lãng phí.

Fix: Let Claude decide. Schema có thôi.

Áp dụng ngay

Bài tập 1: Research query (15 phút)

Ask Claude (with web search) research latest trend in your industry. Observe:

  • Search queries used
  • Sources found
  • Citations in output

Bài tập 2: Restricted search (10 phút)

Ask medical-related question 2 ways:

  1. allowed_domains=["nih.gov"]
  2. No restriction

Compare quality + sources.

Tóm tắt bài học

Những điều cốt lõi
  • Web search = built-in tool, server-side, Anthropic handles search.
  • Enable in console first, schema với max_uses, allowed_domains.
  • Multi-block response: text + server_tool_use + results + citations.
  • Whitelist domains cho domain-critical applications.
  • Claude auto-decide — sometimes skip search if answer in training.
Kiểm tra nhanh1 câu hỏi · chọn đáp án để xem giải thích
    Bạn đã hoàn thành bài này chưa?Tiến độ lưu trên trình duyệt của bạn.