Guardrails & Safety

AgentBackend provides a three-layer guardrail system to keep your AI agents safe in production: input validation, processing rules, and output filtering.

Overview

1

Input Guardrails

Validate and filter user messages before they reach the agent

2

Processing Guardrails

Control tool execution, require approvals, enforce limits during agent runs

3

Output Guardrails

Mask PII, filter content, and enforce response limits before delivery

Input Guardrails

Input guardrails validate user messages before they are processed by the agent. Blocked messages return a 400 status with a descriptive reason.

GuardDescriptionDefault
block_injectionDetects prompt injection attemptstrue
content_policyBlocks messages violating content policytrue
max_lengthMaximum input message length (characters)8000

Processing Guardrails

Processing guardrails control what happens during an agent run, including tool call approvals, DDL confirmation, and execution limits.

GuardDescriptionDefault
tool_approvalList of tool names requiring human approval[]
ddl_confirmationRequire approval for DDL operations (CREATE, ALTER, DROP)true
max_tool_callsMaximum tool calls per run20

Output Guardrails

Output guardrails process the agent's response before it reaches the end user.

GuardDescriptionDefault
pii_maskingMasks emails, phone numbers, names in responsesfalse
content_filterBlocks responses violating content policytrue
max_lengthMaximum response length (characters)16000
PII Masking Example
# With pii_masking enabled, the agent response transforms:
# "Customer John Smith (john@example.com) ordered item #1234"
# into:
# "Customer J*** S**** (j***@example.com) ordered item #1234"

Configuring Guardrails

Pass a guardrails object when creating or updating an agent. Each layer (input, processing, output) is configured independently.

Python SDK
agent = ab.agents.create(
    name="Customer Support",
    instructions="You are a helpful support agent.",
    guardrails={
        "input": {
            "max_length": 4000,
            "block_injection": True,
            "content_policy": True,
        },
        "processing": {
            "tool_approval": ["tenant_query"],
            "ddl_confirmation": True,
            "max_tool_calls": 10,
        },
        "output": {
            "pii_masking": True,
            "max_length": 8000,
            "content_filter": True,
        },
    },
)
JavaScript SDK
const agent = await ab.agents.create({
  name: "Customer Support",
  instructions: "You are a helpful support agent.",
  guardrails: {
    input: {
      max_length: 4000,
      block_injection: true,
      content_policy: true,
    },
    processing: {
      tool_approval: ["tenant_query"],
      ddl_confirmation: true,
      max_tool_calls: 10,
    },
    output: {
      pii_masking: true,
      max_length: 8000,
      content_filter: true,
    },
  },
});

Approval Flows

When a tool listed in tool_approval is called, the run pauses and emits an approval_pending event. Your application must approve or reject the call before the run continues.

Python SDK
# When tool_approval is enabled, tool calls pause for approval
# The stream emits an "approval_pending" event

import json

for event in ab.agent(agent.agent_id).run_stream(
    message="Delete all inactive users",
    session_id="admin_session"
):
    if event.type == "approval_pending":
        print(f"Tool: {event.tool_name}")
        print(f"Args: {json.dumps(event.tool_args, indent=2)}")

        # Approve or reject
        ab.run.approve(event.run_id, event.tool_call_id, approved=True)
JavaScript SDK
// When tool_approval is enabled, tool calls pause for approval
// The stream emits an "approval_pending" event

const stream = ab.agent(agent.agentId).runStream({
  message: "Delete all inactive users",
  sessionId: "admin_session",
});

for await (const event of stream) {
  if (event.type === "approval_pending") {
    console.log("Tool:", event.toolName);
    console.log("Args:", JSON.stringify(event.toolArgs, null, 2));

    // Approve or reject
    await ab.run.approve(event.runId, event.toolCallId, { approved: true });
  }
}

DDL Confirmation: When ddl_confirmation is enabled, any CREATE, ALTER, or DROP operation on the data store automatically triggers an approval flow, even if the tool is not in the tool_approval list.

Monitoring

Track guardrail triggers through the observability API. Each blocked input, masked output, or rejected tool call is logged with a timestamp and reason.

cURL
curl https://api.agentbackend.ai/v1/agents/:id/guardrail-events \
  -H "Authorization: Bearer ak_..."
Response
{
  "events": [
    {
      "type": "input_blocked",
      "reason": "prompt_injection_detected",
      "timestamp": "2026-03-27T10:15:00Z",
      "session_id": "sess_abc123"
    },
    {
      "type": "output_masked",
      "reason": "pii_detected",
      "fields_masked": ["email", "phone"],
      "timestamp": "2026-03-27T10:16:30Z",
      "session_id": "sess_abc123"
    }
  ]
}