DATA_AGENT_RUN
functiondeferredAI & MLRuns a Cortex Agent and returns the response as JSON. You can use this function to run a Cortex Agent, which orchestrates across both structured and unstructured data sources to deliver insights. This includes planning tasks, using tools to execute these tasks, and generating responses. Note SNOWFLAKE.CORTEX.DATA_AGENT_RUN is a utility wrapper around the Cortex Agents Run API. For most application integrations, Snowflake recommends calling the streaming REST API directly.
Syntax
supportedpartialnot supportednot scored
SNOWFLAKE.CORTEX.DATA_AGENT_RUN( '<agent_name>[!<version>]', <request_body> [, <create_thread_if_not_present> ] )
Parameters
| Name | Required | Description |
|---|---|---|
'agent_name[!version]' | required | Fully qualified name of the agent to run, in the form database.schema.agent_name. You can optionally append !<version> to target a specific agent version. If no version suffix is specified, the agent’s DEFAULT version is used (which falls back to the LIVE version if no default has been explicitly set). The following version values are supported: Version suffix Description !LIVE Runs the current LIVE (draft) version. !DEFAULT Runs the DEFAULT version. !VERSION$N Runs a specific committed version (for example, !VERSION$2). !LAST Runs the most recently committed version. !FIRST Runs the first committed version.ExpandShow lessSee more If the agent name contains a ! character, enclose that part of the name in double quotes so it is not treated as a version separator. For example, 'db.schema."my!agent"!LIVE' targets the LIVE version of an agent named my!agent. |
request_body | required | JSON request body to send to the agent. This value must be a string (for example, a $$...$$ literal). The following fields are supported in the request body: Field Type Description thread_id integer The thread ID for the conversation. If thread_id is used, then parent_message_id must be passed as well. parent_message_id integer The ID of the parent message in the thread. If this is the first message, parent_message_id should be 0. messages array of Message If thread_id and parent_message_id are passed in the request, messages includes the current user message in the conversation. Else, messages includes the conversation history and the current message. Messages contains both user queries and assistant responses in chronological order. background boolean Whether to run the agent asynchronously. If true, the agent runs asynchronously in the background with a 6 hour timeout, even if the client disconnects. For a background run with stream set to false, the API returns immediately with status: in_progress and a run_id; retrieve the response after the run completes by streaming from the Stream Agent Run endpoint with the run_id (REST API) or by polling the THREAD_MESSAGES SQL function with the thread ID. If stream is true, the response is streamed as Server-Sent Events. If background is false, the agent runs synchronously with a 15 minute timeout. Only available when using threads to manage conversation history. stream boolean Whether to return a streaming response (text/event-stream) or a non-streaming JSON response (application/json). If true, the response will be streamed as Server-Sent Events. If false, the response will be returned as JSON. tool_choice ToolChoice Configures how the agent should select and use tools during the interaction. Controls whether tool use is automatic, required, or whether specific tools should be used. client_metadata object Free-form JSON object your application attaches to the request, for example to record which end user or client made it. Snowflake does not interpret it. It is not sent to the model. The serialized object must be at most 4096 bytes (UTF-8); larger requests are rejected, not truncated. It is recorded on the CORTEX_AGENT_REQUEST event in the agent’s AI Observability event table, under the attribute snow.ai.observability.client_metadata, as a JSON string. It is not a column in CORTEX_AGENT_USAGE_HISTORY; to attribute usage, join the event to that view on the request ID. Do not put secrets or personal data in it. Prefer opaque identifiers over emails.ExpandShow lessSee more Example Copy code { "thread_id": 0, "parent_message_id": 0, "messages": [ { "role": "user", "content": [ { "type": "text", "text": "What is the total revenue for 2023?" } ], "status": "completed", "error": { "code": "399504", "message": "Error during execution" } } ], "background": false, "stream": false, "tool_choice": { "type": "auto", "name": [ "analyst_tool", "search_tool" ] }, "client_metadata": { "user_id": "u-1234", "client": "CLIENT_A" } } |
create_thread_if_not_present | required | A BOOLEAN value that specifies whether to automatically create a new thread when the request body does not include a thread_id. Default: FALSE. |
Returns
Returns a JSON string containing the agent’s response. The function returns the final aggregated response rather than the individual SSE events produced by the Cortex Agents Run API. If a non-fatal warning occurs, the response includes a top-level warnings array. For example, warning code 399569 means that the caller’s role couldn’t access a named tool and the agent continued with its remaining tools.
Usage notes
- The function returns a JSON string. Pass this string to TRY_PARSE_JSON to convert the response to a VARIANT value.
- Inspect the top-level warnings array in the parsed response and surface relevant warnings to users. For inaccessible-tool warning behavior, see .
- To continue a conversation in a thread, pass thread_id and set parent_message_id to the assistant_message_id from the metadata object of the previous response. Use 0 for the first message in a thread.
- When create_thread_if_not_present is set to TRUE, a new thread is automatically created if the request body does not contain a thread_id. The response includes the thread_id of the newly created thread, which you can use in subsequent requests to continue the conversation.
- To run the agent asynchronously, set "background": true in the request body and include a thread_id. The function returns immediately with an in-progress status and a run_id. Use THREAD_MESSAGES (SNOWFLAKE.CORTEX) to poll for the completed response.
Access control requirements
To run an agent, you must use a role that can access Cortex Agents and the agent object you’re calling. For details, see API access roles.
Known gaps
- The emulator does not run Cortex Agents or keep their conversation threads, so there is no agent response or thread to return.