Building Workflows
Create powerful AI applications by combining components.
Workflow Patterns
Simple Chain
The most basic pattern:
Input → LLM → OutputUse for: Simple Q&A, text generation
Chain with Context
Add context to improve responses:
Input → Prompt Template → LLM → Output
↑
System MessageUse for: Specialized assistants, role-playing
RAG (Retrieval-Augmented Generation)
Ground responses in your data:
Input → Retriever → Prompt → LLM → Output
↑
Vector StoreUse for: Q&A over documents, knowledge bases
Agent with Tools
Let the AI use tools:
Input → Agent → Output
↓
[Search, Calculator, Database]Use for: Complex tasks requiring external actions
Multi-Agent
Multiple specialized agents:
Input → Router → Agent A → Combiner → Output
↓
Agent BUse for: Complex workflows with different specialties
Building a RAG Pipeline
Step 1: Prepare Documents
Add document loading:
- Add Document Loader component
- Configure for your file type
- Connect to Text Splitter
- Splitter outputs document chunks
Step 2: Create Embeddings
Convert text to vectors:
- Add Embeddings component (OpenAI or local)
- Connect Text Splitter → Embeddings
- Embeddings outputs vector representations
Step 3: Set Up Vector Store
Store the vectors:
- Add Vector Store (Chroma for local)
- Connect Embeddings → Vector Store
- Vector Store is now searchable
Step 4: Build Retriever
Search for relevant content:
- Add Retriever component
- Connect to Vector Store
- Configure
k(number of results)
Step 5: Create RAG Chain
Combine retrieval with generation:
- Add Prompt Template:
Use the following context to answer:
{context}
Question: {question}- Connect: Retriever → context input
- Connect: Chat Input → question input
- Connect: Prompt → LLM → Output
Complete RAG Flow
Documents → Splitter → Embeddings → Vector Store
↓
Chat Input → Retriever ← ← ← ← ← ← ← ← ←
↓ ↓
↓ [context]
↓ ↓
→ → → → Prompt → LLM → Chat OutputBuilding an Agent
Step 1: Define Tools
Add tools the agent can use:
- Add Web Search for internet searches
- Add Calculator for math
- Add Python REPL for code execution
Step 2: Create Agent
- Add ReAct Agent component
- Connect your tools to the agent
- Connect LLM to the agent
Step 3: Configure Agent
Set agent behavior:
- Max iterations: Limit reasoning steps
- Early stopping: Stop when answer found
- Return intermediate: Show reasoning
Step 4: Wire I/O
- Connect Chat Input → Agent
- Connect Agent → Chat Output
Example Agent Flow
┌─ Web Search
│
Chat Input → ReAct Agent → Chat Output
│ ↑
├─ Calculator
│ ↑
└─ PythonBuilding Conditional Flows
Simple Branching
Route based on input:
Input → Classifier → Branch A → Merge → Output
↓
Branch BImplementation
- Add Conditional component
- Set condition:
"code" in {input} - True path: Code assistant
- False path: General assistant
Multi-Way Routing
For multiple paths:
- Use Router component
- Define categories
- Each category → different chain
Parallel Processing
Run Branches Simultaneously
For independent operations:
┌→ Analysis A →┐
Input → ┼→ Analysis B →┼→ Combiner → Output
└→ Analysis C →┘Implementation
- Split input to multiple chains
- Each chain processes independently
- Combiner merges results
Loops and Iteration
Self-Improving Responses
- Initial response from LLM
- Critique by second LLM
- Revise based on critique
- Output final version
Input → LLM₁ → Critic → [needs revision?] → LLM₂ → Output
↓
OutputVariables and State
Using Variables
Pass data between components:
- Define input variables in Prompt
- Map connections to variables
- Variables populate at runtime
State Management
For complex state:
- Use Memory components
- Store intermediate results
- Access across the flow
Best Practices
Keep It Simple
- Start with minimal components
- Add complexity incrementally
- Test at each step
Name Your Components
- Rename from defaults
- Use descriptive names
- Makes debugging easier
Document Your Flows
- Add comments to components
- Write flow descriptions
- Note any non-obvious logic
Handle Errors
- Add error handling paths
- Set timeouts appropriately
- Log for debugging
Optimize for Cost
- Use cheaper models where possible
- Cache repeated operations
- Limit token usage
Common Issues
Circular Dependencies
Problem: A → B → A creates infinite loop
Solution: Ensure flows are directed (no cycles)
Missing Connections
Problem: Component shows error, not connected
Solution: Check all required inputs have connections
Type Mismatches
Problem: Connection shows incompatible
Solution: Check output/input types match
Token Limits
Problem: Response truncated
Solution:
- Reduce context size
- Summarize long inputs
- Use larger context model