Assistant nested function calls - large data

I am currently experimenting a little with the OpenAI API and have a c# .NET 8 program that uses the API to create an assistant and provide functions to it.

So I ask the assistant a question and it tells me what function should be called. The result of the function call has to be returned to the assistant so it can proceed.

In this way it is of course possible and even desirable that nested function calls can arise because the output of a function can serve as input for the following one.

Now I have a specific use case that I can’t really find on the internet:

I created a E-Mail assistant that should be able to answer me questions like

What were the subjects of my last 3 e-mails?

The assistant has following functions

Function Name Input Paramters
get_emails
sort_emails_descending_by_date email_list
take_email_amount email_list, amount

When I ask the assistant the specified question i expect a nested function calling like this

// get e-mails -> sort them by date descending -> take first 3 e-mails
take_email_amount(sort_emails_descending_by_date(get_emails()) ,3)

The assistant starts correctly with calling get_emails().

BUT NOW HERE’S THE ISSUE:

The result has to be passed back to the assistant so that the next function can be called.

Since I have more than 1000 emails in my mailbox, the function returns a correspondingly large amount of data, which of course brings the OpenAI API to the token limit quite quickly.

So my question is:

How do you deal with such large amounts of data in this case?

I realize that I could write a function for this case that does it all in one go so that only a single function call has to be made, but I would like to keep it as elementary as possible so that the assistant can combine basic functions.

Hi there, adding some other parameters to you functions would help: like from/to date, limit, search query etc.

But basically once you found a large piece of data, you should split the processing in either subsequent calls (slow) or parallel calls (faster) and handle it in two steps: split and process → combine and return