Building AI Agents · Part 6 of 7 · Workflow Patterns, Embeddings & Events · Oct 2026

Quick answer: Workflow patterns (sequential, parallel, evaluation loops, orchestration and routing) give agents structure and reliability. Vector embeddings and reranking let agents search your own documents (RAG). Events are the units of information that flow through an agent's lifecycle, and they can be streamed to users in real time.

Workflow Patterns

Letting a model decide everything is flexible but unpredictable. Workflow patterns add structure and reliability by defining how steps fit together. There are five core patterns:

PatternHow it worksExample
Sequential processingSteps run in a chain. The output of each step becomes the input of the next.Extract invoice data, then validate it, then post it.
Parallel processingIndependent tasks run at the same time.Check a supplier's tax status, CIPC record and bank details at once.
Evaluation / feedback loopResults are checked and improved over several rounds.A reviewer agent scores a draft and the writer revises it.
OrchestrationOne component coordinates several others.A manager agent assigns work to specialist subagents.
RoutingThe model chooses which path to take based on context and intermediate results.A support agent sends billing questions to one flow and technical ones to another.

In routing, the model becomes the intelligent router. That is why context is so important (Part 4): the better the context, the better the routing decisions.

Combining these patterns gives you graph-based workflows, where each step is a node and the edges define what can happen next. Frameworks like Mastra provide workflow engines for exactly this.

Vector Embeddings and RAG

Agents often need knowledge the model wasn't trained on: your policies, product catalogue or past records. Retrieval-augmented generation (RAG) solves this by finding the relevant documents and adding them to the agent's context.

RAG relies on vector embeddings: lists of numbers that represent the meaning of a piece of text. Texts with similar meanings have embeddings that are close together, so you can search by meaning rather than exact keywords.

import { embed, embedMany, cosineSimilarity } from 'ai';

// Embed your documents once and store them
const { embeddings } = await embedMany({
  model: 'openai/text-embedding-3-small',
  values: documents,
});

// Embed the user's question and compare
const { embedding } = await embed({
  model: 'openai/text-embedding-3-small',
  value: 'What is our refund policy?',
});

const similarity = cosineSimilarity(embedding, embeddings[0]);

When you query embeddings, you measure the distance between vectors. Cosine similarity is the most common measure. The closest documents are the most relevant.

Where Embeddings Are Stored

Embeddings are stored in a vector database, or in a regular database with vector support. Popular options include PostgreSQL with the pgvector extension, Pinecone, Qdrant, Weaviate and Chroma. If you already run PostgreSQL, pgvector is often the simplest place to start.

To give an agent access to this knowledge, wrap the search in a tool, for example a "searchKnowledgeBase" tool. The agent then decides when it needs to look something up.

Advertisement

Reranking

Similarity search is fast but approximate. Reranking is a second pass that reorders the retrieved documents by how relevant they really are to the query, so the best ones go to the model first. It is especially useful in RAG systems with large document collections.

import { rerank } from 'ai';
import { cohere } from '@ai-sdk/cohere';

const { rerankedDocuments } = await rerank({
  model: cohere.reranking('rerank-v3.5'),
  documents: retrievedDocuments,
  query: 'What is our refund policy?',
  topN: 3,
});

Events

Events are the fundamental units of information flow in an agent. They represent every significant occurrence in the agent's interaction lifecycle: the user's input, each step in between (model calls, tool calls, tool results) and the final response.

Understanding events is crucial because they are the main way an agent's components interact. They also let you:

  • Stream progress to users, such as "Searching invoices…" or "Posting journal entry…", instead of making them wait.
  • Log and audit exactly what the agent did and why.
  • Trigger other parts of your system when something happens.

Events can be streamed and broadcast to front-ends with real-time technologies such as Pusher or WebSockets. If you use the Laravel AI SDK, streaming and broadcasting through Laravel's event system are built in.

What's Next

In Part 7 we look beyond a single application at the protocols that let agents, tools and users communicate across frameworks and platforms.

Cover photo: Growtika on Unsplash

Chainbook builds intelligent business systems for African professionals, and AI agents are at the heart of how we build them.