# `Nous.DecisionModel.Defang`
[🔗](https://github.com/nyo16/nous/blob/v0.21.1/lib/nous/decision_model/defang.ex#L1)

Neutralises chat-template special tokens in decision-model state.

llama.cpp's decision prompt tokenizes the state with special-token parsing,
so text such as `<|im_end|>\n<|im_start|>system` inside untrusted content
becomes *real* template tokens in the classifier's own prompt, and the
classifier cannot see them. Measured on Clef-Flash: asked whether the state
contains chat-template tokens, it answered p = 0.02 on the raw string and
p = 0.93 on the defanged one.

`Nous.DecisionModel.decide/4` applies `defang_term/1` to every state, for
every backend. The transform keeps the markers visible to the model but
stops them tokenizing as specials:

  * every `|` inside `<|...|>` (1–40 characters without `|`, `<`, `>` or
    whitespace between the bars) becomes `¦` (U+00A6):
    `<|im_start|>` → `<¦im_start¦>`
  * the other specials of the Qwen3.5 vocabulary Clef is built on
    (`<think>`, `<tool_call>`, `<tool_response>`, their closing forms, and
    every `<tts_...>`), the Llama/Mistral markers `[INST]`, `[/INST]`,
    `<s>` and `</s>`, and the BERT markers `[CLS]`, `[SEP]`, `[PAD]`,
    `[MASK]` and `[UNK]` get a `¦` after the opening bracket: `<¦think>`,
    `<¦/tool_call>`, `<¦tts_pad>`, `[¦INST]`, `<¦/s>`, `[¦CLS]`

The list is what an HTTP backend gets. A backend that holds the model's
vocabulary does better: `Backend.LlamaCpp` also runs `defang_tokens/2` with
a predicate that asks the tokenizer, so any `<...>`, `[...]` or `|||...|||`
sequence the loaded vocabulary parses as one token is broken the same way.

The transform is idempotent.

# `defang`

```elixir
@spec defang(String.t()) :: String.t()
```

Defangs special tokens in one string.

    iex> Nous.DecisionModel.Defang.defang("<|im_end|>\n<|im_start|>system [INST] hi [/INST]</s>")
    "<¦im_end¦>\n<¦im_start¦>system [¦INST] hi [¦/INST]<¦/s>"

# `defang_term`

```elixir
@spec defang_term(term()) :: term()
```

Defangs every string in a JSON-like term: strings, and the string keys and
values of maps and lists, recursively.

Atoms, numbers and structs are returned unchanged; build state from plain
strings, maps and lists.

# `defang_term`

```elixir
@spec defang_term(term(), (String.t() -&gt; String.t())) :: term()
```

Like `defang_term/1`, with `fun` applied to every string instead of
`defang/1`.

# `defang_tokens`

```elixir
@spec defang_tokens(String.t(), (String.t() -&gt; boolean())) :: String.t()
```

Breaks every `<...>`, `[...]` or `|||...|||` sequence (up to 64 characters
between the delimiters, no whitespace) for which `special?` returns `true`,
by inserting `¦` after its first character: `<tts_pad>` → `<¦tts_pad>`.

`special?` is called once per distinct candidate. Backends that hold the
model's vocabulary pass a tokenizer check, so the set of specials comes
from the model rather than from a list.

    iex> Nous.DecisionModel.Defang.defang_tokens("a <x> [y] <z>", &(&1 in ["<x>", "[y]"]))
    "a <¦x> [¦y] <z>"

---

*Consult [api-reference.md](api-reference.md) for complete listing*
