agent.pypersonalLangchainv1.0.0
LangChain: Research Assistant
Research agent with web search. Gathers info, synthesizes findings, writes reports.
Setup time: ~15 min
Model: GPT-4o
Cost: ~$0.25/day
Last updated: Mar 16, 2026
Template
agent.py
# Install: pip install langchain langchain-openai langchain-community google-search-results
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_community.tools import DuckDuckGoSearchRun
from langchain.tools import Tool
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.memory import ConversationBufferMemory
import os
# --- Config ---
llm = ChatOpenAI(model="gpt-4o", temperature=0.2)
# --- Tools ---
search = DuckDuckGoSearchRun()
tools = [
Tool(
name="web_search",
func=search.run,
description="Search the web for current information. Use for facts, news, and research.",
),
]
# --- Agent prompt ---
prompt = ChatPromptTemplate.from_messages([
("system", """You are a thorough research assistant. When given a research topic:
1. Break it into sub-questions
2. Search for each sub-question
3. Synthesize findings into a clear, structured report
4. Cite your sources with URLs when possible
5. Distinguish between facts and opinions
6. Note any conflicting information you find
Be systematic. Search multiple times to get comprehensive coverage."""),
MessagesPlaceholder(variable_name="chat_history"),
("human", "{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad"),
])
# --- Build agent ---
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
agent = create_openai_tools_agent(llm, tools, prompt)
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
memory=memory,
verbose=True,
max_iterations=10,
)
if __name__ == "__main__":
print("Research Assistant ready. Ask me to research any topic.")
while True:
query = input("\nResearch: ")
if query.lower() == "quit":
break
result = agent_executor.invoke({"input": query})
print(f"\n{result['output']}")Setup
- 1
Copy the agent.py content above.
- 2
Create a Python virtual environment and install dependencies.
- 3
Set your OPENAI_API_KEY environment variable.
- 4
Run: python agent.py
Run with LangChain
This is a Python script using LangChain. Set up a virtual environment and install dependencies.
# 1. Create a virtual environment
python -m venv venv && source venv/bin/activate
# 2. Install dependencies
pip install langchain langchain-openai langchain-community chromadb
# 3. Set your API key
export OPENAI_API_KEY="sk-..."
# 4. Save the agent.py from above and run it
python agent.pyVersion History
v1.0.0Initial releaseMar 16, 2026
Framework
LangchainRequirements
Python 3.10+
OpenAI API key
SerpAPI key
Estimated cost
~$0.25/day
on GPT-4o model
File type
agent.py
Version
v1.0.0
Updated Mar 16, 2026
You might also like
HEARTBEAT.md
Health Tracker
Daily check-in on sleep, exercise, and mood. Weekly trends.
~$0.05/day · ~3 min setup
SOUL.md
Habit Monitor
Track daily habits. Gentle reminders, streak tracking.
~$0.05/day · ~3 min setup
SOUL.md
Reading List Manager
Track books and articles. Weekly reading suggestions based on your interests.
~$0.10/day · ~3 min setup