AI Engineering · Module 10
Cost, latency and scaling
- Lessons
- 11
- Free
- 0
- Reading
- ~3 h
Your progress
0 / 11 lessons done
Sign in to trackLessons
- Where the time goes in an LLM requestWhen a person waits for a model, “it’s slow” is not something you can fix. Engineers who can say which stage holds the seconds, and which lever moves that stage, spend their time on the change that helps instead of the one that sounds fast.
- Caching for LLM apps: exact, semantic and promptEvery LLM feature repeats itself: the same questions from users, the same instructions on every call. Caching the right repeat cuts the bill and the wait; caching the wrong one shows a customer someone else’s answer, with no error to warn you.
- Token economics: caching, batch and effort togetherEvery team that ships model features gets asked to make them cheaper. The cuts that look biggest are often the smallest, the savings don’t add up the way a spreadsheet suggests, and the wrong cut moves the cost from the invoice onto your users.
- Model routing: small models first, large when neededMost AI features serve a mix of easy and hard requests. Routing is how you pay large-model prices only for the hard ones, and a careless router can cost more than never routing at all.
- Batch and background processing for LLM jobsEvery AI product collects work nobody waits for: eval runs, audits, and backfills after a prompt or model change. Running it at half price, without crowding out live traffic and without paying twice after a crash, is everyday production work.
- Provider rate limits, queues and fallbacksOnce several features call the same model, they share one set of limits. On the busiest morning the work nobody is waiting for can take the room from the person who is, unless your own code decides the order.
- Cost control: budgets, quotas and alertsA model API bills every call, and a bug or an abusive customer can multiply the calls overnight without any error. Cost control is what turns that from a surprise on the invoice into a page within minutes and a feature that switches off on purpose.
- Charging for AI features: metering and quotas per customerEvery AI feature sold to customers needs a bill they trust and a price above what the model calls cost. A meter that counts the wrong thing, or counts it twice, turns into refunds, angry emails and features that lose money on the biggest customers.
- Self-hosting open models: when and howOnce one AI job runs a hundred thousand times a day, someone asks whether a small model on your own GPU would be cheaper. The honest answer needs the GPU price, the memory arithmetic and the people who keep it running.
- Case: Instacart’s batch platform for LLM jobsOnce several teams send millions of prompts, the provider’s batch API is the easy part. Splitting files, surviving crashes, sorting failures and charging each team is most of the work, and Instacart published how it shared that work across the company.
- Design: scaling a chat assistant to a million usersAn AI feature that works for a few customers usually breaks at scale on limits nobody sized: the provider’s tokens per minute, budgets set for normal days, and retries that multiply load. Planning the peak, and what users see when it is exceeded, is the work that keeps a launch from turning into an outage.
Practise with
No tools or paper guides for this module yet.
Terms this module teaches 46
Open glossaryAcceleration limit
A provider limit on how fast an organization’s usage may rise, which can return 429 errors after a sharp jump even while usage is below the per-minute limits.
Admin API key
A Claude API credential for managing an organization and reading its usage and cost reports. Ordinary workspace API keys can’t call these endpoints.
AI gateway
An internal service that every model call passes through, giving one interface to several model providers and recording usage and cost per caller.
Batch processing (model APIs)
Sending many model requests as one job that the provider runs when it has capacity, returning results later at a lower price.
Billable unit
The thing a usage price is attached to, such as one AI draft shown or one chat conversation. A good one is something the customer can see and count, with a cost behind it the seller can bound.
Cache pre-warming
Sending a request ahead of real traffic so the provider writes a prompt prefix to its cache; on the Claude API, a request with max_tokens set to 0 writes the cache and generates no output.
Continuous batching
Batching in which requests join and leave the running batch at every model step, instead of the whole batch starting and finishing together.
Cost meter
Code that prices every model call from the usage in its response, as soon as it returns, and records the cost with labels such as feature, customer and prompt version.
Cost reconciliation
Comparing your own record of what model calls cost with the provider’s report for the same period, to find wrong prices and calls that skipped your meter.
Custom id (batch request)
The id you give each request in a Message Batch, 1 to 64 letters, digits, hyphens or underscores, unique in that batch. Results come back in any order, so it is the only way to match a result to its request.
Degradation ladder
An ordered list of lighter ways a feature can answer when capacity runs short, each freeing a known share of a limit, from the full answer down to a fallback that uses no model at all.
Durable execution
Running a long job so that each finished step is recorded, and after a crash the job resumes from the last recorded step instead of starting over.
Escalation check
The test in a model cascade that decides whether the cheaper model’s answer is kept or sent to a larger model. It runs on every request, so its own cost is part of every task.
Escalation rate
The share of requests a router or cascade sends to the more capable model. It sets most of the routed cost and is the first sign that a check or a router has changed.
Fallback model
A second model, tested on the eval set in advance, that a feature switches to when its main model is unavailable or misbehaving.
Fast mode
A request setting, speed: "fast", that runs a supported Claude model with a faster inference configuration for more output tokens per second, at a higher price. It does not shorten the time to first token.
Gross margin
The price of what was sold minus the direct cost of delivering it, often given as a share of the price.
Hard limit
A usage level at which a feature stops until the period ends or the limit is raised.
Help-center version
A per-shop number in Deskora that goes up whenever the shop’s searchable articles change, put in cache keys so every reply made from older articles is no longer looked up.
Inference server
A program that loads a model’s weights onto one or more GPUs and answers model requests over HTTP, running many of them at once. vLLM and SGLang are open-source examples.
ITPM (input tokens per minute)
A provider rate limit on how many input tokens one organization may send to a model each minute. On most current Claude models, tokens read from the prompt cache don’t count toward it.
Load test
Running generated traffic against a system at and above its expected peak to measure latency, errors and limits before real users arrive.
Model cascade
A way of routing in which a cheaper model answers first, a check tests the answer, and only answers that fail the check are redone by a more capable model.
Model fallback
Sending a task to a second model or provider when the main one is rate-limited, failing or down, usually with its own prompt version, tested before it is needed.
Model routing
Sending each request to one of several models by a rule, so easy requests go to a cheap model and hard ones to a more capable one.
Normalised question
A user’s question after changes that can’t alter its meaning, such as letter case, extra spaces and trailing punctuation, used as part of a cache key.
PagedAttention
vLLM’s way of storing the KV cache in small fixed-size blocks, so each request uses only the memory its tokens fill and more requests fit on one GPU.
Parquet
An open file format that stores a table column by column, compressed, so large data sets take less space and a program can read only the parts it needs.
Priority lane
A class of work that shares a rate limit with others but has its own rule for how much of the remaining room it may take, so work a person is waiting for goes first.
Rate-limit headers
Response headers in which a model provider reports, for each limit, the maximum, what remains and when the bucket is full again, so a client can slow down before it gets 429 errors.
Response cache
A cache in your own app that stores a finished model reply under a key, so a later request with the same key gets the stored reply without calling the model.
Routing classifier
A small model, or a cheap model call, that reads a request before any answer is written and predicts whether the cheaper model will handle it, so code can send it to the cheaper or the more capable model.
Seat pricing
Charging a business customer a fixed amount per user each month, where each user is a seat. Features in the plan, such as an AI allowance, are often sized per seat.
Semantic cache
A response cache that looks up a question by the meaning of its embedding, returning a stored reply when the closest cached question is similar enough.
Similarity threshold
The lowest similarity score at which a semantic cache treats two questions as the same; higher means fewer hits and fewer wrong answers.
Soft limit
A usage level that triggers a warning but doesn’t stop anything.
Spend reservation
Adding a call’s worst-case cost to a spend counter before the call, and correcting it to the real cost afterwards, so many workers running at once can’t pass a cap together.
Spend spike alert
An alert that compares spend over the last few minutes with spend at the same time on normal days, and pages when it is several times higher and above a dollar floor.
Token counting endpoint
An API call that estimates how many input tokens a request would use on a given model, without running the model. On the Claude API it is free and has its own rate limit.
Uncached input tokens
Input tokens the model processes fresh, neither written to nor read from the prompt cache, billed at the normal input price. On the Claude API they are input_tokens in a response and uncached_input_tokens in the usage report.
Usage and Cost API
Anthropic’s Admin API endpoints that report an organization’s billed token counts (by minute, hour or day) and costs (by day), grouped by workspace, API key, model and other dimensions.
Usage credit
A new usage record with a negative quantity that cancels an earlier one, used instead of editing or deleting the original so the bill can be rebuilt and every change has a reason.
Usage reconciliation
A regular job that compares billed usage records with the system’s own records of the work, and holds an invoice when they disagree.
Versioned price table
A table of model prices in which each set of prices has a version and a start date, and each recorded call keeps the version it was priced with, so past costs don’t change when prices do.
Workspace (Claude API)
A named group of API keys inside one organization, with its own limits and its own prompt cache, that usage and cost reports can group by.
Workspace spend limit
A monthly cap on what one workspace of a model provider account may spend, set lower than the organization’s cap. Requests in that workspace fail once it is reached.