# Self‑Correcting LangGraph Agent This repository contains a minimal implementation of a **self‑correcting agent** using LangGraph. The agent: 1. Executes a user‑supplied task via an unreliable tool that may fail. 2. Uses an LLM judge to verify the result (must answer `success` or `failed`). 3. Retries until the result is judged `success` or a maximum number of attempts is reached. The implementation follows the specification from the course assignment. ## Requirements The project uses the following packages (all versions are >= 1.0.0): * `langchain>=1.0.0` * `langgraph>=1.0.0` * `langchain-openai>=0.2.0` Install them via: ```bash pip install -r requirements.txt ``` ## Usage ```bash # Run the agent for a simple arithmetic task python agent.py "2+2" ``` You can also specify the maximum number of attempts: ```bash python agent.py "2+2" --max_attempts 5 ``` The output will show the number of attempts and the final status. ## How it works * **`unreliable_tool`** – a function that evaluates the task string with a 30 % chance of raising a `ValueError`. * **`execute_task`** – calls the tool and stores the result or error. * **`verify_result`** – asks the LLM to judge the result. The LLM must reply with either `success` or `failed`. * **`handle_error`** – increments the attempt counter and resets the state for a retry. * **Graph** – a LangGraph `StateGraph` that loops between these nodes until success or the attempt limit is reached. ## Extending Replace `unreliable_tool` with any external API or function. Adjust the LLM prompt in `verify_result` to fit your domain.