> For the complete documentation index, see [llms.txt](https://docs.chatlayer.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.chatlayer.ai/~/changes/zWOY8fNiBBrqneKkuGvA/understanding-users/using-context.md).

# Context

Need to reuse the same intent twice or more? The answer is context.

Context makes it possible to reuse the same intent in several bot dialogs, an important feature in bot building! 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;

## Step 1: Helping the bot understand the user's reply

First, for the bot to understand the user's 'yes' or 'no', we need to create two bot dialogs and link each one to one of the following intents:&#x20;

* general\_yes
* general\_no

<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&amp;token=32f1fc49-8cfe-46a9-95ea-dbd3ac798776" alt=""><figcaption><p>Two bot dialogs, one for each possible user response</p></figcaption></figure>

## Step 2: Linking bot message through context

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2F73DX6TIubA9eDwTgffKD%2FScreenshot%202023-05-08%20at%2016.23.42.png?alt=media&amp;token=190deea2-5f7d-4006-a64e-dcd0f33ca7a9" alt=""><figcaption></figcaption></figure>

In the screenshot above, you can see that we've added the `general_yes` and `general_no` intent nodes to the canvas and linked them to the bot dialogs. Now all we have to do is connect these dialogs to the bot message `Ask a question`, so the bot knows which question is being answered.&#x20;

We can do this by creating `output context` for the bot dialog containing the question. Open the `Ask a question` dialog and go to the `NLP` section. Create an `output context` that you will later link to the dialogs containing the `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%2FBeQvEKVEJsgT4VY6qXXH%2FScreenshot%202023-05-08%20at%2009.59.18%20(1).png?alt=media&amp;token=5550302f-561c-4c50-890a-399a29dfe69b" alt=""><figcaption><p>Adding an output context to a bot dialog </p></figcaption></figure>

{% hint style="info" %}
You can tell the bot how often this context can be repeated throughout the entire bot conversation. For example: 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 %}

## Step 3: Linking intents through context

The last thing to do is make sure that the two bot dialogs containing the `general_yes` and `general_no` intents are linked correctly to the dialog containing the question. To do so, we have to open each intent nodes, go to the NLP section, and add the `required context` we defined earlier as `required context` :

<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&amp;token=7fd05b84-b91f-41f1-a785-2c791f74b4a2" alt=""><figcaption><p>Adding a required context to an intent</p></figcaption></figure>

Once you click 'save', the bot can understand a 'yes' or 'no' response to the question of booking a ticket.&#x20;

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.

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FLTFXOVH4w3JHZWX7apws%2FGroup%209847.png?alt=media&amp;token=9b35ce1a-cef6-44f7-a4c6-220ca7f75266" alt=""><figcaption><p>Hovering an icon to see all required context linked to an intent </p></figcaption></figure>

To improve visualization, you can link the 'ask a question' bot dialog as a 'parent' to the intent. This creates a clear visual connection, represented by an arrow, from the 'ask a question' dialog to the 'general\_yes' and 'general\_no' intent nodes.

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2Fk6NNnwRDTd7gZsiY50Qc%2FScreenshot%202023-05-08%20at%2014.37.12.png?alt=media&amp;token=5cb7d322-577a-402a-abb0-b5d9a78fdb45" alt=""><figcaption><p>Linking a bot message as 'parent' to the intents</p></figcaption></figure>

By establishing this connection, you can easily understand the flow of the conversation and identify which intents are associated with specific dialogues. This makes it easier to make adjustments to your bot's conversation flow and ensure that it operates smoothly.

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2Faoih05n95zHu27kfefmR%2FScreenshot%202023-05-08%20at%2014.37.39.png?alt=media&amp;token=4e071492-359f-40bf-b99e-01a0be6bed8b" alt=""><figcaption><p>Parent-child arrow visualization</p></figcaption></figure>

## Step 4: Reusing intents in the same conversation

Later in the conversation, the bot will again ask a 'yes' or 'no' question to its user:&#x20;

*"Can I help you with anything else?"*

To make sure the user can reply 'yes' or 'no' to this question as well, we'll need to use `output context` again:

<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&amp;token=de762701-04b4-4528-a3fa-23362274b0b1" alt=""><figcaption><p>Using different output context so we can use the yes/no intent again</p></figcaption></figure>

Now, when the `general_yes` intent is recognized, the bot will know which question this answer belongs to checking the `output context`. Depending on this context, a user will be directed to either the `Yes book a ticket` bot dialog (when the **book\_ticket** context is active) or to the `Yes book a new ticket` bot dialog (if the **do\_anything\_else** context is active).

{% hint style="info" %}
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 %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.chatlayer.ai/~/changes/zWOY8fNiBBrqneKkuGvA/understanding-users/using-context.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
