Should I include previous tool call returns in the chat history?

I’m building a chatbot to help users book appointments, which involves multiple tool calls. I’m wondering whether I should include the previous tool call returns in the chat history for the subsequent prompts or just retain the user and assistant messages and re-query the necessary data.

Here’s an example of a standard flow:

Example Flow:

  1. User: Does Service A have availability at 5 PM?

  2. Assistant: Tool call + tool return (querying service ID, staff availability, etc., resulting in long outputs).

Assistant: Yes, there’s availability.

  1. User: Great, please book it for me.

  2. Assistant: Tool call + tool return (booking the service using the previous data or re-querying).

Assistant: Your appointment is booked.

My Question:

For step 4, when responding to the user’s second prompt:

Option 1: Should I include the tool call results from step 2 in the chat history (even though the outputs can be very long)?

Option 2: Should I exclude the tool call results and rely on re-querying the required data?

I’m concerned about performance, especially when chat histories and tool call outputs grow longer. Which approach tends to work better for managing context while ensuring efficient performance?

Let’s make a lengthy look at the internal dialog that might happen, since multiple internal iterations are possible.

Below is an expanded version of the initial exchange, detailing the intermediate steps where the assistant interacts with the scheduler functions you may have.

Following that, I’ve added a new user request to move the appointment an hour earlier, because the user changes their mind. Read through, and see the prior knowledge of tool use and tool return that would be needed:

  • Does the AI know still the initial desired time to extrapolate “an hour earlier”?
  • Is an appointment available, will the AI delete the original and make a new one? Can it “delete” if the information is lost?
  • Does the AI need to know information about the success and appointment ID in order to make the change?
  • Does the AI already have a listing of the day’s other appointments from a prior tool return?

Such an additional request is contextually-dependent. Not having enough information about tools, or the tools not showing usage, can make the AI model then think that it has the ability to schedule appointments without invoking a tool, or there may be loss of useful information, such as a previous document search with a troubleshooting database answer that may continue to be needed for reference.

It is also good to not force the AI to make repeated calls, as each multi-step tool invocation is paying for the chat history multiple times, with delays.

There is less need to manage the context window length to be short with new models, but budget and attention without distraction is a concern. You can look at how long the interactions in your application must have perfect understanding of the past, and answer this for yourself.

Additionally, context caching may offer a 50% discount on all past input that is identical. Deleting chat turns produces a different input sequence, breaking your discount when the chat history differs.


Initial Appointment Inquiry and Booking

Turn 1: User Inquiry

User:
Does Service A have availability at 5 PM?


Turn 2: Assistant Processes the Inquiry

Assistant:
Initiates a function call to check availability.

Message to functions.scheduler:

{
  "action": "search",
  "service": "Service A",
  "datetime": "2024-04-27T17:00:00"
}

Turn 3: Scheduler Function Returns Availability

Tool (functions.scheduler) Response:

{
  "available": true,
  "service_id": "service_a_123",
  "staff_available": ["staff_1", "staff_2"]
}

Turn 4: Assistant Responds to User

Assistant:
Yes, there’s availability for Service A at 5 PM.


Turn 5: User Requests Booking

User:
Great, please book it for me.


Turn 6: Assistant Processes the Booking

Assistant:
Initiates a function call to book the service.

Message to functions.scheduler:

{
  "action": "book",
  "service_id": "service_a_123",
  "datetime": "2024-04-27T17:00:00",
  "client_info": {
    "name": "John Doe",
    "contact": "john.doe@example.com"
  },
  "staff_assigned": "staff_1"
}

Turn 7: Scheduler Function Confirms Booking

Tool (functions.scheduler) Response:

{
  "success": true,
  "appointment_id": "appt_456",
  "confirmation_message": "Your appointment for Service A at 5 PM has been booked successfully."
}

Turn 8: Assistant Confirms to User

Assistant:
Your appointment for Service A at 5 PM has been booked successfully. Your appointment ID is appt_456.


New Request: Moving Appointment an Hour Earlier

Turn 9: User Requests to Reschedule

User:
I’d like to move my appointment an hour earlier.


Turn 10: Assistant Processes the Rescheduling Request

Assistant:
Retrieves existing appointment details.

Message to functions.scheduler:

{
  "action": "retrieve",
  "appointment_id": "appt_456"
}

Turn 11: Scheduler Function Returns Current Appointment Details

Tool (functions.scheduler) Response:

{
  "appointment_id": "appt_456",
  "service_id": "service_a_123",
  "datetime": "2024-04-27T17:00:00",
  "client_info": {
    "name": "John Doe",
    "contact": "john.doe@example.com"
  },
  "staff_assigned": "staff_1"
}

Turn 12: Assistant Checks Availability for New Time

Assistant:
Calculates the new desired time (one hour earlier: 4 PM). Initiates a function call to check availability.

Message to functions.scheduler:

{
  "action": "search",
  "service": "Service A",
  "datetime": "2024-04-27T16:00:00"
}

Turn 13: Scheduler Function Returns Availability for New Time

Tool (functions.scheduler) Response:

{
  "available": true,
  "service_id": "service_a_123",
  "staff_available": ["staff_1", "staff_2"]
}

Turn 14: Assistant Initiates Rescheduling

Assistant:
Initiates a function call to update the appointment time.

Message to functions.scheduler:

{
  "action": "update",
  "appointment_id": "appt_456",
  "new_datetime": "2024-04-27T16:00:00",
  "staff_assigned": "staff_1"
}

Turn 15: Scheduler Function Confirms Rescheduling

Tool (functions.scheduler) Response:

{
  "success": true,
  "confirmation_message": "Your appointment has been moved to 4 PM."
}

Turn 16: Assistant Confirms Rescheduling to User

Assistant:
Your appointment has been moved to 4 PM successfully.


Summary of Tool Function Calls for Rescheduling

  1. Retrieve Existing Appointment:

    • Action: retrieve
    • Parameters: appointment_id
  2. Check Availability for New Time:

    • Action: search
    • Parameters: service, datetime
  3. Update Appointment Time:

    • Action: update
    • Parameters: appointment_id, new_datetime, staff_assigned

This expanded flow demonstrates how the assistant interacts with a hypothetical scheduler functions to handle user inquiries, bookings, and rescheduling requests. It gives an “update appointment” method to change a time, but you can imagine that a booking service may work differently - like you can’t just change your Taylor Swift tickets to a different seat number, you’d have to cancel the existing order.

2 Likes