We’ve changed our name! API.AI is now Dialogflow. Learn more here.

How contexts and follow-up intents work

March 7, 2018

Using contexts and follow-up intents to respond correctly every time


Contexts are a tool that allows Dialogflow developers to build complex, branching conversations that feel natural and real.

Here’s an example of a dialog powered by contexts.

User: “Will it rain in Mountain View today?”

Agent: “No, the forecast is for sunshine.”

User: “How about San Francisco?”

Agent: “San Francisco is expecting rain, so bring an umbrella!”

While the follow up, “How about San Francisco?”, doesn’t make sense as a standalone question, the agent knows the contextual inquiry is still about rain.

Dialogflow uses contexts to manage conversation state, flow and branching. You can use contexts to keep track of a conversation’s state, influence what intents are matched and direct the conversation based on a user’s previous responses. Contexts can also contain the values of entities and parameters, based on what the user has said previously.

In this blog post, we’ll be exploring the concept of contexts and showing the various ways you can work with them. By the end of the post, you’ll be able to use contexts as a tool in your own agents.

Input and output contexts

In a Dialogflow agent, each intent is configured with two lists of contexts:

  • Output contexts

  • Input contexts

Output contexts

Output contexts attach contexts to the session - the conversation’s state - after an intent has been matched. For instance, if you have an intent that is matched when a user mentions that they like cats, you specify that the output context “likes cats” is attached to the session after the intent is matched.

This means that when further requests are handled by Dialogflow or in your business logic, they can observe that the “likes cats” context is attached to the session and respond accordingly. For example, an entertainment app might know to show the user cat-related content when they ask for recommendations.

Input contexts

Input contexts can be used to filter which intents are matched, according to the following rules:

  • An intent will only be matched if all of the input contexts it specifies are currently active.

  • Given two intents with identical training examples, the intent whose input contexts are currently active will be matched.

The following table gives examples of how input contexts affect matching in various scenarios.

Contexts in the session Intent’s Input Contexts Can intent be matched?
No Contexts No Input Contexts Yes
No Contexts likes_cats No
likes_cats likes_cats Yes
likes_cats No Input Contexts Yes
likes_cats likes_dogs No
likes_dogs Yes

Using input and output contexts, you can control dialog in the following ways:

  • Setting contexts when certain criteria are met

  • Creating intents with the proper input contexts

This can be useful in filling out forms: questions may only need to be asked if the user provides certain answers to other questions. It can also help manage conversational games, and ensure intents are matched in a certain order.

Adding Context to your intents

To add input or output contexts to your intent, first scroll to the top of your intent and click on Contexts as seen below:

Contexts UI

In the “Add input context” or “Add output context” sections, add your input or output contexts. If your agent uses a webhook for fulfillment, you can set output contexts in your webhook responses. Learn more about adding contexts here.

Context lifespan

To further control conversation state, contexts can have specific lifespans. The context will be attached to the session as long as the number of interactions between your agent and the user does not exceed the lifespan of the context when it was set. In the following image, the lifespan is set to 5.


Output contexts can be set again in subsequent intents and can even be “cleared” by setting the lifespan of the context to 0. This may be useful if the user wants to start the conversation over, or if you’d like to reset a context that is no longer relevant. Contexts are automatically cleared from the session ten minutes after being applied, regardless of their lifespan.

See the documentation for more information.

Follow-up Intents and Contexts

Follow-up intents provide a simple way to shape dialog without having to manage contexts manually. Here’s an example.

Nested follow-up intents

In this sequence, there are two sets of intents that can handle a yes or no answer. The intents handling yes or no for “Do you like cats?” are distinct from those handling yes or no for “Would you like to see a cat picture?”.

One set of intents is nested as follow-up intents for “Do you like cats?”, meaning they will only be matched in immediate response to the “Do you like cats?” intent.

The other set of intents is nested as follow-up intents for “Do you like cats? - yes”. This means that they will only be matched if the user had previously answered “yes” to the “Do you like cats?” question.

The structure of this conversation, along with the ability to correctly match the appropriate “yes” or “no” intent even when there are multiple equivalents, is powered by contexts.

When a follow-up intent is created, an output context is added to the parent intent and an input context of the same name is added to the newly created child intent. This means that the follow-up intent can only be matched when the parent intent was matched on the previous turn of conversation.

Follow-up intents allow you to conveniently apply the power of contexts to your conversation. See the documentation for further detail.

Parameters and Contexts

Contexts can also include parameter values from when the context was set. For instance, if an intent is matched when the user answers a question, “What is your favorite band?”, that includes a parameter for the name of the band, the name of the band can be surfaced in subsequent intents.

You can access this name in Dialogflow by entering #context_name.parameter_name (where context_name is the name of the context and parameter_name is the name of the parameter). This works for any response, as long as the context is currently active and the user has provided a value for the parameter.

For example, in the first screenshot below, the output context “favorite-city” is applied, and a parameter value for “geo-city” is extracted from what the user says.

Intent showing parameters

In the second screenshot, representing a subsequently matched intent, we can use the string #favorite-city.geo-city to access and output this value in the “Text response”. Since “favorite-city” has been added as an input context, this intent will only be matched after the previous one.

Intent showing response

When a user says “My favorite city is New York”, matching the “Remember Favorite City” intent, the value “New York” will be stored in the context. When they subsequently ask “What is my favorite city”, the agent will respond with “Your favorite city is New York.”

Learn more about extracting parameter values from contexts. Remember that if your agent uses a webhook for fulfillment, you can activate contexts and get parameter values in your fulfillment code.

Thanks for reading! Learn more about contexts or head over to your developer console to try them out. You can also discuss this more over on our developer community or ask questions in our support forum.

Posted by Matt Carroll and Daniel Imrie-Situnayake, Dialogflow Developer Relations.