# Cortex Agents Cost Queries

Queries for analyzing credit and token cost for Cortex Agents interactions.

**Semantic keywords:** Cortex Agents, agent credits, agent costs, agent cost, cortex agent spend

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

> **Note:** This view does not include requests originating from Snowflake Intelligence. For SI cost, see `snowflake-intelligence.md`.

---

### Cortex Agent Cost by User

**Triggered by:** "How many Cortex Agent credits did each user consume?", "agent cost by user", "which users used Cortex Agents the most?"

> **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.CORTEX_AGENT_USAGE_HISTORY
WHERE START_TIME >= '<START_TIME>'
  AND START_TIME < '<END_TIME>'
GROUP BY USER_NAME
ORDER BY total_credits DESC;
```

---

### Cortex Agent Cost by Agent

**Triggered by:** "Which Cortex Agents are the most expensive?", "agent cost breakdown", "most expensive agents", "top agents by credit spend"

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

```sql
SELECT
    AGENT_DATABASE_NAME,
    AGENT_SCHEMA_NAME,
    AGENT_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.CORTEX_AGENT_USAGE_HISTORY
WHERE START_TIME >= '<START_TIME>'
  AND START_TIME < '<END_TIME>'
GROUP BY AGENT_DATABASE_NAME, AGENT_SCHEMA_NAME, AGENT_NAME
ORDER BY total_credits DESC;
```

---

### Cortex Agent Cost by Model

**Triggered by:** "Which models did Cortex Agents use the most?", "agent model cost breakdown", "credits by model for Cortex Agents"

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

```sql
SELECT
    cf4.key AS model_name,
    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)
    ) 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.CORTEX_AGENT_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;
```

---

### Cortex Agent Cost by Underlying Service Type

**Triggered by:** "What tools are my Cortex Agents calling?", "agent service type breakdown", "how much of agent cost is from Cortex Analyst vs the agent itself?"

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

```sql
SELECT
    cf3.key AS service_type,
    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)
    ) 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.CORTEX_AGENT_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;
```
