Perplexity-Style Iterative Research Agent using Google Gemini and Redis - n8n Workflow

A complex n8n workflow template that replicates advanced AI research agents (like Perplexity), using Google Gemini, native Google Search, and Redis for state management to conduct iterative, cited web research.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Markdown-formatted:
AI Developers and Automation Engineers seeking advanced n8n workflow examples.
Users needing robust research reports generated automatically with traceable citations.
Organizations that require complex looping and state management within their n8n templates.
Technical professionals looking to build deep Retrieval-Augmented Generation (RAG) pipelines using an n8n node ecosystem.

Overview

This advanced n8n workflow provides a robust solution for deep, verifiable research. Unlike simple single-query searches, this n8n template employs a recursive research methodology inspired by modern AI agents. It starts with a user question, generates initial search queries, conducts web research (leveraging the native Google Search tool within the Gemini API via an n8n node), and then performs an AI 'reflection' step. The reflection identifies knowledge gaps. If gaps exist, it generates new follow-up queries, repeating the process up to a defined maximum loop count (maxresearchloops). Crucially, it uses Redis to manage conversation state, store research history, and maintain source citations throughout the complex n8n workflow, ensuring the final output is comprehensive and fully sourced. This powerful n8n solution turns a simple user input (the n8n trigger) into a comprehensive research report.

How it Works

Markdown-formatted:


  1. Trigger and Configuration: The automation starts with the 'When chat message received' n8n trigger. This immediately initiates the process by loading key configurations (max loops, conversation ID) into the n8n workflow using the 'Configs' set n8n node.

  2. Initial Query Generation: The 'generatequery' n8n node (powered by Gemini 2.0 Flash and a structured output parser) analyzes the user prompt and generates a list of targeted search queries.

  3. Search Execution: The generated queries are split out. For each query, the 'GeminiSearch' n8n node (an HTTP Request configured to use the Gemini API with the googlesearch tool) executes a web search.

  4. Citation and Source Processing: The subsequent 'websearch' code n8n node parses the AI's response, extracts the grounding metadata, resolves short URLs, inserts citation markers into the text summary, and gathers all sources.

  5. State Persistence (Redis): Results, sources, and step records are merged and immediately pushed to Redis using dedicated Redis n8n nodes (historywebresearchresult, historysourcesgathered, etc.) to maintain a running history across the entire n8n workflow.

  6. Reflection Loop: All historical research summaries are retrieved. The 'reflection' HTTP Request n8n node sends this history back to Gemini (using the 2.5 Flash model with a strict JSON schema) to determine if the question is sufficiently answered (issufficient: true).

  7. Loop Control ('If finish'): The 'If finish' n8n node checks if the reflection was sufficient or if the maximum research loops defined in the 'Configs' n8n node have been reached.

If insufficient: The newly generated followupqueries are injected, and the n8n workflow loops back to step
3.
If sufficient: The n8n workflow proceeds to finalization.

  1. Finalization: The 'finalizeanswer' n8n node uses Gemini to synthesize the complete, high-quality answer using the full history. The 'format answer' code n8n node replaces temporary citation URLs with original source links and formats the final report, including the complete research steps taken by the n8n workflow.

Installation Guide

Markdown-formatted:


  1. Import the n8n Workflow: Copy the provided JSON and import it as a new n8n workflow template in your n8n instance.

  2. Google Gemini Credentials: Ensure you have two Google Gemini API credentials set up for the HTTP Request and Langchain nodes.

The Google Gemini Chat Model nodes require one credential (used for initial query generation and final answer synthesis).
The GeminiSearch and reflection HTTP Request n8n nodes require another credential (ensure these are correctly linked as they are used for tooling and structured output).

  1. Redis Credentials: This n8n workflow relies heavily on Redis for cross-node state management. Configure a Redis connection credential and link it to all Redis n8n nodes (historywebresearchresult, researchloop_count, etc.).

  2. Test the n8n Trigger: Activate the workflow and use the provided 'When chat message received' n8n trigger webhook (or test environment) to send a research question and observe the multi-step execution.

Node Details

Markdown-formatted:
When chat message received (n8n trigger): The starting point of the n8n workflow. It accepts a user prompt/question to begin the research process.
Configs (Set n8n node): Centralized configuration point. Key settings include numberofinitialqueries: 3 and maxresearchloops: 3.
generatequery (Langchain LLM n8n node): Uses Gemini 2.0 Flash combined with a 'Structured Output Parser' n8n node to reliably generate a list of search terms in a predictable JSON format.
GeminiSearch (HTTP Request n8n node): Crucial step where the Google Search tool is invoked. The JSON body explicitly includes "tools": [{"googlesearch": {}}], enabling the AI to retrieve real-time search results.
websearch (Code n8n node): Contains complex JavaScript logic to parse the Gemini API's grounding metadata. It handles URL resolution, citation extraction, and inserts citation markers into the text summary.
Redis n8n nodes (e.g., historywebresearchresult, researchloopcount): Used extensively throughout the n8n workflow to maintain state, particularly the research loop count and the aggregated summaries and source lists using the list push/get operations.
reflection (HTTP Request n8n node): Calls the powerful Gemini 2.5 Flash model with a precise JSON output schema defined in the request body to analyze current summaries and decide if more research is required, generating new queries if necessary.
finalize
answer (Langchain LLM n8n node): Synthesizes all gathered information from the Redis history into a coherent final answer, ensuring all citations remain intact.

Related n8n Workflows

Free

Nodes: 13 Nodes
Updated: December 26 2025
View all
Created by

Featured*