Authenticate with Snowflake
If you are new to Snowflake, there is some setup you’ll need to do to get an application running, like setting up a user that is able to access the API. Head into your Snowflake instance and follow the guide provided by Snowflake for authenticating against the REST API, and the guide for authenticating against the Cortex REST API. Pay attention to:- It’s recommended to create a new user to access the API so that its permissions, privileges and access can be limited to the necessary scope
- Make sure to grant the role of the user you’re authenticating as a
SNOWFLAKE.CORTEX_USERdatabase role if it’s not already present - If you are using a Personal Access Token, make sure to apply a Network Policy that allow-lists the IP you’ll be accessing from
- Capture your account identifier, which can be found by accessing the lower-left button on the UI that contains your name and account role.
- Click your name
- Hover over your active account
- In the popover menu, select “View account details”
- Copy the field labeled “Account/Server URL”
Set up an AgentControl config
Before we write any code, we’ll go into LaunchDarkly and create a config to be used in the integration. Navigate to your LaunchDarkly instance and follow these steps:- Navigate to “AgentControl” and click “Create config” on the top-right side

Create an AgentControl config
- Give your Config a name, and then select “Cortex” from the provider dropdown.

Select Cortex
- Now that Cortex is selected, the model dropdown will be filtered to the available models. For this first variation, we’ll select
claude-3-5-sonnet
Make sure that the region you’re accessing from has support for the model you select. You can view model availability on this page.

Select Claude
- Add your messages for the completion. We’ll add a single system message, as well as a template for where the user message will go:

Add messages
{{variables}} signify a variable that will be replaced at the time you retrieve your config. This is how you provide dynamic content to your Configs such as contextual user information- Click “Review and save”. You’ll be given a chance to review your changes before committing them.

Review and save
- Your config is now saved so it’s time to serve our new variation to our users. Click the “Targeting” tab on the top of the config:

Targeting tab
- By default your config will be serving the
disabledvariation which is used to signal that a config is turned off. We’ll revisit this aspect later in the code.

Default disabled
- Click “Edit” on the default rule and select your variation from the dropdown, click “Review and save”, and then confirm the changes:

Select variation

Copy key
Set up the server
Next, we’ll set up our sample application so that we can see AgentControl and the Snowflake REST API interacting in real-time. This section assumes some knowledge with TypeScript and the NodeJS ecosystem, but can be accomplished in any language with AI SDK support.For the following sections, these are instructions to set it up as a new application. If you’re not concerned about which piece does what or having a clean slate, you can also just clone this repository and run
npm install and then npm run start after filling out the .env section.Set up an ExpressJS application
Follow the ExpressJS installation guide to set up a new project leveraging Express.Basic setup
Let’s create some of the structure we’ll need for the app:.env
The last command created a .env file that we’ll use to register our secrets so they can be securely loaded by the application.
Within this file, fill out the following values:
package.json
Grab the contents of the package.json file from the repository and replace your local package.json file.
Now run npm install to install our dependencies. Once that finishes, run typescript --init from the project folder to create a tsconfig.json file. You’ll need the dependencies in here to process TypeScript files and run your local application.
The dependencies in this file do the following:
- Add TypeScript support to ExpressJS (
@types/express,typescript) - Add utilities to run the application (
nodemon,ts-node,dotenv) - Initialize the LaunchDarkly SDKs (
@launchdarkly/node-server-sdk,@launchdarkly/server-sdk-ai)
We are using default TypeScript settings. Feel free to edit these to match your project’s needs.
index.ts
The index.ts file is responsible for initializing the application. We’ll be including two routes; one to render an HTML page and one to respond to the completion.
Grab the index.ts file from the repo and replace this content. The file has comments explaining the functionality.
index.html
Replace the index.html file in views/index.html with the same content from the repository, or use it as a guideline to build your own interface for the chat. This file is also commented, but outside of the HTML structure, you’ll want to pay attention to the <script> at the bottom of the page which handles making the API call.
Make our completion calls
Now that we have an application, we can start wiring up LaunchDarkly to the Snowflake API. We’ll set up the LaunchDarkly clients inapp/launchdarklyClient.ts:
app/completions.ts let’s go ahead and set up the Snowflake call:
Run the app
Navigate to your root directory and runnpm run start to start the application.
When you navigate to localhost:3000 (or whichever port you changed it to) you should see a simple screen that looks like this:

Landing page

Landing page with completion
Make runtime changes
Now that we have a completion endpoint set up, let’s create a new variation and change the model at runtime. You can minimize your code editor for now; we’ll only need to make changes in the LaunchDarkly UI!- Navigate back to your config in the LaunchDarkly UI
- Click “Add another variation” and then repeat the steps from earlier, but this time select a different model. We’ll use
llama3.1-8band edit the system message slightly for tone:

Adding a second variation
- Click save and head back over to targeting
- On the targeting page, edit the default rule and select the new variation you created:

Second variation selection
- Click save and confirm the changes
localhost URL. Without refreshing the page or restarting the server, go ahead and re-submit the request.
Our output was now generated by our llama3.1-8b model rather than the Sonnet model we were leveraging earlier:

Llama output
Setting up monitoring
To set up monitoring, we need to extract thetracker object from our config and call some track methods that will communicate the metrics to LaunchDarkly.
Let’s update the app/completions.ts file to include tracking:
usage parameter returned from Snowflake to capture the token usage. These metrics will appear in your dashboard for your config under the “Monitoring” tab:

Monitoring
Wrapping up
This is a simple example but demonstrates how you can use the power of Snowflake’s Cortex completion gateway in conjunction with AgentControl. Together, they allow you to tweak models in real-time by selecting any models available in your region and having them update seamlessly without requiring any code changes. Additionally, with the power of LaunchDarkly’s targeting you can serve different models and prompts to different users, and even run experiments against your AgentControl to see which model and prompts best fit your features. To Get started with AgentControl, sign up for a free trial. You can also contact us ataiproduct@launchdarkly.com with any questions.