# Print mode and exec

> Run one task from a script or CI with zelk -p, or a headless, resumable job with zelk exec.

zelk has two ways to run without the terminal UI. **Print mode** (`zelk -p`)
runs one task, prints the answer and exits; its session ends with the
process. **`zelk exec`** is the headless runner: its sessions are kept, so
you can resume them, and it is read-only unless you say otherwise.

Both take the provider and the model from flags and environment variables
(`-provider`, `-model`, `OPENAI_API_KEY` and its siblings); `config.json`
supplies run options such as the sandbox and permission rules, and rejects
the provider and the model. See [Credentials](/cli/credentials/) and
[Get zelk](/start/get/#give-it-a-model).

## Print mode

```sh
zelk -p "fix the failing test in ./pkg/foo"        # one task in this directory, then exit
zelk -p -plan "how would you split this package?"  # read-only: plans, changes nothing
zelk -p "fix the typo in @README.md"               # @path attaches a file
zelk -p "/init"                                    # /name runs a skill
echo "fix the test" | zelk                         # piped: the task, in print mode
```

`zelk` runs in print mode whenever standard input or output isn't a
terminal, and whenever you give a print-only flag (`-json`, `-o`,
`-output-schema`, `-result`, `-input`, `-progress`), so `-p` is needed
only on a terminal.

`zelk -p "task"` authorizes `bypass` mode on the working directory:
anything a rule doesn't deny runs, but protected paths such as `.git/`
still need a person, and with no one to ask they are refused. Use `-plan`
or `-mode` for less. The tool calls go to standard error as they happen,
and the answer to standard output.

## zelk exec

```sh
zelk exec "summarize the TODOs"                          # plan mode: read-only, ends with a plan
zelk exec -mode accept-edits "fix the typo in README.md"
git diff | zelk exec "review this diff"                  # piped input is context
```

`exec` runs in `plan` mode unless `-mode`, `-plan` or a saved default says
otherwise, and says so on standard error: a headless run has no one to
approve leaving plan mode. Piped standard input is context for the task, or
the task itself when none is given.

Sessions are kept in the state directory (`-state`, else
`$XDG_STATE_HOME/zelk`, else `~/.local/state/zelk`, or on Windows
`%LOCALAPPDATA%\zelk`), and each directory
remembers its last one:

```sh
zelk exec resume --last "now fix them"                 # continue this directory's last exec session
zelk exec -mode default resume --last "now fix them"   # flags go before resume
zelk exec resume --last --compact "the API decisions"  # compact it now, keeping what you name
zelk exec resume --last --fork "try the other approach"  # continue in a copy; the original stays
```

`--compact` runs a turn that only summarizes the conversation, with what
follows as the summary's focus. `--fork` copies the session and points the
directory at the copy. [Sessions](/cli/sessions/) covers listing,
renaming and inspecting them.

## On Windows

Print mode and `exec` work on Windows as they do elsewhere
([Get zelk](/start/get/#windows)); the [terminal UI](/cli/tui/) works there
too. The examples above run unchanged in PowerShell, with its quoting and
paths, and the shells zelk gives the model are PowerShell, the primary one,
and Git for Windows' Bash.

```powershell
zelk -p "fix the failing test in .\pkg\foo"
zelk -p -plan "how would you split this package?"
zelk exec -mode accept-edits "fix the typo in README.md"
git diff | zelk exec "review this diff"
zelk exec resume --last "now fix them"
```

Commands zelk prints for you to paste, such as the one that resumes a
session elsewhere, are written in PowerShell on Windows.

## Output

| Flag | Writes |
|---|---|
| (none) | the final message to standard output, progress to standard error |
| `-json` | every event as JSON Lines to standard output |
| `-progress` | with `-json`, also a `{"live":"progress"}` record while a model call or tool streams |
| `-o FILE` | the last message (or the structured result) to a file; a run that gave neither writes why it ended |
| `-output-schema FILE` | makes the run deliver a value that fits a JSON Schema, written to standard output |
| `-result FILE` | the outcome, usage, report, final text and any structured output, as JSON |

The schema is the contract of the run it's given to, not of the session:
a later `resume` or `--fork` of that session answers in text unless you
give it an `-output-schema` of its own.

An output schema must close every object with `"additionalProperties":
false`:

```json title="todo.schema.json"
{
  "type": "object",
  "properties": {"todos": {"type": "array", "items": {"type": "string"}}},
  "required": ["todos"],
  "additionalProperties": false
}
```

```sh
zelk exec -output-schema todo.schema.json -result result.json "list the TODOs in README.md"
```

```text
{"todos":["README.md:3: TODO: write docs"]}
```

## Approvals and questions

`-input` says who answers when the agent needs approval or asks a question:

- `deny`, the default: no one is asked, so the action is refused, or the
  question goes unanswered.
- `prompt`: zelk asks on this terminal, reading answers from standard input.
  It can't be combined with piped input.

```sh
zelk -mode default -input prompt "fix the test"   # asks here before anything not read-only
```

## Exit status

| Status | Means |
|---|---|
| 0 | the run succeeded |
| 1 | the run didn't succeed, or an error stopped it |
| 2 | a usage error |
| 3 | unverified: the run finished without sufficient evidence of success |
| 130 | interrupted |

The first Ctrl+C records an interrupt and exits 130; a second stops at once.

## In CI

Use `-mode plan -input deny` for unattended read-only jobs. Name the mode
explicitly, especially in print mode, which otherwise defaults to `bypass`.
Use `-max-turns` to cap work and treat every nonzero exit status, including
`3` (unverified), as a reason to withhold the result.

| Runner | Launcher | Default without a flag, saved setting or managed policy |
|---|---|---|
| Linux | `-sandbox bwrap` | Unattended `auto` selects bwrap. Other modes run unconfined. |
| macOS | `-sandbox seatbelt` | Commands run unconfined. |
| Windows | `-sandbox mxc` | Unattended `auto` selects MXC if setup is verified and the launcher can start. Otherwise it prints an unconfined notice. Other modes run unconfined. |

Choose the launcher explicitly in CI. `-network off` disables network access for
sandboxed commands and the web tools. The CLI still needs to reach its model
provider. [Managed policy](/cli/policy/) can require a working sandbox, limit
the modes and network access available to a run, and restrict tools.

Pass the model API key through the CI secret store to the model step. Keep
write credentials, including a review-posting token, in a separate trusted
step. Give the CLI a clean workspace and pipe untrusted changes as data. When
there is no input to pipe, close stdin so `exec` can finish reading it.

The [code review in CI example](/examples/ci-review/) shows a Linux workflow
that checks a PR diff and posts the resulting JSON as a review comment.

## Where to go deeper

- [`zelk exec` in the CLI reference](/reference/cli/#zelk-exec), every flag
- [Runs and completion](/concepts/runs/), what an outcome means
- [Permissions in practice](/cli/permissions/)
