Help with Telegram + AI Agent + Gmail flow: How to direct the action correctly?

Hello everyone! I’m building an automation workflow in n8n to manage my email through Telegram, but I’m hitting a blocker with the logic and would really appreciate some guidance.

What the workflow should do:

  1. Receives a message on Telegram (e.g.: “Move the latest email to Trash 2026”).
  2. Goes through an AI Agent (using Ollama) that processes the request.
  3. I want the workflow to be able to identify the intent (whether it’s read, send, move, or delete emails) and execute the corresponding action on Gmail.

The current problem:

When the agent receives the request, it responds in descriptive/chat text format on Telegram listing the emails, but the linear workflow downstream (such as Switch nodes or search filters) doesn’t advance correctly to the actions of modifying labels (add/remove).

I have the structure divided with Gmail nodes, a JavaScript code node to extract the ID, but the agent’s behavior doesn’t always force the expected linear routing.

Question:

How can I solve this problem?

What is the best practice or recommended architecture in n8n to connect an AI Agent to Gmail conditional actions (such as Move/Remove labels) based on commands received via chat?

Basically my bot can: Read and Send Emails.

It can’t: Move/Remove Emails.

To be clear, each node I have was structured correctly. The AI Agent prompt, as well as the configuration of each node.

I’m using qwen 2.5

I’m attaching images of my current workflow structure. Thank you in advance for the help!

Describe the problem/error/question

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hello @misteral ! Is the move/remove tool erroring? Or is the tool getting called but doing nothing, or is the agent not calling the tools?

The thing is, it doesn’t advance to the “Gmail Remove” or “Gmail Move to Trash 2026” tools. Sometimes it goes as far as the “Search Filter” but then sends me a message on Telegram saying what my latest received emails were. It only reads the emails and doesn’t take any action because it doesn’t reach “Gmail Move” or “Gmail Remove”. It can only: Read and Send. But no error message appears. It thinks it did everything correctly. I’ve improved the AI Agent Prompt countless times. The same thing keeps happening.

Hi @misteral Welcome!
qwen2.5 handles the first tool call, then writes the next one out as chat text instead of emitting a structured tool call, so “Filtro de Pesquisa” runs while the addLabels and removeLabels tools never fire, and the agent reports success because nothing errored. Five tools attached to a small local model makes it worse. Change the model in the Ollama Chat Model node to qwen3:8b, that family holds the tool-calling format across multiple steps far better at the same size.
See this:

If you end up weighing a hosted model instead of a local one, this covers the options:

The root of your cause seems to be that the model is not strong enough to do the tool=calling correctly. It does the first tool call, but not the second so changing your model to a stronger one would help!

What other models do you have, or can you download a stronger ollama model?

If you can’t do a stronger ollama model, openrouter.ai has free models!

I ended up using those models. I tried all the categories but it didn’t work because it said it was overloaded. It had too many “tokens”. I tried Open Router. It didn’t work out and I went back to putting Ollama - qwen 2.5

What was the openrouter issues you were getting? Can you try a stronger ollama model?

For the tokens issue, open your gmail tool nodes, and set a limit to how many emails come back, the model is being overloaded with emails, andf it hits the token limit!

@Anshul_Namdev I tried other free models. but it said I had too many “tokens” so it wouldn’t work.. I went back to setting up Ollama..

@misteral Add this to your gmail nodes!

It should help!

Specifically, it’s the limit tab, 5 or 10, works well, ensure it’s not retrieving all of them!

Yes, I did that too @achamm. I reduced it to just 1. Still didn’t work.

What model are you using when it gives you the token issue? Your local qwen?

No. This problem with “Tokens” only appears when I use Gemini or Open AI. With Ollama - qwen 2.5 it sends a message to Telegram just to read my emails, instead of taking action.

can you try openrouter with this specific model, and screenshot any errors? nvidia/nemotron-3-ultra-550b-a55b:free

@misteral You can also try this! it won’t be able to tool call the best like frontier models, but here’s an optimized workflow for qwen 2.5!

telegram-email-agent-qwen25.json (12.5 KB)

Simplify is off in that Gmail node, so it returns the raw Gmail payload with the full base64 body instead of just the message IDs, labels and headers. A single raw message can run to tens of thousands of tokens, which is why lowering the limit to 1 changed nothing on Gemini and OpenAI. Turn Simplify on in every Gmail tool the agent can call, then retry the stronger model.

Yes, I’ll give it a try!

@misteral Rather than if statements you can add the labels as tools and the filters as tools also, but the filters u need to set urself and itll run that set tool!

@misteral

You should look for the free model