Cortex Fine-Tuning
Cortex Fine-Tuning lets you customize a foundation model on your own data to improve accuracy for domain-specific tasks. The fine-tuned model runs inside Snowflake and is called through AI_COMPLETE just like any built-in model.
What Is Fine-Tuning?
Fine-tuning takes a pre-trained language model and continues training it on a smaller, task-specific dataset. The model learns patterns, terminology, and output formats relevant to your use case while retaining its general language abilities.
When to Fine-Tune vs Prompt Engineering
| Approach | Best For | Tradeoffs |
|---|---|---|
| Prompt engineering | General tasks, quick iteration, low data volume | No training cost; limited by context window |
| Few-shot prompting | Tasks where examples clarify the desired output | No training cost; adds tokens to every call |
| Fine-tuning | Consistent style, domain jargon, structured output at scale | Training cost and time; requires labeled data |
Choose fine-tuning when prompt engineering alone cannot achieve the required accuracy, or when you need consistent behavior across thousands of calls without repeating lengthy prompts.
Preparing Training Data
Training data must be a table or view with two columns in a prompt/completion conversational format.
Required Format
| Column | Type | Description |
|---|---|---|
prompt | STRING | The input text or instruction |
completion | STRING | The desired model output |
CREATE OR REPLACE TABLE FINE_TUNE_DATA (
prompt STRING,
completion STRING
);
INSERT INTO FINE_TUNE_DATA VALUES
('Classify this support ticket: "My account was charged twice for the same order."',
'{"category": "billing", "priority": "high", "sentiment": "negative"}'),
('Classify this support ticket: "When will the new dashboard feature be available?"',
'{"category": "feature_request", "priority": "low", "sentiment": "neutral"}'),
('Classify this support ticket: "I cannot log in after resetting my password."',
'{"category": "authentication", "priority": "high", "sentiment": "negative"}');
Data Quality Guidelines
- Minimum rows: At least 100 examples; 500+ recommended for strong results.
- Balanced classes: Ensure each category has roughly equal representation.
- Consistent format: Use identical output structure across all completions.
- Clean text: Remove HTML tags, excessive whitespace, and encoding artifacts.
- No data leakage: Do not include test examples in the training set.
Creating a Fine-Tuning Job
SELECT SNOWFLAKE.CORTEX.FINETUNE(
'CREATE',
'TICKET_CLASSIFIER_FT',
'llama3.1-8b',
'SELECT prompt, completion FROM FINE_TUNE_DATA',
'SELECT prompt, completion FROM FINE_TUNE_VALIDATION'
);
Parameters
| Parameter | Description |
|---|---|
| Operation | 'CREATE' to start a new job |
| Job name | Unique identifier for this fine-tuning run |
| Base model | Foundation model to fine-tune (e.g., llama3.1-8b, mistral-7b) |
| Training query | SELECT returning prompt and completion columns |
| Validation query | Optional holdout set for evaluation during training |
Monitoring Fine-Tuning Jobs
-- Check job status
SELECT SNOWFLAKE.CORTEX.FINETUNE('DESCRIBE', 'TICKET_CLASSIFIER_FT');
The response includes:
| Field | Description |
|---|---|
status | PENDING, IN_PROGRESS, SUCCESS, FAILED |
progress | Percentage complete |
base_model | The foundation model used |
trained_model | The output model name (available after SUCCESS) |
training_loss | Final training loss value |
validation_loss | Final validation loss (if validation data was provided) |
Listing All Jobs
SELECT SNOWFLAKE.CORTEX.FINETUNE('SHOW');
Using the Fine-Tuned Model
Once the job status is SUCCESS, use the output model name with AI_COMPLETE.
SELECT SNOWFLAKE.CORTEX.COMPLETE(
'TICKET_CLASSIFIER_FT', -- fine-tuned model name
'Classify this support ticket: "Your product broke after one week of use."'
) AS classification;
Batch Inference
SELECT
TICKET_ID,
TICKET_TEXT,
SNOWFLAKE.CORTEX.COMPLETE(
'TICKET_CLASSIFIER_FT',
'Classify this support ticket: "' || TICKET_TEXT || '"'
) AS classification
FROM SUPPORT_TICKETS
WHERE CREATED_AT >= DATEADD('day', -1, CURRENT_DATE());
Evaluation and Iteration
After deploying a fine-tuned model, evaluate it against a held-out test set.
-- Compare model output to ground truth
SELECT
t.TICKET_ID,
t.EXPECTED_CATEGORY,
SNOWFLAKE.CORTEX.COMPLETE(
'TICKET_CLASSIFIER_FT',
'Classify this support ticket: "' || t.TICKET_TEXT || '"'
) AS predicted,
CASE WHEN predicted ILIKE '%' || t.EXPECTED_CATEGORY || '%' THEN 1 ELSE 0 END AS correct
FROM TEST_TICKETS t;
If accuracy is insufficient:
- Add more training examples for underperforming categories.
- Improve prompt consistency in training data.
- Try a larger base model (e.g., move from 8B to 70B parameters).
- Clean noisy labels — a small set of high-quality data outperforms a large set of noisy data.
Cost Considerations
| Cost Factor | Details |
|---|---|
| Training compute | Billed per token processed during training |
| Model storage | Fine-tuned model weights stored in your account |
| Inference | Per-token cost when calling AI_COMPLETE with the model |
Fine-tuning a smaller model (8B) on a focused dataset is often more cost-effective than repeatedly sending long few-shot prompts to a larger model.
Cancelling a Job
SELECT SNOWFLAKE.CORTEX.FINETUNE('CANCEL', 'TICKET_CLASSIFIER_FT');
Key Takeaways
- Fine-tuning customizes a foundation model to your domain using your data inside Snowflake.
- Prepare clean, balanced training data with
promptandcompletioncolumns. - Monitor jobs with
FINETUNE('DESCRIBE', ...)and use the output model viaAI_COMPLETE. - Start with prompt engineering; move to fine-tuning only when prompting alone falls short.
- Evaluate rigorously and iterate on data quality before scaling up model size.