π LangChain Agent with Tools, Memory & Tavily Search (LangChain v1)
A beginner-friendly AI Agent built using LangChain v1, OpenAI, Tavily Search, and custom tools. Supports:
β Tool Calling β Short-Term Memory (Chat History) β Web Search using Tavily β Weather Tool β Datetime Tool β Interactive CLI Chat β Clean & simple LangChain v1 setup
π Features π§ Built-in Tools
get_current_datetime β Returns the current system time
get_weather(city) β Weather using the free wttr.in API (no key needed)
TavilySearchResults β Web search with Tavily
π§ Memory System
Stores up to 10 recent message pairs (20 messages total)
Maintains context across conversations
Automatically formats memory for LangChain v1
π€ Agent System
Uses ChatOpenAI (gpt-4o-mini)
Supports structured tool calling (LangChain v1 standard)
Handles parsing errors
Verbose mode enabled for debugging
π¦ Requirements
Install using your Python 3.10 environment:
pip install langchain==0.3.14 pip install langchain-openai==0.2.14 pip install langchain-core==0.3.29 pip install langchain-community==0.3.14 pip install tavily-python==0.5.0 pip install python-dotenv==1.0.1 pip install requests==2.32.3 pip install openai>=1.0.0 pip install pydantic>=2.0.0
Or, add everything inside requirements.txt and run:
pip install -r requirements.txt
π Environment Variables
Create a file called .env in the same folder as your script:
OPENAI_API_KEY=your-openai-key-here TAVILY_API_KEY=your-tavily-key-here
Both keys are required:
OPENAI_API_KEY β for the LLM
TAVILY_API_KEY β for web search
2οΈβ£ Run the agent python main.py
You will see:
============================================================ LangChain Agent with Tools and Memory (v1)
π Loading API keys... π€ Initializing agent... β Agent ready!
π¬ Usage (Inside the Chat)
Enter messages normally:
You: what is the time now? π€ Agent: The current time is...
β± Useful Commands Command Action quit / exit / q Stop the agent history Show recent chat history clear Clear memory π Project Structure project/ β βββ main.py # Your agent code βββ .env # API keys βββ requirements.txt # Dependencies βββ README.md # Documentation
π How the Agent Works
- Loads tools
- Loads prompt with memory placeholders
- Uses create_tool_calling_agent() (correct v1 method)
- Uses AgentExecutor to run
- Stores every human/assistant message in memory
- Trims memory to last 10 interactions
- Responds using LLM β with tool support when needed
Done !