Skip to content

Guide

How to implement guardrails in an LLM agent

A practical pattern for detecting prompt injection, checking model output and stopping the agent safely — in Python or TypeScript.

Why runtime guardrails?

Prompt filters in the system prompt can be overridden by the very input they protect against. Runtime guardrails run outside the model: your code sends the prompt, retrieved documents and output to an independent check, and only continues when the verdict allows it.

The three checkpoints

  1. Before the model: scan the user prompt and RAG documents for direct and indirect prompt injection and sensitive data.
  2. Before any tool runs: check planned tool calls against an allowlist. A trace recorded afterwards cannot undo an action.
  3. Before releasing the answer: check the output and use the redacted version when one is returned.

How to detect prompt injection

Vigil's detectors are deterministic: the same input always produces the same verdict, which makes them testable and auditable. They catch common instruction-override, role-hijack and data-exfiltration patterns, including text hidden inside retrieved documents. No pattern-based detector catches every attack, so combine it with least-privilege tools and output checks. You can try the detectors in the browser with the free scanner — your text is not sent anywhere.

Implementation

typescript
import { VigilClient, SecurityViolationError } from "./vigil-client";

const vigil = new VigilClient(process.env.VIGIL_API_KEY!);

export async function answer(userPrompt: string, ragDocs: string[]) {
  try {
    // 1. Pre-flight: scan the prompt and retrieved documents
    const pre = await vigil.preFlightCheck(userPrompt, ragDocs);
    const safeInput = pre.redacted_input ?? userPrompt;

    // 2. Call your model only after an allow/flag verdict
    const output = await callYourModel(safeInput);

    // 3. Check the output before releasing it
    const post = await vigil.postExecutionCheck(safeInput, output);
    return post.redacted_output ?? output;
  } catch (e) {
    if (e instanceof SecurityViolationError) return "Request blocked by policy.";
    throw e;
  }
}

Fail closed, always

  • A block decision stops the agent.
  • Timeouts, network errors, unexpected HTTP statuses and unknown or missing decisions also stop it.
  • Never log full response bodies — they can contain the sensitive data you are protecting.

Both downloadable clients implement these rules and are covered by automated tests. Full request fields are in the API reference.

Get an API key

Register an agent on the free Starter plan and paste its key into the client.

Create a free account →