Cortex Agents REST API¶
Use this API to simplify the creation of an interactive chat application.
Run agent¶
POST <account_url>/api/v2/cortex/agent:run
Sends a user query to the Cortex Agents service provided in the request body and then generates a response.
Request¶
Path parameters¶
Parameter |
Description |
---|---|
|
(Required) Your Snowflake Account URL. For instructions on finding your account URL, see Finding the organization and account name for an account. |
Request headers¶
Header |
Description |
---|---|
|
(Required) Authorization token. For more information, see Configure Key-Pair Authentication. |
|
(Required) application/json |
Request body¶
The request body contains the model, response instruction, experimental fields, tools, tool resources, and messages.
Field |
Type |
Description |
---|---|---|
|
string |
The name of the model used by the Agent to generate a response. For a list of supported models, see Supported models. |
|
string |
The instructions the model follows when it generates the response. |
|
object |
Experimental flags passed to the agent. |
|
array |
An array of tool specifications available to the agent. |
|
object |
Resources required by the tools. |
|
object |
The configuration used to select the tool. |
|
array |
Array of messages in the conversation. |
Response instruction¶
The response_instruction
field is a string that provides instructions to the model for response generation.
The template uses placeholders that are populated at runtime with user input, agent configuration, and context to create a prompt for the foundation model to process.
Variable |
Replaced By |
---|---|
|
Query generated by the orchestration prompt model when next step is knowledge base querying |
|
Retrieved results for the user query |
Tools configuration¶
This section details the supported tool types, their configuration options, and how to specify the necessary resources for each tool.
Tool specifications¶
The tools
field contains an array of available tools:
Field |
Type |
Description |
---|---|---|
|
string |
The type of tool. A combination of type and name is a unique identifier. |
|
string |
The name of the tool. A combination of type and name is a unique identifier. |
Supported tool_spec.type
values include the following:
cortex_search
: Used for Cortex search functionalitycortex_analyst_text_to_sql
: Used for Cortex Analyst text-to-SQL
Tool Resources¶
The tool_resources
field is an object that maps tool names to their configuration objects:
tool_resources: {
"toolName1": {configuration object for toolName1},
"toolName2": {configuration object for toolName2},
...
}
The following configurations are supported for each tool type:
cortex_search
¶
"SearchName": {
"name": "database.schema.service_name", # Required: The Snowflake search service identifier
"max_results": 5, # Optional: Number of search results to include
"title_column": "title", # Optional: Column to use as document title
"id_column": "id", # Optional: Column to use as document identifier
"filter": { # Optional: Filters to apply to search results
"@eq": {"<column>": "<value>"}
}
}
cortex_analyst_text_to_sql
¶
"AnalystName": {
"semantic_model_file": "@database.schema.stage/my_semantic_model.yaml" # Required: Stage path to semantic model file
}
Tool choice¶
The tool_choice
field configures tool selection behavior:
Field |
Type |
Description |
---|---|---|
|
string |
How tools should be selected. Valid values:
|
|
array |
Optional array of tool names to use. Only valid when |
Messages¶
The messages
array contains the conversation history between the user and assistant:
Field |
Type |
Description |
---|---|---|
|
string |
The role of the message sender. Valid values:
|
|
array |
Array of content elements making up the message. |
Message Content Structure¶
Each message’s content
array can contain multiple content elements with different types:
Field |
Type |
Description |
---|---|---|
|
string |
The content type. Valid values:
|
Text Content¶
When type
is "text"
:
Field |
Type |
Description |
---|---|---|
|
string |
The text content of the message. |
Tool Use¶
When type
is “tool_use”:
Field |
Type |
Description |
---|---|---|
|
object |
Container for tool use request details. |
|
string |
Unique identifier for this tool use request. |
|
string |
Name of the tool to execute. Must match a tool name from the tools array. |
|
object |
Input parameters for the tool execution. |
Tool Results¶
When type
is "tool_results"
:
Field |
Type |
Description |
---|---|---|
|
object |
Container for tool execution results and metadata. |
|
string |
References the tool_use_id that generated these results. |
|
string |
Name of the tool that was executed. Must match a tool name from the tools array. |
|
string |
Tool execution status. Values:
|
|
array |
Array of content elements produced by the tool execution. |
Tool Results Content Types¶
The tool_results.content
array can contain elements of the following types:
Type |
Fields |
Description |
---|---|---|
|
type: "text" text: string |
Plain text content returned by the tool. |
|
type: "json" json: object |
Structured JSON data returned by the tool. |
Examples¶
Text Content example¶
{
"type": "text",
"text": "Show me sales by region for Q1 2024"
}
Tool Use example¶
{
"type": "tool_use",
"tool_use": {
"tool_use_id": "tu_01",
"name": "Analyst1",
"input": {
"query": "Show me sales by region for Q1 2024"
}
}
}
Tool Results example¶
{
"type": "tool_results",
"tool_results": {
"tool_use_id": "tu_01",
"status": "success",
"content": [
{
"type": "json",
"json": {
"sql": "SELECT region, SUM(sales) FROM sales_data WHERE quarter='Q1' AND year=2024 GROUP BY region"
"text": "This is our interpretation of your question: Show me sales by region for Q1 2024. We have generated the following SQL query for you: SELECT region, SUM(sales) FROM sales_data WHERE quarter='Q1' AND year=2024 GROUP BY region"
}
}
]
}
}
Complete Message example¶
{
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Show me sales by region for Q1 2024"
}
]
},
{
"role": "assistant",
"content": [
{
"type": "tool_use",
"tool_use": {
"tool_use_id": "tu_01",
"name": "Analyst1",
"input": {
"query": "Show me sales by region for Q1 2024"
}
}
},
{
"type": "tool_results",
"tool_results": {
"tool_use_id": "tu_01",
"name": "Analyst1",
"status": "success",
"content": [
{
"type": "json",
"json": {
"sql": "SELECT region, SUM(sales) FROM sales_data WHERE quarter='Q1' AND year=2024 GROUP BY region"
"text": "This is our interpretation of your question: Show me sales by region for Q1 2024. We have generated the following SQL query for you: SELECT region, SUM(sales) FROM sales_data WHERE quarter='Q1' AND year=2024 GROUP BY region"
}
}
]
}
]
}
]
}
Response¶
When streaming, each event typically arrives in a Server-Sent Events (SSE) format, with the following primary patterns:
Incremental
message.delta
events for partial outputerror
events if something goes wrong.
message.delta
Event¶
Field |
Type |
Description |
---|---|---|
|
string |
|
|
object |
Contains incremental update data. |
|
string |
Unique identifier for the message. |
|
string |
|
|
array |
A list of chunks or partial message segments. |
|
integer |
The position of this chunk within the current message. |
|
string |
Content type. Valid values:
- |
|
string |
If |
|
object |
If |
|
string |
The unique identifier for the tool call. |
|
string |
The name of the tool being called. |
|
object |
JSON payload for the tool. |
|
object |
If |
|
string |
The unique identifier for the tool output. |
|
string |
The tool execution status. Valid values:
|
|
array |
A list of items describing the tool’s returned data. |
|
string |
The type of content returned by the tool. Valid values:
|
|
object |
If |
|
string |
If |
error Event¶
Field |
Type |
Description |
---|---|---|
|
string |
|
|
object |
Contains error details. |
|
string |
The Snowflake error code. For example, |
|
string |
A description of what went wrong. For example, |
Sample request¶
{
"model": "llama3.3-70b",
"response_instruction": "You will always maintain a friendly tone and provide concise response. When a user asks you a question, you will also be given excerpts from different sources provided by a search engine. Use that information to provide a summary that addresses the user's question. Question: {{.Question}}\n\nContext: {{.Context}}",
"experimental": {},
"tools": [
{
"tool_spec": {
"type": "cortex_analyst_text_to_sql",
"name": "Analyst1"
}
},
{
"tool_spec": {
"type": "cortex_analyst_text_to_sql",
"name": "Analyst2"
}
},
{
"tool_spec": {
"type": "cortex_analyst_sql_exec",
"name": "SQL_exec1"
}
},
{
"tool_spec": {
"type": "cortex_search",
"name": "Search1"
}
}
],
"tool_resources": {
"Analyst1": { "semantic_model_file": "stage1"},
"Analyst2": { "semantic_model_file": "stage2"},
"Search1": {
"name": "db.schema.service_name",
"filter": {"@eq": {"region": "North America"} }
}
},
"tool_choice": {
"type": "auto"
},
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What are the top three customers by revenue?"
}
]
},
{
"role": "assistant",
"content": [
{
"type": "tool_use",
"tool_use": {
"tool_use_id": "tool_001",
"name": "Analyst1",
"input": {
"messages": [
"role:USER content:{text:{text:\"What are the top three customers by revenue?\"}}"
],
"model": "snowflake-hosted-semantic",
}
}
},
{
"type": "tool_results",
"tool_results": {
"status": "success",
"tool_use_id": "tool_001",
"content": [
{
"type": "json",
"json": {
"sql": "select * from table",
"text": "This is our interpretation of your question: What are the top three customers by revenue? \n\n"
}
}
]
}
},
{
"type": "tool_use",
"tool_use": {
"tool_use_id": "tool_002",
"name": "SQL_exec1",
"input": {
"sql": "select * from table"
}
}
}
]
},
{
"role": "user",
"content": [
{
"type": "tool_results",
"tool_results": {
"tool_use_id": "tool_002",
"result": {
"query_id": "kjlasdfasdfh234a"
}
}
}
]
}
]
}
Sample response¶
{
"id": "msg_001",
"object": "message.delta",
"delta": {
"content": [
{
"index": 0,
"type": "tool_use",
"tool_use": {
"tool_use_id": "toolu_XXXXXX",
"name": "analyst1",
"input": {
"messages": [
"role:USER content:{text:{text:\"count book id\"}}"
],
"model": "snowflake-hosted-semantic",
"experimental": ""
}
}
},
{
"index": 0,
"type": "tool_results",
"tool_results": {
"tool_use_id": "toolu_XXXXXX",
"content": [
{
"type": "json",
"json": {
"suggestions": [],
"sql": "WITH __books AS (\n SELECT\n book_id\n FROM user_database.user_schema.user_table\n)\nSELECT\n COUNT(book_id) AS book_count\nFROM __books\n -- Generated by Cortex Analyst\n;",
"text": "This is our interpretation of your question:\n\n__Count the total number of books__ \n\n"
}
}
],
"status": "success"
}
}
]
}
}