I need to run through the same LLM node a variable number of times with unique data input each time (start date). On the first run, the input data comes from the trigger. On the remaining runs, the input data will need to be calculated based on some output data of the immediately preceding run.
The “loop through items” node seems inappropriate for this purpose. I’m just looking to be pointed in the right direction or for a basic workflow I can follow.
Thanks in advance for your help.
Could you copy paste your workflow into the field that appears when clicking the ‘</>’ button on the forums?
There are many things you can do but am unsure what to recommend specifically for your circumstance without more context.
Some things you can do:
- Use the code node to create the items to iterate over
- Use $itemIndex to dynamically add to current position
- Use the Execute Workflow node, to perform “recursive” functions with new data
I’m trying not to post my code because it’s already complex and something I’m building for a customer. Let me describe in a bit more detail.
Trigger:
- start date: 07/03/2025
- total events: 5 (let’s assume each event lasts 10 days)
- stagger events by this number of days: 4
Generate Blank Calendar:
- using code node
- populate into spreadsheet
LLM for event 1 of 5:
- generate event 1 schedule starting on 07/03/2025
- LLM accounts for things like weekends and holidays
- output the result which is a schedule spanning 10 days
- update the spreadsheet with this schedule for event 1
- analyze the schedule
- determine/calculate “next start date” (for event 2) which is the 4th day of the output event 1 schedule so that it can be fed into LLM for event 2 of 5 as the start date
LLM for event 2 of 5:
- generate event 2 schedule starting on “next start date” from above
- this is not necessarily 07/03/2025 + 4 because of weekends and holidays, that’s why I need the output of the prior event (which has avoided inappropriate days) to serve as the input to determine the start day of event 2
- etc, etc.
If the LLM wasn’t accounting for weekends and holidays, the final schedule staggered by 4 days as per the trigger would look like this:
Event1: 07/03/2025 start date, 07/12/2025 last date (10 days)
Event2: 07/07/2025 start date, 07/16/2025 last date (10 days)
Event3: 07/11/2025 start date, 07/20/2025 last date (10 days)
Event4: 07/15/2025 start date, 07/24/2025 last date (10 days)
Event5: 07/19/2025 start date, 07/28/2025 last date (10 days)
If the LLM is accounting for weekends and holidays, the final schedule schedule staggered by 4 days as per the trigger would look like this:
Event1: 07/03/2025 start date, 07/17/2025 last date (10 days)
Event2: 07/09/2025 start date, 07/22/2025 last date (10 days)
Event3: 07/15/2025 start date, 07/28/2025 last date (10 days)
Event4: 07/21/2025 start date, 08/01/2025 last date (10 days)
Event5: 07/25/2025 start date, 08/07/2025 last date (10 days)
Regarding your suggestions:
- I know how to use the code node but I cannot calculate all the schedule days for all 5 events up front so that I can easily iterate over them because I need event 1 schedule to determine event 2 schedule, event 2 schedule to determine event 3 schedule, event 3 schedule to determine event 4 schedule, event 4 schedule to determine event 5 schedule, and then I’m done. Besides, the whole reason I’m doing this is to use the LLM to figure out a schedule for me as opposed to having to write everything in code.
- I have no idea how to use $itemindex or if it would work in this scenario.
- I thought about using the Execute Workflow node before you suggested it so this might be the best approach to take. The question here is as follows:
Event1:
Can I take the start date from the trigger, then pass it through the LLM, then return the generated schedule as well as the 4th date of event 1 schedule?
Event2:
Can I take the 4th date of event 1, then pass it through the LLM, then return the generated schedule as well as the 4th date of event 2 schedule?
Etc, etc.
1 Like
That is more helpful, but I still don’t 100% follow.
At a glance I would say yes, use the execute workflow node to recursively calculate, may be your best option. (For your current setup*)
But I do feel you are falling into the modern pitfall of “AI is the answer”, when that simplicity actually causes much more complexity.
I really don’t think an LLM is a good recommendation for determining if a date is a holiday or weekend. We’ve seen dates as a huge issue with LLMs in our uses in the past. Even with live internet access.
I’d agree calculating them would be a pain, but there are many holidays that aren’t on a specific date. But weekends for sure have a calculation to perform to determine “isDateWeekend”.
I’d rather recommend using a date/cal api or hooking into google’s self updating holiday calendar. Almost all google cal’s have access to this holiday calendar.
See here for very simple weekend (far more accurate than an LLM) calculations in pure JavaScript:
https://stackoverflow.com/questions/3551795/how-to-determine-if-date-is-weekend-in-javascript/3551826#3551826
Here for an API for global holidays:
Using google API for holidays or weekends:
https://stackoverflow.com/questions/30833844/get-holidays-list-of-a-country-from-google-calendar-api
Or if you are already using a sheet you could make a year request once a year and reference that and just compare potentially.
Alright, I’m still struggling with this, so I’ve decided to post my workflow and code.
Well, I just tried to do that but it said my post is too long (limit 32k characters or something close, my post was 40k). Is there some way for us to collaborate outside this forum?
I got it working. In summary:
Form trigger (# iterations/events, start date, # stagger days)
Code node (python: generate iterations)
Loop over items (iterations, one at a time)
Inside loop:
Code node (python: if first iteration use form trigger start date, else pull date from calculated date in Code node after LLM node)
LLM node (generate schedule per my rules)
Code node (python: store generated schedule and calculate next start date)
Execute external workflow (log schedule for this iteration to Airtable)
Wait
Return to loop start
The main lesson I learned has to do with calculating the next start date (that was easy), but then passing it “backward” from a node after the LLM to a node before the LLM. Technically since this is a loop, maybe I’m not passing it backward but forward. Either way, this was the piece I was struggling to think about. I guess the syntax for pulling values from another node doesn’t discriminate where that other node is in the workflow, which was a good lesson. Hope this helps someone else out. Cheers.