Skip to main content
Agent Builder is currently in preview and is subject to change. Features described in this pattern may evolve in future releases.

Overview

AI nodes in FlowX workflows are stateless — each invocation starts fresh with no memory of previous turns. For multi-turn conversations such as chatbots, advisors, or guided flows, you need a strategy to persist and reload conversation history between interactions. This pattern uses a FlowX Database data source to store session state, loading it at the start of each turn and saving the updated history after the AI response.

When to use

  • Multi-turn chatbots or conversational assistants
  • Guided workflows where the AI needs context from earlier steps
  • Any scenario where the user returns to a conversation after a delay
  • Task-oriented workflows requiring custom memory strategies

Architecture

The session state management pattern follows a read-process-write cycle on every turn:
User message
  → Load session (DB read)
    → Inject history into prompt
      → AI processing
        → Append exchange to history
          → Save session (DB write)
            → Response to user
Each workflow invocation performs these steps:
  1. Load — Query the ChatSessions data source by sessionId to retrieve existing conversation history.
  2. Inject — Insert the retrieved history into the AI node’s operation prompt as context.
  3. Process — The AI node generates a response informed by the full conversation context.
  4. Append — Add both the user message and the AI response to the messages array.
  5. Save — Write the updated session back to the data source.

Data model

Create a FlowX Database data source named ChatSessions with the following schema:
FieldTypeDescription
sessionIdSTRINGUnique identifier for the conversation session
messagesOBJECT[]Array of message objects storing the conversation history
Each entry in the messages array follows this structure:
FieldTypeDescription
roleSTRINGThe message author — user or assistant
contentSTRINGThe text content of the message
Example stored session:
{
  "sessionId": "sess-a1b2c3d4",
  "messages": [
    { "role": "user", "content": "I want to apply for a mortgage." },
    { "role": "assistant", "content": "I can help with that. What type of property are you looking to finance?" },
    { "role": "user", "content": "A single-family home, around 250,000 EUR." },
    { "role": "assistant", "content": "Great. Do you have a preferred loan term — 15, 20, or 30 years?" }
  ]
}

Implementation

1

Create the ChatSessions data source

In the Integration Designer, add a new FlowX Database data source named ChatSessions. Define the sessionId (STRING) and messages (OBJECT array) fields.
2

Load session history at workflow start

Add a Query node at the beginning of your workflow that reads from ChatSessions, filtering by the incoming sessionId. If no session exists, initialize an empty messages array.
3

Inject history into the AI node prompt

In the AI node’s operation prompt, include the loaded conversation history as context. Format the messages array into a readable block:
Previous conversation:
User: I want to apply for a mortgage.
Assistant: I can help with that. What type of property are you looking to finance?
User: A single-family home, around 250,000 EUR.
Assistant: Great. Do you have a preferred loan term — 15, 20, or 30 years?

Current user message: {currentMessage}
Use a Data Operations node before the AI node to transform the messages array into this string format and map it to the prompt input.
4

Append the new exchange to history

After the AI node produces a response, use a Data Operations node to append two new entries to the messages array:
[
  { "role": "user", "content": "{currentMessage}" },
  { "role": "assistant", "content": "{aiResponse}" }
]
5

Save the updated session

Add an Upsert node that writes the updated session back to the ChatSessions data source, using sessionId as the key. This ensures new sessions are created automatically and existing ones are updated.

Context window management

Long conversations can exceed the AI model’s context window or degrade response quality. Use these strategies to keep history manageable:

Summarization

For conversations exceeding a threshold (for example, 20 turns), use a separate AI node to generate a summary of older messages. Replace the full history with:
Summary of earlier conversation:
The user is applying for a 30-year mortgage on a 250,000 EUR single-family home.
They have a 50,000 EUR down payment and prefer a fixed rate.

Recent conversation:
User: What documents do I need to provide?
Assistant: You will need proof of income, bank statements, and a property appraisal.
This preserves key context while staying within token limits.

Token counting

Track approximate token usage for the history block. When it approaches 60-70% of the model’s context window, trigger summarization. This leaves sufficient room for the system prompt, current message, and the AI response.

Real-world example

The Mortgage advisor chatbot tutorial demonstrates this pattern end to end — from session creation through multi-turn advising to session cleanup. It includes handling for session expiry and context summarization. For the complete walkthrough, see the Mortgage advisor tutorial.

Variations

Sliding window

Keep only the last N turns (for example, 10) in the prompt context. Older messages remain stored in the database for audit purposes but are not injected into the prompt. This is the simplest approach and works well for short, focused conversations.

Topic-based memory

Partition conversation history by topic. When the user switches subjects, start a new topic segment while retaining a brief summary of previous topics. This works well for advisor-style bots that cover multiple domains in one session.

Cross-session memory

Maintain a persistent user profile that carries context across separate conversations. Store preferences, decisions, and key facts in a dedicated data source keyed by user ID rather than session ID. Inject a profile summary at the start of each new session so the AI can reference prior interactions.
Available starting with FlowX.AI 5.6.0FlowX 5.6+ introduces built-in session memory for conversational workflows, which automates the read-inject-append-save cycle described in this pattern. For task-oriented workflows or custom memory strategies (sliding window, topic-based, cross-session), the manual approach described here still applies.For details, see the Conversational workflows documentation.

Last modified on March 16, 2026