Use a knowledge agent in a chatbot
___

Add a knowledge agent to your [chatbot](https://www.infobip.com/docs/automation-studio/chatbots) using the **Agent connector** element in the [chatbot editor](https://www.infobip.com/docs/automation-studio/chatbots/build-chatbots).

The agent handles end user questions by retrieving answers from connected knowledge sources.
___

## Before you begin

- [Create a knowledge agent](https://www.infobip.com/docs/agentos-ai-agents/knowledge-agents/create-knowledge-agent) and [connect knowledge sources](https://www.infobip.com/docs/agentos-ai-agents/knowledge-agents/connect-knowledge-sources).
- Have a chatbot ready in the [chatbot editor](https://www.infobip.com/docs/automation-studio/chatbots/build-chatbots).

___

## How it works

1. The chatbot captures the end user's message in an attribute.
2. The Agent connector element passes the message to the knowledge agent.
3. The knowledge agent generates a response based on the message, agent configuration, and connected knowledge sources.
4. The chatbot receives the response and sends it to the end user.
___

## Save the end user's message

Before the Agent connector element can process a question, the chatbot needs to capture the end user's message in an attribute. Use one of the following options:

- Use the predefined attribute **lastReceivedTextMessage**, which captures the last message sent by the end user.
- Use the **Save user response** element to save the message in a standard attribute.
___

## Add the Agent connector element

Add the **Agent connector** element to a [dialog](https://www.infobip.com/docs/automation-studio/chatbots/build-chatbots) in your chatbot. You can select only one knowledge agent per element, but you can add multiple Agent connector elements across dialogs.

The Agent connector has three tabs: **Request**, **Response**, and **Fallback**.

### Request tab [#request-tab-add-the-agent-connector-element]

| Field | Description |
|-------|-------------|
| **Select AI Agent** | Select the knowledge agent from the dropdown. |
| **User Message** | Define a value or select an attribute that contains the end user's message. The chatbot passes this value to the knowledge agent. |
| **Select Agent Version** | Select the agent version to use. |
| **Agent Context (Optional)** | Select an attribute to pass as additional context to the agent. To create a new attribute, select [Create an attribute](#create-an-attribute-request-tab). |

### Create an attribute [#create-an-attribute-request-tab]

1. In **Agent Context (Optional)**, select **Create new attribute**.
2. Enter a **Name** for the attribute.
3. The **Type** is set to **JSON** automatically.
4. (Optional) Enter a **Default value**.
5. Select the **Scope**:
   - **Chatbot** — the attribute value persists across all dialogs in the chatbot.
   - **Dialog** — the attribute value resets when the conversation moves to a different dialog.
6. (Optional) Select **Available to agents** to make the attribute visible to live agents in Conversations.
7. (Optional) Select **Sensitive data** to hide the attribute value in Chatbots and Inbox.
8. Select **Save**.
___

### Response tab [#response-tab-add-the-agent-connector-element]

| Field | Description |
|-------|-------------|
| **Response code** | (Optional) Select an attribute to save the HTTP response code. |
| **Response body attributes** | Extract values from the response body. Select **Show response code JSON schema** to view the response structure, then select **Add attribute** to map a JSON path to an attribute. Use `$.content` to extract the agent's response. |
| **Timeout** | Maximum time in seconds to wait for a response before triggering the fallback. |
___

### Fallback tab [#fallback-tab-add-the-agent-connector-element]

| Field | Description |
|-------|-------------|
| **Fallback action** | Select the action to take when the fallback triggers: **Go to dialog** routes the end user to another dialog; **Connect to agent** transfers the conversation to a live agent in [Inbox](https://www.infobip.com/docs/inbox). |
| **Dialog** | Select the dialog to route to. |
| **Fallback message (optional)** | Enter a message to send to the end user when the fallback triggers. |
| **Link preview** | Enable to show a preview for links in the fallback message. |
| **Use also for API failures** | Enable to apply the fallback for API failures in addition to timeout failures. |
___

## Send the response to the end user

Use a **Text** element and add the attribute where you saved the knowledge agent response.
___

## Create a conversational experience

To create a more advanced conversational flow, route the conversation back to the knowledge agent after each response:

1. After the knowledge agent responds, send the response in a **Text** element.
2. In a follow-up dialog, capture a new user input.
3. Use the **GenAI Intent Detection** element to determine where to route the conversation.
4. Route the conversation back to the dialog with the Agent connector element for a continuous, conversational experience.