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:
Receives a message on Telegram (e.g.: “Move the latest email to Trash 2026”).
Goes through an AI Agent (using Ollama) that processes the request.
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!
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!
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.
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.
@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!