Availability
For how filters work, refer to Filters.
Write a Tengo script that blocks or changes content. Test it on sample input, then save it as a filter.
Create the Filter
- In the admin console, go to Context management > Filters. The list shows the name and description of each filter. Use the menu of a filter to edit or delete it.
- Select Add Filter.
- Enter a Name, such as
PII Detector, and an optional Description. - In Filter type, select Script. For guardrail filters, refer to Guardrails.
- To filter LLM responses instead of requests, select Is this a Response Filter?. Refer to Filter LLM Responses.
- Optional: select a script template in Load Template. If the editor already contains a script, AI Studio asks for confirmation before it replaces the content.
- Write or edit the script. The script must set
output. For the input and output objects, refer to Filter Scripting Reference. - Test the script in the Test panel. Test it with each vendor format that you use, because OpenAI, Anthropic, and Google AI have different formats.
- Select Add filter, or Update filter for an existing filter.
Script Templates
Templates are a starting point. Edit the script after you load a template to match your own regex patterns, blocked phrases, or LLM ID. The available templates depend on the Is this a Response Filter? checkbox:PII Redaction (Request Filter)
Redacts email addresses, phone numbers, and US Social Security Numbers from the request payload, then allows the request to continue. Each redaction step re-parses the payload before it applies the next pattern. The filter also logs apii_redacted compliance event that summarizes what it redacted.
Response Guardrails (Pattern Matching)
A response filter that blocks LLM responses with forbidden phrases, such as refund promises, harmful instructions, or unauthorized commitments. It works for both streaming and non-streaming responses. For a streaming buffer, it waits until it has 150 characters of context before it evaluates.Response Guardrails (LLM Check)
A response filter that sends the response to a second, fast LLM and asks it to judge policy compliance, instead of matching fixed phrases. It waits for 200 characters of streaming context before it evaluates. From 2.3.0, the template fails closed. It blocks the response unless the checker answers exactlyno. It also blocks when the tyk.llm call fails. The template puts the response between tags and tells the checker not to follow instructions in it.
This template calls
tyk.llm(1, settings, policy_check_prompt). The 1 is a placeholder LLM ID. Change it to your policy-checker LLM’s ID before you save the filter. Otherwise the script calls whichever LLM has ID 1 in your AI Studio instance.Test the Script
A collapsible Test panel sits below the script editor on the filter form. Use it to run the current script against sample input and check the output, without saving the filter first. Expand the panel, fill in the fields, and click Run filter. The fields map directly onto the script input object:
If Raw Input is a JSON request body with a
messages array, the panel sends that array as input.messages. If Raw Input is plain text, input.messages is empty. If the script raises compliance events, the panel shows them below the result.
To test a script on input.segments, set Request body format to a vendor format. The default, Plain text, leaves input.segments empty.
AI Studio runs the script in a sandbox with a 5 second timeout. If the script does not finish in time, AI Studio stops the script and the panel reports a timeout. On success, the panel shows the resulting output object: block, payload, message, and messages when the script sets it. On failure, it shows the Tengo runtime error.
The panel calls POST /api/v1/filters/test with a body of {"script": "<tengo script>", "input": {<script input object>}}, and an admin session or API token. To validate a script from a CI pipeline before you create or update the filter through the API, call this endpoint directly.