# Turns & state

## Turns

Every conversation is made up of a series of turns; an exchange where one person speaks and the other responds. Within the world of conversational AI, a *turn* is just the same. When a user interacts with your AI application and the AI responds, this single back-and-forth from each participant constitutes a turn.

<figure><img src="https://2737319166-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHCxYxhIU0Bqkjj942mGk%2Fuploads%2FMHTwnI1mZ8yeCajLfQq3%2Fimage.png?alt=media&#x26;token=812eaf56-499f-4e09-b4d6-9d62f18d86a9" alt=""><figcaption><p>A turn represented in a flow (highlighted in blue)</p></figcaption></figure>

Here’s how it looks in conversation:

```
User: Yes, I'd like to schedule an appointment.
AI: Sure, I can help with that! 
AI: What day works best for you?

User: This Thursday.
AI: OK, and what time would you prefer?
```

Because a user message + AI response = one turn, the above conversation currently involves two turns. Notice that the first turn contains two messages from the AI. That's because your application can deliver as many messages as is designed in it's response back to the user.

What about the initial welcome?

```
User: [Clicks a web widget]
AI: Hello there! May I help you get started with booking an appointment?
```

The very first turn in a conversation generally involves the action of a user clicking open a chat widget on a website or calling or texting a number that then connects them with your AI application. This action of initiating the conversation counts as the user message, so in the above example, one turn has already taken place (user interaction + AI response).

### How turns end and start

When looking at a flow in your NLX workspace, a turn can either be comprised of dozens of [nodes](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/overview/nodes) (i.e., an individual step or block that collectively represent a conversation flow) or simply one node.

<figure><img src="https://2737319166-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHCxYxhIU0Bqkjj942mGk%2Fuploads%2FoDeaIFXvK1C9kJ4GIZzB%2Fimage.png?alt=media&#x26;token=c08ccae7-8424-4608-8a9c-965d85dafed3" alt=""><figcaption><p>A single node in the NLX flow builder</p></figcaption></figure>

The AI's response may traverse through several nodes that define logic it must follow, including generating outputs, evaluating IF conditions, routing to new paths or flows, invoking API calls, delivering a series of messages, and so on.&#x20;

A user's response, however, is supported and collected through one of the following node types in a flow:

* [*User choice*](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/nodes#user-choice): Extracts a single [variable](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/flows-and-variables#variables-in-flows) from the user (e.g., Y/N, flight option, PIN, Name, etc.)
* [*User input*](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/nodes#user-input): Extracts the user's purpose for interaction so an appropriate flow may be matched and redirected (e.g., "I want to book a flight" or "do you provide pet insurance?")
* [*Generative Journey*](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/nodes#generative-journey): Extracts multiple variables from the user within the node type, using as many turns as needed for the AI to gather all required information before exiting the node (e.g., collects departure and arrival cities, departure time, passenger contact information, and seat preference for a flow that will book a flight with this extracted info)
* [*Voice+*](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/nodes#voice): If using a Voice+ script, a predefined number of turns exist with voice prompts from the AI and user interaction with the front-end digital asset. For bidirectional Voice+, turns are driven by the number of user requests given to the AI and the AI's response (web navigation, form filling, responding to a question)

Because the AI can deliver several messages and traverse hundreds of nodes in a turn, a turn officially ends by the passing of the conversation back to the user, or if the conversation reaches a defined end.

In the below simplified example, the first turn goes as such:

```
User: [Click web widget]
AI: Hello there!
AI: Is there anything I can help answer for you?
```

<figure><img src="https://2737319166-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHCxYxhIU0Bqkjj942mGk%2Fuploads%2FjYISQwutj8EwAX5NmlwV%2Fimage.png?alt=media&#x26;token=2cde218c-daa0-4f62-aa5a-77f8f2299a24" alt=""><figcaption><p>An initial turn in a conversation</p></figcaption></figure>

The turn ends on the *User choice* node after the AI delivers its message.&#x20;

And a new turn begins from the same *User choice* node once a user replies with the expected variable:

```
User: No
AI: Thank you for contacting us. Have a great day!
```

<figure><img src="https://2737319166-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHCxYxhIU0Bqkjj942mGk%2Fuploads%2Fj4jfu63eKvbuh1BuRbpm%2Fimage.png?alt=media&#x26;token=3ebb6acd-dffa-4f20-b812-0ba138e41d3c" alt=""><figcaption><p>A new turn in the same flow begins when a user responds</p></figcaption></figure>

***

## State

State functions as the memory of the conversation. State keeps track of:

* What turn the conversation is on
* Which flows, API calls, and nodes have been traversed
* What the user has already said
* Which variables have been collected, set, cleared, etc.

NLX tracks state with every conversation occurring between users and your conversational AI application. Using state, the AI can respond more intelligently, like skipping questions it already asked or continuing where it left off in a flow if the user returns later.

### Changing state

By design, you may wish to control what the AI retains, forgets, and so on throughout a conversation. To do this, you can enable [state modifications](https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/nodes#state-modifications) throughout most nodes of a flow to strategically alter state.

<figure><img src="https://2737319166-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHCxYxhIU0Bqkjj942mGk%2Fuploads%2FArR0y5aLBnfCkedU2ZuU%2FFlows_Turns%20%26%20state%20mods.png?alt=media&#x26;token=361411e0-e1e0-4bd9-8d7d-307fcdd1861c" alt=""><figcaption><p>State modification added to a <em>Basic</em> node in a flow</p></figcaption></figure>

You can *set* state to store important details (like a user’s name or selection), *clear* state to reset parts of the memory (useful for revisiting flows or nodes without auto-traversal "skipping"), and other state modifications that adapt the conversation based on desired behavior.

***

## Troubleshooting flows through turns

Often when testing our conversations, we may notice errors appear or unanticipated behavior from the application. These issues usually stem from problems with state and variables in a node of a turn:

* Missing required variables used in application messaging, payloads, or state modifications
* Uncleared variables in retry or revisit scenarios

If an issue is detected by NLX's state tracker, the entire turn will fail to execute or will trigger the application's fallback behavior. Within a [test chat](https://docs.nlx.ai/platform/nlx-platform-guide/ai-applications/testing) in your NLX workspace, you can engage in conversation with your AI application to troubleshoot or view potential errors.&#x20;

Note the four turns that have taken place in this test chat so far (application messages in grey):

<figure><img src="https://2737319166-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHCxYxhIU0Bqkjj942mGk%2Fuploads%2FqodC9crMtvJICU9rRV7F%2FScreenshot%202025-06-26%20at%203.57.08%E2%80%AFPM.png?alt=media&#x26;token=d6d16480-353f-4219-b609-ca0051d03b10" alt=""><figcaption><p>Turns (boxed in yellow) in the Test chat</p></figcaption></figure>

To troubleshoot unexpected errors with a test chat, click the most recent application message to open the *Debugger*. This shows a top-down timeline of events for that turn, allowing you to expand each logged event and identify the cause of the issue. The debugger also highlights the nodes involved in the turn directly on the Canvas of a current flow:

<figure><img src="https://2737319166-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHCxYxhIU0Bqkjj942mGk%2Fuploads%2FtUhA3VEBAQ0THK8GfRT5%2Fimage.png?alt=media&#x26;token=6a64a48d-c715-45b5-9381-62fe5d954f88" alt=""><figcaption><p>Debugger panel opened from a selected message in the test chat view</p></figcaption></figure>

{% hint style="info" %}
Keep in mind that a single turn can span multiple flows if *Redirect* nodes are used.
{% endhint %}

If having trouble pinpointing which variable's state is causing an issue, start debugging from the first node in the affected turn. Detach it from all subsequent nodes in the turn and connect it to a simple *Basic* node with a success message. If the turn reaches the success message, continue slowly iterating the process by reconnecting another node, saving, and testing again. When the turn fails, you’ll know the last connected node is where the issue lies.


---

# Agent Instructions: 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:

```
GET https://docs.nlx.ai/platform/nlx-platform-guide/flows-and-building-blocks/overview/turns-and-state.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
