LangGraph · Multi-Agent Systems · Case Study

Agents that write code. Humans who hold the keys.

A pipeline of four AI agents — planner, coder, reviewer, fixer — orchestrated as a LangGraph state machine. Every risky transition passes a human approval gate, every file passes a safety scan, and every run is checkpointed to SQLite so it can pause, resume, and remember.

LangGraph StateGraph + interrupt() 3 human-in-the-loop gates 4 guardrail layers SQLite checkpoints + memory 53 tests, zero real API calls
01The Case

Why put a state machine between an LLM and your shell?

Letting a single LLM generate and run code in one shot is fast — and unaccountable. You can't see the plan before the code, the code before it runs, or the review before you accept it. This project makes each of those moments a first-class, durable decision point.

The problem with one-shot codegen

  • No inspection point between "idea" and "executed code"
  • Dangerous patterns (eval, subprocess, network calls) run unnoticed
  • Failures loop forever or die silently — no cost ceiling
  • Every session starts from zero; lessons are never retained

What the pipeline changes

  • Plan, execution, and verdict each pause for explicit human approval
  • Deterministic AST scan flags risky code before the sandbox
  • Reflection loop capped at 3 fixes; token budget enforced per run
  • Review findings distill into lessons the planner recalls next session
02The Flow

One run, eight moves

A task enters as plain text and leaves as a reviewed, human-accepted multi-file Python project in workspace/<run_id>/.

Input guardrail

A cheap LLM classifier answers one question: is this a legitimate coding task? Malicious or off-topic requests end the run immediately.

Plan planner

A strong LLM drafts files, steps, and a test strategy — after recalling relevant lessons from previous runs out of SQLite memory.

Gate 1 — approve the plan

The graph pauses via interrupt(). The human sees the structured plan and approves or rejects before a single line is written.

Code, then scan coder

The coder returns files as a validated tool call. A deterministic AST + regex scan flags eval, subprocess, network imports, path escapes.

Gate 2 — approve execution

The human reads the actual generated files next to the safety-scan flags and decides whether the code may run in the sandbox.

Execute executor

Files land in a path-jailed workspace; pytest or the entrypoint runs in a subprocess with a stripped environment and a hard timeout.

Review & Gate 3 reviewer

Severity-ranked findings plus a pass/fail verdict. The human accepts the code, sends it to the fixer, or rejects the run.

Fix loop, then out

The fixer repairs and re-enters the scan → gate → execute → review cycle — at most 3 times. The output guardrail moderates the final summary; lessons are saved.

03The Graph

Nodes, edges, and every conditional path

This is the exact topology compiled in src/graph.py. Solid animated edges are the happy path; amber is the reflection loop; dashed red edges are conditional exits (rejections, errors, guard refusals). Diamond-labelled routers are LangGraph conditional edges.

conditional exits → END START guardrail_in INPUT CLASSIFIER · cheap LLM input_ok rejected planner PLAN + MEMORY RECALL · strong LLM error ⏸ hitl_plan GATE 1 · interrupt() · human approve reject coder MULTI-FILE TOOL CALL · strong LLM error safety_scan AST + REGEX · deterministic ⏸ hitl_exec GATE 2 · files + flags · human approve reject executor SANDBOX SUBPROCESS · path jail reviewer FINDINGS + VERDICT · strong LLM error ⏸ hitl_verdict GATE 3 · accept / fix / reject accept or fix + limit hit reject fix · iteration < 3 fixer REPAIR PER FINDINGS · strong LLM re-scan → gate 2 → re-run error guardrail_out MODERATION + SAVE LESSONS · cheap LLM END every node transition checkpointed to SQLite — pause at any ⏸ gate, restart the app, resume by thread_id
LLM agent node deterministic node ⏸ human gate (interrupt) guardrail node reflection loop conditional exit
04In Action

A real run, gate by gate

Captured live from the Streamlit app — task: “Build a CLI temperature converter (Celsius/Fahrenheit) with tests.”

SHOT 01

Task input & run history

The sidebar lists previous runs straight out of memory.db — outcome, fix count, task. Cross-session memory isn't a claim, it's on screen.

Task input screen with run history sidebar
SHOT 02 · GATE 1

Approve the plan

The graph is paused here — a durable interrupt() checkpoint. The structured plan (files, steps, test strategy) renders as JSON; nothing proceeds until a human clicks Approve.

HITL gate 1: plan approval
SHOT 03 · GATE 2

Read the code before it runs

Every generated file is inspectable, and the deterministic safety scan reports next to it. The human — not the model — decides whether this code touches the sandbox.

HITL gate 2: safety scan and file preview
SHOT 04 · GATE 3

The reviewer earns its keep

Verdict: fail — the coder shipped pure functions but no CLI entrypoint and no tests. Severity-ranked findings feed the fixer if the human chooses “Send to fixer”.

HITL gate 3: review verdict with findings table
SHOT 05

Done — and remembered

The output guardrail moderates the final summary, the run lands in the history sidebar, and high-severity findings become lessons the planner will recall on the next similar task.

Run complete with updated history
05Under the Hood

Eight agentic patterns, each with an address

Planning & Reasoning

Structured plan with files, steps, test strategy; memory lessons injected into the prompt.

src/agents/planner.py

Tool Calling

Code returned as a validated {files:[{path,content}]} tool call — retried once on parse failure.

src/agents/base.py

Agent Memory

SQLite lessons ranked by keyword overlap; runs table powers the history sidebar.

src/memory/store.py

Orchestration

One StateGraph, deterministic conditional edges — no flaky LLM router.

src/graph.py

Reflection Loop

reviewer → fixer → scan → gate → executor → reviewer, capped at 3 iterations.

route_verdict_gate

Human-in-the-Loop

Three interrupt() gates; Streamlit resumes with Command(resume=…).

hitl_* nodes

Guardrails

Input classifier, AST safety scan, iteration/token caps, output moderation.

src/guardrails/

Statefulness

SqliteSaver checkpoints every transition; any run resumes by thread_id after restart.

data/checkpoints.db
06Defense in Depth

Four guardrails, one sandbox

LayerMechanismOn breach
Input validationLLM classifier on the task before the graph proceedsRun ends with a refusal reason
Code safety scanDeterministic AST + regex: eval/exec, os.system, subprocess, network imports, path escapesFlags surface at Gate 2 — human decides
Loop & cost limitsMax 3 fix iterations + per-run token ceiling + graph recursion limitGraceful end with partial results
Output moderationLLM check on the final summary before displaySummary replaced or withheld
Execution sandboxPath-jailed workspace, stripped env, 30s timeout, output truncationFailure captured as data, fed to reviewer — never crashes the graph