Guardrails

TsgcAIGuardrails sits between your application and the language model. Personal data is replaced by placeholders before a request leaves the application and restored in the answer, tools are allowed, denied or sent for approval by name, and structured answers are validated against a JSON schema.

TsgcAIGuardrails

One component for the rules an AI feature needs before it goes to production. Assign it to TsgcAIChat.Guardrails and every request of the chat, and every run of an agent on that chat, follows the same rules whatever provider answers.

Component class

TsgcAIGuardrails

Purpose

PII redaction, tool rules and answer validation

Family

Chat & agents

Platforms

Windows, macOS, Linux, iOS and Android

Editions. TsgcAIGuardrails ships in sgcAI, in sgcWebSockets Enterprise and in All-Access, the same editions as TsgcAIChat. New in 2026.11.0. sgcAI is self-contained: it includes the sgcWebSockets Core runtime the component is built on. Available for Delphi 7 to Delphi 13 and C++ Builder.

Keep personal data and risky tools in check

Switch on redaction, list the allowed and denied tools, set the schema, assign the guardrails to the chat.

uses
  sgcAI, sgcAI_Chat, sgcAI_Guardrails;

procedure TForm1.ProtectChat;
var
  vText, vError: string;
begin
  // personal data never leaves the application
  sgcAIGuardrails1.PII.Enabled := True;
  sgcAIGuardrails1.PII.CustomPatterns.Add('EmployeeId=EMP-####');
  // read and search tools are allowed, deleting never is
  sgcAIGuardrails1.AllowedTools.Add('read_*');
  sgcAIGuardrails1.AllowedTools.Add('search_*');
  sgcAIGuardrails1.DeniedTools.Add('*delete*');
  sgcAIGuardrails1.ToolApproval := aigaNotReadOnly;
  // structured answers must match the schema
  sgcAIGuardrails1.Schema.Enabled := True;
  sgcAIGuardrails1.Schema.JSONSchema := '{"type":"object","properties":' +
    '{"price":{"type":"number","minimum":0}},"required":["price"]}';

  sgcAIChat1.Guardrails := sgcAIGuardrails1;
  sgcAIAgent1.Chat := sgcAIChat1; // the agent follows the same tool rules

  vText := sgcAIGuardrails1.Redact('Mail bob@example.com, call +34 600 111 222');
  Memo1.Lines.Add(vText); // Mail [EMAIL_1], call [PHONE_1]
  Memo1.Lines.Add(sgcAIGuardrails1.Restore(vText)); // the original text
  if not sgcAIGuardrails1.ValidateJSON('{"price":"cheap"}',
    sgcAIGuardrails1.Schema.JSONSchema, vError) then
    Memo1.Lines.Add(vError); // $.price: expected number
end;

Key properties & methods

The members you reach for most often.

PII redaction

With PII.Enabled, e-mail addresses, phone numbers, card numbers (Luhn checked), IBANs (mod 97 checked) and IPv4 and IPv6 addresses are replaced by placeholders such as [EMAIL_1] before a request is sent, and restored in the answer while PII.RestoreInAnswers is on. PII.Kinds selects the kinds.

Custom patterns

PII.CustomPatterns adds your own identifiers as Name=Pattern, where # is a digit, @ a letter, ? any character and * the rest of a word, for example EmployeeId=EMP-#### gives [EMPLOYEEID_1].

Tool rules

AllowedTools and DeniedTools take tool names with * wildcards, compared without case, and a denied name always wins. A refused tool is not offered to the model and is refused again if the model still calls it. OnToolBlocked reports every refusal with its reason.

Approval

ToolApproval decides which tools ask TsgcAIAgent.OnToolApproval before they run: aigaNone, aigaNotReadOnly (the default) or aigaAll.

Schema validation

With Schema.Enabled, structured answers are validated against Schema.JSONSchema: type, required, properties, additionalProperties, enum, const, minimum, maximum, lengths and item counts. A violation fires OnSchemaViolation with a path such as $.price: expected number, and the chat reports an error instead of storing the answer.

Methods

Redact, Restore, ValidateJSON and IsToolAllowed also work on their own, without a chat. ClearRedactions forgets the placeholder mapping.

Specifications & references

Authoritative sources for the services and protocols this component uses.

Keep exploring

Online HelpFull API reference and usage guide for the sgcAI components.
All sgcAI ComponentsBrowse the feature matrix of every sgcAI component.
Download Free TrialTry it with your own API keys.
PricingSingle, Team and Site licenses, full source code.
Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Ready to Get Started?

Download the free trial and put rules around the language model before it reaches your users.