1.6 KiB
1.6 KiB
Self‑Correcting LangGraph Agent
This repository contains a minimal implementation of a self‑correcting agent using LangGraph. The agent:
- Executes a user‑supplied task via an unreliable tool that may fail.
- Uses an LLM judge to verify the result (must answer
successorfailed). - Retries until the result is judged
successor 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.0langgraph>=1.0.0langchain-openai>=0.2.0
Install them via:
pip install -r requirements.txt
Usage
# Run the agent for a simple arithmetic task
python agent.py "2+2"
You can also specify the maximum number of attempts:
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 aValueError.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 eithersuccessorfailed.handle_error– increments the attempt counter and resets the state for a retry.- Graph – a LangGraph
StateGraphthat 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.