intermediate15 min read· Module 6, Lesson 3
🔧Tools & Web Search
Give Claude the ability to search the web, run code, and use custom tools
Tools — Extending Claude's Abilities
Tools let Claude call functions and interact with external systems. Think of tools as "superpowers" you give to Claude.
Types of Tools
| Type | Where It Runs | Examples |
|---|---|---|
| Server tools | Anthropic's servers | Web search, code execution, web fetch |
| Client tools | Your application | Custom functions you define |
Web Search — Real-Time Information
const response = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 1024,
tools: [{
type: "web_search_20250305",
name: "web_search",
max_uses: 5 // Limit searches per request
}],
messages: [{
role: "user",
content: "What's the current stock price of Apple?"
}]
});Claude will:
- Decide it needs to search
- Execute the search
- Read the results
- Respond with cited sources
Web Search Options
tools: [{
type: "web_search_20250305",
name: "web_search",
max_uses: 5,
allowed_domains: ["reuters.com", "bbc.com"], // Only these sites
blocked_domains: ["spam-site.com"], // Never these sites
user_location: { // Localize results
type: "approximate",
city: "New York",
country: "US"
}
}]Custom Tools (Client-Side)
Define your own tools that Claude can call:
const response = await client.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 1024,
tools: [{
name: "get_weather",
description: "Get current weather for a location",
input_schema: {
type: "object",
properties: {
location: {
type: "string",
description: "City name, e.g., 'New York'"
},
unit: {
type: "string",
enum: ["celsius", "fahrenheit"],
description: "Temperature unit"
}
},
required: ["location"]
}
}],
messages: [{
role: "user",
content: "What's the weather in London?"
}]
});
// Claude responds with a tool_use block:
// { type: "tool_use", name: "get_weather", input: { location: "London" } }
// YOU execute the function and send the result backThe Tool Use Loop
- You send a message with tool definitions
- Claude decides if it needs a tool and returns a
tool_useblock - You execute the tool and return a
tool_result - Claude uses the result to form its final response
Pricing
- Web search: $10 per 1,000 searches + normal token costs
- Client tools: Same as normal API pricing
Next up: Structured Outputs — guarantee valid JSON responses.