Optimising the Cost of AI and ML Workloads
This lesson explains the cost levers for generative AI and ML inference: model choice, service tiers, batching, caching and scale-to-zero hosting.
Learning outcomes
After completing this lesson, you will be able to:
- Explain why model choice is the largest generative AI cost lever.
- Choose between service tiers, batch inference, prompt caching and semantic caching.
- Identify cost levers for self-hosted inference on SageMaker AI.
Model choice and service tiers
The largest lever is the model: smaller models cost a fraction of the largest ones per token and respond faster, so evaluations should find the smallest model that meets quality targets for each task. Intelligent prompt routing can send simpler requests to a smaller model in the same family, and model distillation trains a smaller model from a larger one's outputs. Bedrock service tiers then price the same model differently: Priority at a premium for latency-critical traffic, Standard by default, Flex at a discount for work that tolerates longer processing, and Reserved capacity for guaranteed tokens per minute.
The biggest AI cost lever
Batching and caching
Batch inference processes large sets of prompts from S3 asynchronously at about half the on-demand price, which suits nightly classification, summarisation or enrichment. Prompt caching stores repeated prompt prefixes, such as long system prompts, instructions or documents, so later requests pay much less for those tokens and respond faster. Application-level semantic caching, for example in ElastiCache for Valkey with vector search, answers repeated or very similar questions without calling the model.
| Workload pattern | Cost lever |
|---|---|
| Same long prompt prefix on every request | Prompt caching |
| Millions of offline prompts | Batch inference |
| Latency-tolerant background generation | Flex tier |
| Repeated user questions | Semantic cache in front of the model |
| Simple tasks on an expensive model | Smaller model or prompt routing |
AI cost levers
Self-hosted inference on SageMaker AI
For models on SageMaker AI, idle instances are the main waste. Serverless inference and asynchronous inference scale to zero; multi-model endpoints and inference components share instances among models; batch transform avoids persistent endpoints; and Machine Learning Savings Plans discount steady usage. Data transfer adds cost too: keep knowledge bases, vector stores and endpoints in the application's Region, use VPC endpoints instead of NAT gateways, and rely on cross-Region inference, which adds no routing charge, rather than building custom multi-Region inference.
Halving an assistant's bill
A support assistant's Bedrock bill doubled in three months. Analysis shows a 9,000-token system prompt on every request, many repeated questions, and a nightly job that summarises every ticket with the largest model.
The team enables prompt caching for the system prompt, adds a semantic cache that answers the most frequent questions, and evaluates a smaller model, which matches quality for summaries.
The nightly job moves to batch inference with the smaller model. Together these changes cut the bill by more than half with no loss of quality in the evaluation set.
Which feature reduces the cost of a 9,000-token system prompt sent with every request?
Show answer
Prompt caching, which stores the repeated prefix so later requests pay much less for those tokens and respond faster.
Exam Tips
Remember for the exam
- Repeated long prompts: prompt caching; large offline jobs: batch inference.
- Latency-tolerant work: Flex tier; guaranteed throughput for critical traffic: Reserved tier.
- Choose the smallest model that meets evaluated quality.
- Idle self-hosted endpoints: serverless or asynchronous inference that scales to zero.