LangChain agents need search tools to access current information. This tutorial creates a custom LangChain tool using Scavio's API that supports multiple platforms (Google, Reddit, YouTube, Amazon, Walmart) through a single tool definition. The agent selects the platform based on query context, and the tool returns structured results ready for the LLM's context.
Prerequisites
- Python 3.8+ installed
- langchain and langchain-core packages installed
- A Scavio API key from scavio.dev
Walkthrough
Step 1: Install LangChain dependencies
Add the required packages for building custom tools.
pip install langchain langchain-core requestsStep 2: Create the search tool
Define a LangChain tool that calls Scavio's API with platform routing.
from langchain.tools import tool
import requests, os
@tool
def web_search(query: str, platform: str = 'google') -> str:
"""Search the web for current information. Use platform='google' for general queries,
'reddit' for community discussions, 'youtube' for video content,
'amazon' for products, 'walmart' for retail prices."""
resp = requests.post('https://api.scavio.dev/api/v1/search',
headers={'x-api-key': os.environ['SCAVIO_API_KEY']},
json={'platform': platform, 'query': query}, timeout=10)
results = resp.json().get('organic', [])[:5]
return '\n'.join(f'[{i+1}] {r["title"]}: {r.get("snippet", "")} ({r.get("link", "")})'
for i, r in enumerate(results))Step 3: Create an agent with the search tool
Build a LangChain agent that uses the search tool for answering questions.
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate
llm = ChatOpenAI(model='gpt-4o')
tools = [web_search]
prompt = ChatPromptTemplate.from_messages([
('system', 'You are a helpful research assistant. Use the web_search tool to find current information.'),
('human', '{input}'),
('placeholder', '{agent_scratchpad}'),
])
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)Step 4: Test the agent
Run queries that should trigger different platform searches.
# General web search (google)
result = executor.invoke({'input': 'What are the top CRM tools in 2026?'})
print(result['output'])
# Reddit discussions
result = executor.invoke({'input': 'What do people on Reddit think about Notion?'})
print(result['output'])
# Product search
result = executor.invoke({'input': 'Find the best-rated wireless earbuds on Amazon under $100'})
print(result['output'])Python Example
from langchain.tools import tool
import requests, os
@tool
def web_search(query: str, platform: str = 'google') -> str:
"""Search the web. Platforms: google, reddit, youtube, amazon, walmart."""
resp = requests.post('https://api.scavio.dev/api/v1/search',
headers={'x-api-key': os.environ['SCAVIO_API_KEY']},
json={'platform': platform, 'query': query}, timeout=10)
return '\n'.join(f'{r["title"]}: {r.get("snippet","")}' for r in resp.json().get('organic', [])[:5])JavaScript Example
import { tool } from '@langchain/core/tools';
import { z } from 'zod';
const webSearch = tool(async ({ query, platform }) => {
const resp = await fetch('https://api.scavio.dev/api/v1/search', {
method: 'POST', headers: {'x-api-key': process.env.SCAVIO_API_KEY, 'Content-Type': 'application/json'},
body: JSON.stringify({platform: platform || 'google', query})
});
return (await resp.json()).organic?.slice(0, 5).map(r => `${r.title}: ${r.snippet}`).join('\n') || 'No results';
}, {
name: 'web_search',
description: 'Search the web. Platforms: google, reddit, youtube, amazon, walmart.',
schema: z.object({ query: z.string(), platform: z.string().optional() })
});Expected Output
A LangChain search tool with multi-platform support and an agent that routes queries to the right platform.