Function Calling with Claude
Connect Desearch to Claude through Tool Use (function calling), so Claude can call Desearch AI search when it needs current web content.
Function Calling with Claude
1. Installation and Environment Setup
Make sure you have <Anchor label="python3" target="_blank" href="https://www.python.org/doc/">python3</Anchor> and <Anchor label="pip" target="_blank" href="https://pip.pypa.io/en/stable/installation/">pip</Anchor> installed.
Install the required packages:
This installs:
- anthropic for Claude API calls
- desearch_py for Desearch AI search
- rich for terminal output
- python-dotenv for loading environment variables
API Key Setup
Create a .env file in your project root:
Get your keys from:
- <Anchor label="Anthropic Console" target="_blank" href="https://console.anthropic.com/settings/keys">Anthropic Console</Anchor>
- <Anchor label="Desearch Dashboard" target="_blank" href="https://console.desearch.ai/api-keys">Desearch Dashboard</Anchor>
Add .env to .gitignore to avoid leaking secrets.
2. Understanding Tool Use in Claude
Claude can generate structured requests to call external tools you define. Here's the tool schema:
When Claude decides to search, it returns something like:
Your code handles this tool call and passes the query to Desearch.
3. Using Desearch with Claude Tool Use
Step 1: Initialize Clients
Step 2: Define the Tool
Step 3: System Prompt
Step 4: Define the Tool Function
Step 5: Tool Call Handler
4. Main Loop: Chat with Search Integration
5. Run the Script
Save everything as claude_desearch.py. Then run it:
You'll be prompted like this:
Sample Output