Availability
Filter execution is an Enterprise Edition feature. In the Community Edition, you can create filters and attach them, but AI Studio does not run them. Each filter passes the content through without changes, and AI Studio logs a warning.
Filter Types
A filter is one of two types. You select the type in Filter type on the filter form.
Both types attach, run, and record compliance events in the same way. In the API, the type is the
kind field of a filter (script or guardrail). Filters that existed before v2.2.0 are script filters. New Enterprise installations include four default guardrails. They are not attached, so they have no effect until you attach them. To skip them, set SKIP_FILTER_DEFAULTS=true.
For guardrail providers, detectors, and actions, refer to Guardrails.
The Filters List View allows administrators to manage filters and middleware applied to prompts or data sent to Large Language Models (LLMs) via the AI Gateway or Chat Rooms. Filters and middleware ensure data governance, compliance, and security by processing or controlling the flow of information. Below is an enhanced description with the distinction between Filters and Middleware:
Filters: Unified Blocking and Modification
Filters in Tyk AI Studio provide comprehensive request/response processing with both blocking and modification capabilities:-
Blocking Filters:
- Purpose: Governance controls that deny requests based on content analysis.
- Behavior:
- Analyze message content, metadata, and context.
- Block requests that violate policies or contain restricted content.
- Example: Block prompts containing PII, sensitive keywords, or unauthorized patterns.
-
Modification Filters:
- Purpose: Transform message content before it reaches the LLM or after tool responses.
- Behavior:
- Redact sensitive information (emails, phone numbers, SSNs).
- Enhance system prompts with safety instructions.
- Normalize or transform content across vendors.
- Example: Automatically redact PII while allowing the request to proceed.
-
Combined Approach:
- Filters can both inspect AND modify in a single script.
- Example: Redact emails from user messages, but block if SSN is detected.
- ✅ Request Filters: Modify/block requests before reaching LLM
- LLM Proxy Requests (before reaching LLM)
- Chat Session Messages (before RAG search and LLM)
- File Content (before RAG indexing)
- Tool Responses (after tool execution)
- ✅ Response Filters: Block LLM responses based on content
- LLM Proxy Responses (REST and streaming)
- Chat Session Responses (regular and streaming)
- ✅ Tool Filters: Run on the arguments sent to a tool and on the response from the tool. Refer to Tool Filters.
Table Overview
-
Name:
- The name of the filter or middleware (e.g.,
Anonymize PII (LLM),Fixed PII Filter).
- The name of the filter or middleware (e.g.,
-
Description:
- A brief summary of the filter or middleware’s functionality (e.g., “Uses Regex to remove obvious PII”).
-
Actions:
- A menu (three-dot icon) that allows administrators to:
- Edit the filter or middleware.
- Delete the filter or middleware.
- A menu (three-dot icon) that allows administrators to:
Features
-
Add Filter Button:
- A green button labeled + ADD FILTER, located in the top-right corner. Clicking this button opens a form to create a new filter or middleware.
-
Pagination Dropdown:
- Located at the bottom-left corner, this control allows administrators to adjust the number of entries displayed per page.
Examples of Filters and Middleware
-
Filters:
- PII Detector: A regex-based filter that blocks prompts containing sensitive PII.
- JIRA Field Analysis: Ensures no PII is included in data retrieved from JIRA fields before passing to the LLM.
-
Middleware:
- Anonymize PII (LLM): Uses an LLM to anonymize sensitive data before sending it downstream.
- NER Service Filter: A Named Entity Recognition (NER) microservice that modifies outputs to remove identified entities.
Use Cases
-
Prompt Validation with Filters:
- Ensures that only compliant and secure prompts are sent to LLMs.
- Example: Blocking a prompt with sensitive data that should not be processed by an unapproved vendor.
-
Data Preprocessing with Middleware:
- Prepares data from tools or external sources for safe interaction with LLMs by modifying or anonymizing content.
- Example: Removing sensitive ticket details from a JIRA query before sending to an LLM.
-
Organizational Security:
- Both filters and middleware ensure sensitive information is protected and handled in line with organizational governance policies.
-
Enhanced Tool Interactions:
- Middleware supports tools by transforming their outputs, enabling richer and safer LLM interactions.
Key Benefits
-
Improved Data Governance:
- Filters and middleware work together to enforce strict controls over data flow, protecting sensitive information.
-
Flexibility:
- Middleware allows for data transformation, enhancing interoperability between tools and LLMs.
- Filters ensure compliance without altering user-provided prompts.
-
Compliance and Security:
- Prevent unauthorized or sensitive data from reaching unapproved vendors, ensuring regulatory compliance.
Filter Edit View (and example Filter)
The Filter Edit View enables administrators to create or modify filters using the Tengo scripting language. Filters serve as governance tools that analyze input data (e.g., prompts or files) and decide whether the content is permitted to pass to the upstream LLM. In this example, the filter uses regular expressions (regex) to detect Personally Identifiable Information (PII) and blocks the prompt if any matches are found.Form Sections and Fields
-
Name (Required):
- Specifies the name of the filter (e.g.,
PII Detector).
- Specifies the name of the filter (e.g.,
-
Description (Optional):
- A brief summary of the filter’s purpose and functionality (e.g., “Simple Regex-based PII detector to prevent the wrong data being sent to LLMs”).
-
Filter type (Required):
- Script or Guardrail. Refer to Filter Types. The rest of this section describes script filters.
-
Script (Required):
- A Tengo script that defines the logic of the filter. The script evaluates input data and determines whether the filter approves or blocks it.
- The example script detects PII using a collection of regex patterns and blocks the data if a match is found.
Script Templates
A Load Template dropdown sits above the script editor. Use it to start a new filter from a ready-made script instead of an empty one. If the editor already contains a script, AI Studio asks for confirmation before it replaces the content. 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.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.New Unified Script API
Modern filters use a unified API that provides rich context and supports both blocking and modification: Input Object:Example Script 1: Blocking Filter (PII Detection)
This script blocks requests containing PII patterns:Example Script 2: Modification Filter (Email Redaction)
This script redacts emails while allowing the request to proceed:Example Script 3: Advanced Modification (Messages Array)
This script shows complex message modification using the messages array approach:Example Script 4: Combined Blocking + Modification
This script redacts emails but blocks if SSN is detected:Available Helper Functions
Themidsommar module provides helper functions for common message modification tasks:
redact_pattern(input, pattern, replacement):- Redacts a regex pattern from all messages (system, user, assistant)
- Parameters:
input- The input object provided to your scriptpattern- Regular expression pattern (string)replacement- Replacement string
- Returns: Modified payload as string
- Example:
tyk.redact_pattern(input, "\\d{3}-\\d{2}-\\d{4}", "[SSN]")
Message Modification Approaches
Approach 1: Helper Functions (Simple, recommended for pattern-based redaction)Accessing Message Context
Scripts can access rich contextual information:Testing Filter Scripts
A collapsible Test panel sits below the script editor on the Add/Edit Filter form. Use it to run the current script against sample input and check the output, without saving the filter first.Testing a filter script, like creating or updating one, requires Tyk AI Studio Enterprise edition. On Community edition, the test request returns an “Enterprise Feature” error.
From v2.2.0, 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.
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.
Under the hood, the panel calls:
{"script": "<tengo script>", "input": {<script input object>}}, and an admin session or API token. You can call this endpoint directly, for example from a CI pipeline. Use it to validate a filter script before you create or update the filter through the API.
Action Buttons
-
Update Filter / Create Filter:
- Saves the filter configuration, making it active for future data processing.
-
Back to Filters:
- Returns to the Filters List View without saving changes.
Purpose and Benefits
-
Data Governance:
- Enforces strict control over what data can be sent to LLMs, ensuring compliance with privacy regulations.
-
Flexibility:
- Filters can be tailored to specific organizational needs using custom scripts.
-
Security:
- Prevents sensitive or unauthorized data from leaking to unapproved vendors or external systems.
Example Middleware for Tools
Middleware filters in the Tyk AI Studio modify data coming from tools before passing it to the LLM. These filters are applied to sanitize, anonymize, or enhance the data to ensure it complies with organizational standards and privacy regulations. Below is an example of a middleware filter that sanitizes Personally Identifiable Information (PII), specifically email addresses, from the tool’s output.Middleware Script: Email Redaction Example
Explanation of the Script
-
Module Import:
- The
textmodule is imported to enable regular expression operations (text.re_replace).
- The
-
Regex Pattern:
- A regex pattern is defined to detect email addresses:
- Example pattern:
[\w\.-]+@[\w\.-]+\.\w+ - This pattern matches standard email formats.
- Example pattern:
- A regex pattern is defined to detect email addresses:
-
Filter Function:
- The
filterfunction accepts an input string (e.g., tool output) and:- Uses
text.re_replaceto identify email addresses. - Replaces detected email addresses with
[REDACTED EMAIL].
- Uses
- The
-
Return Processed Output:
- The sanitized output is returned, ensuring that sensitive information like email addresses is redacted before reaching the LLM.
Use Case for Middleware
Tool Example: Imagine a tool, such asSupport Ticket Viewer, which retrieves user tickets from a system. These tickets often contain email addresses. Middleware ensures that no sensitive email information is included in the output sent to the LLM.
-
Input Payload Example:
-
Sanitized Output:
Benefits of Middleware
-
Data Privacy:
- Protects sensitive user information by ensuring it is sanitized before being sent to external systems.
-
Compliance:
- Ensures organizational adherence to privacy laws like GDPR or HIPAA.
-
Enhanced Security:
- Prevents accidental sharing of PII with external vendors or LLMs.
Available Tengo Modules
Filters have access to powerful standard library modules:text - String Operations
json - JSON Operations
fmt - Formatting and Printing
tyk - Extended Capabilities (Enterprise)
Compliance Event Reporting
Available from Tyk AI Studio v2.1.0 (Enterprise edition). Filter scripts can emit structured Compliance Events to record governance-relevant activity, such as PII redactions, content rewrites, or guardrail triggers, without affecting the block/allow decision. Events flow through the analytics pipeline into the Compliance dashboard, where they appear in the Filter Events tab with severity filters, drill-down, and CSV export. To emit events, set the optionalcompliance_events field on the script output:
Conditional Emission. Most filters should only emit events when a condition actually fires. Build the list dynamically:
- Recording is asynchronous, so filter latency is unaffected.
- Events never change the block/allow decision. Redact-and-allow with a
criticalevent is a legitimate pattern. - Events work in every filter scope: proxy request/response, chat request/response, file reference, and tool response filters.
- On Edge Gateways, events are batched and forwarded to the control plane on the analytics pulse.
Runtime Limits and Outbound Calls (Enterprise)
Filter scripts run on the goroutine that serves the request. For this reason, AI Studio limits every script execution. Each limit is an environment variable with a safe default. A change takes effect on the next execution, without a restart.tyk.makeHTTPRequest uses the same outbound rules as LLM upstreams:
- Only
httpandhttpsURLs are allowed. - When
LLM_UPSTREAM_ALLOWED_HOSTSis set, only those hosts are allowed. - When
LLM_UPSTREAM_BLOCK_INTERNAL=true, internal addresses are blocked. To allow an internal classifier, add it toLLM_UPSTREAM_ALLOWED_INTERNAL_HOSTS. - AI Studio checks redirects against the same rules.
tyk.llm gets the credential of the target LLM in the same way as the gateway. An LLM with a key stored as a $SECRET/... or $ENV/... reference works from a script.
Because response filters let the response through when they time out, a slow response filter does not block. If a response filter removes sensitive data, such as PII, a timeout sends the response to the user without that change. Keep response filter scripts fast, and test them with large responses.
AI Studio compiles each script one time and caches it. Each execution runs on its own copy, so executions do not share state. When the caller of a request or chat session disconnects, AI Studio stops the script.
Tool Filters
You can attach filters to tools. From v2.2.0, tool filters run in two directions:
Tool filters run on every way that a tool is called: in chat, on the REST endpoint, and on all MCP transports. They run on AI Studio and on Edge Gateways.
The arguments that a request filter gets have the same format on every transport:
parameters, payload, and headers, and return them in output.payload. It cannot change operation_id. If it tries, AI Studio ignores the change and logs a warning. input.context contains tool_id, tool_name, app_id, and user_id, and also session_id and call_id when they are available.
Tool filters fail closed in both directions. If a filter blocks the call, or the script fails, the caller gets a generic refusal: HTTP 403 with blocked by policy, or the MCP equivalent. The refusal is the same for input and output blocks. It does not contain the filter name or the reason, so a caller cannot use it to learn your filter configuration. AI Studio writes the reason to the logs and records a compliance event for every block.
Tool Response Filter Examples
Tool responses are plain strings, not JSON-structured messages, so they require simpler handling.Example 1: Block Tool Responses Containing Errors
Example 2: Redact PII from Tool Responses
Example 3: Filter Based on Tool Name
tyk.redact_pattern() helper is designed for LLM message structures (JSON format) and will not work with tool responses. For tool responses, use direct string manipulation with the text module as shown above.
Filter Execution Order in Chat Sessions
When a user sends a message in a chat session, filters are executed at multiple points in the pipeline:1. User Message Filters (Before RAG)
When: After preprocessing, before RAG vector search Purpose: Redact PII from user messages before they’re used for vector similarity search Context:input.messages[0].role == "user"
- ✅ RAG vector similarity search
- ✅ Subsequent LLM processing
- ✅ Chat history storage
2. File Content Filters (Before RAG)
When: When files are attached to messages Purpose: Filter sensitive content from uploaded files before indexing Context:input.context.file_ref contains the file reference
3. Tool Response Filters (After Tool Execution)
When: After a tool returns data, before sending to LLM Purpose: Filter sensitive data from external API responses Context:input.messages[0].role == "tool", input.context.tool_name available
Best Practices
- Always define
output- Scripts must set the output variable - Use
tyk.redact_patternfor LLM messages - Handles vendor differences automatically - Use
textmodule for tool responses - Direct string manipulation - Use messages array for complex LLM modifications - Gives you full control
- Provide clear block messages - Help users understand policy violations
- Test across vendors - OpenAI, Anthropic, and Google AI have different formats
- Check message roles - Different logic for system, user, assistant, and tool messages
- Handle edge cases - Empty arrays, missing fields, etc.
- Consider RAG impact - Filters run before RAG, so redactions affect vector search
Migration from Legacy API
Old API (still supported for backward compatibility):Response Filters
Response Filters enable administrators to block LLM responses based on content analysis, providing governance controls on what LLMs can say to end users.Key Characteristics
- Block-Only: Response filters can only block responses, not modify them
- Works on LLM Responses: On LLMs and chat, response filters run on the LLM response. On tools, the same flag selects the tool output direction. Refer to Tool Filters.
- Streaming Support: Execute per-chunk during streaming with access to accumulated buffer
- Script-Controlled: Filter scripts decide when to evaluate based on buffer length
Configuration
Enable response filtering by checking “Is this a Response Filter?” when creating or editing a filter in the admin UI.Script API for Response Filters
Response filters use the sameScriptInput/ScriptOutput structure as request filters, with additional response-specific fields:
Input Object (Non-Streaming):
payload and messages fields in output are ignored for response filters.
Example 1: Block Refund Promises (Works for Streaming and Non-Streaming)
Example 2: Block Harmful Content (Streaming with Buffer Check)
Example 3: Combined with LLM Analysis
Use another LLM to analyze the response for policy violations:Response Filter Execution
Proxy (REST):- Executes after response hooks (if any)
- Full response available in
input.raw_input - If blocked: Error returned to client instead of response
- Executes on every chunk
- Executes one more time when the stream is complete, with
is_final: true, an emptyraw_input, and the full response incurrent_buffer. A short response that never reached the buffer threshold of the script is then still checked. - Access to both
raw_input(current chunk) andcurrent_buffer(accumulated text) - If blocked: Streaming stops, error sent to client
- If blocked on the final run: The stream ends with an error event, and AI Studio logs a blocked response. The chunks that the client already received are not removed. To block content before the client receives it, check
current_bufferon each chunk. Do not rely only on the final run.
- Executes before adding to chat history
- If blocked: Error published to queue instead of response
- Executes on every chunk before publishing to queue
- If blocked: Error published, streaming callback returns error to stop further chunks
Best Practices for Response Filters
- Buffer Management (Streaming): Script controls evaluation timing based on
len(current_buffer) - Performance: Keep filter logic lightweight (executes per-chunk in streaming)
- Clear Messages: Provide helpful block messages for users
- Fail Open: Filters fail open on script errors (response allowed through)
- LLM-Only: Response filters only work on LLM responses, not tool responses
- Block-Only: Cannot modify responses, only block them
When to Use Response Filters vs Request Filters
Use Request Filters When:- Preventing sensitive data from reaching the LLM
- Redacting PII before LLM processing
- Enforcing input policies
- Preventing LLMs from making commitments (refunds, promises)
- Blocking harmful or inappropriate LLM outputs
- Enforcing corporate communication policies
- Detecting policy violations in LLM responses
This unified filter system demonstrates how flexible and powerful Tyk AI Studio’s scripting capabilities are, enabling administrators to enforce strict data governance policies while supporting advanced LLM and tool integration workflows with both blocking and modification capabilities.