Concepts / Implementing Guardrails

Implementing Guardrails

Input and output guardrails share a purpose but differ in position. That is why a complete design usually considers both, and why the placement rule about exposures decides how many guardrails are needed.

  • Programming

The Two-Step Design That Raises a Question

Imagine you have built an application in which a person types a question, the question is passed to a model, and the model's answer appears on the screen. The design looks finished. Yet it raises an uncomfortable question: what happens if the text coming in is harmful, misleading, or simply not something the application should handle? And what happens if the text going out is the problem?

Guardrails are the design answer to that question. Keep the simple two-point flow in mind, text comes in and text goes out, because the rest of this article builds on it. Each point in that flow is a place where something can go wrong, and each is a place where a guardrail can be placed.

What a Guardrail Is and Whose Risks It Covers

Definition

A guardrail is a mechanism that mitigates risk and protects both the builder and the users of an application.

The word mechanism matters. A guardrail is not a single rule written in a document. It is a component of the system that actively limits what can go wrong. Its job is defensive. It does not make the application more capable, faster, or more interesting. It exists so that when a risk is present, the consequences of that risk are contained before they reach a user or the operator running the system.

The phrase protect you and your users captures two audiences. The builder is exposed to risks such as an application behaving in ways that reflect poorly on the product or create legal or operational trouble. The users are exposed to risks such as receiving harmful content or having their own information handled carelessly. A well-placed guardrail considers both audiences, because a risk to one is usually a risk to the other.

AudienceTypical exposure named in the sourceWhat the guardrail protects
BuilderThe application behaves in ways that reflect poorly on the productThe product's reputation
BuilderThe application creates legal or operational troubleThe builder from legal and operational consequences
UsersReceiving harmful contentThe user from content that should not reach them
UsersHaving their own information handled carelesslyThe user's information during processing

Whose risks a guardrail addresses. A single guardrail often covers rows from both audiences.

Finding Exposures Before Placing Guardrails

The source gives a clear placement rule: guardrails should be put wherever there is an exposure to risk. This turns a vague worry into a concrete design task. Instead of asking whether the application feels safe, you walk through the system and ask at which points risk can enter, travel, or leave.

An exposure is a point in the system where something could go wrong and cause harm. Exposures are not all the same size. Some sit at the edges of the system, where data arrives from outside or leaves to the outside. Others sit inside the processing itself. The placement rule asks you to put a guardrail at each exposure you can identify, rather than relying on one check at the very beginning and hoping it covers everything.

Usertypes a questionApplicationreceives the textModelproduces a replyScreenshows the answerrequest enterstext passed onreply returnsanswer leaves
At which points between the user, the application, and the model should guardrails sit so that every request and response is covered?

Read the diagram as a list of places to check. The request enters at the edge, is passed inward toward the model, and the reply travels back through the application to the screen. Each arrow is a point where data moves, so each arrow is a candidate exposure. The design task is to decide which of those points need a guardrail, and the placement rule says to cover every one you can identify.

Tracing One Request Through the Flow

The best way to see how the two families of guardrails fit together is to follow one request from start to finish. Read the trace as a sequence of states. At each step, ask which exposure is currently active and which guardrail, if any, is responsible for it.

Worked example

A user question about a shipping delay

A user types a question into a support chatbot built on a model. Trace which exposure is active at each state and which guardrail owns it.

  1. 1
    State 1: text arrives from the user

    The request is on the way in. The exposure is at the edge where outside data enters the system. The input guardrail is responsible for this stretch.

  2. 2
    State 2: text is passed to the model

    The request is now inside the processing. The input guardrail has already handled it, so it is no longer the active check on this data.

  3. 3
    State 3: the model produces a reply

    The reply is now being produced. The exposure is in the content that is about to leave the system, so the output guardrail is the one responsible.

  4. 4
    State 4: the reply reaches the screen

    The reply is on the way out to the user. The output guardrail covers this stretch, and the user is the party whose risk it contains.

Result

The input guardrail owns the request on the way in, and the output guardrail owns the reply on the way out. They never handle the same data at the same moment.

User textInput guardrailModelOutput guardrailReply to userchecks incoming textpasses checked textchecks outgoing replypasses checked reply
Where exactly does data pass through an input guardrail versus an output guardrail, and what gets checked at each point?

Input Guardrails Versus Output Guardrails

The source states that, in general, guardrails can be grouped into two categories: guardrails around inputs and guardrails around outputs. This grouping follows the direction of data through the system. Data either comes into the application or goes out of it, and the two families are named for the side of the flow they protect.

FeatureInput guardrailOutput guardrail
Position in the flowOn the way in, before the text reaches the modelOn the way out, after the model has produced a reply
Data it handlesThe request written by the userThe reply produced for the user
Shared purposeMitigates risk and protects builder and usersMitigates risk and protects builder and users
Exposure it coversHarmful, misleading, or out-of-scope text coming inProblematic text going out

Input and output guardrails share a purpose and differ in position.

Key point

Input and output guardrails share a purpose but differ in position. That is why a complete design usually considers both, and why the placement rule about exposures decides how many guardrails are needed.

Following a Blocked, Modified, or Allowed Request

A guardrail on the input side does not have to do only one thing with a request. The flowchart below shows one illustrative set of outcomes for an incoming prompt. The branches are a generated teaching example of how an input check might respond. The source establishes the placement and the two families; the specific outcomes shown here are one way to read the design.

Prompt arrivesInput checkBlockednot sent to modelModifiedchanged before sendingModelOutput checkReply shownAllowedpassed unchangedrisk foundfixableno risksend revised textsend textreply producedreply passes
What happens next, step by step, when a user prompt is blocked, modified, or allowed to reach the model?

Notice that a blocked request never reaches the model, so the output side has nothing to check for that prompt. An allowed or modified request does reach the model, and its reply then passes through the output check. Both families remain relevant to the same interaction, each covering its own stretch of the flow.

Mistakes When Placing Guardrails

  • Common mistake

    Relying on one check at the very beginning of the flow

    Why it fails

    The placement rule asks for a guardrail at each exposure, not one check that is assumed to cover everything. A check on incoming text does nothing about a reply the model produces afterward.

    Do this instead

    Walk through the system, identify each point where risk can enter, travel, or leave, and decide where a guardrail belongs.

  • Common mistake

    Thinking only the builder or only the users need protection

    Why it fails

    The source says a guardrail protects both the builder and the users, and that a risk to one is usually a risk to the other.

    Do this instead

    When placing a guardrail, consider both audiences and name the risks each one faces.

  • Common mistake

    Treating a guardrail as a single written rule

    Why it fails

    The source describes a guardrail as a component of the system that actively limits what can go wrong, not a line in a document.

    Do this instead

    Think of a guardrail as a working part of the flow that checks data at a specific point.

  • Common mistake

    Assuming an output guardrail makes an input guardrail unnecessary

Practice: Placing Guardrails in a New Application

MEDIUM

A team is building an application in which a student types a homework question, a model answers it, and the answer is displayed in a classroom app. Before you read further, list every point in the flow where you think risk could enter, travel, or leave. Then decide, for each point, whether it belongs to the input side or the output side, and name one risk that a guardrail at that point would address for the student and one for the school operating the app.

Hints
  • Start at the edge where the student's text arrives and follow the path to the screen.
  • Points where data leaves the system are exposures too, not only points where it enters.
  • A single guardrail can protect both the student and the operator, so look for risks that affect both.

Key Takeaways

  • A guardrail is a mechanism that mitigates risk and protects both the builder and the users of an application.
  • Guardrails should be placed wherever there is an exposure to risk, so the design task starts by locating exposures.
  • Input guardrails sit on the way in and output guardrails sit on the way out; they share a purpose but differ in position.
  • A complete design usually considers both families, because each covers a distinct stretch of the data flow.

More concepts starting with I