Once LLM calls live inside your pipelines (pb-21), they become a line item — and often a shocking one, because token spend scales with data volume and nobody budgeted for it. The good news: LLM cost is the most engineerable cost in your stack.
Tier: Advanced · Workflow area: Orchestration, ops & cost
Batch APIs halve that spend. Prompt caching cuts repeated-context spend by up to ~90%. Model right-sizing routinely takes 5–10x off classification-heavy stages. This playbook installs the four levers — batching, caching, right-sizing, and token budgeting — as pipeline engineering, with measurements that prove each lever's effect.
If the pipeline's LLM spend is trivially small, spend your time on warehouse cost instead (pb-25) — same skills, bigger number.
Meter first. You cannot engineer what you don't measure. Every LLM task writes usage per run to a metering table: model, input/output tokens, cache-read tokens, cost, rows processed → cost-per-thousand-rows per stage. One week of metering tells you which stage deserves which lever.
Lever 1 — batch everything batchable (~50% off). Pipelines are asynchronous by nature; any stage that tolerates minutes-to-hours latency belongs on the provider batch API at half price. This is usually the single largest, lowest-risk saving:
from anthropic.types.message_create_params import MessageCreateParamsNonStreaming
from anthropic.types.messages.batch_create_params import Request
batch = client.messages.batches.create(requests=[
Request(
custom_id=row_hash,
params=MessageCreateParamsNonStreaming(
model="claude-opus-4-8",
max_tokens=1024,
thinking={"type": "adaptive"},
messages=[{"role": "user", "content": build_prompt(row)}],
),
)
for row_hash, row in partition_rows
])
Wire it with the persist-batch-id / deferrable-poll / dead-letter pattern from pb-21 so retries never double-bill.
Lever 2 — prompt caching for repeated context. If every request carries the same instruction block, taxonomy, or document preamble, structure prompts as stable prefix first, varying row last and mark the prefix with a cache breakpoint. Cache reads bill at ~0.1x; verify hits by checking cache-read tokens in your metering table — a zero there means a silent invalidator (timestamps or unsorted JSON in the prefix are the classic culprits). Caching stacks with batching.
Lever 3 — right-size the model per stage. A frontier model classifying sentiment is a truck delivering a letter. Build a small eval set (200–500 labeled rows per stage), run it against a small model, a mid-tier, and your current one, and pick the cheapest model that clears your quality bar per stage — classification and routing usually drop to a small model; nuanced extraction and generation stay frontier. Warehouse-native per-token SQL functions (Snowflake Cortex) and self-hosted serving (vLLM; Ollama for prototyping) enter the comparison here too — self-hosting wins only at sustained high volume once GPU cost and ops burden are priced honestly.
Lever 4 — token budgeting as a pipeline control. Budgets, not hopes: a per-run token budget parameter, checked before submission (estimate = sampled tokens-per-row × row count) with fail-fast when exceeded; truncation/summarization rules for oversized inputs so one pathological document doesn't cost a dollar; max_tokens set per stage to what the output schema needs, not a default 4k; and an alert when cost-per-thousand-rows drifts >20% from trailing average — drift means someone changed a prompt or the data changed shape.
Embeddings get the same treatment. Batch embedding endpoints or warehouse-native generation for bulk; skip re-embedding unchanged content (content-hash check — the single biggest embedding saving in CDC-fed indexes); and consider dimension truncation (Matryoshka-style) where your vector store's footprint, not the API bill, is the cost center.
Prove each lever. Turn levers on one at a time across runs and read the metering table: batch on → cost-per-kilorow halves; caching on → cache-read tokens appear and input cost drops; model swap → cost drops and the eval score stays above bar. Publish the before/after per lever — that table is the deliverable that gets the next AI pipeline approved.
Right-sizing without an eval set is just downgrading. The small model is only cheaper if it still meets the quality bar — measure on your labeled rows before and after every model change, and keep the eval in CI so a provider model update can't silently degrade a stage.
Cost engineering is one of the safest AI engagements to run for clients — the levers operate on how calls are made, not on new data exposure, and every change is measured against the client's own metering. Build the metering table in the client's warehouse from day one, use the client's approved provider or in-platform functions, and present each lever as a before/after row with the eval evidence attached for any model change. Contract the outcome as a cost-per-thousand-rows reduction with quality held constant — it's measurable, it survives audit, and it makes the follow-on conversation ("apply this to your other three pipelines") sell itself.