Tool Use & Search/Retrieval
Tool use breaks Claude out of its training data constraints. Define functions Claude can call, and watch it reason about when to use them, execute them, and incorporate the results — becoming a true autonomous agent.
What Tool Use Is: Extending Claude Beyond Training Data
Claude's training data has a knowledge cutoff and covers only information that was text-form and publicly available. It cannot look up a current stock price, check your database, send an email, or run a Python function. Tool use — also called function calling — solves this by letting you define a menu of capabilities Claude can invoke at will.
When Claude has tools available, it reasons about which tool to call, generates a structured function call, your code executes the function with real-world access, and returns the result to Claude — which then incorporates it into its reasoning and response. Claude becomes an agent with real capabilities, not just a text generator.
The Tool Definition Schema
Each tool is defined as a JSON object with three required fields:
{
"name": "get_weather", // Snake_case function name — clear and specific
"description": "Get the current weather conditions and forecast for a city. Use this when the user asks about weather, temperature, or forecast for any location. Returns current conditions and a 3-day forecast.",
"input_schema": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city name, e.g. 'London', 'Tokyo'. Include country code for ambiguous cities: 'Springfield, IL, US'"
},
"units": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature units. Default to celsius unless user specifies."
}
},
"required": ["city"]
}
}
Three Foundational Tool Examples
TOOLS = [
{
"name": "web_search",
"description": "Search the web for current information, news, facts, and data. Use when the user asks about recent events, specific facts, or anything that may have changed after your training cutoff. Returns a list of relevant excerpts with source URLs.",
"input_schema": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query — 2-6 words, specific"},
"num_results": {"type": "integer", "description": "Number of results, 1-10. Default 5."}
},
"required": ["query"]
}
},
{
"name": "calculator",
"description": "Perform precise mathematical calculations including arithmetic, statistics, and financial formulas. Use this for any calculation that requires precision — do not calculate in your head. Returns the numeric result.",
"input_schema": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "A valid mathematical expression as a Python expression string. E.g.: '(1500000 * 0.08) / 12' or 'sum([12, 15, 8, 22]) / 4'"
}
},
"required": ["expression"]
}
},
{
"name": "query_database",
"description": "Query the company's internal database for customer data, orders, product inventory, and metrics. Use when the user asks about specific accounts, orders, or internal data. Returns rows matching the query.",
"input_schema": {
"type": "object",
"properties": {
"table": {"type": "string", "enum": ["customers", "orders", "products", "metrics"]},
"filters": {"type": "object", "description": "Key-value pairs to filter records, e.g. {\"customer_id\": \"C1234\"}"},
"limit": {"type": "integer", "description": "Max records to return. Default 10, max 100."}
},
"required": ["table"]
}
}
]
The Tool Use Cycle: Think → Call → Observe → Respond
Tool use follows a deterministic cycle. Understanding it is essential for implementing the execution loop correctly:
Complete Tool Use Implementation
import anthropic
import json
client = anthropic.Anthropic()
# Your actual tool implementations
def web_search(query: str, num_results: int = 5) -> list:
"""Call your search API here."""
# ... implementation
return [{"title": "...", "snippet": "...", "url": "..."}]
def calculator(expression: str) -> float:
"""Safe expression evaluator."""
import ast, math
return eval(expression, {"__builtins__": {}, "math": math})
TOOL_IMPLEMENTATIONS = {
"web_search": web_search,
"calculator": calculator,
}
def run_agent(user_message: str) -> str:
"""Run the tool use loop until Claude is done."""
messages = [{"role": "user", "content": user_message}]
while True:
response = client.messages.create(
model="claude-opus-4-5",
max_tokens=4096,
tools=TOOLS,
messages=messages
)
# Add Claude's response to the conversation
messages.append({"role": "assistant", "content": response.content})
# If Claude is done (no more tool calls), return the final text
if response.stop_reason == "end_turn":
for block in response.content:
if hasattr(block, 'text'):
return block.text
# Process all tool calls in this response
tool_results = []
for block in response.content:
if block.type == "tool_use":
tool_fn = TOOL_IMPLEMENTATIONS.get(block.name)
if tool_fn:
try:
result = tool_fn(**block.input)
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": json.dumps(result)
})
except Exception as e:
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"is_error": True,
"content": str(e)
})
# Return tool results to Claude for the next iteration
if tool_results:
messages.append({"role": "user", "content": tool_results})
# Usage
answer = run_agent("What's the current EUR/USD exchange rate and how has it changed this week?")
print(answer)
Best Practices for Tool Descriptions
Tool descriptions are mini-prompts. Claude reads them to decide which tool to use and how to call it. Treat them with the same care as your main prompts:
RAG with Tool Use: Retrieval as a Tool
Retrieval-Augmented Generation (RAG) and tool use are naturally combined: your vector search becomes a tool Claude can call when it needs to look up specific information:
{
"name": "search_knowledge_base",
"description": "Search our internal knowledge base for product documentation, support articles, policy documents, and internal guides. Use this tool BEFORE answering any question about our products, policies, or procedures. Always search before responding — do not rely on memory. Returns relevant document excerpts with source document names.",
"input_schema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query. Rephrase the user's question as a direct search query. E.g. if user asks 'can I get a refund?' search for 'refund policy'"
},
"max_results": {
"type": "integer",
"description": "Maximum number of document chunks to retrieve. Default 3, max 8."
}
},
"required": ["query"]
}
}
Security Considerations
When Claude can execute real-world actions via tools, security is critical:
Building a Research Assistant with Web Search
You are a research assistant with web search capabilities.
PROCESS FOR RESEARCH QUESTIONS:
1. Identify what you need to find: break the question into searchable sub-questions
2. Search for each sub-question using the web_search tool
3. Evaluate the search results: note the source quality and recency
4. Synthesize the information into a coherent, cited answer
CITATION FORMAT:
For every factual claim, include the source in brackets: [Source: Publication Name, URL]
If you searched but couldn't find reliable information, say so explicitly.
QUALITY STANDARDS:
- Prefer primary sources (official sites, academic papers, original reporting)
- Flag outdated information (more than 2 years old for rapidly-changing topics)
- Distinguish confirmed facts from interpretations or estimates
- If sources conflict, present both views and note the discrepancy
NEVER answer factual questions from memory alone when web_search is available.
Always search first, then synthesize from real results.
The Future: Autonomous Agents with Tool Use
Tool use is the foundation of autonomous AI agents — systems that can take a goal and execute multi-step plans using a combination of reasoning, tool calls, and iterative refinement without human intervention at each step.