# Context

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2F4ZGlsMIzIXwj9GxEsEVV%2FScreenshot%202024-08-26%20at%2015.20.43.png?alt=media&#x26;token=cc2a6893-ed53-4dba-a638-4dac6b560579" alt=""><figcaption></figcaption></figure>

Context makes it possible to reuse the same intent in several blocks, an important feature in bot building!&#x20;

<details>

<summary>Example</summary>

Let's learn how to use context with this short example:

So, we've built a bot that can help users place a food order. At one point in the conversation, the bot will ask the user if they'd like a free dessert. The user can reply with 'yes' or 'no'. A bit later in the conversation, the bot will ask the user if they're ready to place their order. Again, the user can reply with 'yes' or 'no'.&#x20;

We need context so that the 'yes' or 'no' lead to different flows depending on where they are said in the conversation.

</details>

## Add context to your flow

To use context in an intent:

1. Make sure that you [created intents](https://docs.chatlayer.ai/navigation/natural-language-processing-nlp/intents#add-a-new-intent) and linked them to [blocks](https://docs.chatlayer.ai/buildabot/flow-logic/dialog-state). In this example, we'll use **general\_yes** and **general\_no** intents.

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FL4Qd9LXP5LjeInMuhqWD%2FScreenshot%202023-05-08%20at%2016.21.37.png?alt=media&#x26;token=32f1fc49-8cfe-46a9-95ea-dbd3ac798776" alt=""><figcaption><p>Two blocks, one for each possible user response</p></figcaption></figure>

2. Open the block containing the question. &#x20;
3. Go to the **NLP** section.&#x20;
4. Under **Give output context and livespan**, create an **output context.** For this example, we will call it **book\_ticket** with a **lifespan** of &#x31;**.**

{% hint style="info" %}
The **lifespan** designates how many times a context can be set throughout the whole conversation. For instance: If the bot offers free dessert twice in one conversation, we should put the **lifespan** at 2 because the user can say *yes* or *no* twice to this question. In this example, we'll only offer free dessert once, so we'll keep the **lifespan** at 1.
{% endhint %}

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FBeQvEKVEJsgT4VY6qXXH%2FScreenshot%202023-05-08%20at%2009.59.18%20(1).png?alt=media&#x26;token=5550302f-561c-4c50-890a-399a29dfe69b" alt=""><figcaption><p>Add an output context and lifespan to a block.</p></figcaption></figure>

5. Click **Save**.
6. Open your [intent block](https://docs.chatlayer.ai/buildabot/flow-logic/dialog-state#intent). For instance, open the **general\_yes** intent block.
7. Navigate to the **NLP** tab.
8. Under **Required context**, look for the context that you just created. In this example it will be **book\_ticket**.

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FM1ZLCpbVh1Yxci7rxMcw%2FScreenshot%202023-05-08%20at%2010.05.24.png?alt=media&#x26;token=7fd05b84-b91f-41f1-a785-2c791f74b4a2" alt=""><figcaption><p>Add a required context to an intent block.</p></figcaption></figure>

9. Click **Save**.

<details>

<summary>Tips on visualizing context in the canvas</summary>

From your bot [canvas](https://docs.chatlayer.ai/navigation/bot-builder/flows), visualizing context at first sight can be not so straightforward.

* **Hover to see context**

By hovering over the input context next to the intent title, you can instantly view the required context that you just configured. This gives you a clear picture of the context requirements for your intent, enabling you to make any necessary adjustments.

<img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FnQmuwBHnMaLDhsKZieHN%2FScreenshot%202024-08-27%20at%2010.01.09.png?alt=media&#x26;token=6b4be4eb-4b59-4c78-bd7a-1fab5893552d" alt="" data-size="original">

* **Use parent connections**

To improve visualization, you can set the **Ask a question** block as a [parent](https://docs.chatlayer.ai/navigation/bot-builder/flows/canvas-functionalities#parent-child-connections) to the intent block. This creates a clear visual connection that improves readability.

<img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FL1yCyz42rJFIT8iuIFXt%2FScreenshot%202024-08-27%20at%2009.54.00.png?alt=media&#x26;token=6c57972f-f4c5-4d2b-9c04-e09b653a0c83" alt="" data-size="original">

</details>

10. Follow the same process for other places in the conversation where those intents are used. That way, you can be sure that the bot provides the right answer!

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FC3shTNZ2OMBimDHJOo9m%2FScreenshot%202023-05-08%20at%2016.06.10.png?alt=media&#x26;token=de762701-04b4-4528-a3fa-23362274b0b1" alt=""><figcaption><p>Use the same intents at different places in the conversation using context.</p></figcaption></figure>

{% hint style="warning" %}
A user can have multiple contexts when navigating between different conversation flows. When multiple intents and input context combinations are found, the user's context with the highest lifespan value is taken.
{% endhint %}
