- Categories:
String & binary functions (AI Functions)
AGENT_ RUN (SNOWFLAKE.CORTEX)¶
Runs a Cortex Agent without an agent object and returns the response as JSON.
You can use this function to interact with Cortex Agents directly without first creating an agent object. You provide the configuration, including the orchestration model and tools, in the request body.
Note
SNOWFLAKE.CORTEX.AGENT_RUN is a utility wrapper around the Cortex Agents Run REST API.
For most application integrations, Snowflake recommends calling the streaming REST API directly.
Syntax¶
Arguments¶
request_bodyJSON 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_idinteger The thread ID for the conversation. If thread_id is used, then parent_message_id must be passed as well.
parent_message_idinteger The ID of the parent message in the thread. If this is the first message, parent_message_id should be 0.
messagesarray 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.
backgroundboolean Whether to run the agent asynchronously. If
true, the agent runs asynchronously and returns immediately with"status": "in_progress"and arun_id. The run continues in the background with a 6 hour timeout, even if the client disconnects. Retrieve the response after the run completes by streaming from the Stream Agent Run endpoint with therun_id(REST API) or by polling THREAD_MESSAGES (SNOWFLAKE.CORTEX) with the thread ID (SQL function). Iffalse, the agent runs synchronously with a 15 minute timeout. Only available when using threads to manage conversation history.streamboolean 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_choiceToolChoice 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.
modelsModelConfig Model configuration for the agent. Includes the orchestration model (e.g., claude-4-sonnet). If not provided, a model is automatically selected. Currently only available for the
orchestrationstep.instructionsAgentInstructions Instructions for the agent’s behavior, including response, orchestration, and sample questions.
orchestrationOrchestrationConfig Orchestration configuration, including budget constraints (e.g., seconds, tokens).
toolsarray of Tool List of tools available for the agent to use. Each tool includes a tool_spec with type, name, description, and input schema. Tools may have a corresponding configuration in tool_resources.
tool_resourcesmap of ToolResource Configuration for each tool referenced in the tools array. Keys must match the name of the respective tool.
Example
create_thread_if_not_presentA BOOLEAN value that specifies whether to automatically create a new thread when the request body does not include a
thread_id. Default:FALSE.
Important
Setting stream to true in the request body is not supported. If you include "stream": true, the function returns an error.
A non-streaming response is always returned.
Asynchronous (background) runs
You can run the agent asynchronously by setting "background": true in the request body. When you do:
- A
thread_idis required in the request body. - The function returns immediately with a response containing
"status": "in_progress"and arun_id. - Poll THREAD_MESSAGES (SNOWFLAKE.CORTEX) with the thread ID to retrieve the assistant response after the run completes.
Returns¶
Returns a JSON string containing the agent’s response.
Access control requirements¶
To run an agent, you must use a role that can access Cortex Agents. For details, see API access roles.
Usage notes¶
- The function returns a JSON string. Pass this string to TRY_PARSE_JSON to convert the response to a VARIANT value.
- Unlike DATA_AGENT_RUN (SNOWFLAKE.CORTEX), this function does not require you to create an agent object first. Instead, you provide the configuration directly in the request body.
- When
create_thread_if_not_presentis set toTRUE, a new thread is automatically created if the request body does not contain athread_id. The response includes thethread_idof the newly created thread, which you can use in subsequent requests to continue the conversation. - To run the agent asynchronously, set
"background": truein the request body and include athread_id. The function returns immediately with an in-progress status and arun_id. Use THREAD_MESSAGES (SNOWFLAKE.CORTEX) to poll for the completed response.
Examples¶
Run an agent and parse the response JSON:
Sample return value:
Run an agent with automatic thread creation: