# How zelk works

> The host, the agent and its sessions, the loop every session runs, and a map of where each idea in zelk happens.

zelk is a library that runs an agent loop, and the `zelk` command is a
program built on it. This page gives you the words zelk uses, shows how the
parts fit inside one another, and names the few ideas everything else rests
on. Each idea has its own page, and the map below shows where each one
happens.

## The parts, from the outside in

| Part | What it is | How long it lives | In the `zelk` command |
|---|---|---|---|
| **Host** | The program that embeds zelk. It owns the process and hands zelk each dependency. | As long as the program runs | The command itself |
| **Agent** | What [`zelk.New`](/reference/go/zelk/#new) builds: the configuration and resources its sessions share | Until the host closes it | One per run of the command |
| **Session** | One conversation, recorded as a log of events | As long as its log is kept | A conversation in the terminal UI; `zelk -c` reopens the last |
| **Execution** | One stretch of work on a session, until it has nothing left to do | From a message until the session is idle or waiting | What a message you send starts or joins |
| **Run** | One request inside an execution, ending with an outcome such as `success` | From its prompt to its outcome | One answer to one message |
| **Turn** | One pass round the loop: ask the model, then do what it asked | Seconds to minutes | One reply and the tool calls in it |

A session keeps the configuration it began with: its system prompt, tools,
permission policy and model are recorded in its log. Reopening it later
behaves as it did, whatever the agent's options are by then.

## The map

The parts nest. A host holds an agent, the agent holds its sessions, and
each session is a loop over its own log. Every page in this section sits at
one of the loop's five stations, where what it explains happens.


## The loop and the host are separate

The **loop** is what zelk owns. It sends the conversation to a model, runs
the tools the model asks for, decides each call against the permission
rules, keeps the conversation inside the model's context window, and
decides when a run has really finished.

The **host** owns everything else: the process, where logs are stored, how
commands are isolated, scheduling, transport and who the user is. It hands
each of these to zelk as an option, such as a provider, a store, a lease, a
workspace environment or an observer. zelk never builds a dependency behind
the host's back, never reads the environment (outside the
zero-configuration [`zelk.Run`](/reference/go/zelk/#run)) and never writes
to standard output.

The `zelk` command is one such host. It uses only the library's public API,
so anything it does, your program can do too. An editor that speaks the
Agent Client Protocol drives zelk through `zelk acp`, another way in to
the same host ([Editors and ACP](/concepts/acp/)).

## Every session is a log

zelk records a session as an append-only log of events: the user's
messages, each model reply, each tool call with its decision and result,
each change of mode or model. A pure function, the **reducer**, folds that
log into the session's state: its history, its pending work, its budget,
its task list. Nothing else holds state that matters. The reducer only
decides; a separate **driver** does what it asks, recording its intent
before it acts and the result after ([The loop](/concepts/loop/)).

That one choice gives zelk its guarantees:

- **A crash leaves a record.** What may have happened is in the log, so the
  next process settles it rather than guessing.
- **A write counts once it is on disk.** A torn tail after a crash is
  repaired; corruption is reported, never dropped.
- **Replay is exact.** Reading a log back reproduces the session without
  calling the model or reading the workspace again. Inspecting a session,
  resuming it elsewhere and rewinding it all start from a replay.

The store is the host's choice: an in-memory store by default, which keeps
nothing once the agent is gone, or a file store
([`store/file`](/reference/go/store-file/)) for sessions that outlive the
process. The `zelk` command keeps its sessions in files under
`~/.local/state/zelk/sessions/`. [Durability and
recovery](/concepts/durability/) says what each guarantee covers.

## One writer at a time

A **lease** gives one process at a time the right to append to a session.
It expires unless renewed, and every append carries the lease's fencing
token, so a process that lost its lease, paused perhaps, can't write over
the one that took over. That is what lets a session move between
processes: a server can pick up a session a crashed worker left, and recover
its pending work from the log. A store whose logs outlive the process needs
a lease whose fences do too, such as [`lease/file`](/reference/go/lease-file/),
and `zelk.New` refuses the pair otherwise.

## Reading this section

The pages are grouped in the sidebar the way the map groups them:

- **The loop and its log:** [The loop](/concepts/loop/), [Runs and
  completion](/concepts/runs/), [A session over time](/concepts/time/),
  [Durability and recovery](/concepts/durability/) and [Rewind, fork and
  checkpoints](/concepts/rewind/). Start here.
- **Deciding and containing:** [Permissions](/concepts/permissions/), [The
  auto-mode reviewer](/concepts/reviewer/), [Sandboxing and
  enforcement](/concepts/sandboxing/), [Worktrees and
  isolation](/concepts/worktrees/) and [Managed policy and
  credentials](/concepts/policy/): what may run, what it can reach, and who
  sets the limits.
- **The model and what it knows:** [Providers and
  models](/concepts/providers/), [Context and compaction](/concepts/context/),
  [Memory](/concepts/memory/) and [Skills](/concepts/skills/).
- **Extending and connecting:** [Extending zelk](/concepts/extending/),
  [MCP servers](/concepts/mcp/), [Subagents](/concepts/subagents/),
  [Hooks](/concepts/hooks/), [Browser automation](/concepts/browser/),
  [Editors and ACP](/concepts/acp/) and
  [Observability](/concepts/observability/).

The contracts behind these pages are in Internals: the
[architecture](/internals/architecture/) says which package owns what, the
[durability contract](/internals/durability/) what each guarantee covers,
and [loop states](/internals/loop-states/) what a session can be doing at
any moment.
