AgentSkillsCN

02 Workflow Patterns

02 工作流模式

SKILL.md

n8n Workflow Patterns

Proven architectural patterns for building production-ready n8n workflows

Overview

This skill covers the 7 core workflow patterns that handle 95%+ of automation use cases, plus advanced patterns for scaling, error handling, and data orchestration.


The 7 Core Patterns

1. Webhook Processing (Most Common)

Pattern: Receive HTTP -> Validate -> Process -> Respond/Notify

code
Webhook -> Validate -> Transform -> Action -> Response

Use when:

  • Receiving data from external systems
  • Building integrations (Slack commands, form submissions, payment webhooks)
  • Need instant response to events

Production Implementation:

json
{
  "nodes": [
    {"type": "n8n-nodes-base.webhook", "name": "Webhook", "responseMode": "responseNode"},
    {"type": "n8n-nodes-base.if", "name": "Validate Signature"},
    {"type": "n8n-nodes-base.set", "name": "Transform Data"},
    {"type": "n8n-nodes-base.httpRequest", "name": "Process"},
    {"type": "n8n-nodes-base.respondToWebhook", "name": "Return Response"}
  ]
}

Response Mode Options:

ModeUse Case
responseNodeFull control via Respond to Webhook node
onReceivedQuick 200 acknowledgment (for async processing)
lastNodeReturn data from final node

Key Considerations:

  • Webhook data is under $json.body (not $json directly)
  • Use responseMode: "onReceived" for quick acknowledgment when processing takes time
  • Validate signatures for security (Stripe, GitHub, etc.)
  • Set appropriate timeout for long-running operations
  • Return meaningful HTTP status codes

2. HTTP API Integration

Pattern: Trigger -> Fetch API -> Transform -> Store/Act

code
Trigger -> HTTP Request -> Transform -> Database/Service -> Error Handler

Use when:

  • Fetching data from REST APIs
  • Synchronizing with third-party services
  • Building data pipelines

Pagination Pattern:

code
Schedule -> Loop Over Items -> HTTP Request (page N) -> Aggregate -> Process
         ^                                              |
         |_______________ hasMore = true _______________|

HTTP Request Best Practices:

SettingRecommendation
Retry On FailEnable for transient errors
Max Retries3-5 for external APIs
Timeout30-60 seconds
Response FormatJSON (auto-parsed)

Key Considerations:

  • Handle pagination for large datasets
  • Implement retry logic with exponential backoff
  • Use authentication credentials properly (never hardcode)
  • Handle rate limiting with Wait nodes

3. Database Operations (ETL)

Pattern: Schedule -> Query -> Transform -> Write -> Verify

code
Schedule -> Read Source -> Transform -> Write Target -> Confirm

Use when:

  • ETL workflows
  • Database synchronization
  • Data migrations
  • Backup operations

Incremental Sync Pattern:

code
Schedule -> Get Last Sync Time -> Query New Records -> Transform -> Upsert -> Update Sync Time

Key Considerations:

  • Use transactions where possible
  • Implement idempotency (check before insert)
  • Handle connection failures gracefully
  • Track sync timestamps for incremental processing
  • Use batch sizes appropriate for your data volume (10-50 items)

4. AI Agent Workflow

Pattern: Trigger -> AI Agent (Model + Tools + Memory) -> Output

code
Webhook -> AI Agent -+- Language Model
                     +- Tool 1 (HTTP Request)
                     +- Tool 2 (Database)
                     +- Memory (Buffer/Vector)
         -> Response

Use when:

  • Building conversational AI
  • Need AI with tool access
  • Multi-step reasoning tasks
  • RAG (Retrieval Augmented Generation)

AI Connection Types (8 types):

Connection TypePurposeExample Nodes
ai_languageModelLLM providerOpenAI, Anthropic, Groq
ai_toolTools the agent can useHTTP Request, Database, Custom
ai_memoryConversation memoryBuffer Window, Postgres, Redis
ai_outputParserStructure outputStructured Output Parser
ai_textSplitterChunk documentsRecursive Text Splitter
ai_documentDocument loadersPDF, HTML, JSON
ai_embeddingEmbedding modelsOpenAI Embeddings, Cohere
ai_vectorStoreVector databasesPinecone, Qdrant, Supabase

Agent Architecture Patterns:

PatternUse CaseComplexity
Single AgentSimple Q&A, basic tasksLow
ReAct AgentMulti-step reasoning with toolsMedium
Multi-AgentSpecialized agents for domainsHigh
RAG AgentKnowledge retrieval + generationMedium

Key Considerations:

  • Manage token costs (use appropriate model size)
  • Implement conversation memory for context
  • Define clear tool descriptions
  • Use streaming for better UX in chat applications
  • Consider usePlannerAgent: true for complex multi-step tasks

5. Scheduled Tasks

Pattern: Schedule -> Fetch -> Process -> Deliver -> Log

code
Schedule Trigger -> Gather Data -> Process -> Deliver -> Log Success/Failure

Use when:

  • Recurring reports or summaries
  • Periodic data fetching
  • Maintenance tasks
  • Cleanup jobs

Cron Expressions:

ScheduleCron Expression
Every hour0 * * * *
Daily at 9 AM0 9 * * *
Weekly Monday 8 AM0 8 * * 1
First of month0 0 1 * *
Every 15 minutes*/15 * * * *
Weekdays only 6 PM0 18 * * 1-5

Key Considerations:

  • Set appropriate timezone in workflow settings
  • Handle failures with error workflows
  • Log execution results for auditing
  • Consider execution overlap for long-running tasks
  • Use $now and $today for date calculations

6. Sub-Workflow Pattern (Modular Design)

Pattern: Parent Workflow -> Execute Sub-Workflow -> Process Results

code
Main Workflow -> Execute Sub-Workflow -> Aggregate Results -> Continue
                      |
                 Sub-Workflow: Input -> Process -> Output

Use when:

  • Reusing common logic across workflows
  • Breaking complex workflows into manageable pieces
  • Parallel processing of different tasks
  • Human approval workflows

n8n 2.0+ Enhancement: Sub-workflows can now return data to parent workflows after send-and-wait or approval steps. This enables:

  • Human-in-the-loop approvals
  • Pause and resume patterns
  • Multi-step verification flows

Data Passing:

DirectionMethod
Parent -> ChildExecute Workflow node parameters
Child -> ParentReturn data from last node (Output Node)
Binary DataPass via workflow execution context

Key Considerations:

  • Keep sub-workflows focused (single responsibility)
  • Use "When Executed by Another Workflow" trigger
  • Test sub-workflows independently
  • Handle errors in sub-workflows appropriately
  • Use waitForSubWorkflow: true when you need results

7. Event-Driven Architecture

Pattern: Event Source -> Queue -> Worker -> Process -> Acknowledge

code
Webhook (fast ack) -> Queue (RabbitMQ/SQS) -> Worker Workflow -> Process -> Update Status

Use when:

  • Handling bursty traffic (payments, checkouts, Git pushes)
  • Need guaranteed delivery
  • Decoupling producers and consumers
  • High-throughput scenarios

Queue Integration Options:

QueueUse Casen8n Node
RabbitMQEnterprise messagingRabbitMQ / RabbitMQ Trigger
AWS SQSCloud-native queueingAWS SQS
Redis StreamsLightweight, fastRedis
Google Pub/SubGCP integrationGoogle Cloud Pub/Sub

Key Considerations:

  • Use queue mode for production scaling
  • Implement dead letter queues for failed messages
  • Handle message acknowledgment properly
  • Consider message ordering requirements
  • Use idempotency keys to prevent duplicate processing

Data Flow Patterns

Linear Flow

code
Trigger -> Transform -> Action -> End

Use for: Simple workflows with single path

Branching Flow (IF/Switch)

code
Trigger -> IF -+- True Path -> Action A
               +- False Path -> Action B

Use for: Conditional processing based on data

Parallel Processing

code
Trigger -+- Branch 1 -+
         +- Branch 2 -+- Merge -> Output
         +- Branch 3 -+

Use for: Independent operations that can run simultaneously

Loop Pattern (Split In Batches)

code
Trigger -> Loop Over Items -> Process -> [Loop back until done] -> Continue

Use for: Processing large datasets in manageable chunks

Performance Guidelines:

Array SizeRecommended Approach
< 100 itemsDirect processing (no batching needed)
100-1000 itemsSplit In Batches (batch size 10-50)
> 1000 itemsSub-workflows with queue pattern
> 10000 itemsExternal processing or dedicated ETL

Loop Context Variables:

javascript
// Check if loop has more items
{{ $("Loop Over Items").context["noItemsLeft"] }}

// Get current iteration index
{{ $("Loop Over Items").context["currentRunIndex"] }}

Fan-Out / Fan-In

code
Trigger -> Split -> [Multiple parallel processes] -> Aggregate -> Continue

Use for: Processing items in parallel, then collecting results


Merge Patterns

Merge Node Modes

ModeDescriptionUse Case
AppendCombine outputs sequentiallyCollecting data from multiple sources
Combine by PositionMatch items by indexParallel arrays of related data
Combine by Matching FieldsJoin on key fieldDatabase-style joins
SQL QueryCustom SQL on inputsComplex data transformations
All CombinationsCartesian productGenerating permutations

Merge vs Aggregate:

NodePurpose
MergeCombine streams from different branches
AggregateCombine multiple items into single item (same branch)

Multi-Branch Merge Pattern (3+ branches):

code
Branch 1 -+
Branch 2 -+- Merge (mode: Append, inputs: 3) -> Process
Branch 3 -+

Error Handling Patterns

1. Error Workflow Pattern

code
Main Workflow (on error) -> Error Workflow -> Log + Alert -> Remediation

Setup:

  1. Create dedicated error workflow with Error Trigger
  2. Set error workflow in main workflow settings
  3. Access error data via $json.execution, $json.workflow, $json.error

2. Retry On Fail Pattern

code
Node Settings:
- Retry On Fail: true
- Max Tries: 3-5
- Wait Between Tries: 1000-5000ms

Recommended Retry Settings by Node Type:

Node TypeRetriesWait (ms)Backoff
HTTP Request3-52000Exponential
Database2-31000Linear
Email/Slack25000Linear
AI/LLM33000Exponential

3. Dead Letter Queue (DLQ) Pattern

code
Main Workflow -> On Error -> HTTP Request to DLQ Webhook -> Store Failed Item
                                    |
DLQ Workflow -> Review -> Retry or Archive

DLQ Implementation:

  1. Create DLQ workflow with Webhook trigger
  2. In main workflow's error branch, POST failed item data to DLQ webhook
  3. Store failed items in database/sheet for review
  4. Implement manual retry or scheduled reprocessing

4. Circuit Breaker Pattern

code
Check Health -> IF (healthy) -> Process -> Update Success Count
                    |
               (unhealthy) -> Skip/Fallback -> Alert

Implementation:

  • Track failure counts in external store (Redis, database)
  • Check threshold before processing
  • Auto-reset after cool-down period

5. Graceful Degradation Pattern

code
Try Primary -> On Error -> Try Fallback -> On Error -> Return Cached/Default

Production Checklist

Planning Phase

  • Identify core pattern (webhook, API, database, AI, scheduled, sub-workflow, event-driven)
  • Map data flow (input -> transform -> output)
  • Plan error handling strategy (retries, DLQ, alerts)
  • Consider scaling requirements (batching, queue mode)

Implementation Phase

  • Create workflow with appropriate trigger
  • Use descriptive node names (action_target format)
  • Keep workflows modular (5-10 nodes per workflow)
  • Configure authentication via credentials (never hardcode)
  • Add error handling (retry on fail, error workflow)
  • Implement logging for critical operations

Validation Phase

  • Validate each node configuration
  • Validate complete workflow structure
  • Test with sample data (happy path)
  • Test edge cases (empty data, errors, timeouts)
  • Test with production-like volumes

Deployment Phase

  • Review workflow settings (timeout, retry, execution order)
  • Set appropriate timezone
  • Configure error workflow
  • Activate workflow
  • Monitor first executions
  • Document workflow purpose and dependencies

Common Gotchas

1. Webhook Data Location

javascript
// WRONG - empty for webhooks
{{ $json.email }}

// CORRECT - webhook data under .body
{{ $json.body.email }}

// For query parameters
{{ $json.query.param }}

// For headers
{{ $json.headers["x-custom-header"] }}

2. Node Execution Order

Check workflow settings -> Execution Order:

  • v0: Top-to-bottom (legacy, avoid)
  • v1: Connection-based (recommended, default for new workflows)

3. Multiple Input Items

javascript
// Access first item explicitly
{{ $json[0].field }}

// Or use "Execute Once" mode in node settings

// For aggregate operations, use Aggregate node first

4. Expression Syntax

javascript
// WRONG - missing braces
$json.field

// CORRECT - expressions need double braces
{{ $json.field }}

// CORRECT - in Code node (no braces needed)
const value = $json.field;

5. Binary Data Handling

javascript
// Access binary data from previous node
{{ $binary.data }}

// Specify binary property name
{{ $binary.attachment.fileName }}

6. Merge Node Timing

The Merge node waits for ALL inputs before executing. If one branch never completes, the merge will hang. Use timeouts or ensure all branches always produce output.


Quick Start Templates

Webhook -> Slack Notification

code
1. Webhook (POST /notify)
2. Set (extract message fields)
3. Slack (post to #channel)
4. Respond to Webhook (200 OK)

Scheduled Report with Error Handling

code
1. Schedule Trigger (daily 9 AM)
2. HTTP Request (fetch analytics)
3. Code (aggregate data)
4. Email (send report)
---
Error Workflow:
1. Error Trigger
2. Slack (notify #alerts)

Database Sync (Incremental)

code
1. Schedule (every 15 minutes)
2. Postgres (get last_sync_time from state table)
3. Postgres (query records WHERE updated_at > last_sync)
4. IF (records exist)
5. MySQL (upsert records)
6. Postgres (update last_sync_time)

AI Chat Assistant with Memory

code
1. Webhook (receive message)
2. AI Agent
   +-- OpenAI Chat Model
   +-- HTTP Request Tool
   +-- Window Buffer Memory
3. Respond to Webhook (return reply)

Event-Driven Order Processing

code
Producer Workflow:
1. Webhook (receive order)
2. Validate (check required fields)
3. RabbitMQ (publish to orders queue)
4. Respond to Webhook (202 Accepted)

Consumer Workflow:
1. RabbitMQ Trigger (orders queue)
2. Process Order (inventory, payment)
3. Update Database
4. Send Confirmation Email

Best Practices Summary

Do

  • Start with the simplest pattern that solves your problem
  • Plan structure before building (use 01-workflow-architect skill)
  • Keep workflows modular (5-10 nodes max)
  • Use error handling on ALL production workflows
  • Test with realistic data volumes
  • Use descriptive node names
  • Document complex logic with Sticky Notes
  • Use sub-workflows for reusable logic

Don't

  • Build monolithic workflows (break into sub-workflows)
  • Skip validation before activation
  • Ignore error scenarios
  • Hardcode credentials in parameters
  • Deploy without testing edge cases
  • Use complex patterns when simple ones work
  • Process large datasets without batching
  • Forget to set timeouts on HTTP requests

Sources & Further Reading


Related Skills

SkillWhen to Use
01-workflow-architectStrategic planning, tool selection
03-node-configurationConfigure specific nodes
04-mcp-tools-expertFind nodes and templates
05-code-javascriptWrite Code node logic
07-expression-syntaxWrite expressions correctly
08-validation-expertValidate and fix errors
09-community-nodesCommunity packages, AI tools, custom nodes