Skip to main content

End of turn detection for smoother AI agent chats with Telegram and Gemini

Workflow preview

Workflow preview
100%
End of turn detection for smoother AI agent chats with Telegram and Gemini preview
Open on n8n.io

Important notice

This workflow is provided as-is. Please review and test before using in production.

1. Workflow Overview

This n8n template demonstrates one approach to achieve a more natural and less frustration conversations with AI agents by reducing interrupts by predicting the end of user utterances. When we text...

Best for

  • Support Chatbot automation workflows
  • AI Chatbot automation workflows
  • advanced n8n builders looking for reusable templates

Tools used

n8n-nodes-base.telegramtrigger, @n8n/n8n-nodes-langchain.agent, @n8n/n8n-nodes-langchain.lmchatgooglegemini, @n8n/n8n-nodes-langchain.memoryredischat, n8n-nodes-base.set, n8n-nodes-base.redis, n8n-nodes-base.if, n8n-nodes-base.noop

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Jimleuk.

Original n8n.io source

1.1 Workflow description

Title
End of turn detection for smoother AI agent chats with Telegram and Gemini
Workflow name
End of turn detection for smoother AI agent chats with Telegram and Gemini

This n8n template demonstrates one approach to achieve a more natural and less frustration conversations with AI agents by reducing interrupts by predicting the end of user utterances.

When we text or chat casually, it's not uncommon to break our sentences over multiple messages or when it comes to voice, break our speech with the odd pause or umms and ahhs. If an agent replies to every message, it's likely to interrupt us before we finish our thoughts and it can get very annoying!

Previously, I demonstrated a simple technique for buffering each incoming message by 5 seconds but that approach still suffers in some scenarios when more time is needed. This technique has no arbitrary time limit and instead uses AI to figure out when its the agent's turn based on the user's message, allowing for the user to take all the time they need.

How it works

  • Telegram messages are received but no reply is generated for them by default. Instead they are sent to the prediction subworkflow to determine if a reply should be generated.
  • The prediction subworkflow begins by checking Redis for the current user's prediction session state. If this is a new "utterance", it kicks off the "predict end of utterance" loop - the purpose of which is to buffer messages in a smart way!
  • New users message can continue to be accepted by the workflow until enough is collected to allow our prediction classifier to determine the end of the utterance has been reached.
  • The loop is then broken and the buffered chat messages are combined and sent to the AI agent to generate a response and sent to the user via the telegram node.
  • The prediction session state is then deleted to signal the workflow is ready to start again with a new message.

How to use

  • This system sits between your preferred chat platform and the AI agent so all you need to do is replace the telegram nodes as required.
  • Where LLM-only prediction isn't working well enough, consider more traditional code-based checking of heuristics to improve the detection.
  • Ideally you'll want a fast but accurate LLM so your user isn't waiting longer than they have to - at time of writing Gemini-2.5-flash-lite was the fastest in testing but keep a look out for smaller and more powerful LLMs in the future.

Requirements

  • Gemini for LLM
  • Redis for session management
  • Telegram for chat platform

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - Telegram Trigger

Type / Role
n8n-nodes-base.telegramTrigger - telegramTrigger
Config choices
Version 1.2

Block 2 - AI Agent

Type / Role
@n8n/n8n-nodes-langchain.agent - agent
Config choices
Version 2

Block 3 - Google Gemini Chat Model

Type / Role
@n8n/n8n-nodes-langchain.lmChatGoogleGemini - lmChatGoogleGemini
Config choices
Version 1

Block 4 - Redis Chat Memory

Type / Role
@n8n/n8n-nodes-langchain.memoryRedisChat - memoryRedisChat
Config choices
Version 1.5

Block 5 - Get Values

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 6 - Update Session

Type / Role
n8n-nodes-base.redis - redis
Config choices
Version 1

Block 7 - Get Session

Type / Role
n8n-nodes-base.redis - redis
Config choices
Version 1

Block 8 - Is Waiting?

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 9 - Update Session1

Type / Role
n8n-nodes-base.redis - redis
Config choices
Version 1

Block 10 - is New Session?

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 11 - Google Gemini Chat Model1

Type / Role
@n8n/n8n-nodes-langchain.lmChatGoogleGemini - lmChatGoogleGemini
Config choices
Version 1

Block 12 - Get Session2

Type / Role
n8n-nodes-base.redis - redis
Config choices
Version 1

Block 13 - Session Ref

Type / Role
n8n-nodes-base.noOp - noOp
Config choices
Version 1

Block 14 - Session Ended?

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 15 - Wait For Webhook

Type / Role
n8n-nodes-base.wait - wait
Config choices
Version 1.1

Block 16 - Update Session2

Type / Role
n8n-nodes-base.redis - redis
Config choices
Version 1

Block 17 - Is Bot command?

Type / Role
n8n-nodes-base.if - if
Config choices
Version 2.2

Block 18 - Predict End of Utterance

Type / Role
@n8n/n8n-nodes-langchain.textClassifier - textClassifier
Config choices
Version 1.1

Block 19 - Respond to User

Type / Role
n8n-nodes-base.telegram - telegram
Config choices
Version 1.2

Block 20 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 21 - Sticky Note1

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 22 - Sticky Note2

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 23 - Sticky Note3

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 24 - Sticky Note4

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Showing the first 24 of 29 workflow blocks. Download the JSON for the full node graph.

3. Summary Table

Workflow End of turn detection for smoother AI agent chats with Telegram and Gemini
Complexity advanced
Nodes 29
Categories Support Chatbot, AI Chatbot
Author Jimleuk
Published 17 Jun 2025

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/5014/5014.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does End of turn detection for smoother AI agent chats with Telegram and Gemini do?

This n8n template demonstrates one approach to achieve a more natural and less frustration conversations with AI agents by reducing interrupts by predicting the end of user utterances. When we text...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Support Chatbot, AI Chatbot use case.