Human‑in‑the‑Loop Agent with LangGraph
This repository contains a minimal example of a Human‑in‑the‑Loop (HITL) agent built on top of LangGraph.
The agent pauses whenever it needs to call an external tool, presents the tool output to the user and waits for a decision (approve or reject). After the decision is made the conversation continues automatically.
⚠️ Prerequisites – The example uses OpenAI‑compatible APIs.
Make sure you have an API key set in the environment variableOPENAI_API_KEY.
📦 Installation
# 1️⃣ Clone the repo (or copy solution.py into a new folder)
git clone https://github.com/your-username/hitl-langgraph.git
cd hitl-langgraph
# 2️⃣ Create and activate a virtual environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate # Windows: .\.venv\Scripts\activate
# 3️⃣ Install dependencies
pip install --upgrade pip
pip install langchain-openai langgraph langchain-tools
Tip
– If you want to use a different LLM (e.g., Anthropic, Gemini), replace the
ChatOpenAIimport with the appropriate wrapper and adjust the model name.
🚀 Running the Agent
The main logic is in solution.py.
Run it directly:
python solution.py
What Happens?
- The agent starts a conversation.
- When it decides to call the
get_weathertool, the execution pauses. - The tool output (e.g.,
"Погода в Москва на 2024-05-28: солнечно 25°C.") is printed to the console. - You are prompted to type approve or reject:
approve: the agent resumes with the tool result as normal input.reject: the agent receives aToolMessageindicating rejection and can decide what to do next.
🔧 Example Interaction
$ python solution.py
Agent: Какую погоду вы хотите узнать?
User: Москва на 2024-05-28
Agent (calling tool): get_weather(city='Москва', date='2024-05-28')
Tool output: Погода в Москва на 2024-05-28: солнечно 25°C.
Please type 'approve' or 'reject': approve
Agent: Спасибо! Как ещё могу помочь?
If you type reject, the agent will receive a rejection message and can, for example, ask for clarification.
📁 Project Structure
| File | Purpose |
|---|---|
solution.py |
Full implementation of the HITL agent. |
README.md |
This documentation file. |
🛠️ Customization
- Add more tools – Decorate any function with
@tooland add it to thetools=[...]list. - Change prompt – Edit
system_promptincreate_react_agent. - Persist memory – Replace
MemorySaver()with a file‑based or database checkpoint if you need persistence across runs.
📜 License
MIT © 2026. Feel free to fork and adapt for your own projects!