Topic 5 of 5 · 6 min read
LLM enrichment steps in pipelines
Goal: Design an LLM enrichment step with a cost estimate, a structured-output check and a sampling plan for human review.
An LLM step in a pipeline is a transformation that is slow, costs money per row and sometimes returns wrong or malformed output. Treat it like any unreliable source: estimate the cost, cache results, validate the output shape and sample the results for a person to check.
Check yourself · 1 of 3
What should an LLM enrichment step do with a response whose label is not in the allowed list?
Why it matters
Models can classify support tickets, pull fields out of invoices or tag products at a scale no team could do by hand. They also guess, return text where you wanted JSON and give different answers to the same input. A script that calls a model for each row looks fine on 50 rows and becomes a large bill and a quality problem on 5 million.
How to build the step
- Estimate cost before running. Cost per row is input tokens times the input price plus output tokens times the output price. Take the average token counts from 100 real rows, multiply by the row count, and check the vendor's current prices. Then decide if a smaller, cheaper model is enough.
- Use a batch API for work that can wait. Claude and OpenAI both offer asynchronous batch interfaces for large jobs, which return results later instead of one request per row. This fits nightly enrichment better than live calls.
- Cache by input hash. Hash the cleaned input text plus the prompt version and model name. If the hash is already stored, reuse the stored answer. Duplicate tickets and re-runs then cost nothing.
- Force structure and check it. Ask for JSON that matches a schema and validate every response: required fields present, label in the allowed list, number in range. Send failures to a retry or a
needs_reviewtable, never into the mart. - Sample for human review. Check a random sample each run, plus every low-confidence or failed row. Track the error rate over time, and recheck when the prompt or model changes.
- Keep the output idempotent. Store the model, prompt version and timestamp with each result.
Before and after
A script loops over 2 million product descriptions, calls a model once per row, and writes the label straight into the catalog. It stops at row 800,000 on a rate limit, restarts from the beginning, and some labels are not in the category list.
The fixed step hashes inputs, skips cached rows, submits the rest as a batch, validates each label against the category list, and writes failures to a review table. Ten percent of a sample is checked by a person each week.
What goes wrong
- No cost estimate, so the bill is a surprise.
- Trusting the output shape. One malformed response breaks the load.
- No version in the cache key. After a prompt change, old answers are reused.
- Sending personal data to a model without checking the vendor's data terms.
Examples
A cost estimate on a napkin
Sample 100 tickets: average 300 input tokens and 20 output tokens. For 1 million tickets that is 300 million input tokens and 20 million output tokens. Multiply each by the current price for the model you picked (check the vendor's pricing page for today's rates). Compare a small and a large model on the same sample for both cost and accuracy before choosing.
A validation rule for a label
ALLOWED = {"billing", "bug", "feature_request", "other"}
def check(result: dict) -> bool:
return result.get("label") in ALLOWED and 0 <= result.get("confidence", -1) <= 1
Rows that fail go to ticket_labels_review. The mart only reads rows that passed.
Key points
- Estimate cost from real token counts per row before running a large LLM step.
- Use a batch API for work that can wait, and cache results by a hash of input, prompt version and model.
- Validate every response against a schema and allowed values; send failures to review, not to the mart.
- Sample results for human review and track the error rate, especially after a prompt or model change.
- Store the model, prompt version and time with each result so a run can be traced and repeated.
Common mistakes
- Calling a model row by row with no cache, so a rerun pays again.
- Writing model output into a mart without checking its shape or values.
- Leaving the prompt version out of the cache key.
- Never checking a sample, so quality drops unnoticed.
Check yourself · 2 of 3
What should the cache key for an LLM result be built from?
Check yourself · 3 of 3
A nightly job must label 2 million rows and nobody needs the results before morning. Which approach fits best?
Practice items, not counted towards your mastery.
Start here
- dbt documentation (opens in a new tab)DocsFree
Models, tests, sources and incremental builds, the reference for SQL transformations inside the warehouse.
- Incremental models (dbt) (opens in a new tab)DocsFree
How an incremental model adds only new rows, and when it needs a unique key or a full refresh.
- Data tests (dbt) (opens in a new tab)DocsFree
The built-in not_null, unique, accepted_values and relationships tests, and how severity and thresholds are set.
More for this topic
- Batch processing (Claude) (opens in a new tab)DocsFree
How asynchronous batch requests work for large model jobs, with limits and result handling.
- Batch API (OpenAI) (opens in a new tab)DocsFree
A second vendor's batch interface for large offline jobs, to compare against.