
Part 1 – From Natural Language to Real Business Actions with Snowflake Cortex Agent
Building an action-oriented Cortex Agent with Semantic Views, Cortex Analyst, custom tools and deterministic business rules.
In the introductory article, we explored the architecture of a governed AI agent. Now let us build its intelligence and action layers.
Imagine asking an agent to show sales orders with discounts above 10%. Then ask it to submit one of those orders for approval. The first request retrieves information. The second creates a business record. How do we make that transition without giving the LLM unrestricted authority?
01 – The business scenario
| Requested discount | Outcome |
|---|---|
| 0–10% | APPROVED automatically |
| Above 10% | PENDING manager decision |
The agent understands intent; the stored procedure determines the outcome. Approval here records a decision in Snowflake, not an external sales transaction.
02 — Two execution paths
Sales Executive → Cortex Agent
↓
| READ PATH SalesAnalyst ↓ Semantic View ↓ SALES_ORDERS |
ACTION PATH SubmitDiscount ↓ Stored Procedure ↓ DISCOUNT_APPROVALS |
The read path answers business questions. The action path invokes a narrowly scoped procedure instead of generating arbitrary DML.
03 – Set up the environment
The following setup is for a fresh POC only. Do not recreate objects or insert sample data into an existing implementation.
USE ROLE SYSADMIN;
CREATE DATABASE IF NOT EXISTS SALES_AI_POC;
CREATE SCHEMA IF NOT EXISTS SALES_AI_POC.APPROVAL_DEMO;
CREATE WAREHOUSE IF NOT EXISTS SALES_POC_WH
WAREHOUSE_SIZE = 'XSMALL' AUTO_SUSPEND = 60
AUTO_RESUME = TRUE INITIALLY_SUSPENDED = TRUE;
USE DATABASE SALES_AI_POC;
USE SCHEMA APPROVAL_DEMO;
CREATE TABLE IF NOT EXISTS SALES_ORDERS (
ORDER_ID VARCHAR PRIMARY KEY,
CUSTOMER_NAME VARCHAR, CUSTOMER_EMAIL VARCHAR,
ORDER_AMOUNT NUMBER(12,2), DISCOUNT_PCT NUMBER(5,2),
CUSTOMER_NOTE VARCHAR
);
INSERT INTO SALES_ORDERS VALUES
('ORD-101','ABC Ltd','contact@abc.example',100000,15,'Customer requesting special pricing'),
('ORD-102','XYZ Ltd','sales@xyz.example',200000,8,'Regular customer discount'),
('ORD-103','PQR Ltd','info@pqr.example',150000,20,'High-value opportunity');
CREATE TABLE IF NOT EXISTS DISCOUNT_APPROVALS (
ORDER_ID VARCHAR PRIMARY KEY, DISCOUNT_PCT NUMBER(5,2),
STATUS VARCHAR, REQUESTED_BY VARCHAR, REQUESTED_AT TIMESTAMP_LTZ,
APPROVED_BY VARCHAR, APPROVED_AT TIMESTAMP_LTZ
);
04 – Teach the agent our business with a Semantic View
Business users speak in terms of customers, order value and discounts. The Semantic View maps those concepts to physical data and defines reusable metrics.
CREATE OR REPLACE SEMANTIC VIEW SV_SALES_ORDERS
TABLES (
ORDERS AS SALES_AI_POC.APPROVAL_DEMO.SALES_ORDERS PRIMARY KEY (ORDER_ID)
)
FACTS (
ORDERS.ORDER_VALUE AS ORDER_AMOUNT,
ORDERS.REQUESTED_DISCOUNT AS DISCOUNT_PCT
)
DIMENSIONS (
ORDERS.ORDER_NUMBER AS ORDER_ID,
ORDERS.CUSTOMER AS CUSTOMER_NAME,
ORDERS.EMAIL AS CUSTOMER_EMAIL,
ORDERS.NOTE AS CUSTOMER_NOTE
)
METRICS (
ORDERS.TOTAL_ORDER_VALUE AS SUM(ORDER_AMOUNT),
ORDERS.AVERAGE_DISCOUNT AS AVG(DISCOUNT_PCT)
)
COMMENT='Sales orders for the secure AI approval POC';

05 – Beyond Semantic Views: Building Business Trust
Following the introductory article, a valuable question emerged from the community: how do we collaborate with business teams to build a semantic layer that delivers accurate, consistent and meaningful responses?
| Design question | Our scenario / enterprise consideration |
|---|---|
| Business collaboration | Agree with sales stakeholders on the 10% threshold and ownership of approvals. |
| KPI standardization | Total order value is SUM(ORDER_AMOUNT); average discount is a simple AVG(DISCOUNT_PCT), not a weighted average. Confirm these definitions with business owners. |
| Business context | Requested discount is not the same as approved discount.
Approval status is stored separately and is not exposed by our current Semantic View. |
| Data foundation | Validate unique order IDs, non-null values and valid discount ranges. |
| Accuracy | Compare agent answers against reference SQL. |
| Verified queries | Curate business-approved question/SQL pairs as a future enhancement. |
Why Business Collaboration Matters – A Practical Example
In our Semantic View, Average Discount is defined as a simple average of the requested discount percentages, resulting in 14.33%. However, if the business expects an order-value-weighted average, the result would be 13.56%.
Both calculations are technically correct, but they represent different business interpretations.
This is where collaboration with business stakeholders becomes critical. A well-designed semantic layer should capture agreed business definitions rather than leave their interpretation to the LLM.
Our POC defines the metrics and business rules. Formal stakeholder sign-off, verified queries and automated regression evaluation are recommended extensions, not features already implemented in this demo.
SELECT ORDER_ID, CUSTOMER_NAME, DISCOUNT_PCT
FROM SALES_AI_POC.APPROVAL_DEMO.SALES_ORDERS
WHERE DISCOUNT_PCT > 10
ORDER BY ORDER_ID;
| Validation question | Expected answer on sample data |
|---|---|
| Orders above 10% discount | ORD-101 and ORD-103 |
| Total order value | 450,000 |
| Average discount | 14.33% |
| Number of orders | 3 |
| Highest discount | ORD-103 (20%) |
These reference answers provide a starting point for business review and accuracy testing. They do not constitute a configured Verified Query Repository. A reliable agent starts with reliable data and agreed business semantics.
06 – Move beyond Text-to-SQL with a custom tool
The agent supplies only the order ID. The procedure retrieves the existing discount, validates it, checks for a prior request, applies the approval rule and records the result.
CREATE OR REPLACE PROCEDURE "SP_SUBMIT_DISCOUNT"("P_ORDER_ID" VARCHAR)
RETURNS VARCHAR
LANGUAGE SQL
EXECUTE AS OWNER
AS
$$
DECLARE
V_COUNT NUMBER;
V_DISCOUNT NUMBER(5,2);
V_STATUS VARCHAR;
BEGIN
SELECT COUNT(*), MAX(DISCOUNT_PCT)
INTO :V_COUNT, :V_DISCOUNT
FROM SALES_AI_POC.APPROVAL_DEMO.SALES_ORDERS
WHERE ORDER_ID = :P_ORDER_ID;
IF (V_COUNT <> 1) THEN
RETURN 'Invalid or duplicate order ID';
END IF;
IF (V_DISCOUNT IS NULL OR V_DISCOUNT < 0 OR V_DISCOUNT > 100) THEN
RETURN 'Invalid discount percentage';
END IF;
SELECT COUNT(*) INTO :V_COUNT
FROM SALES_AI_POC.APPROVAL_DEMO.DISCOUNT_APPROVALS
WHERE ORDER_ID = :P_ORDER_ID;
IF (V_COUNT > 0) THEN
RETURN 'Approval request already exists';
END IF;
IF (V_DISCOUNT <= 10) THEN
V_STATUS := 'APPROVED';
ELSE
V_STATUS := 'PENDING';
END IF;
INSERT INTO SALES_AI_POC.APPROVAL_DEMO.DISCOUNT_APPROVALS
(ORDER_ID, DISCOUNT_PCT, STATUS, REQUESTED_BY, REQUESTED_AT,
APPROVED_BY, APPROVED_AT)
VALUES (:P_ORDER_ID, :V_DISCOUNT, :V_STATUS, CURRENT_USER(),
CURRENT_TIMESTAMP(), IFF(:V_STATUS = 'APPROVED', 'SYSTEM', NULL),
IFF(:V_STATUS = 'APPROVED', CURRENT_TIMESTAMP(), NULL));
RETURN 'Order ' || P_ORDER_ID ||
' | Discount: ' || V_DISCOUNT ||
'% | Status: ' || V_STATUS;
END;
$$;
Let deterministic code enforce business rules. The owner-rights procedure is a controlled write interface.The duplicate check is sufficient for this sequential demo, not a concurrency guarantee.
07 – Configure the Cortex Agent
Create SALES_APPROVAL_AGENT with the following two tools. Code Execution is intentionally excluded from this baseline; native HITL is covered separately in Part 3.
| Setting | SalesAnalyst | SubmitDiscount |
|---|---|---|
| Type | Cortex Analyst | Custom stored procedure |
| Resource | SV_SALES_ORDERS | SP_SUBMIT_DISCOUNT |
| Warehouse | SALES_POC_WH | SALES_POC_WH |
| Input | Natural language | P_ORDER_ID (required string) |
Orchestration instructions
Use SalesAnalyst for read-only questions about sales orders.
Use SubmitDiscount only when the user explicitly asks to submit a discount
request for a specific order. If the order ID is missing, ask for it.
The procedure uses the discount already stored for the order. If the user
asks to set or change the discount percentage, explain that this tool
accepts only an order ID.
Response instructions
Report the outcome returned by SubmitDiscount. If the request is pending,
say that it is awaiting the sales manager's decision.
Use SALES_EXECUTIVE_R for the executive and SALES_MANAGER_R for the manager.
The executive needs access to the agent, Semantic View, warehouse and submission procedure, but must not receive direct write privileges or permission to execute the manager approval procedure.


08 – See the agent in action
Experiment 1 – Read-only intelligence
Prompt: Show all sales orders where the requested discount exceeds 10%.
Expected result on a fresh POC: ORD-101 (15%) and ORD-103 (20%).

Experiment 2 — Automatic approval
Prompt: Submit the existing discount request for order ORD-102.
Expected result on a fresh POC: 8% → APPROVED.

Experiment 3 — Manager review
Prompt: Submit the existing discount request for order ORD-103 for manager review.
Expected result on a fresh POC: 20% → PENDING.

09 – Verify the business outcome
SELECT ORDER_ID, DISCOUNT_PCT, STATUS, REQUESTED_BY,
REQUESTED_AT, APPROVED_BY, APPROVED_AT
FROM SALES_AI_POC.APPROVAL_DEMO.DISCOUNT_APPROVALS
ORDER BY ORDER_ID;
| Order | Discount | Expected status after initial submissions |
|---|---|---|
| ORD-102 | 8% | APPROVED |
| ORD-103 | 20% | PENDING |

The persisted record, not the agent’s conversational claim, is the evidence of the business operation.
Coming next – Security, Governance and Human Control
Part 2 — Can You Really Trust Your AI Agent?
Agent-aware masking (IS_AGENT_ACTIVATED), indirect prompt injection detection, AI Guardrails, RBAC and observability.
Part 3 — When AI Must Stop and Ask a Human
Native tool execution permissions and manager-driven business approvals.
The real power of Agentic AI is translating business intent into controlled, reliable and auditable business actions.