0
0
LangChainframework~10 mins

Human-in-the-loop with LangGraph in LangChain - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Human-in-the-loop with LangGraph
Start: User Input
LangGraph Receives Input
LangGraph Processes Input & Generates Query
Human Review & Feedback
Approve
Execute Query
Back to Human Review
Return Results
End
The flow starts with user input, LangGraph processes it, then a human reviews and approves or requests changes before execution and returning results.
Execution Sample
LangChain
from typing import TypedDict
from langgraph.graph import StateGraph, END
from langgraph.checkpoint.memory import MemorySaver

class State(TypedDict):
    input: str
    query: str
    results: str | None

def generate_query(state: State) -> State:
    state["query"] = "SELECT * FROM sales WHERE date > DATE_SUB(NOW(), INTERVAL 1 MONTH)"
    return state

def execute_query(state: State) -> State:
    state["results"] = f"Results from executing: {state['query']}"
    return state

workflow = StateGraph(State)
workflow.add_node("generate", generate_query)
workflow.add_node("execute", execute_query)
workflow.set_entry_point("generate")
workflow.add_edge("generate", "execute")
workflow.add_edge("execute", END)

app = workflow.compile(checkpointer=MemorySaver(), interrupt_before=["execute"])

config = {"configurable": {"thread_id": "1"}}
app.invoke({"input": "Find recent sales data"}, config)

# Human review
state = app.get_state(config).values
query = state["query"]
if human_approves(query):
    results = app.invoke(None, config)["results"]
This code shows LangGraph processing input to generate a query, interrupting for human approval via get_state/update if needed, then resuming execution with invoke(None).
Execution Table
StepActionInput/StateOutput/StateNotes
1Receive user input'Find recent sales data'Input storedUser input captured by LangGraph
2Process inputInput storedGenerated query: 'SELECT * FROM sales WHERE date > DATE_SUB(NOW(), INTERVAL 1 MONTH)'LangGraph creates query from input
3Human reviewGenerated queryApproved or modification requestedHuman checks query via app.get_state(config)
4If approvedApproved queryExecute queryResume with app.invoke(None, config)
5If modification requestedModification feedbackQuery updatedLangGraph updates via app.update_state(config, updates)
6Return resultsQuery executedResults returned to userFinal output delivered
7EndResults returnedProcess completeCycle ends
💡 Process ends after results are returned and user is satisfied.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
user_inputNone'Find recent sales data''Find recent sales data''Find recent sales data''Find recent sales data'
queryNone'SELECT * FROM sales WHERE date > DATE_SUB(NOW(), INTERVAL 1 MONTH)''Approved' or 'Needs change''Updated query if changed''Final approved query'
resultsNoneNoneNoneNone'Query results data'
Key Moments - 3 Insights
Why does the human need to review the query before execution?
Because LangGraph generates queries automatically, human review ensures the query matches the user's intent and avoids errors, as shown in step 3 of the execution_table.
What happens if the human requests a change to the query?
The query is updated based on feedback and sent back for review again, creating a loop between steps 3 and 5 until approval, as seen in the execution_table rows 3 and 5.
Why is the process considered 'human-in-the-loop'?
Because a human actively participates in reviewing and approving or modifying the automated query before execution, ensuring accuracy and control, demonstrated by the decision branch after step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after step 2?
AUser input stored
BResults returned to user
CGenerated query: 'SELECT * FROM sales WHERE date > DATE_SUB(NOW(), INTERVAL 1 MONTH)'
DQuery executed
💡 Hint
Check the 'Output/State' column for step 2 in the execution_table.
At which step does the human decide to approve or request changes?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table where human review happens.
If the human requests a change, which step updates the query?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Refer to the 'Action' and 'Output/State' columns for step 5 in the execution_table.
Concept Snapshot
Human-in-the-loop with LangGraph:
- User inputs request
- LangGraph generates query
- Human reviews and approves or requests changes
- Approved query executes
- Results returned
- Loop continues until satisfied
Full Transcript
This visual execution trace shows how LangGraph works with a human in the loop. First, the user inputs a request. LangGraph processes this input and generates a query. Then, a human reviews the query to ensure it matches the intent. If approved, the query executes and results are returned. If changes are needed, the human requests modifications, and LangGraph updates the query. This loop continues until the human approves the final query, ensuring accuracy and control over automated query generation.