Concepts / Model Gateways and Routing

Model Gateways and Routing

Guardrails sit on both sides of the model call, with input guardrails checking requests and output guardrails checking responses.

  • Programming

A Customer-Support Assistant Under Pressure

Imagine a customer-support assistant built on a generative model. A user types a question, the application sends it to a model, and a reply comes back. That sounds simple, but each step in the path is a place where something can go wrong. The user might try to manipulate the model. The model might produce an unsafe or off-topic reply. The reply might leak information it should not contain. Guardrails are the checks that catch these problems.

This article follows one request through such an application. The goal is to see where guardrails sit, what they cost, and how a team chooses among the options. A useful way to hold the whole topic in mind is that guardrails are not free. Adding them changes how fast the application responds, how much the team depends on outside providers, and how much engineering work the team takes on. A good implementation is a set of deliberate choices, not a checklist of every check that could be added.

What do you think happens?

When the customer types a question, does the text go straight to the model, or does something check it first?

Reveal answer
Answer:

It passes through input guardrails first, which examine what the user sent. Only after that does the request reach the model API.

The user's input does not go straight to the model. Input guardrails sit between the user and the model call, so a request can be checked, and potentially blocked or changed, before the model ever sees it.

Tracing One Request Through the Pipeline

Start with the request path. The user's input does not go directly to the model. It first passes through input guardrails, which examine what the user sent. Only then does the request reach the model API. The model's raw output does not go directly back to the user either. It passes through output guardrails before the response is shown. The path therefore has a gate on each side of the model call.

User inputthe customer's questionInput guardrailsexamine what the user sentModel APIgenerative model callScorersoften AI-powered, usuallysmaller and fasterOutput guardrailscheck the raw model outputResponse shownwhat the user seesrequestif allowedraw outputif allowedscoring support
In what order does a user request pass through input guardrails, the model call, and output guardrails, and where can it be blocked or changed?

Read the diagram as a pipeline with two gates. The input gate protects the model from what comes in. The output gate protects the user from what goes out. The scorers are the tools that help make those judgments. The source places scorers under the model APIs because they are often AI-powered themselves, even though they are usually smaller and faster than the generative models. Scorers can also sit inside the output guardrails stage instead, so the scoring work can attach to either side of the model call depending on the design.

Worked example

Tracing a Single Support Question

A customer asks the support assistant a question. Trace what happens to the request and the reply, identifying each place a guardrail could stop or change the content.

  1. 1
    Step 1: The question enters the application

    The user's input is not sent to the model yet. It is held at the input guardrails, which examine what the user sent.

  2. 2
    Step 2: Input guardrails decide

    If the input is a manipulation attempt or otherwise fails the check, the request can be blocked or changed here, and the model is never called for it. If it passes, it moves on.

  3. 3
    Step 3: The model call happens

    The request reaches the model API, which generates a raw reply. Scorers may be involved near this call, helping judge content.

  4. 4
    Step 4: Output guardrails check the reply

    The raw reply does not go straight to the customer. It is checked for unsafe, off-topic, or leaking content before it is shown.

  5. 5
    Step 5: The user sees the result

    Only content that clears the output gate is displayed. This is the protection the output side exists to provide.

Result

The request is guarded twice: once before the model call to protect the model from incoming problems, and once after it to protect the user from outgoing problems.

Who Builds Guardrails at Each Level

Because many different parts of a system can fail, guardrails can be placed at several levels. Two groups do most of this work: the companies that build and host models, and the teams that build applications on top of those models. Model providers and application developers can both implement guardrails, and each level has its own responsibilities. The two groups have different tools, so a careful design usually uses both.

Guardrail levelsModel providersbuild and host modelsProvider-suppliedchecksoften included withthird-party APIsModel gatewaysome gateways offerguardrail functionsTeam-built checkscustom code around themodel callApplicationdevelopersbuild on top of models
Which layers of the stack contain which guardrails, and how do they relate to the model call?

The gateway layer deserves its own check. Some model gateways also provide guardrail functionality, so a team that already runs a gateway should look at what that gateway covers before duplicating the same checks in its own code. The team's own checks then fill the gaps the gateway and provider do not cover.

Hosting Choice and How Many Guardrails You Build

The number of guardrails a team needs depends on whether it hosts its own models or uses third-party APIs. Guardrails can be built on top of either option. However, third-party API providers typically offer many guardrails out of the box, which reduces how much a team must build itself.

Self-hosting pulls in the opposite direction on one front. When models run inside your own system, requests do not have to be sent to an external service. That reduces the need for many input guardrails that exist to handle data crossing that outside boundary. The engineering burden shifts rather than disappearing. A self-hosted team gives up provider-supplied protections and gains control over where its checks run.

Latency, Reliability, and Streaming Output

The central trade-off in guardrail implementation is reliability versus latency. Stronger checking usually takes more time, and users notice delay. The source reports that some teams felt latency mattered more than the guardrails and chose not to implement them, because those checks can significantly increase an application's latency. That is a real engineering decision, and the right answer depends on the product and its risks.

The trade-off becomes sharper with output guardrails and the way responses are delivered. By default, the whole response is generated before anyone sees it, and that can take a long time. In stream completion mode, tokens are sent to the user as they are generated, so the user waits less. The cost of streaming is that partial responses are hard to evaluate. An unsafe response might reach the user before the guardrails decide to block it.

Token batch 1 sentuser begins readingToken batch 2 sentpartial text growsMore tokens sentresponse still incompleteOutput check onpartial textevaluation of what isvisible so farUnsafe text alreadyvisiblepossible if the checkarrives latestreamingstreamingcheck runsblock decision may lag
When tokens reach the user before the full response exists, how does an output guardrail check content that is already partly visible?

Comparing Full-Response and Streaming Checks

The two delivery modes ask different things of the output guardrail. A check on a complete response sees everything the model produced before any decision is made. A check during streaming sees only the part that has been generated so far. The table below summarizes the trade-off described in the source.

AspectFull response checked before returnStreaming tokens checked as generated
What is evaluatedThe complete responseOnly the partial text generated so far
Wait time for the userLonger, because the whole response is generated firstShorter, because tokens are sent as they are produced
Reliability of the checkStronger, since the full content is availableHarder, since partial responses are difficult to evaluate
Main riskLatency that users noticeUnsafe content may be visible before a block decision

Full-response checking favors reliability, while streaming favors speed and makes output evaluation harder.

Guardrail Solutions and What They Protect

Instead of building every check from scratch, a team can adopt an existing guardrail solution. The source notes that several can be used out of the box, and knowing these exist is the first step. Choosing among them depends on what each one covers. The source pack for this article does not name specific products, so treat any product list as something to verify against current provider documentation.

Map each risk to the side of the model call it threatens. Input risks include a user trying to manipulate the model, which is checked on the way in. Output risks include an unsafe or off-topic reply and a reply that leaks information it should not contain, which are checked on the way out. Because risks overlap between inputs and outputs, a single solution will often protect both sides, so a team should not assume it needs one tool for input and another for output.

Mistakes Teams Make With Guardrails

  • Common mistake

    Treating guardrails as free

    What it looks like

    Adding every available check to every request without measuring the added wait time.

    Why it fails

    The source states that guardrails change response speed, dependence on outside providers, and engineering effort.

    Do this instead

    Choose checks deliberately and measure what each one adds to latency.

  • Common mistake

    Assuming streaming output is fully checked

    What it looks like

    Streaming tokens to the user while assuming the output guardrail has already reviewed the whole reply.

    Why it fails

    Partial responses are hard to evaluate, so unsafe content might reach the user before the guardrails block it.

    Do this instead

    Decide in advance whether streaming is acceptable for the product's risk level, or accept full-response checking for sensitive use.

  • Common mistake

    Duplicating a gateway's checks in application code

    What it looks like

    Writing your own check for a risk that the team's model gateway already covers.

    Why it fails

    Some model gateways provide guardrail functionality, so the same check may already exist.

    Do this instead

    Review what the gateway covers before adding a separate check.

  • Common mistake

    Building one-sided protection

    What it looks like

    Protecting only the input and forgetting that the model's reply can leak information.

    Why it fails

    A request is guarded on both sides of the model call, and each side protects against different failures.

    Do this instead

    Place checks on both the input gate and the output gate.

Practice: Placing Guardrails for a Streaming Product

MEDIUM

A product team wants fast replies and is considering streaming tokens to users. Identify one way streaming changes the output guardrail, and name one decision the team must make before choosing streaming.

Hints
  • Recall what the guardrail can evaluate when the response is only partly generated.
  • Think about whether the product's risks can tolerate a brief window where unsafe text is visible.
  • The source frames this as a reliability versus latency decision.

Summary of Request Guarding

  1. 1
    Guardrails sit on both sides of the model call: input guardrails check the user's request before the model, and output guardrails check the model's reply before it is shown.
  2. 2
    Model providers and application developers can both implement guardrails, and some model gateways also provide them, so teams should check existing layers before duplicating checks.
  3. 3
    Reliability and latency trade off against each other. Streaming reduces wait time but makes partial output hard to evaluate.
  4. 4
    Self-hosting shifts the guardrail burden rather than removing it, and third-party APIs often provide many guardrails out of the box.

At a glance

  • Guardrails sit on both sides of the model call, with input guardrails checking requests and output guardrails checking responses.
  • Model providers, gateways, and application developers can each provide guardrails, and a careful design usually uses more than one level.
  • Reliability and latency trade off against each other, and streaming responses makes output evaluation harder because partial text is difficult to judge.
  • The hosting choice between self-hosted models and third-party APIs changes how many guardrails a team must build itself.

More concepts starting with M