> For the complete documentation index, see [llms.txt](https://ai.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ai.ortusbooks.com/main-components/middleware/output-guard.md).

# OutputGuardMiddleware

Redact secrets and strip exfiltration channels from model outputs and reasoning

Class: `bxModules.bxai.models.middleware.security.OutputGuardMiddleware`

Guards outbound model content by redacting sensitive values and removing exfiltration markdown channels.

## Features

* Redacts secrets and PII patterns with configurable mask
* Strips markdown image/link exfiltration patterns with host allowlists
* Scans reasoning content as well as final answer content
* Actions: `redact`, `flag`, `block`
* Optional scan of final agent output in `afterAgentRun`

## Constructor

```javascript
middleware = new bxModules.bxai.models.middleware.security.OutputGuardMiddleware(
    action             : "redact",
    redactors          : [],
    customRedactors    : {},
    mask               : "[REDACTED]",
    stripMarkdownImages: true,
    stripExternalLinks : false,
    allowedImageHosts  : [],
    allowedLinkHosts   : [],
    scanAgentOutput    : true
)
```

## Configuration

| Option                | Type    | Default        | Description                                      |
| --------------------- | ------- | -------------- | ------------------------------------------------ |
| `action`              | string  | `"redact"`     | Behavior on findings: `redact`, `flag`, `block`  |
| `redactors`           | array   | `[]`           | Named redactor set; empty uses default redactors |
| `customRedactors`     | struct  | `{}`           | Extra redactors as regex or closure mappers      |
| `mask`                | string  | `"[REDACTED]"` | Replacement text for redacted values             |
| `stripMarkdownImages` | boolean | `true`         | Strip non-allowlisted markdown images            |
| `stripExternalLinks`  | boolean | `false`        | Rewrite external links to visible text           |
| `allowedImageHosts`   | array   | `[]`           | Allowlist for image hostnames                    |
| `allowedLinkHosts`    | array   | `[]`           | Allowlist for link hostnames                     |
| `scanAgentOutput`     | boolean | `true`         | Also scan final output in `afterAgentRun`        |

## Hooks Used

* `afterLLMCall`
* `afterAgentRun`

## Action Semantics

* `redact`: mutates resolved response fields to scrub detected data
* `flag`: preserves response, logs and records findings
* `block`: throws `BXAI.SecurityViolation`

## Example

```javascript
agent = aiAgent(
    middleware: [
        new bxModules.bxai.models.middleware.security.OutputGuardMiddleware(
            action            : "redact",
            mask              : "[MASKED]",
            stripExternalLinks: true,
            allowedLinkHosts  : [ "docs.ortusbooks.com" ]
        )
    ]
)
```

## Notes

* Invalid actions throw `InvalidAction` at construction.
* Streaming is detection-oriented: `afterLLMCall` runs once the stream has already emitted chunks.
* String-only agent responses cannot always be rewritten in `afterAgentRun`; redaction reliability is strongest at `afterLLMCall`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://ai.ortusbooks.com/main-components/middleware/output-guard.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
