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

Extending context duration in Dialogflow

Website Name Change

I have changed the name of this website from Mining Business Data to BotFlo. I am offering a 60% off discount on both my Dialogflow ES and Dialogflow CX courses till April 20th 2021 for people who can help me spread the word about my new website.

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.

Similar Posts


  1. 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!!!

    1. 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.