How zelk works
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
Section titled “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 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
Section titled “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.
host your program, or the zelk command
Owns the process and hands zelk each dependency as an option.
agent zelk.New
Holds the configuration its sessions share.
session one conversation, one log
1fold
loop.StepFolding the log says what comes next, and whether the work is done.
2ask
effectsAsking the model: which one, what fits in its window, and what it knows.
3record intent
effect_startedEach call is decided before its intent is recorded.
4do it
model · toolsDoing it: a model call, a command, a tool of your own.
5record result
effect_finishedEvery result is recorded, so a crash, a handover or a rewind starts from the log.
The loop and the host are separate
Section titled “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) 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).
Every session is a log
Section titled “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).
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) for sessions that outlive the
process. The zelk command keeps its sessions in files under
~/.local/state/zelk/sessions/. Durability and
recovery says what each guarantee covers.
One writer at a time
Section titled “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,
and zelk.New refuses the pair otherwise.
Reading this section
Section titled “Reading this section”The pages are grouped in the sidebar the way the map groups them:
- The loop and its log: The loop, Runs and completion, A session over time, Durability and recovery and Rewind, fork and checkpoints. Start here.
- Deciding and containing: Permissions, The auto-mode reviewer, Sandboxing and enforcement, Worktrees and isolation and Managed policy and credentials: what may run, what it can reach, and who sets the limits.
- The model and what it knows: Providers and models, Context and compaction, Memory and Skills.
- Extending and connecting: Extending zelk, MCP servers, Subagents, Hooks, Browser automation, Editors and ACP and Observability.
The contracts behind these pages are in Internals: the architecture says which package owns what, the durability contract what each guarantee covers, and loop states what a session can be doing at any moment.