Still learning the basics, and I’m trying to work on an agent that (among other things) helps me manage my e-mails.
I’ve asked my agent to go review a handful of my e-mails and “process” them (kick off a sub-workflow to label them.)
But I’m running into an issue where my agent calls the “Get Message” API repeatedly, and eventually ends up hitting the prompt limit for my model. I’m guessing that it’s trying to retrieve all the e-mails at once, before starting to process them.
Is there a way to force the agent to process the e-mails one at a time - or is there a better pattern I should be using here?
In the node AI Agent, you can use the ‘max Iterations’ setting, which defines how many times your LLM will be called/executed. You can also limit the number of emails returned by your tool; perhaps this helps avoid reaching the limit of your LLM.
Hello @jfc ! If you want to get you can do a get all messages node, into a loop node and set the batch size to 1. If you want to make this production ready, you can use a on new email gmail node.
Hi @jfc Welcome!
What i recommend is that instead of letting AI model fetch emails for you let it have the fetched emails via the node and passing emails as a javascript parameter, with this your token limit would not exceed and this is the best practice and make sure to use a strong model like GPT-4o mini that is by far the best one for bulk data tasks , and consider using this approach to safe cost over TPMs.
An example workflow approach should be:
This will help you fix the issue with a scalable approach.
Hi @achamm that workflow is intended to read emails in bulk not to send or do anything around, read the question again, this is a good practise approach to let the AI knows the kind of emails the user is getting than to take action according to it.
Yea but he is intending to I”’ve asked my agent to go review a handful of my e-mails and “process” them (kick off a sub-workflow to label them.)” which is what i meant
your solution is good tho i was just clarfying a misconception i saw
My workflow performs the solution just removing the aggregate node would make the AI agent run for 1 email at a time and will fix the issue of AI fetching 20-30 emails at a time which also creates overhead of rate limiting as attaching Gmail tool for get action in AI agent can result in this, and also a really good practice here is to tighten up the system prompt to instruct the AI to exactly do what is is required.
I ended up doing a hybrid-ish solution. Basically, I’m having the AI agent only handle message IDs. Either:
The workflow is started from a “New Messages” GMail trigger. The incoming items are run through an Aggregate node to extract their message IDs, which are sent along with a prompt to the agent node.
The workflow is started via chat, in which case the AI agent kicks off a sub-workflow to get the desired set of messages. The sub-workflow retrieves the messages, and extracts their ID via Aggregate node, and returns them back to the agent node.
Either way, the AI agent then calls the “review” sub-workflow and passes it the list of message IDs. The sub-workflow batches the message IDs up and then retrieves/processes one batch at a time (using a couple Basic LLM Chain nodes, rather than a single AI Agent node) - so that no LLM node is ever handling the entire set of full messages all at once.