707c35d49de681113625fd51687d3d4674c2cb1c
2
@ 7c8d02b756
2-3-tavily
@ 08e2e01b18
3
@ ddd2bdb8a9
8-deep-agents-from-scratch
@ 865926c001
human-in-the-loop-interrupt-resume
@ 3c81f16ab4
human-in-the-loop-middleware
@ 082d5fb669
llm-interrupt
@ 67ab81df8f
mcp
@ 1fbb6def58
rag
@ f3a37e6521
rag-chromadb
@ d6805973d6
structured-output-union-api
@ 993512bd88
LangGraph Reflection Agent
This project demonstrates a simple LangGraph agent that:
- Generates a concise answer to a user‑supplied question.
- Critiques the answer using an LLM.
- Rewrites the answer if the critic says needs_revision, up to a maximum number of rounds.
The agent is implemented in Python 3.10+ and uses the langgraph framework together with langchain-openai.
Features
- Draft generation – 5–10 sentence answer.
- LLM critic – returns a verdict (
okorneeds_revision) and 2–3 critique points. - Rewrite loop – rewrites the draft until the verdict is
okor the maximum number of rounds is reached. - CLI – run the agent from the command line.
Installation
# Create a virtual environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate # On Windows use `.venv\Scripts\activate`
# Install dependencies
pip install -r requirements.txt
OpenAI API Key
The agent uses OpenAI’s GPT‑3.5‑Turbo by default.
Set your API key in the environment:
export OPENAI_API_KEY="sk-..."
If you prefer to use a local LLM via Ollama, replace the langchain-openai dependency with langchain-ollama and adjust the LLM initialization in src/graph.py.
Usage
python -m src.main "Explain the difference between a tool and a resource in MCP to a student."
Optional arguments:
--max_rounds N– maximum number of rewrite rounds (default: 2).
Example:
python -m src.main "Explain the difference between a tool and a resource in MCP." --max_rounds 3
The script will print:
=== Final Answer ===
<rewritten answer>
=== Verdict ===
ok
If the final verdict is needs_revision, the critique points will also be shown.
Project Structure
src/
├── main.py # CLI entry point
├── graph.py # LangGraph graph definition
└── state.py # TypedDict for the agent state
requirements.txt
README.md
License
MIT License
Languages
Python
51.9%
JavaScript
39.3%
TypeScript
8.8%