LLM Application Engineering
What an LLM really does, prompt vs context engineering, and structured outputs with validation outside the model.
A language model guesses likely text, so it can be wrong. Treat its answer as a proposal: your code checks the shape and the business rules before accepting it. Keep passwords and permission decisions out of prompts, because code can enforce rules and prompts cannot.
- Context engineering
- Choosing everything the model sees, not just the wording.
- Structured output
- An answer in a fixed format.
- Validation
- Checking an answer before you use it.
Keep passwords and permission decisions in code. Code can enforce rules. Prompts cannot.
With the foundations in place, this lesson covers building applications that call models. It shows how to think about what an LLM really is, how to design instructions and context, and how to get structured output that you can trust. The theme that keeps coming back is this: the model proposes, and your code decides.
What a large language model actually does
An LLM estimates the likely continuation of a sequence of tokens, based on patterns it learned in training. It predicts the next token is technically right, but it is not a full picture for engineers. Scale, data, design, instruction tuning, preference tuning and the context you give at run time all combine. Together they let the system do sophisticated language and reasoning tasks.
- It is probabilistic. The same input can give different outputs. Even at a low temperature, do not assume the answer is always right. Treat model output as the result of a computation you do not trust. Check it whenever correctness matters.
- Its abilities are uneven. It is excellent at summarizing. It is unreliable at arithmetic, at interpreting policy, and at your own company's workflow. Tools, search, limited outputs and checking move work from uncertain generating to dependable software.
- Choose models on quality, speed, cost, context size, how reliably they use tools, support for structured output, privacy needs, regional availability and operating limits. Do not choose on leaderboard scores alone.
Prompt engineering and context engineering
Prompt engineering is the deliberate design of instructions, examples, limits and output requirements. Context engineering is wider. It covers everything the model receives when it runs: system instructions, user input, retrieved documents, tool definitions, conversation state, memory and intermediate results. A strong prompt defines the job, the limits, the information available, the output contract and what to do on failure.
INSTRUCTION You extract invoice data for the accounts team. Be literal; never guess.
CONTEXT <invoice_text> ...untrusted document content... </invoice_text>
<vendor_list> ...the approved vendors... </vendor_list>
TASK Extract the fields below from the invoice.
OUTPUT JSON matching the schema; use null for anything not stated.
FAILURE If the document is not an invoice, return {"error": "not_an_invoice"}.
Pattern: instruction -> context -> task -> output contract -> validation (in code, after the model).
Keep secrets, authorization decisions and business-critical invariants OUT of the prompt.- Few-shot examples show the behavior you want. But they use up context and can add accidental bias. Pick examples that are representative and varied.
- Long instructions can clash with user content or retrieved text. Give priority by relevance, authority, how recent it is, and clear boundaries.
- Treat retrieved content as data, not as trusted instructions.
Structured outputs and validation
You can ask a model for JSON. But valid syntax is not correct meaning. A production service checks the output against a schema, and then checks business rules in application code. For an invoice, a JSON schema checks the types and required fields. Business checks confirm that the total is not negative, that the tax is plausible, that the dates are valid and that the vendor is allowed.
from pydantic import BaseModel, ValidationError
class Invoice(BaseModel):
invoice_number: str
vendor: str
total_cents: int
tax_cents: int
def accept(raw_model_output: str, approved_vendors: set[str]) -> Invoice:
inv = Invoice.model_validate_json(raw_model_output) # 1) shape
if inv.total_cents < 0 or inv.tax_cents > inv.total_cents: # 2) business invariants
raise ValueError("implausible amounts")
if inv.vendor not in approved_vendors: # 3) policy
raise ValueError("vendor not permitted")
return inv
# The model PROPOSES a result. Your validator DECIDES whether it is acceptable.
# Invalid output can trigger repair, a retry, a fallback model, or human review.This separation becomes critical for agents, because tool inputs are also made by the model. A tool schema reduces accidental misuse. It does not replace authorization and policy checks. Use fixed lists, ranges, required fields and bounded strings. And never treat inputs that pass the schema as automatically authorized.
- ✓The model is a probabilistic component that reasons and generates. It is not the whole application.
- ✓Context engineering covers instructions, search, tools, memory and state, and not just wording.
- ✓The pattern is: instruction, context, task, output contract, then checking. Secrets and authorization stay outside the prompt.
- ✓Schema checks catch errors of shape. Domain checks catch business errors. Neither is authorization.
Build a checked extraction endpoint
- Write a small endpoint that accepts text and returns an invoice, or any record, using a model with a strict schema.
- Add a second layer of checks for three business rules. Return a clear error when they fail.
- Log the request ID, model name and version, delay, token counts and the result of the checks.
- Feed it five hostile inputs: a non-invoice, text with ignore previous instructions, a missing total, a negative total and a very long document. Record what the system does.
Q1Why does a production system separate generating from accepting?show answer
The model's output is a proposal. Code that does not guess checks the shape and the business rules. It then decides whether to accept, repair, retry, fall back or send it to a person.
Q2What belongs outside the prompt?show answer
Secrets, authorization decisions and business-critical rules. They must be enforced in code, where a prompt cannot argue them away.
Q3Why is a leaderboard not enough for choosing a model?show answer
It does not reflect your tasks, speed, cost, tool-use reliability, privacy or availability needs. You need an evaluation set built from your own workload.