Understand how contexts work

Introducing the new capabilities, confirmation and suggestion, requires us to augment an existing path in conversation, which means we need to either extend the path or create a detour. In both cases, we need to create intents whose behaviors depend on the conditions from the previous dialogs. This means that our agent needs to be able to perform the following actions:

  • State management: Pass information from one intent to another.
  • Conversation flow control: Invoke a specific intent when certain conditions are met.

Contexts provide the mechanism that enables an agent to perform these actions. Let's examine a context's key features that are vital to the implementation of the agent in this tutorial.

State management

A context allows us to store and retrieve information, so data can persist across intents. For example, the following code snippet, which uses the Dialogflow client library, shows how to access the date and time parameters of the context MakeAppointment-followup:

const context = agent.context.get('makeappointment-followup');
const dateTimeStart = convertTimestampToDate(context.parameters.date, context.parameters.time);

Conversation flow control

We can programmatically activate and deactivate contexts to control intent matching using input and output contexts settings. A typical setup for input and output contexts looks like the following:

  • First_Intent has an output context named MyContextwith a lifespan set to 5.

  • Second_Intent has an input context named MyContext.

Input contexts serve as prerequisites for intents to participate in matching. Because Second_Intent has MyContext as an input context, Second_Intent cannot participate in matching unless MyContext is active. In order for MyContext to become active, First_Intent needs to be previously matched.

Follow-up intents

Instead of creating and managing contexts manually, Dialogflow offers a fast and easy way to replicate the context setup between First_Intent and Second_Intent with follow-up intents from an existing intent. A follow-up intent is an intent that participates in matching only after its parent intent is invoked.

Figure 3. The arrow on the intents display indicates that Make Appointment - yes is a follow-up intent of Make Appointment.

Context's lifespan

A context remains active in conversation until its lifespan becomes 0; the value of every context's lifespan is decreased by 1 at each turn of a dialog. However, if we don't want a context to decay naturally in conversation, we can use one of the following two methods to force the context to be removed from the conversation at any turn:

  • In the Dialogflow console, set the value of the context's lifespan to 0:

    When we use this method, the intent always removes the context after it is invoked. We can use this method to make an intent clear any remaining contexts in the conversation. For instance, an intent at the end of a path uses this method to clean up the states from the previous conversation, which is necessary for the flow to return to the start of conversation.

  • In your fulfillment code, use the agent.context.delete() function:


    Some situations require an intent to decide which contexts to switch on and off based on the user utterance and other conditions. In these cases, use this method in combination with other functions such as context.get() and context.set() to manage the contexts programmatically.

In the next section, we use contexts to implement the first capability on our list: confirmation.