Large Language Models (LLMs) are powerful, but they have a fundamental limitation: they lack real-time awareness. Their responses are constrained by training data cutoffs, which makes them unsuitable for use cases that require up-to-date information.
To address this, this project demonstrates a LangChain-based web search agent capable of deciding when to search the internet, retrieving fresh data, and synthesizing accurate answers with explicit source citations using the Tavily Search API.
👉 GitHub repository langchain-tavily-search-agent
🚀 What This Project Does
This project implements an AI-powered search agent that:
- Understands a user’s question using a large language model
- Determines whether external web search is required
- Retrieves up-to-date information from the internet
- Synthesizes a coherent final answer
- Displays the source URLs used to generate the response
Unlike simple “search wrappers”, this solution follows an agentic architecture, combining reasoning and tools, which is a foundational pattern in modern AI systems.
🧠 Core Features
🤖 Agent-based reasoning using LangChain
🔍 Real-time web search via Tavily (optimized for LLMs and RAG)
📚 Automatic source citations
🛠️ Custom LangChain tools using the @tool decorator
🎯 Clean, structured output
⚙️ Simple and extensible architecture
🧩 How It Works (High Level)
- A user submits a question
- The LangChain agent analyzes the intent
- The agent decides whether to invoke the search tool
- Tavily performs a web search (up to 5 results)
- Results are returned with title, URL, and content snippets
- The LLM synthesizes a final response grounded in those sources
This pattern is well-suited for RAG pipelines, research agents, and AI assistants that require current information.
🛠️ Key Implementation Details
Custom Search Tool
The Tavily integration is exposed as a LangChain tool:
@tool
def search(query: str) -> str:
response = tavily.search(query, max_results=5)
...
This enables the agent to autonomously decide when a web search is necessary rather than forcing every query through a search step.
Language Model Configuration
llm = ChatOpenAI(
model=os.getenv("OPENAI_MODEL"),
temperature=0.2
)
- Low temperature ensures focused, deterministic responses
- The model is configurable via environment variables
- Compatible with OpenAI-compatible endpoints
Agent Creation
agent = create_agent(model=llm, tools=[search])
LangChain manages:
- Tool invocation
- Message history
- Reasoning steps
- Final response synthesis
▶️ Running the Agent
python main.py
Example output includes:
📚 Sources Used (URLs)
🤖 Final Answer, synthesized from multiple sources
📦 Tech Stack
- Python 3.13
- LangChain >= 1.2.0
- OpenAI (via langchain-openai)
- Tavily Search API
- uv for fast dependency management

No comments:
Post a Comment