Skip to content

About

Production-grade stateful agentic workflows & multi-pattern orchestration engine with LangGraph: branching, self-correction, resilience retries, HITL checkpoints, and supervisor multi-agent.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

2 Commits

Folders and files

Repository files navigation

⚑ StateCraft: Production-Grade Stateful Agentic Workflows with LangGraph

Python 3.10+ LangGraph 1.2+ LangChain 1.4+ Pytest 14/14 Passing License: MIT

StateCraft is an enterprise-grade learning and portfolio project demonstrating stateful, resilient, self-healing agentic workflows using LangGraph and LangChain.

It bridges the gap between toy linear chains and production autonomous systems by implementing:

  • πŸ”€ Dynamic Branching & Conditional Routing (Intent and risk-based graph traversal)
  • πŸ”„ Self-Correction & Evaluator-Optimizer Loop (AST static validation + sandboxed unit test reflection)
  • πŸ›‘οΈ Resilience, Retries & Fallback Circuit (Transient 429/503 fault tolerance with backoff & L2 cache)
  • πŸ‘€ Human-in-the-Loop (HITL) Checkpoints (LangGraph native interrupt() and Command(resume=...) with time-travel)
  • πŸ‘‘ Hierarchical Supervisor Multi-Agent Subgraphs (Specialized Researcher, Engineer, and Auditor delegation)
  • πŸš€ Flagship Enterprise Orchestrator Engine (Autonomous software feature & security release gate)

πŸ—οΈ System Architecture

                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚     User Ticket Ingest     β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                     β–Ό
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚ 1. Triage & Risk Branching β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                     β”‚ Risk Assessment
                                     β–Ό
                             ◆───────────────◆
                            β•±    Risk Tier    β•²
                           ◆─────────┬─────────◆
                 TIER_1: Standard    β”‚    TIER_2/3: High Risk
                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                   β–Ό                                   β–Ό
     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
     β”‚       Standard Prep       β”‚       β”‚ 2. Resilient SecOps Fetch β”‚
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚   (Exponential Retries)   β”‚
                   β”‚                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                     β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ί    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚                    β”‚ 3. Spec & Code Synthesis   β”‚
  β”‚                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  β”‚                                  β–Ό
  β”‚ Fails              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ (iter < max)       β”‚   4. Evaluator-Optimizer   β”‚
  └───────────────────── (AST & Unit Test Sandbox)  β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                     β”‚ Passes
                                     β–Ό
                          ◆─────────────────────◆
                         β•± Human Signoff Needed? β•²
                        ◆────────────┬────────────◆
              TIER_1 Standard        β”‚        TIER_2/3 Elevated
                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                   β”‚                                   β–Ό
                   β”‚                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                   β”‚                     β”‚    5. HITL Checkpoint     β”‚
                   β”‚                     β”‚        interrupt()        β”‚
                   β”‚                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚                                   β”‚ Human Review
                   β”‚                                   β–Ό
                   β”‚                            ◆─────────────◆
                   β”‚      Approve / Edit       β•±   Approved?   β•²
                   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β—†β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β—†
                   β–Ό                                   β”‚ Reject
     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                     β–Ό
     β”‚ 6. Production Deployment  β”‚       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β”‚ Incident Aborted & Logged β”‚
                   β”‚                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                     β–Ό
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚ Release Live in Production β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

⚑ Quickstart

1. Zero-Cost Offline Mode (Default)

StateCraft includes a built-in Deterministic Mock LLM Engine. You can run the entire test suite, CLI, Streamlit studio, and API 100% locally with ZERO API keys or cloud costs!

# Clone and enter repository
git clone https://github.com/subasah/statecraft-langgraph.git
cd statecraft-langgraph

# Create and activate virtual environment (using uv or standard python)
uv venv .venv
source .venv/bin/activate

# Install dependencies and statecraft package in editable mode
uv pip install -e .

2. Optional: Connect Real LLMs (OpenAI / Google Gemini)

Copy .env.example to .env and set your preferred provider:

cp .env.example .env
# Set LLM_PROVIDER=openai or LLM_PROVIDER=google

πŸ› οΈ The 3 Ways to Run & Showcase

1. Interactive Streamlit Visual Studio

Launch the browser UI to visually step through graphs, trigger human interrupts, and view live state transitions:

streamlit run streamlit_app.py

Access in browser at http://localhost:8501

2. Rich Terminal CLI

Interactive terminal with colored banners, ASCII tables, and prompt runners:

python -m statecraft.cli
# Or using the installed entry point:
statecraft

3. FastAPI REST Server

Start the production headless orchestration API:

uvicorn statecraft.api:app --reload --port 8000

Interactive Swagger docs available at http://localhost:8000/docs


πŸ§ͺ Automated Test Suite

Run all 14 unit and integration tests verifying every pattern:

pytest tests/ -v

Output:

tests/test_branching.py::test_branching_security PASSED                  [  7%]
tests/test_branching.py::test_branching_technical PASSED                 [ 14%]
tests/test_branching.py::test_branching_billing PASSED                   [ 21%]
tests/test_branching.py::test_branching_general_fast_path PASSED         [ 28%]
tests/test_enterprise_pipeline.py::test_enterprise_pipeline_standard_track PASSED [ 35%]
tests/test_enterprise_pipeline.py::test_enterprise_pipeline_critical_track_with_hitl_signoff PASSED [ 42%]
tests/test_human_in_the_loop.py::test_hitl_low_risk_auto_approves PASSED [ 50%]
tests/test_human_in_the_loop.py::test_hitl_high_risk_pauses_and_resumes_with_approval PASSED [ 57%]
tests/test_human_in_the_loop.py::test_hitl_resumes_with_human_edits_mutating_state PASSED [ 64%]
tests/test_resilience.py::test_resilience_flaky_service_recovers PASSED  [ 71%]
tests/test_resilience.py::test_resilience_down_service_trips_circuit_breaker PASSED [ 78%]
tests/test_self_correction.py::test_self_correction_recovers_and_passes PASSED [ 85%]
tests/test_self_correction.py::test_self_correction_bounds_iteration_limit PASSED [ 92%]
tests/test_supervisor.py::test_supervisor_orchestrates_all_specialists PASSED [100%]
============================== 14 passed in 1.19s ==============================

πŸ“š Complete Documentation Catalog


πŸ“ Repository Structure

β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ index.html                 # Interactive Web Documentation Portal
β”‚   β”œβ”€β”€ 01_HIGH_LEVEL_ARCHITECTURE.md
β”‚   β”œβ”€β”€ 02_LOW_LEVEL_ENGINEERING.md
β”‚   β”œβ”€β”€ 03_REUSABLE_PATTERNS.md
β”‚   └── 04_RECRUITER_INTERVIEW_CHEATSHEET.md
β”œβ”€β”€ src/
β”‚   └── statecraft/
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ state.py               # TypedDict schemas and append reducers
β”‚       β”œβ”€β”€ llm.py                 # Multi-provider LLM adapter (Mock, OpenAI, Google)
β”‚       β”œβ”€β”€ patterns/
β”‚       β”‚   β”œβ”€β”€ dynamic_branching.py       # Pattern 1
β”‚       β”‚   β”œβ”€β”€ self_correction.py         # Pattern 2
β”‚       β”‚   β”œβ”€β”€ resilience_retries.py      # Pattern 3
β”‚       β”‚   β”œβ”€β”€ human_in_the_loop.py       # Pattern 4
β”‚       β”‚   └── hierarchical_supervisor.py # Pattern 5
β”‚       β”œβ”€β”€ orchestrator/
β”‚       β”‚   └── enterprise_pipeline.py     # Flagship Unified Engine
β”‚       β”œβ”€β”€ cli.py                 # Rich Terminal CLI
β”‚       └── api.py                 # FastAPI REST Endpoints
β”œβ”€β”€ tests/                         # Pytest test suite (14 passing tests)
β”œβ”€β”€ streamlit_app.py               # Interactive Streamlit Visual Playground
β”œβ”€β”€ pyproject.toml                 # Package configuration
β”œβ”€β”€ requirements.txt               # Pinned dependencies
β”œβ”€β”€ Makefile                       # One-command CLI targets
└── README.md

πŸ’Ό Recruiter Talking Points (Quick Summary)

  • Why LangGraph over standard chains? Real reasoning is non-linear and requires cycles (Evaluator-Optimizer). LangGraph is a state machine that natively supports cyclic graphs, persistent checkpoints, and time travel.
  • How does Human-in-the-Loop work without blocking threads? Using LangGraph 1.2+'s interrupt(), the graph serializes state to storage (Postgres/Redis/Memory) and yields execution immediately. When a reviewer submits a verdict via Web UI or API, Command(resume=decision) awakens the thread at the exact checkpoint.
  • How do you avoid infinite loops in self-correction? State tracks both error critiques and an iteration counter. Conditional edges enforce iterations < max_iterations, safely diverting to human escalation if the budget is exhausted.

About

Production-grade stateful agentic workflows & multi-pattern orchestration engine with LangGraph: branching, self-correction, resilience retries, HITL checkpoints, and supervisor multi-agent.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages