Course file
week14_tool_use/README.md
Last week your agent had mocked tools. This week you give it real ones.
The key idea: Claude doesn't run tools. Claude asks you to run them. Your code runs the tool and sends the result back. Claude just picks which tool to call and what arguments to pass.
When you call Claude with tools, you send a JSON schema for each tool. The schema tells Claude:
Claude reads these descriptions and decides which tool fits the situation. Better descriptions = better tool selection.
You: "What's on the front page of Hacker News?"
|
v
Claude: "I should use web_search to check Hacker News"
→ tool_use: web_search(url="https://news.ycombinator.com")
|
v
Your code: fetches the URL, returns the HTML text
|
v
Claude: "Here are the top stories: ..."
The stop_reason in Claude's response tells you what happened:
"end_turn" = Claude is done talking, here's the answer"tool_use" = Claude wants to use a tool, run it and send the result backOpen tools.py — this has three real tool functions:
Open tool_agent.py — this is the agent that uses those tools in a loop.
Same as last week. Make sure you have your .env file:
cp .env.example .env
Install dependencies:
pip install anthropic python-dotenv
python tool_agent.py
Try these:
tools.py. Understand each function. What could go wrong with each one?list_directory(path) — lists files in a folder using os.listdir()write_file(path, content) — writes content to a fileget_weather(city) — fetch weather from a free API like wttr.in: https://wttr.in/{city}?format=3reflection.md.tool_agent.py and tools.py with at least one new toolreflection.md filled inSame pattern as always — paste your error into Claude or ChatGPT:
"I'm building a tool-use agent with the Anthropic SDK. My tool returns [X] but Claude doesn't seem to understand it. Here's my tool definition: [paste]. What's wrong?"