AI Prompt Refinement using LLMs and Google Sheets - n8n Workflow

Use this advanced n8n workflow to automatically transform basic user prompts stored in Google Sheets into professional, structured, and detailed AI prompts using multiple LLMs (Gemini, Groq) with smart retry logic.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Content creators needing consistent, high-quality AI inputs.

  • AI developers testing prompt engineering techniques.

  • Marketing teams automating copy generation based on simple ideas.

  • Technical users looking for advanced n8n templates integrating multiple LLMs and complex control logic.

Overview

This robust n8n workflow template solves the challenge of inconsistent AI output by ensuring every prompt is professionally refined before use. By leveraging a specialized n8n node powered by multiple LLMs (like Groq's Qwen and Google's Gemini), the system analyzes brief inputs originating from a Google Sheet (detected by an n8n trigger) and transforms them into detailed, structured, and complete prompts.
The core innovation is the integrated retry mechanism within this n8n workflow, which intelligently identifies missing fields and regenerates the prompt up to three times, guaranteeing the final output includes critical metadata like topic, categories, and specific modification notes. The results are systematically stored back into Google Sheets, providing a reliable audit trail and a library of optimized prompts. This is a crucial, high-value n8n workflow for anyone serious about production-grade AI automation.

How it Works


  1. Trigger & Initialization: The Google Sheets n8n trigger monitors a designated input sheet for new, unprocessed prompts. The Loop Over Items n8n node ensures sequential processing, handling one prompt at a time.

  2. Execution Control: The ExecutionController code n8n node is central to the logic. It manages execution tracking, prevents duplicate runs, and dynamically generates the specialized systemMessage required for the AI agent. It differentiates between a first-time prompt enhancement run and a focused regeneration retry.

  3. AI Enhancement: The PromptModifer n8n node, configured as a Langchain Agent, executes the refinement task. It routes the prompt and system instructions to powerful models like Gemini and Qwen (via Groq) to enhance the basic request into a detailed, structured JSON object.

  4. Parsing & Validation: The Parse AI Chat code n8n node is essential for reliability. It extracts structured data (Title, Topic, Categories, Notes) from the AI's potentially inconsistent text response, using three fallback methods for robust JSON parsing. The Switch n8n node then checks if all required metadata fields are complete.

  5. Retry Logic (Self-Correction): If required fields are missing and the execution count is below the configured limit (3), the n8n workflow automatically routes the item back to the ExecutionController for a focused regeneration attempt, asking the AI to complete the missing data.

  6. Finalization & Storage: Upon successful validation, the Finalize ModifiedPrompt DataModel n8n node prepares the data structure. The Create/Upate Modified Prompt n8n node then writes the fully optimized prompt and its rich metadata back to the result Google Sheet.

  7. Notification: A Telegram n8n node sends a final notification confirming the prompt refinement success, including a summary of the enhanced output, offering real-time visibility into the n8n workflow's operation.

Installation Guide


  1. Spreadsheet Setup: Create a Google Sheet with two tabs: one for 'Original Prompt' inputs and a second 'Modified Prompt' sheet to receive the structured, enhanced outputs. Ensure columns match the expected fields (e.g., 'Original Prompt ID', 'Topic', 'Modified Prompt', 'Improvement Notes').

  2. Credential Configuration: Set up credentials in your n8n instance for:

Google Sheets (OAuth2) for the n8n trigger and data storage.
Gemini API.
Groq API (for Qwen LLM).
Telegram API (optional, for notifications).

  1. Import and Update: Import the provided n8n workflow JSON. You must update all Google Sheets n8n nodes with your specific Spreadsheet Document ID. Additionally, update the Telegram n8n node's Chat ID for notifications.

  2. Activation: Ensure the n8n trigger is active. Test the n8n workflow by adding a simple, basic prompt to your input sheet.

Node Details


  • Google Sheets Trigger: The initial n8n trigger. It polls the designated sheet for new rows marked for processing, initiating the entire n8n workflow.

  • ExecutionController (Code): A highly customized code n8n node that acts as the prompt pipeline manager. It generates context-specific system messages for the AI and strictly limits the maximum number of attempts (3) to prevent infinite loops during regeneration.

  • PromptModifer (Langchain Agent): The core AI processing n8n node. It accepts the dynamic system message and routes the refinement task to the connected LLMs (Gemini/Groq).

  • Qwen3 - PM (Langchain Chat Groq) & Gemini - PM (Langchain Chat Google Gemini): These n8n nodes provide the generative capabilities, allowing the n8n workflow to utilize high-speed and reliable models for prompt enhancement.

  • Parse AI Chat (Code): A critical custom n8n node that uses JavaScript to extract the structured JSON response from the raw AI text output, ensuring data integrity before validation.

  • Switch: This n8n node implements the self-correction logic, routing the flow based on whether data validation passed, failed (leading to a retry back to ExecutionController), or failed definitively (leading to a Telegram error notification).

  • Create/Upate Modified Prompt (Google Sheets): The final storage n8n node. It uses the 'Original Prompt ID' to create a new record or update an existing one with the fully refined and complete prompt data.

Related n8n Workflows

Free

Nodes: 12 Nodes
Updated: December 26 2025
View all
Created by
EoCi - Mr.Eo
EoCi - Mr.Eo

Featured*