2026-06-02 15:47:49 +00:00
2026-06-02 15:47:38 +00:00
2026-06-02 15:47:44 +00:00
2026-06-02 15:47:49 +00:00

Selfcorrecting LangGraph Agent

This repository contains a small demo of a selfcorrecting LangGraph agent. The agent receives a task string, executes it via an unreliable tool, then asks an LLM to judge the result. If the judge says the result is failed, the agent retries until it reaches a maximum number of attempts.

Features

  • Unreliable tool 30% chance of raising an exception.
  • LLM judge forces the model to answer only success or failed.
  • Retry logic automatically retries until success or a maximum number of attempts.
  • LangGraph lowlevel graph with three nodes: execute_task, verify_result, handle_error.

Setup

# Optional: create a virtual environment
python -m venv venv
source venv/bin/activate   # Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Running the Agent

python agent.py

You will be prompted to enter a task. The agent will then perform the task, verify the result, and retry if necessary. Example output:

Selfcorrecting LangGraph agent demo
Enter a task: 2+2

--- Result ---
Task: 2+2
Attempts: 2
Status: success
Result: 22

Project Structure

  • agent.py main implementation.
  • README.md this documentation.
  • requirements.txt Python dependencies.

Notes

  • The LLM used is OpenAI's gpt-4o-mini. If you prefer Ollama, change the ChatOpenAI import to ChatOllama and adjust the model name accordingly.
  • The unreliable tool is a toy example; replace it with a real tool for production use.
S
Description
Экзамен: Самокорректирующийся агент
Readme 56 KiB
Languages
Python 100%