AI Prompt Optimization and Telegram Delivery using GPT-4o-mini - n8n Workflow

Use this powerful n8n workflow to automatically enhance vague AI prompts into highly specific, optimized instructions using GPT-4o-mini. Delivers the refined prompt via Telegram.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

-
Developers or researchers who frequently use AI models and need consistently high-quality, detailed prompts.
Automation specialists looking for advanced n8n templates that integrate custom code with AI services.
Users of n8n who want to build modular n8n workflows for prompt engineering.
Anyone needing reliable messaging delivery for long AI outputs.

Overview

Achieving precise results from modern LLMs often requires highly detailed and structured prompts. This n8n workflow solves the problem of vague user input by acting as a 'Prompt Enhancer.' Utilizing the powerful GPT-4o-mini model within an n8n node structure, it automatically takes a simple, initial prompt and rewrites it according to strict quality guidelines (e.g., adding context, specifying format, defining length). This specific n8n workflow is designed to be a subprocess, allowing it to be easily integrated into larger applications. Crucially, the final optimized prompt is robustly delivered via Telegram, using a custom code n8n node to ensure even very long outputs are correctly chunked and formatted to respect Telegram's character limits. This makes it an invaluable addition to any advanced n8n automation toolkit.

How it Works

This modular n8n workflow is designed to be executed by a parent n8n workflow or a preceding n8n trigger (such as a Telegram message trigger).


  1. Input Trigger: The process begins with the 'When Executed by Another Workflow' n8n trigger, which receives the original prompt query ($json.query) and the recipient's Telegram chat ID (chatid) from the calling n8n workflow.

  2. Language Model Setup: The 'OpenAI Chat Model' n8n node sets the language engine, specifically configured here to use gpt-4o-mini for cost-effective and high-quality processing.

  3. AI Enhancement: The core logic resides in the 'AI Agent' n8n node. This agent uses a highly prescriptive system message to analyze the initial prompt. The system message instructs the AI to enhance the prompt by ensuring clarity, providing context, defining format, and avoiding vague language, resulting in a superior optimized prompt.

  4. Chunking and Formatting: The output from the AI Agent is passed to the 'Split into chunks1' custom code n8n node. This node is critical: it formats the optimized text using Markdown and splits the content into multiple messages, ensuring each chunk respects the Telegram character limit (3072 characters). This prevents message truncation.

  5. Delivery: Finally, the 'Telegram3' n8n node iterates through the generated text chunks and sends the complete, optimized prompt back to the original user's chatid provided by the initial n8n trigger. This completes the n8n automation cycle.

Installation Guide

To utilize this prompt optimization n8n workflow, follow these steps:


  1. Import: Copy the entire n8n workflow JSON provided and paste it into your n8n instance using the 'New' -> 'Import from JSON' function.

  2. OpenAI Credential Setup: Locate the 'OpenAI Chat Model' n8n node. Click on the credentials section and set up or select your OpenAI API key or a compatible Litellm endpoint configured for GPT models.

  3. Telegram Credential Setup: Locate the 'Telegram3' n8n node. Configure the 'Telegram API' credential by providing your Bot Token. Ensure the bot has the necessary permissions to send messages to the intended chat IDs.

  4. Integration: Since this uses an 'Execute Workflow Trigger' n8n trigger, you must call this workflow ID from another parent n8n workflow, passing the necessary query (the prompt to be optimized) and chat_id parameters.

  5. Activation: Save the n8n workflow and set the status to 'Active' to enable the n8n automation.

Node Details

-
When Executed by Another Workflow (n8n trigger):
Function: Serves as the entry point, allowing this to function as a modular subroutine within a larger n8n workflow structure. It receives the input prompt (query) and the destination chat ID.
Key Configuration: Input Source set to passthrough.
OpenAI Chat Model (n8n node):
Function: Defines the AI model used for prompt enhancement.
Key Configuration: Model selected is gpt-4o-mini.
AI Agent (n8n node):
Function: Executes the core prompt engineering logic based on a highly detailed system prompt to refine user input.
Key Configuration: The system message specifies requirements for clarity, format, specific details, and markdown output.
Split into chunks1 (n8n node - Code):
Function: Custom JavaScript code to handle post-processing. It splits the potentially large optimized prompt into smaller chunks (max 3072 chars) compatible with Telegram limits, ensuring reliable delivery. It also adds Markdown formatting.
Telegram3 (n8n node):
Function: Sends the optimized, chunked prompt messages to the specific recipient identified by the input chatId.
Key Configuration: Text uses an expression referencing the chunked output ({{ $json.text }}), and ChatId is dynamically pulled from the initial n8n trigger input.

Related n8n Workflows

Free

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

Medical doctor and self-taught IT-enthusiast.

Featured*