Files
2026-06-04 23:11:01 +00:00

50 lines
1.6 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# SelfCorrecting LangGraph Agent
This repository contains a minimal implementation of a **selfcorrecting agent** using LangGraph. The agent:
1. Executes a usersupplied 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.