DEV Community

Cover image for From Loops to Graphs: Building the Next Generation of AI Agents
Harish Kotra (he/him)
Harish Kotra (he/him)

Posted on

From Loops to Graphs: Building the Next Generation of AI Agents

For the past two years, "AI Agent" development has been dominated by Loop Engineering. We’ve all written the code: while (!success) { try(); retry(); }. It works, but it’s brittle, stateless, and burns through tokens with blind retries.

The industry is shifting. The next paradigm is Graph Engineering.
Today, I’m open-sourcing Rashomon, a multi-agent debate simulator built in a single weekend to demonstrate exactly why graphs are the future of agentic workflows.

The Architecture: Parallel, Conditional, Stateful

Rashomon doesn’t just ask an LLM to "debate itself." It uses @langchain/langgraph to construct a deterministic, multi-stage workflow:

  1. Parallel Fan-out: When a topic is submitted, all 4 personas (Optimist, Cynic, Pedant, Chaos) are invoked simultaneously via Promise.all. No sequential waiting.
  2. Conditional Routing: The graph evaluates the accumulated "chaos level." If it’s high, it routes to a Debate node. If low, it skips straight to Synthesis.
  3. Stateful Accumulation: A shared DebateState object tracks which agents have spoken, preventing infinite feedback loops between the same two personas.
  4. Real-time Streaming: Using Server-Sent Events (SSE), the frontend visualizes the graph building itself node-by-node via React Flow.

The Code: A Glimpse Under the Hood

Here’s how trivial it is to define conditional routing in LangGraph.js compared to writing nested if/else retry loops:

// Define the graph
const workflow = new StateGraph<DebateState>({ channels: { /* state schema */ } });

// Add specialized nodes
workflow.addNode('initial', initialResponsesNode);
workflow.addNode('debate', debateNode);
workflow.addNode('synthesis', synthesisNode);

// Conditional routing based on state
workflow.addConditionalEdges('initial', (state) => {
  return state.chaosLevel >= 2 ? 'debate' : 'synthesis';
});

// Compile and stream
const app = workflow.compile();
const stream = await app.astream(initialState);
Enter fullscreen mode Exit fullscreen mode

Why This Matters

Loops are reactive. Graphs are architectural. By explicitly defining nodes (agents), edges (routing logic), and state (memory), we get:

  • Observability: You can see exactly which node failed and why.
  • Efficiency: Parallel execution cuts latency by 60-70%.
  • Control: You dictate the flow, not the LLM’s hallucinated retry logic.

The full source code, complete with a real-time React Flow visualization and support for OpenRouter/Ollama, is available on GitHub.

Check out the repo, fork it, and let’s build the next generation of agents together: https://www.dailybuild.xyz/project/199-rashomon-debate

Top comments (0)