An intelligent AI-powered web search assistant built with Next.js, TypeScript, and TailwindCSS. The agent uses OpenAI GPT-4o or Claude Sonnet to reason about user queries and perform real-time web searches to provide accurate, cited answers.
- π§ Dual LLM Support: Choose between OpenAI GPT-4o or Claude Sonnet
- π Real-Time Web Search: Live search integration using SerpApi
- π¬ Interactive Chat UI: Modern, responsive chat interface
- π Source Citations: All answers include proper source attribution
- π― Smart Tool Usage: Agent intelligently decides when to search
- β‘ Fast & Efficient: No vector database overhead - everything happens in real-time
- π¨ Beautiful Design: Gradient backgrounds and smooth animations
This project follows the ReAct pattern (Observe β Think β Act β Answer):
- User Query β User asks a question
- LLM Reasoning β Agent decides if search is needed
- Tool Execution β Searches web via SerpApi if necessary
- Synthesis β Agent processes results and formulates answer
- Response β User receives cited, accurate information
- Frontend: React, Next.js 14 (App Router), TailwindCSS
- Backend: Next.js API Routes, TypeScript
- LLMs: OpenAI GPT-4o, Anthropic Claude Sonnet
- Search: SerpApi (Google Search API)
- Deployment: Vercel-ready
- Node.js 18+ installed
- API keys for:
- Clone or download the project
cd web-search-agent- Install dependencies
npm install- Set up environment variables
Create a .env.local file in the root directory:
cp .env.local.example .env.localEdit .env.local and add your API keys:
# At least one LLM provider is required
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
# Required for web search
SERPAPI_KEY=your_serpapi_key- Run the development server
npm run dev- Open your browser
Navigate to http://localhost:3000
- Type your question in the input field
- Press Enter or click Send
- The agent will:
- Determine if a web search is needed
- Search the web if necessary
- Provide a cited answer with sources
Toggle between OpenAI and Claude using the buttons in the header.
- "What's the latest news about AI?"
- "Tell me about recent SpaceX launches"
- "What are the current stock market trends?"
- "Best programming languages in 2025?"
web-search-agent/
βββ app/
β βββ api/
β β βββ chat/
β β βββ route.ts # Chat API endpoint
β βββ layout.tsx # Root layout
β βββ page.tsx # Home page
βββ components/
β βββ ChatUI.tsx # Main chat interface
βββ lib/
β βββ agent.ts # Agent orchestration logic
β βββ tools/
β βββ searchTool.ts # Web search implementation
βββ styles/
β βββ globals.css # Global styles
βββ .env.local.example # Environment template
βββ next.config.js # Next.js configuration
βββ tailwind.config.js # TailwindCSS configuration
βββ tsconfig.json # TypeScript configuration
βββ package.json # Dependencies
Edit lib/agent.ts to customize default models:
// For OpenAI
model: string = 'gpt-4o'
// For Claude
model: string = 'claude-sonnet-4-20250514'Modify the default number of search results in lib/tools/searchTool.ts:
export async function searchWeb(
query: string,
numResults: number = 5 // Change this default value
)Customize the agent's behavior by editing the SYSTEM_PROMPT in lib/agent.ts.
-
Push your code to GitHub
-
Import your repository in Vercel
-
Add environment variables in the Vercel dashboard:
OPENAI_API_KEYANTHROPIC_API_KEYSERPAPI_KEY
-
Deploy! π
This is a standard Next.js app and can be deployed to any platform that supports Node.js:
- Netlify
- Railway
- Render
- AWS Amplify
- DigitalOcean App Platform
Run type checking:
npm run type-checkRun linting:
npm run lintSend a chat message and receive an AI response.
Request Body:
{
"message": "Your question here",
"history": [
{
"role": "user",
"content": "Previous message"
},
{
"role": "assistant",
"content": "Previous response"
}
],
"provider": "openai", // or "claude"
"model": "gpt-4o" // optional
}Response:
{
"content": "The agent's response",
"searchPerformed": true,
"searchQuery": "the search query used"
}- Never commit
.env.localto version control - API keys are only used server-side
- Rate limiting is recommended for production
- Consider adding authentication for public deployments
Make sure you've created .env.local and added your API keys.
Free tier: 100 searches/month. Upgrade at serpapi.com/pricing
Run npm install to ensure all dependencies are installed.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License Β© 2025
- OpenAI for GPT-4o
- Anthropic for Claude
- SerpApi for search capabilities
- Vercel for Next.js and hosting
- Next.js Documentation
- OpenAI API Documentation
- Anthropic Claude Documentation
- SerpApi Documentation
- TailwindCSS Documentation
Built with β€οΈ using Next.js, TypeScript, and AI
