> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/guardrails/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/guardrails/_mcp/server.

# nemoguardrails.library.context_bloat_detection.actions

Context bloat detection action

Detects context-manipulation attacks where attacker-controlled content
(retrieved chunks or user input) is padded, oversized, or repetitively
structured to cause system prompt forgetting, bury instructions mid-context
(harder to detect), or exhaust token budget.

Wire as retrieval rail (RAG chunks) or input rail.

## Module Contents

### Classes

| Name                                                                                               | Description |
| -------------------------------------------------------------------------------------------------- | ----------- |
| [`ContextBloatResult`](#nemoguardrails-library-context_bloat_detection-actions-ContextBloatResult) | -           |

### Functions

| Name                                                                                                         | Description                                                     |
| ------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------- |
| [`_check_entropy`](#nemoguardrails-library-context_bloat_detection-actions-_check_entropy)                   | -                                                               |
| [`_check_longest_run`](#nemoguardrails-library-context_bloat_detection-actions-_check_longest_run)           | -                                                               |
| [`_check_repetition`](#nemoguardrails-library-context_bloat_detection-actions-_check_repetition)             | -                                                               |
| [`_longest_run_ratio`](#nemoguardrails-library-context_bloat_detection-actions-_longest_run_ratio)           | Fraction of text that is the longest run of a single character. |
| [`_repetition_ratio`](#nemoguardrails-library-context_bloat_detection-actions-_repetition_ratio)             | High values are a padding-attack signature.                     |
| [`_shannon_entropy`](#nemoguardrails-library-context_bloat_detection-actions-_shannon_entropy)               | Samples large inputs to bound runtime.                          |
| [`_stratified_sample`](#nemoguardrails-library-context_bloat_detection-actions-_stratified_sample)           | -                                                               |
| [`_validate_config`](#nemoguardrails-library-context_bloat_detection-actions-_validate_config)               | -                                                               |
| [`context_bloat_detection`](#nemoguardrails-library-context_bloat_detection-actions-context_bloat_detection) | Detect context-bloat / context-manipulation attacks.            |

### Data

[`ENTROPY_SAMPLE_CHARS`](#nemoguardrails-library-context_bloat_detection-actions-ENTROPY_SAMPLE_CHARS)

[`ENTROPY_SAMPLE_THRESHOLD`](#nemoguardrails-library-context_bloat_detection-actions-ENTROPY_SAMPLE_THRESHOLD)

[`log`](#nemoguardrails-library-context_bloat_detection-actions-log)

### API

```python
class nemoguardrails.library.context_bloat_detection.actions.ContextBloatResult
```

**Bases:** `typing.TypedDict`

```python
nemoguardrails.library.context_bloat_detection.actions._check_entropy(
    text: str,
    cfg,
    detections: typing.List[str],
    metrics: dict
) -> typing.Optional[nemoguardrails.library.context_bloat_detection.actions.ContextBloatResult]
```

```python
nemoguardrails.library.context_bloat_detection.actions._check_longest_run(
    text: str,
    cfg,
    detections: typing.List[str],
    metrics: dict
) -> typing.Optional[nemoguardrails.library.context_bloat_detection.actions.ContextBloatResult]
```

```python
nemoguardrails.library.context_bloat_detection.actions._check_repetition(
    text: str,
    cfg,
    detections: typing.List[str],
    metrics: dict
) -> typing.Optional[nemoguardrails.library.context_bloat_detection.actions.ContextBloatResult]
```

```python
nemoguardrails.library.context_bloat_detection.actions._longest_run_ratio(
    text: str
) -> float
```

Fraction of text that is the longest run of a single character.

```python
nemoguardrails.library.context_bloat_detection.actions._repetition_ratio(
    text: str,
    n: int = 3
) -> float
```

High values are a padding-attack signature.

```python
nemoguardrails.library.context_bloat_detection.actions._shannon_entropy(
    text: str
) -> float
```

Samples large inputs to bound runtime.

```python
nemoguardrails.library.context_bloat_detection.actions._stratified_sample(
    text: str,
    sample_chars: int
) -> str
```

```python
nemoguardrails.library.context_bloat_detection.actions._validate_config(
    config: nemoguardrails.RailsConfig
) -> None
```

```python
nemoguardrails.library.context_bloat_detection.actions.context_bloat_detection(
    text: str,
    config: nemoguardrails.RailsConfig
) -> nemoguardrails.library.context_bloat_detection.actions.ContextBloatResult
```

async

Detect context-bloat / context-manipulation attacks.
Check order is cheapest first to enable early-exit.

**Parameters:**

The text to inspect (joined chunks or user message).

RailsConfig with rails.config.context\_bloat\_detection settings.

**Returns:** `ContextBloatResult`

ContextBloatResult with is\_bloat flag, processed text, reason, metrics.

```python
nemoguardrails.library.context_bloat_detection.actions.ENTROPY_SAMPLE_CHARS = 8000
```

```python
nemoguardrails.library.context_bloat_detection.actions.ENTROPY_SAMPLE_THRESHOLD = 10000
```

```python
nemoguardrails.library.context_bloat_detection.actions.log = logging.getLogger(__name__)
```