> ## 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.

# Invoice chaser

> T2 chain · finance: overdue reminders drafted, and nothing goes out until a human says yes.

> **T2 chain · finance / freelance**: the human-in-the-loop example.
> `nika:prompt` blocks the workflow until someone answers; the
> drafting is automated, the **sending decision never is**.

## The job

Friday: open the ledger, find who's late, write polite-but-firm
reminders, don't send the awkward one to the client who paid this
morning. This file filters the CSV deterministically (jq, not an LLM
guessing at numbers), drafts every reminder, then stops and asks you.

## The shape

```mermaid theme={"system"}
flowchart LR
  ledger["ledger · nika:read"]:::invoke
  rows["rows · nika:convert"]:::invoke
  overdue["overdue · nika:jq"]:::invoke
  drafts["drafts"]:::infer
  approve["approve · nika:prompt"]:::invoke
  save["save · nika:write"]:::invoke
  ledger --> rows
  rows --> overdue
  overdue --> drafts
  drafts --> approve
  drafts --> save
  approve --> save
  classDef infer fill:#5b8cff22,stroke:#5b8cff,color:#5b8cff
  classDef invoke fill:#22d3ee22,stroke:#22d3ee,color:#22d3ee
```

## The file

```yaml invoice-chaser.nika theme={"system"}
nika: invoice-chaser

model: ollama/qwen3.5:4b   # local · zero key · swap for groq/llama-3.3-70b-versatile (drafting is a fast-model job)

const:
  ledger_csv: "examples/fixtures/invoices.csv"
permits:
  tools: ["nika:convert", "nika:jq", "nika:prompt", "nika:read", "nika:write"]
  fs:
    # Two files, both spelled out — `const.ledger_csv` in, the drafts file
    # out. A const is baked in and a run cannot move it, so the boundary is
    # exact: no glob, and nothing else on disk is reachable either way.
    read: ["examples/fixtures/invoices.csv"]
    write: ["out/reminders-to-send.md"]

tasks:
  ledger:
    invoke:
      tool: "nika:read"
      args: { path: "${{ const.ledger_csv }}" }

  rows:
    with:
      csv: ${{ tasks.ledger.output }}
    invoke:
      tool: "nika:convert"
      args:
        input: "${{ with.csv }}"
        from: csv
        to: json
        has_header: true

  overdue:
    with:
      rows: ${{ tasks.rows.output }}
    invoke:
      tool: "nika:jq"
      args:
        input: "${{ with.rows }}"
        expression: 'map(select(.status == "overdue"))'

  drafts:
    with:
      overdue: ${{ tasks.overdue.output }}
    when: ${{ size(with.overdue) > 0 }}
    infer:
      # a reasoning model needs 16384 here, or a documented run.reasoning.effort: low
      max_tokens: 900       # a ceiling makes the cost report a number, not UNBOUNDED
      prompt: |
        Draft one short, polite payment reminder email per overdue invoice ·
        ${{ with.overdue }}
        Markdown · one section per client · firm but warm.

  approve:
    with:
      drafts: ${{ tasks.drafts.output }}
    when: ${{ with.drafts != null && size(with.drafts) > 0 }}   # nothing drafted — OR an empty answer (a thinking seat can spend the budget in its think block) · nothing to approve
    invoke:
      tool: "nika:prompt"
      args:
        message: "Save the reminder drafts for sending?"
        default: false

  save:
    with:
      drafts: ${{ tasks.drafts.output }}
      approved: ${{ tasks.approve.output }}
    when: ${{ with.drafts != null && size(with.drafts) > 0 && with.approved == true }}   # skipped (null) OR empty drafts → never write a 0-byte deliverable under a green run
    invoke:
      tool: "nika:write"
      args:
        path: out/reminders-to-send.md
        create_dirs: true
        content: "${{ with.drafts }}"

outputs:
  overdue:
    value: ${{ tasks.overdue.output }}
    description: "The overdue rows the reminders were drafted for"
```

## How it works

**1. Data work is jq work**

CSV → JSON via `nika:convert`, then `map(select(.status == "overdue"))`
in `nika:jq`. No model touches the numbers: deterministic, free,
auditable.

**2. size() gates the whole tail**

The list crosses the boundary (`overdue: ${{ tasks.overdue.output }}`
in `with:`), then `when: ${{ size(with.overdue) > 0 }}` is the
canonical empty-check. No overdue invoices → the rest of the file
skips (and value edges pass the skip along as `null`).

**3. The approval gate**

`nika:prompt` asks at a terminal; with no terminal it takes its
`default: false`, so a script or a scheduler never sends anything by
accident. The save binds the verdict
(`approved: ${{ tasks.approve.output }}`) and runs only
`when: ${{ with.drafts != null && size(with.drafts) > 0 && with.approved == true }}`.

## Constructs you just used

| Construct | Where | Reference |
| - | - | - |
| `nika:convert` from/to | `rows` | [Builtins](https://docs.nika.sh/reference/builtins.md) |
| `nika:jq` filtering | `overdue` | [Builtins](https://docs.nika.sh/reference/builtins.md) |
| CEL `size()` | `drafts.when` | [Workflows](https://docs.nika.sh/concepts/workflows.md) |
| `nika:prompt` human gate | `approve` | [Builtins](https://docs.nika.sh/reference/builtins.md) |

## Run it

> [!NOTE]
>
> **What it needs:** A local model ([Ollama](https://ollama.com) serving `qwen3.5:4b`) and the sample ledger: three of its five rows are overdue, so the run reaches the gate.

**1. Rehearse it**

```bash theme={"system"}
nika try invoice-chaser
```

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 `invoice-chaser.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/invoices.csv "$FIXTURES/invoices.csv"
```

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

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

The run writes `out/reminders-to-send.md`, once you approve.

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

On a terminal the gate asks you. Piped, in CI or under a scheduler it takes `default: false` and skips the write.

## Make it yours

* Per-client emails instead of one file: `for_each` over the overdue rows ([fan-out tier](https://docs.nika.sh/examples/competitor-radar.md)).
* Escalate 60-days-late differently: a second jq filter + a sterner prompt.
* Wire the approved drafts to your mail API with `nika:fetch method: POST`.

> [card] [Next · Support triage](https://docs.nika.sh/examples/support-triage.md)
>
> Classify a whole queue in one schema-typed call, then let jq slice the
> urgent ones out.


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