A simple iterative research agent built with LangGraph that searches the web and refines answers automatically.
Built for Google Colab | Uses Groq (Free) | Real Web Search | No API Cost for Search
This notebook demonstrates LangGraph - a framework for building AI agents that can:
- β Loop and retry (not just linear chains)
- β Make decisions (should I search again?)
- β Refine queries automatically
- β Prevent infinite loops (max 3 iterations)
Example Flow:
Question β Search β Bad Answer? β Refine Query β Search Again β Good Answer β
- Go to groq.com
- Sign up (it's free!)
- Create an API key
- Copy it
# In Colab: Click π (Secrets) on left sidebar
# Add: Name = "GROQ", Value = "your-api-key-here"Press Runtime > Run all and watch it work!
# This happens automatically in Cell 1:
!pip install langgraph langchain_groq langchain_community -U ddgsNo OpenAI required! Uses:
- Groq β Free fast LLM (llama-3.3-70b)
- DuckDuckGo β Free web search (no API key!)
question: str # Original question
search_query: str # Refined search query
search_results: str # What we found
answer: str # Final answer
needs_more_info: bool # Should we loop?
iteration_count: int # Safety counter (max 3)analyze_questionβ Creates/refines search querysearch_webβ Searches DuckDuckGogenerate_answerβ Uses LLM to answer
START β analyze β search β generate β β‘ Decision Point
β β
ββββββ(loop)ββββ or END
The Magic: Conditional edge decides:
- If
needs_more_info == TrueANDiterations < 3β Loop back - Otherwise β Stop and return best answer
β Question: How many episodes in One Piece anime?
π Searching for: How many episodes in One Piece anime?
β
Found results (length: 1237 chars)
π€ Generating answer (iteration 1)...
β οΈ Answer incomplete, needs more research
π Looping back for more research (iteration 1/3)
π Searching for: One Piece anime total episodes count 2024
β
Found results (length: 760 chars)
π€ Generating answer (iteration 2)...
β
Answer complete!
FINAL ANSWER:
The One Piece anime has released more than 1150 episodes as of November 2024.
Total iterations: 2
question = "What are the latest AI trends?" # Your question here# In should_continue_research function:
if state["needs_more_info"] and state["iteration_count"] < 5: # Change 3 to 5llm = ChatGroq(
model="mixtral-8x7b-32768", # Faster, cheaper
# or "llama-3.3-70b-versatile" # Smarter, slower
)| Feature | LangChain | LangGraph |
|---|---|---|
| Flow | Linear (AβBβC) | Graph (loops, branches) |
| Decisions | No | Yes |
| Retries | Manual | Built-in |
| Use Case | Simple RAG | Complex agents |
Example:
- LangChain:
Question β Retrieve β Answerβ Good for simple Q&A - LangGraph:
Question β Search β Bad? β Retry β Good!β Better for complex tasks
- Research Assistant - Searches until it finds complete answer
- Customer Support - Routes questions to right department
- Code Reviewer - Analyzes β Finds issues β Suggests fixes β Loops
- Content Writer - Research β Outline β Write β Self-edit β Publish
Error: "NameError: name 'GROQ' is not defined"
- Solution: Add your Groq API key in Colab Secrets (π icon on left)
Search returns empty results
- DuckDuckGo might be rate-limiting
- Try again in a few seconds
Answer says "NEED_MORE_INFO" 3 times
- Question might be too specific or recent
- Try rephrasing the question
Found a bug? Have an improvement?
- Fork this notebook
- Make changes
- Share your version!
MIT License - Use freely in your projects!
Built with β€οΈ for beginners learning AI agents
After this, try:
- Add memory (save conversation history)
- Use multiple agents (researcher + writer)
- Add human-in-the-loop (ask user for clarification)
- Connect to your own data (RAG with iteration)