# Snowflake Intelligence Cost Queries

Queries for analyzing credit and token cost for Snowflake Intelligence interactions.

**Semantic keywords:** Snowflake Intelligence, SI, intelligence agent, SI credits, intelligence cost, intelligence costs

**Base View:** `SNOWFLAKE.ACCOUNT_USAGE.SNOWFLAKE_INTELLIGENCE_USAGE_HISTORY`

---

### Snowflake Intelligence Cost per User

**Triggered by:** "How many Snowflake Intelligence credits did each user consume?", "SI credits by user", "which users used the most Snowflake Intelligence?"

> **Note:** Keep the time window to at most one month to avoid query timeouts.

```sql
SELECT
    USER_NAME,
    SUM(TOKEN_CREDITS) AS total_credits,
    SUM(TOKENS) AS total_tokens,
    COUNT(DISTINCT REQUEST_ID) AS request_count
FROM SNOWFLAKE.ACCOUNT_USAGE.SNOWFLAKE_INTELLIGENCE_USAGE_HISTORY
WHERE START_TIME >= '<START_TIME>'
  AND START_TIME < '<END_TIME>'
GROUP BY USER_NAME
ORDER BY total_credits DESC;
```

---

### Snowflake Intelligence Cost by Instance

**Triggered by:** "Which SI instance is consuming the most credits?", "credits by intelligence instance", "most expensive Snowflake Intelligence deployment"

> **Note:** Keep the time window to at most one month to avoid query timeouts.

```sql
SELECT
    SNOWFLAKE_INTELLIGENCE_NAME,
    SUM(TOKEN_CREDITS) AS total_credits,
    SUM(TOKENS) AS total_tokens,
    COUNT(DISTINCT REQUEST_ID) AS request_count,
    COUNT(DISTINCT USER_NAME) AS unique_users
FROM SNOWFLAKE.ACCOUNT_USAGE.SNOWFLAKE_INTELLIGENCE_USAGE_HISTORY
WHERE START_TIME >= '<START_TIME>'
  AND START_TIME < '<END_TIME>'
GROUP BY SNOWFLAKE_INTELLIGENCE_NAME
ORDER BY total_credits DESC;
```

---

### Snowflake Intelligence Cost by Agent

**Triggered by:** "Which agents inside Snowflake Intelligence cost the most?", "SI agent cost breakdown", "most expensive agents"

> **Note:** Keep the time window to at most one month to avoid query timeouts.

```sql
SELECT
    SNOWFLAKE_INTELLIGENCE_NAME,
    AGENT_NAME,
    SUM(TOKEN_CREDITS) AS total_credits,
    SUM(TOKENS) AS total_tokens,
    COUNT(DISTINCT REQUEST_ID) AS request_count
FROM SNOWFLAKE.ACCOUNT_USAGE.SNOWFLAKE_INTELLIGENCE_USAGE_HISTORY
WHERE START_TIME >= '<START_TIME>'
  AND START_TIME < '<END_TIME>'
GROUP BY SNOWFLAKE_INTELLIGENCE_NAME, AGENT_NAME
ORDER BY total_credits DESC;
```

---

### Snowflake Intelligence Daily Cost Trend (with Week-over-Week Change)

**Triggered by:** "Show me the daily trend of Snowflake Intelligence cost", "SI cost over time", "how has SI spend trended?"

> **Note:** Keep the time window to at most one month to avoid query timeouts.

```sql
WITH daily AS (
    SELECT
        DATE(START_TIME) AS usage_date,
        ROUND(SUM(TOKEN_CREDITS), 4) AS daily_credits,
        SUM(TOKENS) AS daily_tokens,
        COUNT(DISTINCT REQUEST_ID) AS daily_requests
    FROM SNOWFLAKE.ACCOUNT_USAGE.SNOWFLAKE_INTELLIGENCE_USAGE_HISTORY
    WHERE START_TIME >= DATEADD('day', -7, '<START_TIME>')
      AND START_TIME < '<END_TIME>'
    GROUP BY DATE(START_TIME)
),
with_prev AS (
    SELECT
        usage_date,
        daily_credits,
        daily_tokens,
        daily_requests,
        LAG(daily_credits, 7) OVER (ORDER BY usage_date) AS prev_week_credits
    FROM daily
)
SELECT
    usage_date,
    daily_credits,
    daily_tokens,
    daily_requests,
    CASE
        WHEN daily_credits >= prev_week_credits
            THEN '+' || ROUND((daily_credits - prev_week_credits) / NULLIF(prev_week_credits, 0) * 100, 1)::VARCHAR || '%'
        ELSE ROUND((daily_credits - prev_week_credits) / NULLIF(prev_week_credits, 0) * 100, 1)::VARCHAR || '%'
    END AS wow_change_pct
FROM with_prev
WHERE usage_date >= '<START_TIME>'
ORDER BY usage_date ASC;
```

---

### Snowflake Intelligence Cost per Model

**Triggered by:** "Which models consumed the most Snowflake Intelligence credits?", "SI model cost breakdown", "credits by model for Snowflake Intelligence"

> **Note:** Keep the time window to at most one month to avoid query timeouts.

```sql
SELECT
    cf4.key AS model_name,
    ROUND(SUM(COALESCE(cf4.value:input::FLOAT, 0)), 4) AS input_credits,
    ROUND(SUM(COALESCE(cf4.value:output::FLOAT, 0)), 4) AS output_credits,
    ROUND(SUM(COALESCE(cf4.value:cache_read_input::FLOAT, 0)), 4) AS cache_read_credits,
    ROUND(SUM(COALESCE(cf4.value:cache_write_input::FLOAT, 0)), 4) AS cache_write_credits,
    ROUND(SUM(
        COALESCE(cf4.value:input::FLOAT, 0) +
        COALESCE(cf4.value:output::FLOAT, 0) +
        COALESCE(cf4.value:cache_read_input::FLOAT, 0) +
        COALESCE(cf4.value:cache_write_input::FLOAT, 0)
    ), 4) AS total_credits,
    SUM(
        COALESCE(tf4.value:input::FLOAT, 0) +
        COALESCE(tf4.value:output::FLOAT, 0) +
        COALESCE(tf4.value:cache_read_input::FLOAT, 0) +
        COALESCE(tf4.value:cache_write_input::FLOAT, 0)
    ) AS total_tokens
FROM SNOWFLAKE.ACCOUNT_USAGE.SNOWFLAKE_INTELLIGENCE_USAGE_HISTORY h,
     LATERAL FLATTEN(input => h.CREDITS_GRANULAR) cf1,
     LATERAL FLATTEN(input => cf1.value) cf2,
     LATERAL FLATTEN(input => cf2.value) cf3,
     LATERAL FLATTEN(input => cf3.value) cf4,
     LATERAL FLATTEN(input => h.TOKENS_GRANULAR) tf1,
     LATERAL FLATTEN(input => tf1.value) tf2,
     LATERAL FLATTEN(input => tf2.value) tf3,
     LATERAL FLATTEN(input => tf3.value) tf4
WHERE cf3.key != 'start_time'
  AND tf3.key != 'start_time'
  AND cf2.key = tf2.key
  AND cf3.key = tf3.key
  AND cf4.key = tf4.key
  AND h.START_TIME >= '<START_TIME>'
  AND h.START_TIME < '<END_TIME>'
GROUP BY cf4.key
ORDER BY total_credits DESC;
```

---

### Snowflake Intelligence Cost by Underlying Service Type

**Triggered by:** "How much of Snowflake Intelligence cost is from Cortex Analyst vs Cortex Agents?", "SI service type breakdown", "which tools are agents calling the most?"

> **Note:** Keep the time window to at most one month to avoid query timeouts.

```sql
SELECT
    cf3.key AS service_type,
    ROUND(SUM(
        COALESCE(cf4.value:input::FLOAT, 0) +
        COALESCE(cf4.value:output::FLOAT, 0) +
        COALESCE(cf4.value:cache_read_input::FLOAT, 0) +
        COALESCE(cf4.value:cache_write_input::FLOAT, 0)
    ), 4) AS total_credits,
    SUM(
        COALESCE(tf4.value:input::FLOAT, 0) +
        COALESCE(tf4.value:output::FLOAT, 0) +
        COALESCE(tf4.value:cache_read_input::FLOAT, 0) +
        COALESCE(tf4.value:cache_write_input::FLOAT, 0)
    ) AS total_tokens,
    COUNT(DISTINCT h.REQUEST_ID) AS request_count
FROM SNOWFLAKE.ACCOUNT_USAGE.SNOWFLAKE_INTELLIGENCE_USAGE_HISTORY h,
     LATERAL FLATTEN(input => h.CREDITS_GRANULAR) cf1,
     LATERAL FLATTEN(input => cf1.value) cf2,
     LATERAL FLATTEN(input => cf2.value) cf3,
     LATERAL FLATTEN(input => cf3.value) cf4,
     LATERAL FLATTEN(input => h.TOKENS_GRANULAR) tf1,
     LATERAL FLATTEN(input => tf1.value) tf2,
     LATERAL FLATTEN(input => tf2.value) tf3,
     LATERAL FLATTEN(input => tf3.value) tf4
WHERE cf3.key != 'start_time'
  AND tf3.key != 'start_time'
  AND cf2.key = tf2.key
  AND cf3.key = tf3.key
  AND h.START_TIME >= '<START_TIME>'
  AND h.START_TIME < '<END_TIME>'
GROUP BY cf3.key
ORDER BY total_credits DESC;
```
