> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nika.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Contract guard

> T2 chain · legal: clause extraction on a LOCAL model. The contract never leaves the machine.

> **T2 chain · legal / compliance**: `model: ollama/qwen3.5:4b` at the
> envelope means **every** model call in this file runs on your own
> hardware. For a contract, that's not a preference. It's the
> requirement. Belt-and-braces: `schema:` at infer time, `nika:validate`
> after, `nika:assert` as the hard gate.

## The job

Outside counsel takes a week. Pasting the contract into a cloud chatbot
takes your confidentiality with it. This workflow extracts every
risk-bearing clause (verbatim quotes, typed risk levels) locally,
re-validates the extraction, and refuses to write the memo if the data
doesn't hold.

## The shape

```mermaid theme={"system"}
flowchart LR
  contract["contract · nika:read"]:::invoke
  clauses["clauses · typed"]:::infer
  check["check · nika:validate"]:::invoke
  gate["gate · nika:assert"]:::invoke
  memo["memo"]:::infer
  save["save · nika:write"]:::invoke
  contract --> clauses
  clauses --> check
  check --> gate
  clauses --> memo
  gate -.->|success| memo
  memo --> save
  classDef infer fill:#5b8cff22,stroke:#5b8cff,color:#5b8cff
  classDef invoke fill:#22d3ee22,stroke:#22d3ee,color:#22d3ee
```

## The file

```yaml contract-guard.nika theme={"system"}
nika: contract-guard

# Local · the whole review runs offline, zero cloud. The house seat is a
# THINKING model: the think block and the JSON both count against
# `max_tokens` (engine#428), so the extraction ceiling below covers both.
model: ollama/qwen3.5:4b

inputs:
  contract_path:
    type: string
    default: "examples/fixtures/contract.md"
    description: "Path to the contract (markdown or plain text)"
permits:
  tools: ["nika:assert", "nika:read", "nika:validate", "nika:write"]
  fs:
    # `permits:` cannot interpolate (NIKA-AUTH-007), so this literal does not
    # follow `inputs.contract_path` around: passing --var without editing this
    # line is refused at run with NIKA-SEC-004. Deliberate — a reviewer can
    # see every file this workflow may open by reading four words.
    read: ["examples/fixtures/contract.md"]
    write: ["out/risk-memo.md"]

tasks:
  contract:
    invoke:
      tool: "nika:read"
      args: { path: "${{ inputs.contract_path }}" }

  clauses:
    with:
      contract: ${{ tasks.contract.output }}
    infer:
      # a reasoning model needs 16384 here, or a documented run.reasoning.effort: low
      max_tokens: 4000   # think block + the clause JSON
      prompt: |
        Extract every risk-bearing clause from this contract ·
        ${{ with.contract }}
        Quote each clause verbatim · classify its risk.
      schema:
        type: object
        additionalProperties: false
        required: [clauses]
        properties:
          clauses:
            type: array
            items:
              type: object
              additionalProperties: false
              required: [quote, type, risk]
              properties:
                quote: { type: string }
                type: { type: string, enum: [liability, termination, ip, payment, data, other] }
                risk: { type: string, enum: [low, medium, high] }

  check:
    with:
      clauses: ${{ tasks.clauses.output }}
    invoke:
      tool: "nika:validate"
      args:
        data: "${{ with.clauses }}"
        format: json
        schema:
          type: object
          required: [clauses]
          properties:
            clauses:
              type: array
              minItems: 1        # an empty extraction is a FAILED extraction

  gate:
    with:
      extraction_ok: ${{ tasks.check.output.valid == true }}   # the whole condition crosses as ONE boundary expression
    invoke:
      tool: "nika:assert"
      args:
        condition: "${{ with.extraction_ok }}"
        message: "Clause extraction failed the schema gate: refusing to write the memo"

  memo:
    with:
      clauses: ${{ tasks.clauses.output.clauses }}
    after:
      gate: success              # state, no data · the assert passed or the memo is cancelled
    infer:
      # a reasoning model needs 16384 here, or a documented run.reasoning.effort: low
      max_tokens: 3000   # think block + the one-page memo
      prompt: |
        Write a one-page risk memo from these clauses ·
        ${{ with.clauses }}
        Order by risk · high first · cite the quoted text.

  save:
    with:
      memo: ${{ tasks.memo.output }}
    invoke:
      tool: "nika:write"
      args:
        path: out/risk-memo.md
        content: "${{ with.memo }}"
        create_dirs: true

outputs:
  clauses:
    value: ${{ tasks.clauses.output.clauses }}
    description: "Typed risk-bearing clauses, verbatim quotes"
  memo: ${{ tasks.memo.output }}
```

## How it works

**1. Sovereignty is one line**

The envelope `model:` points at Ollama. Same file, same schema, same
everything, just no cloud. Swap back to a cloud provider for
non-sensitive documents; the workflow doesn't change shape.

**2. validate re-checks what schema promised**

`nika:validate` runs the extraction against a second, stricter
schema (`minItems: 1`) and returns `{valid, errors}`: belt and
braces for legal-grade output.

**3. assert is the fail-fast guard**

`when:` skips; `nika:assert` FAILS. A memo built on a bad extraction
is worse than no memo, so the gate throws, loudly.

## Constructs you just used

| Construct | Where | Reference |
| - | - | - |
| local provider (`ollama/…`) | envelope `model:` | [Providers](https://docs.nika.sh/concepts/providers.md) |
| `nika:validate` | `check` | [Builtins](https://docs.nika.sh/reference/builtins.md) |
| `nika:assert` vs `when:` | `gate` | [Builtins](https://docs.nika.sh/reference/builtins.md) |
| verbatim-quote schema | `clauses` | [The 4 verbs](https://docs.nika.sh/concepts/verbs.md) |

## Run it

> [!NOTE]
>
> **What it needs:** A local model ([Ollama](https://ollama.com) serving `qwen3.5:4b`) and the sample contract. No network at all: the file declares none.

**1. Rehearse it**

```bash theme={"system"}
nika try contract-guard
```

A mock model answers, the sample files come with it, and nothing is written in your folder.

**2. Get your own copy**

Save the file above as `contract-guard.nika` in a new folder, then download the sample it reads:

```bash theme={"system"}
FIXTURES=https://raw.githubusercontent.com/supernovae-st/nika/v0.124.3/crates/nika-pack/pack/examples/fixtures
mkdir -p examples/fixtures
curl -fsSLo examples/fixtures/contract.md "$FIXTURES/contract.md"
```

**3. Check it, then run it**

```bash theme={"system"}
nika check contract-guard.nika
nika run contract-guard.nika --model mock/echo   # a mock model, no key
ollama pull qwen3.5:4b   # once, for the real run
nika run contract-guard.nika
```

The run writes `out/risk-memo.md`.

Any other model works: pass `--model <provider>/<name>` with its key, and cap a cloud model with `--max-cost-usd`.

To review your own contract, pass `--var contract_path=./yours.md` and add that path to `permits.fs.read`, or the read is refused (`NIKA-SEC-004`).

## Make it yours

* Compare against your clause playbook: `nika:read` the playbook + a second `infer` that flags deviations.
* Batch a folder of NDAs: `nika:glob` + `for_each` ([Localization factory](https://docs.nika.sh/examples/localization-factory.md) shows the chained fan-out).
* HR version: same shape over candidate agreements or policy docs. Sensitive data, local model, typed findings.

> [card] [Level up · T3 fan-out](https://docs.nika.sh/examples/competitor-radar.md)
>
> Next tier: collections the workflow discovers at runtime, processed in
> parallel with retry and a leash.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.