Based on the MLA-C01 exam guideReport an error
MaxWaitTimeInSeconds ≥ MaxRuntimeInSeconds is the matching pair.If everything so far was about being safe (security), today is about being cheap. ML workloads see costs balloon quickly because of GPU instances, large-scale training, and always-on endpoints. The levers fall into three axes.
| Axis | Main levers | One-line gist |
|---|---|---|
| Training | Managed Spot + checkpoints · right-sizing · early stopping for tuning · MaxRuntimeInSeconds | These are batch jobs you use briefly and discard, so discounts and caps work well |
| Inference | Serverless · asynchronous · batch transform · MME · Inferentia · Auto Scaling | The key is eliminating always-on cost during idle time |
| Operations | Idle auto-shutdown for notebooks · clean up unused endpoints · Savings Plans · tags/Budgets/Cost Explorer | Catch resources left on and make usage visible |
Training is usually a job that runs once and ends (and can be resumed if interrupted), so it pairs well with Spot Instances. Managed Spot Training has SageMaker manage the Spot capacity and cuts cost by up to 90%.
{
"EnableManagedSpotTraining": true,
"StoppingCondition": {
"MaxRuntimeInSeconds": 86400,
"MaxWaitTimeInSeconds": 90000
},
"CheckpointConfig": {
"S3Uri": "s3://ml-checkpoints/job-123/"
}
}In the Python SDK, you give the same settings as Estimator parameters.
from sagemaker.estimator import Estimator
estimator = Estimator(
image_uri=container,
role=role,
instance_count=1,
instance_type="ml.g5.xlarge",
use_spot_instances=True, # Turn on Spot training
max_run=86400, # Max time for the training itself (seconds)
max_wait=90000, # Max time including Spot waiting (seconds)
checkpoint_s3_uri="s3://ml-checkpoints/job-123/", # Checkpoint in case of reclamation
)MaxWaitTimeInSeconds (max_wait) must be greater than or equal to MaxRuntimeInSeconds (max_run), because it includes the time spent waiting for Spot capacity.CheckpointConfig, then after a reclamation it resumes from the checkpoint rather than from the beginning. Using Spot without checkpoints means retraining from scratch every time it is reclaimed.You can verify whether the savings actually materialized in the job description.
import boto3
sm = boto3.client("sagemaker")
d = sm.describe_training_job(TrainingJobName="job-123")
print(d["TrainingTimeInSeconds"]) # Time actually spent training
print(d["BillableTimeInSeconds"]) # Billed time: with Spot, this is much smallerThe reason Spot suits training is that "training is a batch job that can be resumed idempotently." Even if reclaimed, continuing from a checkpoint yields the same result. Conversely, a real-time inference endpoint cuts off service when reclaimed, so Spot is inappropriate for it. "Spot for training, not for real-time inference" is the exam's basic principle.
⚠️ Pitfall: For "training with a tight deadline that must never be delayed," Spot is the wrong answer. Spot may wait for capacity and may be reclaimed, so it does not guarantee a completion time. Even in a cost question, if "deadline guarantee" is attached as a condition, On-Demand is the answer.
Choosing the wrong instance wastes money. There are two directions of waste: too big (over-provisioning), or the wrong kind for the job.
| Workload | Choose | Reason |
|---|---|---|
| Traditional ML training (XGBoost, linear, etc.) | CPU (m5/c5) | Tree and linear models gain almost nothing from a GPU. Attaching a p4d is a waste of money |
| Deep learning training | GPU (g5/p4d) | The larger the model, the more GPU memory and parallel compute performance matter |
| Reducing deep learning inference unit cost | Inferentia (inf1/inf2) | An inference-dedicated accelerator that lowers the unit cost |
| When you don't know which inference instance fits | Inference Recommender | Recommends the cost- and latency-optimal instance through load testing |
For training there is no automatic recommendation, so run a trial with a small amount of data, look at the metrics (GPU utilization, throughput), and adjust.
The principle of right-sizing is "the minimum sufficient size that fits the workload's characteristics." A GPU pays off only in parallel matrix operations (deep learning) and is nearly useless for tree models. In cost scenarios, "GPU for XGBoost" tends to be a wrong answer, and "Inferentia for deep learning inference" is a candidate for the right answer.
Tuning runs training jobs many times, so the cost grows as number of jobs × cost per job. The money that leaks here is surprisingly large.
from sagemaker.tuner import HyperparameterTuner
tuner = HyperparameterTuner(
estimator=estimator,
objective_metric_name="validation:auc",
hyperparameter_ranges=ranges,
max_jobs=20, # Total training jobs = cost ceiling
max_parallel_jobs=2, # Concurrent runs (too many reduces search efficiency)
early_stopping_type="Auto", # Stop hopeless jobs early
)max_jobs is effectively the tuning budget ceiling. Narrowing the ranges and reducing the number of jobs is the surest saving.early_stopping_type cuts jobs whose objective metric is hopeless midway to prevent waste.The big picture of inference cost is "eliminating the always-on cost of periods with zero traffic" (connected to the Week 6 review).
| Option | What is billed | When traffic is 0 | Suitable situation |
|---|---|---|---|
| Real-time endpoint | Time the instances are up | At least 1 instance's cost keeps accruing | Steady traffic, low latency essential |
| Serverless inference | Invocation processing time and memory | 0 | Intermittent, hard-to-predict traffic. Accept cold starts |
| Asynchronous inference | Instance time | Can scale down to 0 when the queue is empty | Large payloads, long processing, no immediate response needed |
| Batch transform | Only while the job runs | 0 (instances terminate when the job ends) | Bulk scoring. Cheapest |
| MME (multi-model endpoint) | Shared instance time | Instance cost still accrues | Very many models, each invoked rarely |
Cost optimization decision
Is there an idle period?
├─ Yes → Can it be batch processed? → Yes: batch transform / No: serverless (small payload) or asynchronous (large payload)
└─ No (constant traffic) → real-time + autoscaling + MME (many models) + Inferentia (deep learning)Cost-perspective decision: if there is a period when traffic drops to zero, use serverless, asynchronous, or batch to bring that period's cost close to zero. If traffic is always full, lower the unit cost with real-time + Auto Scaling + (for many models) MME + (for deep learning) Inferentia.
⚠️ Pitfall: The habit of creating one endpoint per model quietly eats away at cost. If you have dozens or hundreds of models each invoked infrequently, the answer is not shrinking instances but consolidating them with MME. Conversely, if all models always have high traffic, MME actually makes them compete for resources.
The hidden culprit of cost is resources that were "used and not turned off."
MaxRuntimeInSeconds: Cuts off a job that would run indefinitely, by time. Prevents the accident of a training job stuck by a bug burning a GPU for days.ML cost incidents usually come from "resources that weren't turned off": idle notebooks, forgotten endpoints, infinite-loop training. Automatic shutdown (idle timers, MaxRuntime, autoscaling scale-in) is a mechanism to block human error with the system. On the exam, the answer to "unexpected cost spike" is usually related to this automatic shutdown/cleanup.
You must make cost visible to control it.
| Tool | What it's for | When to pick it |
|---|---|---|
| Cost allocation tags | Attribute costs by Project, Team, Environment | When you want to see "who spent how much" separately |
| Cost Explorer | Cost trend and composition analysis, aggregation by tag | Figure out which services/instances eat the money |
| AWS Budgets | Set budget limits, alerts based on thresholds and forecasts | When you want to set a limit and get alerted when it's exceeded |
| Cost Anomaly Detection | Automatically detect cost spikes unlike the usual | When you want to catch anomalies early, before the budget |
| CloudWatch | Utilization metrics such as Invocations and ModelLatency | When proving idle or over-provisioning with evidence |
| Savings Plans / Reserved Instances | Lower rates than On-Demand through commitment | When usage is predictable, such as always-on endpoints and regular training |
[Cost governance cycle]
Apply tags ──▶ Confirm attribution with Cost Explorer ──▶ Limits/alerts with Budgets ──▶ Act on the cause of overruns
▲ │
└──────────── Enforce tags on newly created resources too (policy/automation) ◀───────────┘
Tag example (cost allocation)
TrainingJob tags: { "Project": "fraud-ml", "Team": "risk", "Environment": "prod" }
→ In Cost Explorer, filter by Project to aggregate only fraud-ml costsThere are situations where you applied tags but they don't show up in Cost Explorer. Tags aren't done just by attaching them: you must activate them as "cost allocation tags" in the Billing console for them to appear in cost reports, and they are aggregated from usage after activation. Remember also that past costs are not retroactively covered.
Cost governance is a cycle of "measure → attribute → budget → alert." You must attribute (allocate) costs to teams and projects with tags to see who spends how much, and set limits with Budgets and get alerted when exceeded. The answer to a "split costs by team" scenario is almost always cost allocation tags.
| Symptom | Common cause | Action |
|---|---|---|
| This month's cost suddenly spikes | Notebooks left on, forgotten endpoints | Set idle auto-shutdown, delete unused endpoints, Cost Anomaly Detection alerts |
| The endpoint is up but is barely invoked | A real-time endpoint used for intermittent traffic | Switch to serverless/asynchronous/batch, lower the Auto Scaling minimum capacity |
| Spot training restarts from scratch every time | CheckpointConfig not specified, or the script doesn't read the checkpoint | Specify the checkpoint path + implement resume logic |
| A Spot job ends without even starting | MaxWaitTimeInSeconds is smaller than MaxRuntimeInSeconds | Raise the ceiling including waiting to at least the training ceiling |
| Training hasn't finished for days | Runaway from failure to converge or a bug | Cap with MaxRuntimeInSeconds, rule-based automatic stop |
| Switched to GPU but cost just went up | Tree-family model gains nothing from a GPU | Return to CPU (m5/c5) |
| Tuning cost is several times what was expected | Too many jobs/concurrent runs | Reduce max_jobs, narrow the search range, enable early stopping |
| Can't tell who spends how much per team | No tags, or cost allocation tags not activated | Apply tags + activate in Billing, then aggregate in Cost Explorer |
| An always-running workload still pays On-Demand rates | Commitment discounts not applied | If usage is stable, consider Savings Plans |
Tomorrow we tie all of Week 9 (security, governance, cost) into a single decision flow.
Click a choice to reveal the answer and explanation.
Question 1
You want to cut the cost of a deep learning training job that takes several days as much as possible. The job can be resumed after interruption. What is the most appropriate configuration?
Question 2
The inference endpoint of an internal ML tool is invoked only occasionally during business hours, and traffic is 0 at night and on weekends. What is the most appropriate way to reduce cost?
Question 3
ML costs exceed expectations by a large margin every month, and an investigation shows that data scientists leave notebook instances and some real-time endpoints running after use. What is the most effective action to prevent recurrence?
Question 4
Several teams use SageMaker in a shared AWS account, and you want to see separately how much ML cost each team and project spends. What is the most appropriate method?
Question 5
Which of the following is an incorrect pairing of instance choice and cost efficiency?