Optimize Prompts with n8n, LangChain AI & Telegram

This workflow improves vague user prompts by leveraging LangChain AI for clarity and detail enhancement, then sends the refined prompts back via Telegram, streamlining prompt optimization for better automation outcomes.
executeWorkflowTrigger
agent
telegram
+3
Workflow Identifier: 2184
NODES in Use: Execute Workflow Trigger, AI Agent, Telegram, OpenAI Chat Model, Code, Simple Memory

Press CTRL+F5 if the workflow didn't load.

Learn how to Build this Workflow with AI:

Visit through Desktop for Best experience

Opening Problem Statement

Meet Sarah, a content manager juggling dozens of tasks daily. She frequently uses AI tools to generate content but often struggles with vague or unclear prompts that result in subpar outputs. This means she spends an extra 30 minutes or more each day revising AI-generated text, slowing down her workflow and increasing frustration. Sarah needs a way to automatically polish her prompts before sending them to AI models to save precious time and improve output quality.

This is exactly what the “Optimize Prompt” n8n workflow aims to solve. It refines user prompts for clarity, specificity, and instructiveness by using advanced AI — perfect for anyone looking to enhance interactions with language models or other AI services.

What This Automation Does

When this workflow runs, here’s what happens:

  • Receives a prompt input from a connected workflow or Telegram message.
  • Processes the prompt using an AI Agent powered by LangChain and OpenAI GPT-4o mini, enhancing clarity, context, and instructions.
  • Splits long outputs into Telegram-friendly message chunks to respect character limits and maintain readability.
  • Sends the refined prompt back to the user on Telegram for immediate use.
  • Maintains conversational memory with a small buffer to improve AI context over multiple requests.
  • Handles errors gracefully to ensure the workflow continues running smoothly even if Telegram encounters issues.

Overall, this saves users like Sarah valuable time daily, reduces ambiguity in AI interactions, and integrates seamlessly into broader automation systems.

Prerequisites ⚙️

  • n8n Automation Platform account (cloud-hosted or self-hosted like Hostinger’s n8n hosting option).
  • OpenAI API account with access to GPT-4o mini model for AI prompt optimization.
  • Telegram Bot account with API access to send messages back to users.
  • Configured Telegram credentials in n8n.
  • Configured OpenAI credentials in n8n.

Step-by-Step Guide to Build This Workflow

Step 1: Set the Trigger Node to Accept Inputs from Other Workflows

In your n8n editor, add the Execute Workflow Trigger node (name: “When Executed by Another Workflow”).

  • Navigation: Click + Add Node > Search “Execute Workflow Trigger”
  • Configuration: Set inputSource parameter to passthrough so it directly receives the workflow input.
  • Visual: You should see this node waiting for input from another workflow.
  • Outcome: It allows this workflow to be called programmatically with a prompt input payload.
  • Common mistake: Forgetting to set inputSource to passthrough, which prevents passing input data correctly.

Step 2: Add the LangChain AI Agent Node for Prompt Enhancement

Add the AI Agent node of type @n8n/n8n-nodes-langchain.agent.

  • Navigation: Click + Add Node > Search for “AI Agent”.
  • Configuration: In the