Minimal ReAct-style loop with explicit turn budget and structured exit.
from typing import TypedDict, Literal
from langgraph.graph import StateGraph, END
class DecisionState(TypedDict):
goal: str
history: list
decision: str | None
turns: int
max_turns: int
done: bool
def decide(state: DecisionState) -> DecisionState:
if state["turns"] >= state["max_turns"]:
state["decision"] = "MAX_TURNS_REACHED — escalate to human"
state["done"] = True
return state
# Call model here with structured output schema
# result = model.invoke(...)
# state["decision"] = result.decision
# state["done"] = result.confidence > 0.8 and result.actionable
state["turns"] += 1
return state
def should_continue(state: DecisionState) -> Literal["decide", END]:
return END if state["done"] else "decide"
graph = StateGraph(DecisionState)
graph.add_node("decide", decide)
graph.set_entry_point("decide")
graph.add_conditional_edges("decide", should_continue)
app = graph.compile()
# Always set max_turns (e.g. 8) and log every transition