Topic 1 of 5 · 5 min read
Input checks before the model call
Goal: Choose input checks for a feature and place each one before or in parallel with the model call.
An input check is a gate that inspects what is about to reach the model, and you choose each gate by the harm it stops and the delay it adds. Cheap deterministic checks go first. Model-based checks go in parallel, or only where the risk justifies the wait.
Check yourself · 1 of 3
A feature needs a maximum message size, a card-number redaction and a moderation call. In what order should the checks run?
Why it matters
Everything you send to a model is paid for, logged and possibly obeyed. A 200,000-character paste costs money and can bury your instructions. A support message with a card number puts personal data into your traces and a third-party API. A web page fetched for a summary can contain text that tells the model to ignore you. Checking before the call is cheaper than cleaning up after it.
The checks, cheapest first
- Limits and format. Maximum characters or tokens, allowed file types, expected language, a schema for structured fields. These are plain code, take microseconds and reject abuse and accidents.
- Personal-data detection. Regex for card, phone and ID formats plus a named-entity model for names and addresses. Decide the action per feature: redact before sending, block, or allow because the user needs it.
- Moderation. A provider moderation endpoint or a policy classifier scores the text for categories such as violence or self-harm. Use it when users write free text.
- Topic classifier. A small model or embedding match that answers "is this about billing?" for a feature that should stay in scope.
- Injection classifier. Run it on untrusted content (retrieved pages, emails, tool results), not only on the user's own message. It lowers risk; it does not remove it.
Placing them
Sequential checks add their latencies together. Run the free ones first and stop at the first failure. Run the model-based ones in parallel with each other, and where blocking is not critical, in parallel with the main call: start generation, and cancel it if a check fails. Do not stream anything to the user until the checks that can fail have finished.
Before and after
Before: a chat feature sends the raw message to the model. After: a length cap, a card-number redaction, and a moderation call run in parallel with a topic classifier. The slowest of those sets the delay, not the sum, and each failure returns a specific message to the user.
What goes wrong
- Checking only the user's text. Retrieved documents and tool results are also input.
- Stacking five serial model calls. Latency and cost double, and each classifier has its own false positives.
- Redacting after logging. The personal data is already in the trace store.
Examples
A check plan for a billing assistant
- Reject messages over 4,000 characters (code, 0 ms).
- Redact card numbers and IBANs (regex, 1 ms).
- In parallel: moderation call and an on-topic classifier (about 150 ms together).
- Retrieved invoice PDFs go through an injection classifier before they enter the prompt.
The off-topic request "write me a poem" fails step 3 and returns "I can only help with billing questions".
Cancel on failure
const checks = Promise.all([moderate(text), onTopic(text)]);
const gen = startGeneration(text); // begins now, not streamed yet
const [mod, topic] = await checks;
if (mod.flagged || !topic.ok) { gen.abort(); return refuse(mod, topic); }
return stream(gen);
Generation overlaps the checks, so a passing request pays no extra delay. The cost of a failing one is a cancelled call.
Key points
- Run deterministic checks first because they cost almost nothing and stop obvious abuse.
- Personal-data detection needs a stated action per feature: redact, block or allow.
- Injection classifiers belong on untrusted content such as retrieved pages and tool results, not only on user messages.
- Independent model-based checks run in parallel so the delay is the slowest check, not the sum.
- Checks must run before logging and before any streaming starts, or the harm has already happened.
Common mistakes
- Checking the user's message but not retrieved documents or tool results.
- Chaining several model-based classifiers in series and paying every delay.
- Redacting personal data after it has already been written to logs or traces.
- Returning a generic block message that does not say which limit was hit.
Check yourself · 2 of 3
Which input most needs an injection classifier before it enters the prompt?
Check yourself · 3 of 3
Two independent classifiers each take 120 ms. Run in parallel, about how much delay do they add?
Practice items, not counted towards your mastery.
Start here
- OWASP Top 10 for LLM Applications (opens in a new tab)DocsFree
Names the risks guardrails address: prompt injection, sensitive information disclosure and improper output handling.
- Llama Guard: LLM-based input-output safeguard for human-AI conversations (opens in a new tab)PaperFree
The paper behind a policy classifier model that labels both prompts and responses against a written taxonomy.
- NVIDIA NeMo Guardrails documentation (opens in a new tab)DocsFree
Overview of input, dialog, retrieval, execution and output rails in an open-source toolkit.
More for this topic
- Moderation guide (OpenAI) (opens in a new tab)DocsFree
A provider moderation endpoint: categories, scores and how to call it on text before and after a model call.