This guide explains how to use TrueFoundry’s built-in Metadata Validation guardrail to enforce that requests carry the metadata your organization requires, with the values you expect.Documentation Index
Fetch the complete documentation index at: https://www.truefoundry.com/llms.txt
Use this file to discover all available pages before exploring further.
What is Metadata Validation?
Metadata Validation is a built-in TrueFoundry guardrail that checks the custom metadata sent with a request against a set of rules you define. It runs directly within the AI Gateway without requiring external API calls, providing fast and cost-effective validation. Metadata is passed to the gateway using theX-TFY-METADATA header (see Log Custom Metadata). This guardrail lets you require that certain keys are always present, constrain their values to a regex pattern or a fixed list, and optionally reject any keys you have not explicitly allowed.
Metadata Validation only runs on the LLM Input hook (
beforeRequestHook). When attached to any other hook (LLM Output, MCP Pre Tool, MCP Post Tool), it passes through without performing any checks.Key Features
- Required Keys: Ensure specific metadata keys are always present in the request.
- Value Constraints: Validate a key’s value against a regex pattern or restrict it to a fixed set of allowed values.
- Unknown Key Control: Optionally reject any metadata key that is not explicitly declared in your rules.
Adding Metadata Validation Guardrail
To add Metadata Validation to your TrueFoundry setup, follow these steps:Create or Select a Guardrails Group
Create a new guardrails group or select an existing one where you want to add the Metadata Validation guardrail.
Add Metadata Validation Integration
Click on Add Guardrail and select Metadata Validation from the TrueFoundry Guardrails section.
Configure the Guardrail
Fill in the configuration form with your desired settings (see Configuration Options below). Toggle Allow unknown keys and add a rule for each metadata key you want to enforce.

Configuration Options
| Parameter | Default | Description |
|---|---|---|
| Name | Required | Unique identifier for this guardrail |
| Operation | validate | Metadata Validation can only be used for validation |
| Enforcing Strategy | enforce_but_ignore_on_error | enforce, enforce_but_ignore_on_error, or audit |
| Allow unknown keys | true | When on, metadata keys not listed in the rules pass through. When off, only listed keys are accepted |
| Keys | None | The per-key rules applied to specific metadata fields (see Key Rules below) |
See Guardrails Overview for details on Operation Modes and Enforcing Strategy.
Key Rules
Each entry under Keys maps a metadata key name to a rule. There are two kinds of rules:Key must exist
Only the presence of the key is checked — any value passes. Once configured, the key must be present in the request metadata.Value must match
Validates the key’s value, with an optional requirement that the key be present:| Field | Default | Description |
|---|---|---|
| Required | true | When on, the key must be present. When off, the key is optional, but if present its value must still satisfy the constraint |
| Value constraint | Required | How the value is validated — either a regex pattern or a fixed list of allowed values |
| Constraint | Description |
|---|---|
| Regex | The value must match the provided regular expression |
| Allowed values | The value must exactly match one of the listed values. Comparison is case-sensitive |
How It Works
When a request reaches the LLM Input hook, the guardrail evaluates the request metadata as follows:- If Allow unknown keys is off, every metadata key that is not declared in your rules raises an
unknown_keyviolation. Keys injected by the gateway itself (for examplesubject,subjectType,tfy_agent_name, and any default gateway metadata) are exempt from this check. - For each declared key rule:
- Key must exist: a missing key raises a
missing_requiredviolation. - Value must match (required): a missing key raises a
missing_requiredviolation. - Value must match (regex): if the key is present, its value must match the pattern, otherwise a
pattern_mismatchviolation is raised. An invalid regex pattern raises aninvalid_regex_patternviolation. - Value must match (allowed values): if the key is present, its value must be in the allowed set, otherwise a
value_not_allowedviolation is raised.
- Key must exist: a missing key raises a
- If any violations are found, the verdict fails. The response handling then depends on the configured Enforcing Strategy.
Violation Reasons
| Reason | Meaning |
|---|---|
missing_required | A required key was not present in the request metadata |
pattern_mismatch | The key’s value did not match the configured regex pattern |
value_not_allowed | The key’s value was not in the configured allowed values list |
unknown_key | A metadata key was present that is not declared in the rules (only when Allow unknown keys is off) |
invalid_regex_pattern | The configured regex pattern itself was invalid |
Examples
Consider a guardrail with Allow unknown keys turned off and the following key rules:environment— Value must match, allowed values:prod,staging,devcustomer_id— Value must match, regex:^cust_[0-9]+$team— Key must exist
Allowed: all rules satisfied
Allowed: all rules satisfied
Metadata:Result: Allowed — all keys are present and valid, and no unknown keys are sent.
Blocked: missing required key
Blocked: missing required key
Metadata:Result: Blocked —
team:missing_required (the team key is missing).Blocked: value not allowed
Blocked: value not allowed
Metadata:Result: Blocked —
environment:value_not_allowed (production is not in [prod, staging, dev]).Blocked: pattern mismatch
Blocked: pattern mismatch
Metadata:Result: Blocked —
customer_id:pattern_mismatch (12345 does not match ^cust_[0-9]+$).Blocked: unknown key
Blocked: unknown key
Metadata:Result: Blocked —
debug:unknown_key (the debug key is not declared and unknown keys are not allowed).Sending Metadata
Metadata is sent using theX-TFY-METADATA header as a JSON object of string key-value pairs:
Use Cases
| Scenario | Configuration |
|---|---|
| Mandatory cost-attribution tags | Require keys like team or customer_id so every request can be attributed in analytics |
| Restrict environment values | Constrain environment to an allowed list (prod, staging, dev) to prevent typos and unexpected values |
| Enforce ID formats | Use a regex to ensure identifiers such as customer_id follow your organization’s format |
| Lock down metadata | Turn off Allow unknown keys to reject any metadata field that is not explicitly approved |
Recommended Hooks
| Hook | Use Case |
|---|---|
| LLM Input | Validate request metadata before the request is processed |
Metadata Validation runs only on the LLM Input hook. Attaching it to other hooks has no effect.