Prerequisites and scoped previews
LLMs, transformers and token generation
Build a useful mental model of tokenization, transformer blocks, self-attention and autoregressive generation.
Context windows and context engineering
Construct bounded, relevant and permission-safe context from instructions, conversation state, retrieved evidence and tool results.
What, when, why and how
Instructions and untrusted content are separate trust classes
- what
- A prompt combines application-owned instructions with untrusted user, document and tool data.
- when
- Use it for RAG, agents, code assistants and external text.
- why
- Indirect prompt injection can appear in relevant documents and attempt to redirect behavior or expose data.
- how
- Keep policy in application-owned templates, label untrusted segments, minimize context and enforce permissions in deterministic code.
Structured output constrains syntax, not truth
- what
- A schema constrains keys, types, enums and required fields but cannot prove correctness, ownership or authorization.
- when
- Use structured output for extraction, routing, workflow state and typed tool proposals.
- why
- Valid JSON can contain fabricated IDs, impossible states or unsafe actions.
- how
- Validate schema, then domain semantics, freshness, evidence and authorization.
A tool call is a proposal to application code
- what
- The model may propose a named tool and arguments; the application owns discovery, validation, authorization, execution and recovery.
- when
- Use it whenever an assistant can access private resources or cause side effects.
- why
- Treating model arguments as trusted authority converts injection into system compromise.
- how
- Expose a minimal typed allowlist, bind identity from the session, reauthorize exact resources, require approval and execute idempotently.
1. Version the prompt contract
Separate stable application instructions from user input and retrieved evidence. A prompt manifest should identify template, model, tokenizer, tool-schema, output-schema and safety-policy versions. Bind each request/job to the exact versions used.
Documents, web pages, code comments and tool results are untrusted. Delimit and label them, but assume the model can still be influenced. Do not put secrets in prompts and do not rely on “ignore previous instructions” as the only injection defense.
2. Validate structure and semantics separately
Example output: {"intent":"refund_status","order_id":"ord_123","needs_human":false}. Schema validation checks enum, string and boolean types. It does not prove the order exists, belongs to the authenticated tenant or is eligible for a refund.
Resolve the order from the system of record, verify tenant ownership and current state, check freshness and policy, and reject unknown or contradictory fields. Prefer narrow command objects over free-form SQL, URLs or arbitrary code.
3. Execute tools under application authority
A tool registry defines minimal name, description, typed input schema, timeout, side-effect classification and authorization policy. The application binds principal and tenant from the authenticated session—not from model arguments—and reauthorizes the exact resource immediately before execution.
For a refund, the model may propose order ID and reason. The backend checks ownership, refundable amount, prior refunds, policy and approval; persists an idempotent command; calls the payment system; verifies resulting state; and reports success only after system-of-record confirmation.
VISUAL WALKTHROUGH
See the system in motion
Trace a typed model proposal through validation, authorization, approval and idempotent execution.
Receive model tool proposal
Validate tool name and argument schema
Resolve identity from server session
Authorize exact resource and action
Require approval when policy says so
Persist idempotent command
Execute and reconcile system-of-record state
Loading interactive walkthrough…
4. Recover safely and evaluate the whole contract
Malformed output, schema drift, unknown tool names, duplicate calls, timeouts, partial streams and provider retries are distinct cases. Retry read-only operations under bounded policy; side effects require idempotency and reconciliation. An unknown outcome is pending until verified.
Evaluate schema-valid rate, semantic-valid rate, tool selection, argument accuracy, authorization denials, unsafe-action attempts, task completion, duplicate side effects and human escalation. JSON validity alone is not an AI quality metric.
Diagrams and relationships
The model proposes data; deterministic application policy decides whether an action runs.
Scroll horizontally on smaller screens, or enlarge the diagram.
Engineer, Senior and Staff follow-ups
These are teaching lenses. Each answer changes the scope of reasoning; they are not official employer leveling criteria.
Engineer: Why is schema-valid output insufficient?
Schema checks shape, not truth, freshness, ownership or policy. Run domain and authorization checks against authoritative systems.
Engineer: Where should tenant identity come from?
From the authenticated server-side session, never from model-generated arguments or untrusted prompt fields.
Senior: How do you handle a timeout after a refund call?
Treat the outcome as unknown, query by idempotency key, reconcile state and retry only when safe; never issue a second unkeyed refund.
Senior: How do you defend against indirect prompt injection?
Label/minimize untrusted content, restrict tools and scope, validate arguments, reauthorize resources, require approval and monitor policy violations.
Staff: How do you evolve tool schemas safely?
Version schemas, bind active jobs to contract versions, support a bounded compatibility window and roll out readers before writers.
Staff: Where is the autonomy boundary?
The model interprets intent and proposes typed actions. The platform owns identity, permissions, quotas, approval, side effects, audit and recovery.
Practice and self-review
Your practice draft
Design an assistant that checks order status and issues refunds. Define prompt/output/tool contracts, authorization, approval, idempotency and evaluation.
Saved locally on this device. The worked answer does not score your draft. Existing lesson notes and completion are separate.
Progressive hints
Hint 1
Write the assumptions and contract before selecting a framework.
Hint 2
Trace a normal request and one failure or adversarial case.
Hint 3
Separate measured facts from synthetic exercise values and assign policy ownership.
Reveal worked answer guidance
A strong solution to this exercise makes the relevant boundary explicit, pins versions, validates inputs and outputs, handles failure without silently widening permissions, and defines evidence that would prove the behavior. Design an assistant that checks order status and issues refunds. Define prompt/output/tool contracts, authorization, approval, idempotency and evaluation.
Evidence to check in your answer
- Contracts and assumptions are explicit.
- A concrete worked example is correct.
- Failure and degraded behavior are addressed.
- Security, evaluation and ownership are explicit.
For each item, record Missing, Prompted, Independent or Adaptive evidence. Adaptive means you can revise the reasoning under a changed constraint. Reading completion and readiness are separate.
Sources and verification scope
Original synthetic examples and reference calculations. Sources support narrowly scoped background; they do not establish production performance or independent chapter review.
Structured Outputs guide ↗ · Schema-constrained output concepts; provider-specific guarantees and supported schema subsets must be checked against deployed API/model versions.
Function calling guide ↗ · Tool/function call schemas and application-managed execution; verify current API behavior and model support before implementation.
Primary background links checked during authoring on 2026-10-19. Arithmetic, temporal eligibility, maturity and split reference functions are checked by the bounded local fixture suite. SQL is explanatory, not a migrated production query. No GPU, external dataset, model API or training job is required.