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

# Deep research brief

> T4 epic · research: plan → budgeted research agent → thinking-model synthesis. Auditable end to end.

> **T4 epic · research / consulting / VC**: plan-then-execute. A planning
> pass turns the topic into four queries, an agent works the web **under
> explicit budgets**, and a synthesis pass writes the brief. Three stages,
> each with its own ceiling, and the brief lands on disk with its sources.

## The job

« Get me up to speed on X by Thursday » is two days of tabs, or it's
this pipeline: four sharp queries planned, researched with verbatim
quotes and source tracking, synthesized into an executive brief that
separates what's true from what's contested.

## The shape

```mermaid theme={"system"}
flowchart LR
  slug["slug · nika:jq"]:::invoke
  plan["plan · typed"]:::infer
  investigate["investigate · 2 tools"]:::agent
  brief["brief"]:::infer
  save["save · nika:write"]:::invoke
  plan --> investigate
  investigate --> brief
  slug --> save
  brief --> save
  classDef infer fill:#5b8cff22,stroke:#5b8cff,color:#5b8cff
  classDef invoke fill:#22d3ee22,stroke:#22d3ee,color:#22d3ee
  classDef agent fill:#b07bff22,stroke:#b07bff,color:#b07bff
```

## The file

```yaml deep-research-brief.nika theme={"system"}
nika: deep-research-brief

model: ollama/qwen3.5:4b   # local · zero key · `--model mock/echo` rehearses the whole file

inputs:
  topic:
    type: string
    default: "local-first AI runtimes"
    description: "What to research · free text (--var topic=…), and it never touches a path directly"

permits:
  tools: ["nika:done", "nika:fetch", "nika:jq", "nika:write"]
  net:
    # There is no `*` here and there never will be: `net.http` entries are exact
    # host names. An agent told to « research the web » therefore cannot — the
    # corpus it may reach is a decision taken HERE, before the loop starts,
    # rather than discovered afterwards in a trace. Add the sources you actually
    # trust; the agent's fetch is judged against this list at RUN, and `check`
    # cannot see it coming because the agent chooses its own URLs.
    http:
      - "arxiv.org"
      - "export.arxiv.org"
      - "en.wikipedia.org"
      - "hn.algolia.com"
  fs:
    # One brief per topic, written DIRECTLY in ./research/. `*` matches a single
    # segment and never crosses `/`, so even a slug that somehow kept a slash is
    # refused rather than steered into a subtree — `./research/**` would let it
    # through. The slug task below makes that doubly hard; the wall makes it
    # impossible, and only one of the two is a guarantee.
    write: ["./research/*.md"]

tasks:
  # The topic is free text — « Local-First AI Runtimes: a survey/2026 » — and it
  # ends up in a filename. Deriving the slug in jq keeps that arithmetic out of
  # the model AND out of the path: the expression is total, so there is no input
  # for which it returns something containing a separator.
  slug:
    invoke:
      tool: "nika:jq"
      args:
        input: "${{ inputs.topic }}"
        expression: 'ascii_downcase | gsub("[^a-z0-9]+"; "-") | gsub("^-|-$"; "")'

  plan:
    infer:
      # a reasoning model needs 16384 here, or a documented run.reasoning.effort: low
      max_tokens: 400              # planning is short · cap it and the cost report is a ceiling
      prompt: "Break '${{ inputs.topic }}' into 4 sharp research queries · no overlap."
      schema:
        # `additionalProperties: false` on every object node — without it the
        # shape is a suggestion and each provider answers with a slightly
        # different object. `plan.output.queries` is read below, so it has to be
        # a promise, not a hope.
        type: object
        additionalProperties: false
        required: [queries]
        properties:
          queries:
            type: array
            items: { type: string }

  investigate:
    with:
      queries: ${{ tasks.plan.output.queries }}
    agent:
      system: |
        You are a rigorous researcher. Work the queries one by one · fetch
        sources · keep verbatim quotes · note what each source actually
        supports, and what it does not. If a fetch is refused, record that the
        source was out of bounds rather than substituting another.
        Call nika:done when the plan is exhausted.
      prompt: "Research plan · ${{ with.queries }}"
      tools:
        # Every id here must also appear in `permits.tools` above — `check`
        # refuses the mismatch statically (NIKA-SEC-004 · agent tool … is
        # outside permits.tools). Note what is NOT granted: `nika:write`. The
        # agent's product is the typed object it returns, so it never needs a
        # file of its own, and the workflow keeps the only pen in the room.
        - "nika:fetch"                 # the corpus named in permits.net.http
        - "nika:done"                  # the loop-completion sentinel
      max_turns: 25                    # the leash · a loop that cannot end is a bill
      max_tokens_total: 120000         # cumulative input + output across requests, not a context window
      schema:
        type: object
        additionalProperties: false
        required: [findings, sources]
        properties:
          findings:
            type: array
            items: { type: string }
          sources:
            type: array
            items: { type: string }

  brief:
    with:
      findings: ${{ tasks.investigate.output.findings }}
      sources: ${{ tasks.investigate.output.sources }}
    infer:
      # a reasoning model needs 16384 here, or a documented run.reasoning.effort: low
      max_tokens: 2000
      prompt: |
        Findings · ${{ with.findings }}
        Sources · ${{ with.sources }}
        Write the executive brief · what's true · what's contested · what it
        means · what to do. Two pages max. Anything the sources do not support
        goes in « contested », never in « true ».
      # No `thinking:` block on purpose: the seat law REFUSES a thinking
      # declaration on a model the catalog knows cannot reason (a dead
      # declaration — the wire drops the budget), and the local seat this
      # file runs on is exactly that. Seat `brief` on a reasoning-capable
      # model via its own `model:` — THAT is what licenses
      # `thinking: { enabled: true, budget_tokens: … }`.

  save:
    with:
      slug: ${{ tasks.slug.output }}
      brief: ${{ tasks.brief.output }}
    invoke:
      tool: "nika:write"
      args:
        path: "./research/${{ with.slug }}.md"
        content: "${{ with.brief }}"
        create_dirs: true

outputs:
  brief: ${{ tasks.brief.output }}
  sources:
    value: ${{ tasks.investigate.output.sources }}
    description: "Every source the agent actually used"
  slug:
    value: ${{ tasks.slug.output }}
    description: "The filename stem the brief landed under · ./research/<slug>.md"
```

## How it works

**1. Each stage has its own ceiling**

`plan` is a schema-typed call capped at 400 tokens, the agent is
bounded by turns and tokens, and `brief` is capped at 2,000 tokens. One
envelope model serves the three stages; a task can pin its own
`model:` when one stage deserves a different model.

**2. The agent has a leash**

`max_turns: 25` + `max_tokens_total: 120000` bound the loop;
`nika:done` lets it exit cleanly when the plan is exhausted. The
final message must match the schema: findings AND sources.

**3. Callable by other workflows**

The typed `outputs:` (`brief`, `sources`) make this a building
block: a parent workflow can consume the contract without parsing
prose.

## Constructs you just used

| Construct | Where | Reference |
| - | - | - |
| `infer.schema:` plan | `plan` | [The 4 verbs](https://docs.nika.sh/concepts/verbs.md) |
| agent budgets + `nika:done` | `investigate` | [The 4 verbs](https://docs.nika.sh/concepts/verbs.md) |
| derived filename slug (`nika:jq`) | `slug` | [Builtins](https://docs.nika.sh/reference/builtins.md) |
| typed `outputs:` | envelope tail | [YAML syntax](https://docs.nika.sh/reference/yaml-syntax.md) |

## Run it

> [!NOTE]
>
> **What it needs:** Nothing to rehearse: under `--model mock/echo` the whole file, agent loop included, runs on the mock model with zero keys and no model call. The mock replaces the model, not the network: the agent's `nika:fetch` stays granted for the hosts in `permits.net.http`. Real research needs a model that calls tools and network access to the hosts in `permits.net.http`.

**1. Rehearse it**

```bash theme={"system"}
nika try deep-research-brief
```

A mock model answers and nothing is written in your folder. Before it runs, `nika try` names the hosts a step may reach: `may reach: arxiv.org · export.arxiv.org · en.wikipedia.org · hn.algolia.com — the mock replaces the model, not the network`.

**2. Get your own copy**

Save the file above as `deep-research-brief.nika` in a new folder. It reads no sample file.

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

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

The run writes `research/<slug>.md`.

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

`--var topic="…"` picks the subject; the file derives a safe slug from it for the filename.

## Make it yours

* Force source-grounding harder: have `brief` cite `${{ tasks.investigate.output.sources }}` inline per claim.
* Competitive variant: seed `plan` with [Competitor radar](https://docs.nika.sh/examples/competitor-radar.md)'s digest instead of a bare topic.
* Weekly cadence: pair with your scheduler and diff this week's brief against last week's with `nika:json_diff`.

> [card] [Next · Incident war room](https://docs.nika.sh/examples/incident-war-room.md)
>
> Parallel evidence gathering, a settle-and-recheck loop, and a
> postmortem that refuses to claim recovery it can't prove.


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