> ## Documentation Index
> Fetch the complete documentation index at: https://help.treble.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate Your Own AI in Treble

> Learn how to integrate your own AI in Treble so it can answer your user's questions.

## Before You Begin

Before starting with this guide, we recommend that you read multiple articles to understand concepts such as session variables, webhooks, and request trigger.

<CardGroup cols={2}>
  <Card title="Session Variables" icon="tag" href="/en/docs/build-with-treble/features/variables">
    Understand how to use session variables in Treble.
  </Card>

  <Card title="Webhooks" icon="code" href="/en/api-reference/webhooks/intro">
    Understand how to use webhooks in Treble.
  </Card>

  <Card title="Request Trigger" icon="code" href="/en/api-reference/webhooks/request-trigger">
    Understand how to use request trigger in Treble.
  </Card>
</CardGroup>

This article explains how to connect your artificial intelligence (AI) system with Treble's conversational flows. This integration allows you to generate dynamic responses for your users based on data processed in your backend. Below, we show you how to structure your flow correctly:

## Initial Block

The first step is to create a **start block** that asks the user a question, such as: "Do you have any questions?", "How can I help you today?".

In the **output line** of this block, click to **add the webhook URL** that will trigger the request to your system.

<Tip>
  Save the user's response in this initial block to use it as a variable in the code. For this, activate the "save response" option and assign a name to the variable that will be sent in the webhook body.
</Tip>

<img src="https://mintcdn.com/trebleai/5XNOo1Uq3QVPV6lD/images/your-ai-1.png?fit=max&auto=format&n=5XNOo1Uq3QVPV6lD&q=85&s=a17278f05ba1f0c876b3520986764780" alt="Configure initial block" width="1248" height="554" data-path="images/your-ai-1.png" />

## Processing and Response from Your System

Once the user responds:

1. Your system will receive the information.
2. It will process that information using AI.
3. It will send a *response* to Treble with the **generated response**.
4. This response should update the session variables, and will be the one shown to the user.

<img src="https://mintcdn.com/trebleai/5XNOo1Uq3QVPV6lD/images/your-ai-2.png?fit=max&auto=format&n=5XNOo1Uq3QVPV6lD&q=85&s=01da9767df6405d636c1f19ce4556328" alt="Configure initial block" width="1704" height="726" data-path="images/your-ai-2.png" />

## Requests that Take More than 10 Seconds: Using `[REQUEST_TRIGGER]`

When AI processing may take longer, it is necessary to insert an **intermediate block** before sending the response:

* This block must have an **option labeled as `[REQUEST_TRIGGER]`**.
* This label indicates that, when the user reaches that node, the system should make a request and continue the flow through that route.

This allows **waiting for the AI's response** without the conversation being blocked.

<img src="https://mintcdn.com/trebleai/5XNOo1Uq3QVPV6lD/images/your-ai-3.png?fit=max&auto=format&n=5XNOo1Uq3QVPV6lD&q=85&s=4ae71b76b14c6279bf5a39199bce204e" alt="Configure intermediate block" width="1988" height="732" data-path="images/your-ai-3.png" />

## Conditional Routes Based on AI Output

If your AI returns a specific value (for example, `"talkToAgent"` or `"fileUrl"`), you can **create branches in the flow** to offer personalized responses. To do this:

1. Send these values as variables from your system.
2. Insert a **conditional block** between the `[REQUEST_TRIGGER]` and the response.
3. Define the **different routes** that the user can follow according to the received variable.

<img src="https://mintcdn.com/trebleai/5XNOo1Uq3QVPV6lD/images/your-ai-4.png?fit=max&auto=format&n=5XNOo1Uq3QVPV6lD&q=85&s=18e8a67a68e005be7c38d786f54bf81c" alt="Configure conditional block" width="1788" height="936" data-path="images/your-ai-4.png" />

## Prevent Users from Getting Stuck

To prevent users from getting blocked within the flow:

* You can configure **alternate flows** to make **automatic follow-up**.
* This can serve to:
  * Retry getting a response from the AI.
  * Escalate the conversation to a human after a certain time without interaction.

<img src="https://mintcdn.com/trebleai/5XNOo1Uq3QVPV6lD/images/your-ai-5.png?fit=max&auto=format&n=5XNOo1Uq3QVPV6lD&q=85&s=b238375805d950f58ce5cadf57b06921" alt="Configure alternate flow" width="1862" height="570" data-path="images/your-ai-5.png" />

## Continuous Flow with Multiple Interactions

Whenever your system needs to **consult the AI again**, make sure that the **return line** to the block that processes the response **also includes the webhook**.

<img src="https://mintcdn.com/trebleai/5XNOo1Uq3QVPV6lD/images/your-ai-6.png?fit=max&auto=format&n=5XNOo1Uq3QVPV6lD&q=85&s=c0767f21734728c3db6ec0c4d174414b" alt="Configure continuous flow" width="1920" height="640" data-path="images/your-ai-6.png" />
