LlamaIndex Workflows
LlamaIndex Workflows are a library for event-driven programming in JavaScript and TypeScript. It provides a simple and lightweight orchestration solution for building complex workflows with minimal boilerplate.
It combines event-driven programming, async context and streaming to create a flexible and efficient way to handle data processing tasks.
The essential concepts of Workflows are:
- Events: are the core building blocks of Workflows. They represent data that flows through the system.
- Handlers: are functions that process events and can produce new events.
- Context: is the environment in which events are processed. It provides access to the event stream and allows sending new events.
- Workflow: is the collection of events, handlers, and context that define the processing logic.
Getting Started
Section titled “Getting Started”npm i @llamaindex/workflow-core
yarn add @llamaindex/workflow-core
pnpm add @llamaindex/workflow-core
bun add @llamaindex/workflow-core
deno add npm:@llamaindex/workflow-core
First Example
Section titled “First Example”With workflowEvent and createWorkflow, you can create a simple workflow that processes events.
For example, imagine you want to create a workflow that uses OpenAI to generate a response to a user’s message.
import { OpenAI } from "openai";import { createWorkflow, workflowEvent } from "@llamaindex/workflow-core";
const main = async () => { const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, });
const startEvent = workflowEvent<string>(); const stopEvent = workflowEvent<string>();
const workflow = createWorkflow();
workflow.handle([startEvent], async (event) => { const response = await openai.chat.completions.create({ model: "gpt-4.1-mini", messages: [{ role: "user", content: event.data }], });
return stopEvent.with(response.choices[0].message.content ?? ""); });
workflow.handle([stopEvent], (event) => { console.log("Response:", event.data); });
const { sendEvent } = workflow.createContext(); sendEvent(startEvent.with("Hello, Workflows!"));};
void main().catch(console.error);
From here, the sky is the limit. You can implement branching, looping, human-in-the-loop, map-reduce, and more!
Architecture
Section titled “Architecture”Workflows are built around components like events, steps/handlers, context, and more. Learn more about these components here.
Deployment
Section titled “Deployment”Workflows are designed to be run anywhere. Deploy in a server, a lambda function, an edge runtime, or a cloudflare function!