Build a chat assistant¶
A chat assistant lets people ask an app questions in plain language. The app sends each question to a model through the Cortex REST API, runs the tools the model asks for, and returns the answer. The tools are the same functions that serve the app’s pages, so chat answers match what the UI shows. The whole loop runs inside the app, and it needs no other Snowflake objects.
The example on this page uses a Next.js app built from the App Runtime template, and the tool list from Build an MCP server with Snowflake App Runtime. An app can have a chat assistant, an MCP server, or both.
Before you begin¶
- A Node.js app on App Runtime that uses the Node.js SDK. See Build a Node.js app for Snowflake App Runtime.
- A
lib/tools.tsfile that lists your tools, as described in Add an MCP server to a Node.js app. You don’t need to add the MCP route, but install Zod withnpm install zod. - Access to the Cortex REST API and the model you choose for the app’s owner
role, through the
SNOWFLAKE.CORTEX_USERorSNOWFLAKE.CORTEX_REST_API_USERdatabase role.SNOWFLAKE.CORTEX_USERis granted toPUBLICby default. See Cortex REST API and Model availability for Snowflake Cortex AI.
How the assistant works¶
Each question runs as a short loop on the app’s server:
- The chat route sends the conversation and the tool list to the Cortex Chat
Completions API,
/api/v2/cortex/v1/chat/completions. - If the model asks for tools, the route runs them from
lib/tools.tsand sends the results back. - When the model answers without asking for a tool, the route returns that answer to the browser.
The SDK’s fetch()
signs each request with the app’s own token, so the app stores no API key.
Add the chat route¶
Create app/api/chat/route.ts:
The route follows the same rules as the MCP server:
- It validates every argument with the tool’s schema before it runs the tool, and the tools pass arguments to SQL as bind variables.
- It returns tool errors to the model instead of failing the request, so the model can try again. Argument errors say which argument was wrong. Query errors stay generic, and the full error goes to the app log.
- It limits the work for each question with
MAX_TURNSandMAX_RESULT_CHARS. - It keeps no state between requests. The browser sends the whole conversation each time, so any instance of the app can answer.
To use a different model, change MODEL to a model that supports tool calling.
See Tool calling.
Add the chat page¶
Create app/chat/page.tsx. The page keeps the conversation in the browser and
sends it to the chat route with each question:
Choose whose rights chat uses¶
The example calls the Cortex REST API and runs its tools with the app’s owner rights. Everyone who can open the app gets the same answers.
To answer as the signed-in user, use caller’s rights in both places: query
with snowflake.asCaller({ headers: request.headers }) in the tools, and call
the model with snowflake.asCaller({ headers: request.headers }).fetch().
Each user then needs access to the Cortex REST API and the model. See
Query Snowflake from a Snowflake App Runtime app.
Run and deploy¶
- Run the app locally and open
/chat. On your machine,fetch()needs a Snowflake CLI connection that signs in with OAuth or a programmatic access token. A connection that uses a password, a key pair, or a browser can run queries but can’t call the Cortex REST API. See Call account REST APIs. - Ask a question that needs a tool, and check the app’s output for
[chat] tool=lines. - Deploy the app, and open
/chaton the deployed app. See Deploy an app to Snowflake App Runtime.
Note
A programmatic access token signs in only when your user has a network
policy, unless an authentication policy sets
NETWORK_POLICY_EVALUATION = ENFORCED_NOT_REQUIRED. See
Using programmatic access tokens for authentication.
Limitations and considerations¶
- Each question runs within one request.
fetch()times out after 120 seconds by default; passtimeoutMsfor slower models or longer loops. - If the app restarts during a question, that answer is lost and the user asks again.
- The example waits for the whole answer. To show text as it arrives, request a streaming response and forward it to the browser. See Streaming.
- Model calls use Cortex REST API credits. See Cost and credit usage for Snowflake App Runtime and the Snowflake Service Consumption Table.