# Use a built-in check

> Name one of Kindgi's built-in checks in a guardrail instead of writing your own, give it the settings it reads, and see where in the turn each one runs.

This guide puts one of Kindgi's built-in checks on an agent, instead of a check you write. It lists the settings each one reads, and where in the turn each one runs.

Kindgi has seven built-in checks for common rules: words an answer must or mustn't contain, a pattern it must match, citations, and which tools a turn may call, how many and in what order. A guardrail can name one instead of shipping a check you [write yourself](https://docs.kindgi.com/v0.8.1/guides/guardrails/write-a-guardrail.md).

## Before you start

- **A pack** on a runtime such as `kindgi dev`: a TypeScript pack with Kindgi 0.2's SDK, or a Python, Java or Scala pack with an SDK that has `built_in_guardrail` (Python) or `builtInCheck` (Java and Scala).
- **An agent to put the guardrail on.**

## Name a built-in

A built-in check needs no code of yours. The guardrail names the built-in as its `check`, gives its `config`, and ships no check implementation. Ask your coding agent: "I want a guardrail, with a built-in check, that stops an answer promising a guarantee." Kindgi's skills in your pack tell it how ([How it knows Kindgi](https://docs.kindgi.com/v0.8.1/start/coding-agents.md)). For reference, the guardrail looks like this:

- **TypeScript**

  `guardrails/no-guarantees/index.ts`

  ```ts
  export default {
    id: 'my-pack.no-guarantees',
    name: 'Never promise a guarantee',
    kind: 'zero-llm',
    check: 'forbidden-substring',
    config: { patterns: ['guaranteed', 'we promise'] },
    action: { 'on-violation': 'halt' },
    severity: 'error',
  };
  ```

- **Python**

  `guardrails/no_guarantees.py`

  ```python
  from kindgi import built_in_guardrail

  no_guarantees = built_in_guardrail(
      id="my-pack.no-guarantees",
      name="Never promise a guarantee",
      check="forbidden-substring",
      config={"patterns": ["guaranteed", "we promise"]},
      on_violation="halt",
      severity="error",
  )
  ```

  `built_in_guardrail` declares the guardrail at module level, like `@guardrail`, with no function. Calling it in Python raises: only the runtime runs a built-in.

- **Java**

  `src/main/java/mypack/guardrails/NoGuarantees.java`

  ```java
  package mypack.guardrails;

  import com.kindgi.pack.Guardrail;
  import java.util.List;
  import java.util.Map;

  public final class NoGuarantees {
    public static final Guardrail<Map<String, Object>> GUARDRAIL = Guardrail.define("my-pack.no-guarantees")
        .name("Never promise a guarantee")
        .onViolation("halt")
        .severity("error")
        .builtInCheck("forbidden-substring", Map.of("patterns", List.of("guaranteed", "we promise")));

    private NoGuarantees() {}
  }
  ```

  `builtInCheck` finishes the builder in place of `check(...)`. `builtInCheck("must-cite")`, with no config, takes the built-in's defaults. Its `evaluate` throws: only the runtime runs a built-in.

- **Scala**

  `src/main/scala/mypack/guardrails/NoGuarantees.scala`

  ```scala
  package mypack.guardrails

  import com.kindgi.pack.scaladsl._

  object NoGuarantees {
    val guardrail: Guardrail[Map[String, Any]] = Guardrail.json("my-pack.no-guarantees")
      .name("Never promise a guarantee")
      .onViolation("halt")
      .severity("error")
      .builtInCheck("forbidden-substring", Map("patterns" -> List("guaranteed", "we promise")))
  }
  ```

  `builtInCheck` finishes the builder in place of `check`. It starts from `Guardrail.json`: a built-in's config is the runtime's to check, so it has no config type.

The config is keyed as on the wire, as [The built-in checks](https://docs.kindgi.com/v0.8.1/guides/guardrails/use-a-built-in-check.md#the-built-in-checks) lists it. Python, Java and Scala refuse the guardrail where it's defined if its check isn't one of the built-ins, or nothing says what a failing check does. Java's and Scala's `builtInCheck` also refuse what a built-in doesn't take: a config given twice, a config type or schema, a `checkId`, a `kind` other than `zero-llm`, `judgeCapabilities`, `sandbox`, `limits` or `network`.

Put it on an agent by its id, as with any guardrail, as [Put it on an agent](https://docs.kindgi.com/v0.8.1/guides/guardrails/write-a-guardrail.md#put-it-on-an-agent) shows. With `halt`, an answer containing "guaranteed" stops the turn, as [A halted turn](https://docs.kindgi.com/v0.8.1/guides/guardrails/halt-or-record.md#a-halted-turn) describes. The failure gives the check's reason. For a `must-cite` guardrail on an answer without a citation:

```text
Error [guardrail-violation]: Turn blocked by guardrail 'acme.cite-or-halt': expected >= 1 citations, found 0
```

## The built-in checks

A tool list holds tool ids, as `<pack>.<tool>`. A setting in **bold** is required.

| Check                 | Fails when                                                                          | `config`                                                                                                                                                         |
| --------------------- | ----------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `must-cite`           | the answer is empty, or has fewer than `minCitations` matches of `sourcePattern`    | `minCitations`: a whole number, 1 or more (1 by default); `sourcePattern`: a regular expression (by default, citations in square brackets, such as `[ord_1003]`) |
| `never-call-tool`     | the turn called any tool in `tools`                                                 | **`tools`**: a list of one or more tool ids                                                                                                                      |
| `max-tool-calls`      | the turn made more than `max` tool calls                                            | `max`: a whole number, 0 or more (10 by default)                                                                                                                 |
| `output-matches`      | the answer doesn't match `pattern`, or, with `negate: true`, it does                | **`pattern`**: a regular expression; `flags`: regular-expression flags (`d`, `g`, `i`, `m`, `s`, `u`, `v`, `y`), such as `"i"`; `negate`: `true` or `false`      |
| `tool-order`          | the tools in `sequence` weren't called in that order (other calls may come between) | **`sequence`**: a list of one or more tool ids                                                                                                                   |
| `required-substring`  | the answer is empty, or lacks any of `patterns`                                     | **`patterns`**: a list of one or more plain-text strings; `caseSensitive` (`false` by default)                                                                   |
| `forbidden-substring` | the answer contains any of `patterns`                                               | **`patterns`**: a list of one or more plain-text strings; `caseSensitive` (`false` by default)                                                                   |

A config the check doesn't take is refused: a required setting left out, a setting of the wrong type, one the check doesn't know, or a regular expression that doesn't compile, or is longer than 8,192 characters with its flags. Registering such a guardrail answers `422 guardrail-config-invalid`, with each problem in `details.issues`. Deploying a pack with one fails with `deployment-validation-failed`. If one reaches a turn anyway, its check can't run, so with `halt` the turn stops, as [When the check can't run](https://docs.kindgi.com/v0.8.1/guides/guardrails/halt-or-record.md#when-the-check-cant-run) describes.

## Where each check runs

Every built-in runs at the final answer, and some at other points of the turn too. A guardrail that names one can set `at` to any of its points: `at=` in Python, `set("at", …)` in Java and Scala ([Where in the turn it runs](https://docs.kindgi.com/v0.8.1/guides/guardrails/configure-a-guardrail.md#where-in-the-turn-it-runs)):

| Check                                             | Runs at                                          |
| ------------------------------------------------- | ------------------------------------------------ |
| `must-cite`, `required-substring`                 | `'final-answer'`                                 |
| `never-call-tool`, `max-tool-calls`, `tool-order` | `'before-tool'`, `'final-answer'`                |
| `output-matches`, `forbidden-substring`           | `'model-step'`, `'after-tool'`, `'final-answer'` |

- **Before a tool call,** `never-call-tool`, `max-tool-calls` and `tool-order` judge that call against the turn's earlier calls. So with `halt` or `retry`, a call they fail never runs: with `tool-order`, for example, a refund before the order lookup.
- **`output-matches` and `forbidden-substring`** read the answer at the final answer, the reply's text on a model reply, and the call's result after a tool call. A result that's a string is read as it is, anything else as JSON, and an error as its message. A reply that only asks for tool calls has no text, so it passes `output-matches`.

Deploying or registering a guardrail at a point its built-in doesn't run at is refused, with the problem at `/at`. If one reaches a turn anyway, its check can't run there: it fails with `check-point-unsupported`, and its action handles it as [When the check can't run](https://docs.kindgi.com/v0.8.1/guides/guardrails/halt-or-record.md#when-the-check-cant-run) describes.

## Your own check under a built-in's name

The built-in ids are reserved. A pack that ships its own check under one, such as a `defineCheck({ id: 'must-cite', … })`, is refused when you build it, with `reserved-check-id`, or `DefinitionError` in Python. Give your check a name of its own, such as `my-pack.checks.must-cite`. Or, to use the built-in, name it and drop your implementation.

A current CLI marks a guardrail that only names a built-in. A pack built with an older CLI may lack that mark. A guardrail that names a built-in, comes with its pack's code, and lacks the mark gets one warning in the runtime's log each time the runtime loads the guardrail. The built-in runs either way. If the guardrail only names the built-in, rebuild the pack with a current CLI and the warning goes. If its module is your own check, give that check a name of its own: the built-in has been running in its place.

## Next

- [Write a guardrail](https://docs.kindgi.com/v0.8.1/guides/guardrails/write-a-guardrail.md)
- [Configure a guardrail](https://docs.kindgi.com/v0.8.1/guides/guardrails/configure-a-guardrail.md)
- [Stop a turn or record a violation](https://docs.kindgi.com/v0.8.1/guides/guardrails/halt-or-record.md)
