Appendix

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.

22 min read 9 code examples Appendix B of B

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.

💡
Tools Are Mini-Prompts
The description field of a tool definition is read by Claude to decide when and how to use the tool. Write tool descriptions like prompts — specific, clear about when to use the tool and what it returns. A poorly described tool is a tool Claude won't use correctly.

The Tool Definition Schema

Each tool is defined as a JSON object with three required fields:

Tool Definition Structure
{
  "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

Weather + Search + Calculator Tools
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:

1
Think: Claude decides to use a tool
Claude processes the user message and determines that answering requires tool use. It returns a response with stop_reason="tool_use" and a tool_use content block containing the tool name and arguments.
2
Call: Your code executes the tool
You parse the tool_use block, find the matching function in your codebase, call it with the provided arguments, and capture the return value. This is where real-world execution happens.
3
Observe: Return results to Claude
Add the tool result to the conversation as a "tool_result" content block in an assistant message. Claude now has access to the real-world data it needed.
4
Respond: Claude synthesizes the final answer
Claude reads the tool result and generates its final response, incorporating the real-world data. If more tools are needed, it repeats the cycle — potentially calling multiple tools in sequence.

Complete Tool Use Implementation

Tool Use Execution Loop (Python)
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:

{ "name": "search", "description": "Search for information.", "input_schema": { "type": "object", "properties": { "query": {"type": "string"} }, "required": ["query"] } }
{ "name": "web_search", "description": "Search the internet for current information, news articles, and facts. Use this tool when: (1) the user asks about recent events or news after your training cutoff, (2) you need to verify a specific fact you're uncertain about, (3) the user asks for current prices, statistics, or data. Do NOT use for general knowledge questions you can answer confidently. Returns: list of results with title, snippet, and URL.", "input_schema": { "type": "object", "properties": { "query": { "type": "string", "description": "Search query. Be specific — 3-6 words. E.g. 'AAPL stock price today' not 'tell me about Apple'" } }, "required": ["query"] } }
Poor description: Claude overuses the tool (calls it for things it could answer directly) or underuses it (misses cases where it should search). The vague description provides no guidance on when or how to call the tool. Good description: Claude calls the tool at exactly the right times, forms optimal search queries, and knows what to expect back. The "Do NOT use for..." instruction is especially powerful — it prevents tool overuse for simple questions.

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:

RAG Tool Definition
{
  "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:

🔐
Validate Tool Inputs
Never trust Claude's tool arguments blindly. Validate types, ranges, and values before executing. A prompt injection in user input could trick Claude into calling tools with unexpected arguments.
🛡️
Principle of Least Privilege
Give Claude only the tools it needs for the specific task. A customer support agent shouldn't have access to a "delete_user" tool. Scope tool access to the minimum required.
👁️
Log and Audit All Tool Calls
Every tool invocation should be logged with the full arguments and result. Anomalies in tool call patterns can indicate prompt injection or misuse. Audit logs are also invaluable for debugging.
⏸️
Human-in-the-Loop for Irreversible Actions
For destructive or irreversible operations (sending emails, deleting records, making purchases), require explicit human confirmation before tool execution. Don't let Claude act unilaterally.

Building a Research Assistant with Web Search

Research Assistant System Prompt
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.

💡
The Agent Stack
A fully autonomous agent combines everything in this course: a role (who the agent is), clear goals (what it achieves), XML-structured state (what it knows), CoT reasoning (how it plans), format specs (structured intermediate outputs), tool use (real-world capabilities), and chaining (multi-step execution). The techniques in this course are the building blocks — tool use is what puts them to work in the world.
Course Complete — What's Next
You've now covered the full stack of Claude prompt engineering: from basic structure to complex multi-tool agents. The techniques compound: every prompt you write should consider role, clarity, data separation, format, reasoning strategy, grounding, examples, and — for production systems — chaining and tool use. The best way to deepen this knowledge is practice. Build something real, run into failures, and apply the diagnostic frameworks from Ch 9. Head to the Playground and start building.