Cortex Code in Snowflake: How to Use It Without Burning Credits
From the publisher’s feed, collected in the ZeroLabs newsroom.
Snowflake Cortex Code (CoCo) is like an AI assistant inside Snowsight (and CLI also). You can ask it to write SQL, create dbt models, explore data, help in ML work, and even do some admin tasks.But one thing people don’t realise early — this tool is powerful, but also costly if used wrongly.Bad prompts → more tokens → more credits → surprise bill.Prompt Engineering (this directly impacts cost)CoCo works on token consumption.what you type → counted2. what it replies → countedIf your prompt is vague → more tool calls → more cost.Example:Bad: Help me with my dataGood: Create staging model for RAW.SALES.ORDERS with not_null on ORDER_IDBest Practices:Use full table names2. Be clear about output3. Keep prompts small4. Provide business logic upfront5. Use AGENTS.md for consistencyMonitoring CoCo usage & NotificationCoCo is serverless → no warehouse used.Use:SNOWFLAKE.ACCOUNT_USAGE.CORTEX_CODE_SNOWSIGHT_USAGE_HISTORYSNOWFLAKE.ACCOUNT_USAGE.CORTEX_CODE_CLI_USAGE_HISTORYTrack:daily credits2. per user usageNote: Data delay of 45 mins to 2 hours.-- ============================================================-- CoCo Monitoring: Daily credit usage by user (last 30 days)-- ============================================================SELECT DATE(u.USAGE_TIME) AS usage_date, us.NAME AS user_name, ROUND(SUM(u.TOKEN_CREDITS), 4) AS daily_credits, SUM(u.TOKENS) AS total_tokens, COUNT(*) AS request_countFROM SNOWFLAKE.ACCOUNT_USAGE.CORTEX_CODE_SNOWSIGHT_USAGE_HISTORY uLEFT JOIN SNOWFLAKE.ACCOUNT_USAGE.USERS us ON u.USER_ID = us.USER_IDWHERE u.USAGE_TIME >= DATEADD('day', -30, CURRENT_TIMESTAMP())GROUP BY DATE(u.USAGE_TIME), us.NAMEORDER BY usage_date DESC, daily_credits DESC;NotificationAccount Budget → only alertsAccount Budget Email Notifications-- Activate the account budgetCALL SNOWFLAKE.LOCAL.ACCOUNT_ROOT_BUDGET!ACTIVATE();-- Set a monthly spending limit (in credits)CALL SNOWFLAKE.LOCAL.ACCOUNT_ROOT_BUDGET!SET_SPENDING_LIMIT(7);-- Set email notifications (emails must be verified)CALL SNOWFLAKE.LOCAL.ACCOUNT_ROOT_BUDGET!SET_EMAIL_NOTIFICATIONS( 'admin@company.com, manager@company.com');2. Snowflake Alerts → custom monitoring-- Create email notification integrationCREATE OR REPLACE NOTIFICATION INTEGRATION coco_cost_alerts TYPE = EMAIL ENABLED = TRUE ALLOWED_RECIPIENTS = ('admin@company.com');-- Create alert: fires when CoCo exceeds 2 credits in 24 hoursCREATE OR REPLACE ALERT coco_daily_spend_alert WAREHOUSE = COMPUTE_WH SCHEDULE = 'USING CRON 0 * * * * UTC' IF (EXISTS ( SELECT 1 FROM SNOWFLAKE.ACCOUNT_USAGE.CORTEX_CODE_SNOWSIGHT_USAGE_HISTORY WHERE USAGE_TIME >= DATEADD('hour', -24, CURRENT_TIMESTAMP()) HAVING SUM(TOKEN_CREDITS) > 2 )) THEN CALL SYSTEM$SEND_EMAIL( 'coco_cost_alerts', 'admin@company.com', 'CoCo Daily Spend Alert', 'Cortex Code Snowsight usage exceeded 2 credits in the last 24 hours.' );-- Resume the alert to activate itALTER ALERT coco_daily_spend_alert RESUME;Reference : https://docs.snowflake.com/en/user-guide/cortex-code/credit-usage-limit3. No hard stop via budgetSetting LimitsSnowflake provides two parameters to set daily credit limits on CoCo per user:CORTEX_CODE_SNOWSIGHT_DAILY_EST_CREDIT_LIMIT_PER_USER CORTEX_CODE_CLI_DAILY_EST_CREDIT_LIMIT_PER_USERHow it works: Tracks a rolling 24-hour window. When a user’s estimated credits hit the limit, access is blocked until usage drops below.Account Level All UsersALTER ACCOUNT SET CORTEX_CODE_SNOWSIGHT_DAILY_EST_CREDIT_LIMIT_PER_USER = 5;ALTER ACCOUNT SET CORTEX_CODE_CLI_DAILY_EST_CREDIT_LIMIT_PER_USER = 10;Per User OverrideALTER USER power_user SET CORTEX_CODE_SNOWSIGHT_DAILY_EST_CREDIT_LIMIT_PER_USER = 20;ALTER USER intern_user SET CORTEX_CODE_SNOWSIGHT_DAILY_EST_CREDIT_LIMIT_PER_USER = 0Remove the LimitsALTER ACCOUNT UNSET CORTEX_CODE_SNOWSIGHT_DAILY_EST_CREDIT_LIMIT_PER_USER;ALTER USER jsmith UNSET CORTEX_CODE_SNOWSIGHT_DAILY_EST_CREDIT_LIMIT_PER_USER;Reference: https://docs.snowflake.com/en/user-guide/cortex-code/credit-usage-limitLimitations of CoCoNot supported:1. file upload in chat2. external API calls3. background jobs4. multi-session memory5. large context handling fully6. free tierWorkarounds:use stage for files2. use external functions for APIs3. use AGENTS.md for memoryFinal Summary1.Prompt clearly2. Monitor usage3. Set limits early4. Understand boundariesClosing ThoughtCoCo is powerful but needs proper usage. Otherwise it becomes expensive and confusing.Cortex Code in Snowflake: How to Use It Without Burning Credits was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.