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()andCommand(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)
ββββββββββββββββββββββββββββββ
β 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 β
ββββββββββββββββββββββββββββββ
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 .Copy .env.example to .env and set your preferred provider:
cp .env.example .env
# Set LLM_PROVIDER=openai or LLM_PROVIDER=googleLaunch the browser UI to visually step through graphs, trigger human interrupts, and view live state transitions:
streamlit run streamlit_app.pyAccess in browser at http://localhost:8501
Interactive terminal with colored banners, ASCII tables, and prompt runners:
python -m statecraft.cli
# Or using the installed entry point:
statecraftStart the production headless orchestration API:
uvicorn statecraft.api:app --reload --port 8000Interactive Swagger docs available at http://localhost:8000/docs
Run all 14 unit and integration tests verifying every pattern:
pytest tests/ -vOutput:
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 ==============================
- π Interactive Web Documentation Portal: Browser-based portal with rendered Mermaid diagrams, code walkthroughs, and an interactive state simulator.
- π 01. High-Level Architecture Guide: Core theory, why stateful graphs beat linear DAGs, and system trade-offs.
- π¬ 02. Low-Level Engineering Guide: State schemas, channel reducers (
Annotated[list, append_list]), checkpointer internals, and moderninterrupt()mechanics. - π¦ 03. Reusable Patterns Catalog: Deep dive into all 5 orchestration patterns, failure modes, and best practices.
- πΌ 04. Recruiter & Interview Cheat Sheet: 60-second elevator pitch, STAR method interview questions, and system design answers.
βββ 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
- 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.