# Cortex Code CLI Cost Queries

Queries for analyzing credit and token cost for Cortex Code via the CLI.

**Semantic keywords:** Cortex Code CLI, code generation credits, code assistant CLI, coding assistant cost, CLI code credits

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

> Cost from the Cortex Code CLI. Does not include Snowsight-based requests.

---

### Cortex Code CLI Cost per User

**Triggered by:** "How many Cortex Code CLI credits did each user consume?", "code CLI credits by user", "who used Cortex Code CLI the most?"

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

```sql
SELECT
    u.NAME AS user_name,
    SUM(h.TOKEN_CREDITS) AS total_credits,
    SUM(h.TOKENS) AS total_tokens,
    COUNT(DISTINCT h.REQUEST_ID) AS request_count
FROM SNOWFLAKE.ACCOUNT_USAGE.CORTEX_CODE_CLI_USAGE_HISTORY h
LEFT JOIN SNOWFLAKE.ACCOUNT_USAGE.USERS u
    ON h.USER_ID = u.USER_ID
WHERE h.USAGE_TIME >= '<START_TIME>'
  AND h.USAGE_TIME < '<END_TIME>'
GROUP BY user_name
ORDER BY total_credits DESC;
```

---

### Cortex Code CLI Cost per Model

**Triggered by:** "Which models did Cortex Code CLI use the most?", "code CLI model cost breakdown", "credits by model for Cortex Code"

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

```sql
SELECT
    f.key AS model_name,
    ROUND(SUM(COALESCE(f.value:input::FLOAT, 0)), 4) AS input_credits,
    ROUND(SUM(COALESCE(f.value:output::FLOAT, 0)), 4) AS output_credits,
    ROUND(SUM(COALESCE(f.value:cache_read_input::FLOAT, 0)), 4) AS cache_read_credits,
    ROUND(SUM(COALESCE(f.value:cache_write_input::FLOAT, 0)), 4) AS cache_write_credits,
    ROUND(SUM(
        COALESCE(f.value:input::FLOAT, 0) +
        COALESCE(f.value:output::FLOAT, 0) +
        COALESCE(f.value:cache_read_input::FLOAT, 0) +
        COALESCE(f.value:cache_write_input::FLOAT, 0)
    ), 4) AS total_credits
FROM SNOWFLAKE.ACCOUNT_USAGE.CORTEX_CODE_CLI_USAGE_HISTORY h,
     LATERAL FLATTEN(input => h.CREDITS_GRANULAR) f
WHERE h.USAGE_TIME >= '<START_TIME>'
  AND h.USAGE_TIME < '<END_TIME>'
GROUP BY f.key
ORDER BY total_credits DESC;
```

---

### Cortex Code CLI Daily Cost Trend (with Week-over-Week Change)

**Triggered by:** "Show me the daily trend of Cortex Code CLI cost", "CLI code cost over time", "how has CLI code spend trended?"

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

```sql
WITH daily AS (
    SELECT
        DATE(USAGE_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.CORTEX_CODE_CLI_USAGE_HISTORY
    WHERE USAGE_TIME >= DATEADD('day', -7, '<START_TIME>')
      AND USAGE_TIME < '<END_TIME>'
    GROUP BY DATE(USAGE_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;
```
