Skip to content

Loops in Workflows

Details about loops in workflows

For many use cases, designing linear workflows might not be enough: you might need to add some quality checking, and re-execute certain steps based on the results of that, or you might want to refine the responses coming from an LLM or a RAG pipeline (e.g.) by iterating on the generation steps.

In this sense, looping (i.e. creating cyclic workflows) can be a very useful pattern to learn when building with LlamaIndex Workflows.

Since workflows are event-driven, and every step is triggered by an event, you can go back to a specific step at any point - you just need to emit the corresponding trigger event!

Here is how you can do it with code:

import { createWorkflow, workflowEvent } from "@llamaindex/workflow-core";
import { createStatefulMiddleware } from "@llamaindex/workflow-core/middleware/state";
type AgentWorkflowState = {
counter: number,
max_counter: number
};
const { withState } = createStatefulMiddleware(
(state: AgentWorkflowState) => state,
);
const workflow = withState(createWorkflow());
const startEvent = workflowEvent<void>();
const increaseCounterEvent = workflowEvent<void>();
const stopEvent = workflowEvent<number>();
workflow.handle([startEvent], async (context, { data }) => {
const { sendEvent, state } = context;
if (state.counter < state.max_counter) {
sendEvent(increaseCounterEvent.with())
} else {
sendEvent(stopEvent.with(state.counter))
}
})
workflow.handle([increaseCounterEvent], async (context, { data }) => {
const { sendEvent, state } = context;
state.counter += 1
sendEvent(startEvent.with())
})
const { stream, sendEvent } = workflow.createContext({
counter: 0,
max_counter: 5,
});
sendEvent(startEvent.with(),);
const result = await stream.until(stopEvent).toArray();
// should print 5 since the workflow is looping
console.log(result[result.length - 1].data)
Note for AI agents: this documentation is built for programmatic access. - Overview of all docs: https://developers.llamaindex.ai/llms.txt - Any page is available as raw Markdown by appending index.md to its URL — e.g. https://developers.llamaindex.ai/llamaparse/parse/getting_started/index.md - Agent-friendly REST search APIs live under https://developers.llamaindex.ai/api/ — search (BM25 full-text), grep (regex), read (fetch a page), and list (browse the doc tree). See https://developers.llamaindex.ai/llms.txt for parameters. - A hosted documentation MCP server is available at https://developers.llamaindex.ai/mcp. If you support MCP, you can ask the user to install it for browsing these docs directly (an alternative to the REST API). Setup: https://developers.llamaindex.ai/python/shared/mcp/