LlamaIndex Workflows Event-Driven Document Agents: A Practical Guide

LlamaIndex Workflows turn document pipelines into typed event graphs: extract, grade, retry, and add a human gate. This guide covers steps, events and testing.

By Galactic News Editorial · Published 2026-11-27T23:22:13+00:00 · Updated 2026-11-27T23:22:13+00:00

LlamaIndex Workflows event-driven document agents pipeline diagram

LlamaIndex Workflows Event-Driven Document Agents: A Practical Guide

LlamaIndex Workflows event-driven document agents solve a real problem. A plain query engine runs in a straight line: retrieve, then synthesise. Document work is rarely a straight line. A contract needs extraction, then grading, then a retry if the grade fails. An invoice needs parsing, then validation, then a human check. Workflows give you that shape. Steps communicate through events. The runtime routes each event to the steps that accept it. This guide shows how the pattern works and when to use it.

Workflows are the engine behind LlamaIndex agents. The prebuilt agents are workflows themselves. You can also use the framework on its own, without any agent. That is the use case here: a document pipeline with clear stages, typed data between stages, and a human gate where it matters.

How LlamaIndex Workflows event-driven document agents work

A workflow is a class. Its methods are steps, marked with the @step decorator. Each step receives an event and returns another event. The engine infers the graph from the type annotations. No edges need declaring. It checks the graph before running. An emitted event that nobody receives raises a validation error. An unreachable step raises one too.

Three events are built in. StartEvent begins the run with the arguments passed to run(). StopEvent ends the run and carries the result. Custom events are Pydantic models that subclass Event. They carry data between steps. A step that may emit several kinds of event declares a union return type.

State lives in the Context. A step that declares a ctx: Context parameter gets the run's context. It holds a store shared by all steps. It also offers methods for sending, collecting and streaming events. The context serialises to a dictionary. A workflow can pause, save and resume later. That is how human validation steps work.

Running is simple. Call run() and it returns a handler at once. Subscribe to the event stream with stream_events(). Then await the result. Steps publish progress into the stream with ctx.write_event_to_stream. The timeout parameter bounds the total duration. A workflow that loops forever gets interrupted.

Building a document pipeline step by step

  1. Install the package. Run pip install llama-index-workflows==2.26.1 for the standalone engine, or pip install llama-index-core==0.14.26 for the full framework. The core package re-exports the workflow classes. Pin the version. The API moved between releases, and older tutorials use names that no longer exist.
  2. Define the events. Create one event per handoff. A document pipeline might use ExtractedEvent with the parsed fields, GradedEvent with a quality score, and ApprovedEvent with the reviewer's decision. Keep each event small and typed.
  3. Write the steps. Each step does one job. One step extracts text from the document. One step retrieves related passages. One step grades the extraction. One step synthesises the answer. Each step declares its input event and its output events in the type hints.
  4. Add the retry loop. The grading step returns a union type. If the grade passes, it emits the synthesis event. If it fails, it emits a retrieval event with a refined query. The loop runs again. Bound it with a counter in the context store so it cannot spin forever.
  5. Add the human gate. Use an input-required event to pause before a high-stakes output. The workflow waits for a human response, then resumes from the saved context. Nothing is lost while it waits.
  6. Stream progress. Publish milestones into the event stream so the interface shows retrieval, grading and synthesis as they happen. Users tolerate slow pipelines. They do not tolerate silent ones.

Here is the shape of a minimal workflow. It shows the imports, one step and the run call. Adapt it to your own events and steps.

import asyncio
from workflows import Workflow, step
from workflows.events import StartEvent, StopEvent

class HelloFlow(Workflow):
    @step
    async def my_step(self, ev: StartEvent) -> StopEvent:
        return StopEvent(result="done")

async def main():
    w = HelloFlow(timeout=10, verbose=False)
    result = await w.run()
    print(result)

asyncio.run(main())

The same classes are available through the core package. Import from llama_index.core.workflow instead of workflows if you already use LlamaIndex. The standalone package and the core re-export are the same classes. Pick one import path and use it consistently.

Loops, branches and parallel steps

Branching comes from union return types. A step that returns SynthesizeEvent | RetrieveEvent | StopEvent can go three ways. Returning to an earlier event creates a loop. That loop is how corrective retrieval works: grade the context, and if it is weak, search again with a better query.

Parallel steps come from shared input events. Two steps that accept the same event type run concurrently. A document pipeline can run a vector search and a keyword search at the same time. A later step collects both results and merges them. The engine handles the fan-out and the join.

Error handling has its own tools. Steps can declare retry policies. A failed retrieval can retry with a delay. A failed extraction can fall back to a simpler parser. The workflow package also supports checkpointing, so a long run can resume after a crash instead of starting over.

Workflows versus query engines and agents

ApproachControl flowBest forWeakness
Query engineLinear: retrieve, then synthesiseSimple question answeringNo retries, no branches
WorkflowsEvent-driven graph with loopsDocument pipelines with grading and reviewMore code to write and test
Prebuilt agentsModel decides the next stepOpen-ended tasks with unknown stepsLess predictable, harder to audit

Choose the query engine when the question is simple. Choose workflows when the process is known but has branches, retries or human gates. Choose an agent when the steps themselves are unknown. A document pipeline usually has known steps. That makes it a workflow job.

Testing matters more with workflows than with linear code. Each step can be tested in isolation by feeding it an event and checking the event it returns. The graph can be drawn with draw_all_possible_flows to check the shape before running. Use both. A workflow that looks right in code and wrong in the diagram has a bug in the type hints.

Bottom line

Workflows turn a document pipeline into a typed, testable graph. Steps receive events and emit events. The engine routes, runs and validates the graph. Loops handle retries. Human gates handle judgement calls. Streaming keeps the interface honest. Start with a three-step pipeline: extract, grade, synthesise. Add the retry loop next. Add the human gate last. That order keeps the system working at every stage.