Implement robust A/B testing for your AI agent prompts using this powerful n8n workflow. Randomly assign new chat sessions to either a baseline or alternative prompt using Supabase for persistent tracking.
Download this n8n workflow template and start using it instantly.
AI Developers and Researchers needing to benchmark LLM prompt performance.
Data Scientists implementing controlled experiments in production environments.
MLOps Engineers seeking robust testing methodologies within their automation pipelines.
Users looking for advanced examples of conditional logic within an n8n workflow.
Optimizing the performance of Large Language Models (LLMs) often requires rigorous testing of the system prompts they use. This n8n workflow provides a structured framework for conducting A/B split tests on different prompts. By integrating Supabase, the workflow ensures that once a user's session ID is assigned to a prompt variant (either 'baseline' or 'alternative'), they consistently receive that prompt throughout their conversation, ensuring clean test data.
The value of this automation lies in its ability to enable data-driven decisions regarding prompt engineering. Instead of guessing which prompt performs better, you can deploy this n8n template, measure metrics (which you can add as a subsequent step), and confirm efficacy. The automation starts with a dedicated n8n trigger for chat messages, making it seamless for integration into chat applications.
This n8n workflow begins execution when a new chat message is received by the When chat message received n8n trigger.
splittestsessions table using the unique sessionId provided by the initial n8n trigger. This checks if the user has been assigned a path previously.showalternative boolean value (50/50 split) and saving the sessionId.showalternative boolean to dynamically select the correct prompt (either baselinevalue or alternativevalue). This final prompt is used for the AI Agent.gpt-4o-mini) and Postgres Chat Memory (backed by Supabase) to generate a contextually relevant response, completing the cycle of this sophisticated n8n workflow.To successfully deploy this powerful n8n workflow, follow these setup steps:
splittestsessions containing at least two columns:sessionid (Type: text)showalternative (Type: bool)baselinevalue and alternativevalue fields with the actual system messages you wish to A/B test.This n8n workflow leverages specialized nodes for AI interaction, database management, and flow control:
When chat message received (n8n trigger):
Function: Serves as the starting point (the n8n trigger) for the automation, capturing the user's chatInput and unique sessionId.
Key Configuration: Langchain Chat Trigger type.
Define Path Values (n8n node):
Function: A Set n8n node used to store the static text variables for the A and B prompt variants (baselinevalue and alternativevalue).
Key Configuration: Defines two string variables for the test prompts.
Check If Session Exists (n8n node):
Function: Queries the Supabase splittestsessions table using the sessionId to determine if a prompt path has already been assigned.
Key Configuration: Operation set to get, filtering by sessionid.
If Session Does Exist (n8n node):
Function: Directs the flow based on the existence of a session record, ensuring new users are randomly assigned a path.
Key Configuration: Checks if an item ID exists in the incoming data from Supabase.
Assign Path To Session (n8n node):
Function: For new sessions, this Supabase n8n node inserts a record, randomly setting the showalternative field to true or false using JavaScript (Math.random() < 0.5).
Key Configuration: Operation set to insert, table splittestsessions.
Get Correct Prompt (n8n node):
Function: Determines the final system prompt by conditionally selecting the baselinevalue or alternativevalue based on the showalternative boolean.
Key Configuration: Uses an expression like {{ $json.showalternative ? alternativevalue : baselinevalue }}.
AI Agent (n8n node):
Function: The central execution node that processes the chat input using the selected prompt variant, connected to the LLM and memory components.
Key Configuration: System message dynamically set to the determined prompt variable.
OpenAI Chat Model (n8n node):
Function: Provides the computational power for the AI Agent, configured here to use a cost-effective model like gpt-4o-mini.
Postgres Chat Memory (n8n node):
* Function: Manages conversational state by storing and retrieving chat history in a PostgreSQL table, keyed by the sessionId.
Learn how to use the 'noItemsLeft' context variable within an n8n workflow to accurately detect when the Split In Batches n8n node has finished processing all items.

Deploy this powerful n8n workflow to automatically scrape GitHub for top developers, score them using a custom AI agent (Gemini), and save ranked leads to Google Sheets. Use this n8n template for talent acquisition.

Use this powerful n8n workflow to automatically sort your Gmail inbox. It utilizes the GPT-4o n8n node to classify emails as ‘Action Required’ or ‘No Action,’ applying labels and archiving messages.

Use this robust n8n workflow to automatically scrape employee demographic review data from Glassdoor using ScrapingBee, leverage OpenAI to perform statistical analysis (Z-Scores, Effect Sizes), and visualize potential workplace discrimination patterns via QuickChart. This is a powerful n8n template for HR and researchers.








































