Home / DialogFlow ES / Extending context duration in Dialogflow
Context | DialogFlow ES

Extending context duration in Dialogflow

As you might know, Dialogflow contexts expire within 10 minutes (or 20 minutes, depending on who you ask). In any case, there is a time limit for all contexts and they expire after that time.

Sometimes, people need to extend the context duration for long running chat scenarios.

This is what someone asked me in a comment:

Yeah i do have undeterministic values for my context variables. I’m writing a Chatbot for my master thesis. I do have different courses – the user is able to get information about it. I use the context variables to track about which course he is now talking about and which intents i have to use (intends that are shared among courses). If the user wants to change the topic to another course i’m setting all the context variables to 0 and initialize the new ones in the new intent and topic.

I’m currently working on getting the lifespan higher than 10 minutes. I want to try to save the context variables from the current sessionID and send it back to dialogflow. I hope this gonna works ?

Do you have another solution for this 10 minutes lifespan problem?
Would be very greatful about an answer!

It is solvable, but requires some extra effort on your part.


There are two prerequisites to getting this to work:

  • custom integration
  • authenticated user

If your users are not authenticated, or you don’t want to develop a custom integration, then there isn’t any way to do this.

How to extend the context lifespan beyond 10 minutes

When you use a custom integration, you are relaying the user’s request to Dialogflow’s REST API by calling the detectIntent method.

This method actually takes in the contexts object as a parameter:

In other words, you can ask Dialogflow to provide a response to your query as if the current context is set to <your_context_name>. As you can imagine, this means if you can figure out a way to store the context long term, you can have long running contexts in your conversations.

So here are the steps to create long running contexts in your conversation.

1 Use a custom integration

Relay user’s message to Dialogflow’s REST API and return response to the user

2 Save output context after each response to your DB

Each time you get a response from the API, make sure you save all the output contexts into your DB. You will need to associate it with a userID (which is why I mentioned the authentication).

3 Next time user logs in, get the last set context

When the user logs in next time (i.e. next time you see a message from the same userID), fetch the last known output contexts

4 Append the last set contexts to your detectIntent API call

Now pass the contexts object you retrieved from your DB as part of the detectIntent API call.

Your query will now behave as if the old context was still active.

<— End of article —>

This website contains affiliate links. See the disclosure page for more details. 
"The magic key I needed as a non-programmer"

The custom payload generator was the magic key I needed (as a non-programmer) to build a good demo with rich responses in DialogFlow Messenger. I've only used it for 30 minutes and am thrilled. I've spent hours trying to figure out some of the intricacies of DialogFlow on my own. Over and over, I kept coming back to Aravind's tutorials available on-line. I trust the other functionalities I learn to use in the app will save me additional time and heartburn.

- Kathleen R
Cofounder, gathrHealth
"Much clearer than the official documentation to be honest"

Thanks a lot for the advice (of buying and following your videos)! They helped a lot indeed. Everything is very clear when you explain, much clearer than the official documentation to be honest 🙂

Neuraz T
Review for Learn Dialogflow CX
"I will strongly recommend this course because even I can learn how to design chatbot (no programming background)"

I think Aravind really did a great job to introduce dialogflow to people like me, without programming background. He organizes his course in very clear manner since I have been a college professor for 20 years. It is very easy for me to recognize how great Aravind’s course is! Very use-friend and very easy to follow. He doesn’t have any strong accent when he gives the lectures. It is so easy for me to understand. Really appreciate it.

Yes, I will strongly recommend this course because even I can learn how to design chatbot (no programming background) after studying Avarind’s course, you definitely can!

Ann Cai
Review for Learn Dialogflow ES

Similar Posts

  • It turns out that together with the context we can pass on the values of variables. Here we get the outgoing context along with the parameters:

    “outputContexts”: [
    “name”: “projects/tripplanner-xnvsps/agent/sessions/a0589e5d-53c0-6cea-b5ae-7183b92a7b2d/contexts/flight_type_context”,
    “lifespanCount”: 1,
    “parameters”: {
    “geo-city.original”: “Moscow”,
    “geo-city”: “Moscow”,
    “flight_type”: “”,
    “date-time.original”: “today”,
    “geo-city1.original”: “Deli”,
    “date-time”: “2020-03-26T12:00:00+03:00”,
    “flight_type.original”: “”,
    “geo-city1”: “Delhi”

    then save it all in the database

    and now we can send it all back with a new message from the user.

    Cool! Thanks a lot!!!

    • Yes, but remember that you need to know which user was having the conversation (that is why I mention that the user has to be authenticated). You don’t want to end up pulling the saved values from the DB and use it for some other user.