---
title: Guard modes
description: Choose whether TrustLoopGuard blocks, rewrites, or asks the model to regenerate unsafe drafts.
---

Create one guardrail when your agent starts, then call it before each reply is
sent or spoken.

```python
import trustloopguard as trustloop

guardrail = trustloop.guard(
    agent_id="support-agent",
    mode=trustloop.GuardMode.REWRITE,
)

reply = await guardrail(input=user_text, draft=agent_draft)
```

```ts
import { GuardMode, guard } from '@trustloopguard/sdk';

const guardrail = guard({
  agentId: 'support-agent',
  mode: GuardMode.Rewrite,
});

const reply = await guardrail({ input: userText, draft: agentDraft });
```

## Modes

| Mode                  | Python                            | TypeScript                      | Behavior                                                                                 |
| --------------------- | --------------------------------- | ------------------------------- | ---------------------------------------------------------------------------------------- |
| Strict                | `GuardMode.STRICT`                | `GuardMode.Strict`              | Blocks unsafe drafts immediately.                                                        |
| Rewrite               | `GuardMode.REWRITE`               | `GuardMode.Rewrite`             | Uses TrustLoopGuard `safe_output`; blocks if no safe output exists.                      |
| Rewrite or regenerate | `GuardMode.REWRITE_OR_REGENERATE` | `GuardMode.RewriteOrRegenerate` | Uses `safe_output`; otherwise calls your `regenerate` function and guards the new draft. |

`rewrite` is the default mode.

## Strict

Use strict mode for compliance-heavy workflows where unsafe output should never
be rephrased by the agent.

```python
guardrail = trustloop.guard(
    agent_id="support-agent",
    mode=trustloop.GuardMode.STRICT,
)
```

```ts
const guardrail = guard({
  agentId: 'support-agent',
  mode: GuardMode.Strict,
});
```

## Rewrite

Use rewrite mode when TrustLoopGuard can provide the replacement text.

```python
guardrail = trustloop.guard(
    agent_id="support-agent",
    mode=trustloop.GuardMode.REWRITE,
)
```

```ts
const guardrail = guard({
  agentId: 'support-agent',
  mode: GuardMode.Rewrite,
});
```

## Rewrite Or Regenerate

Use regeneration when you want the model to try again in real time if
TrustLoopGuard cannot provide a safe replacement.

```python
async def regenerate_reply(feedback: trustloop.RegenerateFeedback) -> str:
    return await model.generate(
        instructions=(
            "The previous draft was blocked by TrustLoopGuard: "
            f"{feedback.reason}. Generate a safer answer."
        )
    )

guardrail = trustloop.guard(
    agent_id="support-agent",
    mode=trustloop.GuardMode.REWRITE_OR_REGENERATE,
    regenerate=regenerate_reply,
    max_regenerations=1,
)
```

```ts
const guardrail = guard({
  agentId: 'support-agent',
  mode: GuardMode.RewriteOrRegenerate,
  maxRegenerations: 1,
  regenerate: async (feedback) => {
    return await model.generate({
      instructions:
        `The previous draft was blocked by TrustLoopGuard: ${feedback.reason}. ` +
        'Generate a safer answer.',
    });
  },
});
```

The SDK guards the regenerated draft again before returning it. Keep
`max_regenerations` / `maxRegenerations` low to avoid loops and latency spikes.

## Choosing A Mode

Use `strict` when the safest answer is to stop or hand off. Use `rewrite` when
low latency matters and TrustLoopGuard safe output is acceptable. Use
`rewrite_or_regenerate` when a better model answer is worth one extra model
call.
