# 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](https://github.com/langchain-ai/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 variable `OPENAI_API_KEY`. --- ## 📦 Installation ```bash # 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 `ChatOpenAI` import with the appropriate wrapper and adjust the model name. --- ## 🚀 Running the Agent The main logic is in `solution.py`. Run it directly: ```bash python solution.py ``` ### What Happens? 1. The agent starts a conversation. 2. When it decides to call the `get_weather` tool, the execution pauses. 3. The tool output (e.g., `"Погода в Москва на 2024-05-28: солнечно 25°C."`) is printed to the console. 4. You are prompted to type **approve** or **reject**: - `approve`: the agent resumes with the tool result as normal input. - `reject`: the agent receives a `ToolMessage` indicating rejection and can decide what to do next. --- ## 🔧 Example Interaction ```text $ 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 `@tool` and add it to the `tools=[...]` list. - **Change prompt** – Edit `system_prompt` in `create_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!