Snowflake Gen AI Functions · Topic 22 of 26
Chat Conversations: Multi-turn Architecture, Message History and Snowflake Intelligence
Build multi-turn chat on stateless Cortex calls by resending the messages array, and know when to use Snowflake Intelligence to chat with data.
- 5 min
- Cortex calls are stateless. Resend the whole
messagesarray each turn. - Roles:
system(optional, first, once),user,assistant. Analyst usesuserandanalyst. - Cost and tokens grow with every turn.
What it is
A chat feels continuous to the user, but Cortex model calls are stateless. The model does not remember the previous call. To make a multi-turn conversation, your application stores the history and sends it again with every new question.
Snowflake Intelligence (documented as Snowflake CoWork in current docs) is the ready made alternative. It is a Snowflake provided chat experience where business users ask questions of their data in natural language, without building an app.
How it works
The messages array
Multi-turn calls use an array of objects, each with a role and a content, in chronological order.
| Role | Meaning |
|---|---|
system | Optional instructions that set behavior; first in the array and only once |
user | A question from the person |
assistant | A previous reply from the model |
After the optional system message, user and assistant turns alternate. The latest user question goes last.
SELECT SNOWFLAKE.CORTEX.COMPLETE(
'claude-sonnet-4-5',
[
{'role': 'system', 'content': 'You are a concise support agent.'},
{'role': 'user', 'content': 'What is a virtual warehouse?'},
{'role': 'assistant', 'content': 'Compute that runs queries.'},
{'role': 'user', 'content': 'How do I make it smaller?'}
],
{'temperature': 0, 'max_tokens': 300}
);
The array form is documented for SNOWFLAKE.CORTEX.COMPLETE, and an array is required whenever the options object is present. Its page states the rule directly: the function does not retain any state from one call to the next, so you include every prior user and assistant message each time. It also recommends AI_COMPLETE for new work and states that the legacy function will be deprecated by the end of 2026. Cortex Analyst follows the same pattern, using a messages array with roles user and analyst.
Multi-turn architecture
A typical app has four parts.
- UI collects the question, for example a Streamlit chat box.
- History store keeps the messages, in session state for a simple app or in a table for persistence across sessions.
- Orchestration builds the array, adds retrieved context when needed, and calls Cortex.
- Cortex services answer:
AI_COMPLETEor the Complete REST API for text, Cortex Search for retrieval, Cortex Analyst for SQL.
For Streamlit the history usually sits in st.session_state. Each turn appends the user message, calls the model with the whole list, then appends the reply.
Updating the parameters on each turn
What changes between turns:
- The messages array grows by two entries per turn (user, then assistant).
- Model options such as
temperature,top_p,max_tokensandguardrailscan be set per call. Without the options argument the function returns a plain string; with it, a JSON string withchoices,created,modelandusage. In the REST API the chat body also carries the model and the messages. - Retrieved context is refreshed per question, not stored forever.
Costs and limits grow with the history, because the whole conversation is processed again on each call. Token use and cost rise every turn, and a long conversation can reach the model's context window.
Ways to keep it under control:
- Window: send only the last N turns.
- Summarize: replace old turns with a short summary produced by
AI_COMPLETE. - Reset: start a new conversation when the topic changes. Cortex Analyst docs also note that long or shifting conversations may need a reset.
- Check size: use
AI_COUNT_TOKENSto estimate before sending.
"The assistant forgot what the user said earlier" means the app did not resend the history. The model is not at fault.
Snowflake Intelligence
Snowflake Intelligence lets people chat with their data using natural language. It draws on Cortex Analyst for structured data through semantic views and Cortex Search for unstructured documents, and it can use custom tools. It keeps conversational context, shows results in text, tables and charts, and inherits Snowflake governance, including row access policies and column level security.
| Choose | When |
|---|---|
| Snowflake Intelligence | Business users need chat over governed data quickly, with no app code |
| Custom Streamlit app | You need your own UI, workflow or extra logic |
| Cortex Analyst REST API directly | You embed text-to-SQL in another application |
The docs also point to Cortex Agents as the way to orchestrate Analyst and Search together, and Snowflake recommends moving from the standalone Analyst API to Cortex Agents.
Common mistakes
Sending only the latest question. The model has no memory, so context is lost.
Putting the system message at the end or repeating it. It goes first, once.
Letting history grow forever. Cost and context limits grow with it.
Building a custom chat app when Snowflake Intelligence already meets the need.
Chat history that contains sensitive data is resent to the model on every call. Store and trim it with the same care as the source data.
What to remember for the exam
- Cortex calls are stateless. Resend the whole
messagesarray each turn. - Roles:
system(optional, first, once),user,assistant. Analyst usesuserandanalyst. - Cost and tokens grow with every turn.
- Snowflake Intelligence: ready made chat over Analyst and Search, with governance inherited.
- Use a window or summary to keep history short.
Check yourself
Why does a chat app resend earlier messages on every call?
Cortex calls are stateless, so the model only knows what is in the current request.
Where does the
systemmessage go in the array?First, and only once.
What does Snowflake Intelligence use to answer questions about tables and documents?
Cortex Analyst for structured data and Cortex Search for unstructured text.
Topic 22 of 26 in Snowflake Gen AI Functions
Test yourself on Snowflake Gen AI Functions
The full Snowflake GES-C02 question bank has 585 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Snowflake. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.