Skip to content
Source on GitHub (opens in a new tab)

Every vendor has its own API for talking to a model. Anthropic’s, OpenAI’s and Google’s differ in how they name a system prompt, how a tool call is shaped and how a reply streams. zelk’s loop doesn’t want to know. It builds one plain request, says “call the model”, and gets back a response.

The thing it says that to is a provider: a small interface that a Go program implements and passes to zelk.New. Between the loop and a vendor sits an adapter, a provider written for one API. It translates the request into that API’s wire format and the answer back into zelk’s, and does nothing else. It never changes what the loop does, so one loop serves every model.

The scenes below play it. The loop and its history are on the left, the adapter in the middle, and the vendor’s API on the right. The box on the right shows what the model is actually sent, which is not always what the loop holds. The scenes are a call, a switch from one model to another, a model that takes no images, and a replay.

  • A call. Every call leaves as the same request. The adapter renders it in the vendor’s API, and the loop never learns which. Only the finished response is recorded; the stream is live only. The model is sent: user: fix the test; model: Let me run it.; tool: go test: 1 failed. The log gains: effect_started call_provider claude-sonnet-5-5; effect_finished end_turn, 1 item.
  • Switching model. A switch is recorded, and the history is not converted. Hooks decide first. The next request is rendered for the new family: its own items as recorded, any other family’s rebuilt, opaque reasoning left out. The model is sent: user: fix the test; model: The test expects 3, not 2.; user: now run the tests. The log gains: command_accepted set_model gpt-6.1-terra, openai, images, reasoning, 400k window; hook_decided PreModelSwitch: allow; hook_decided PostModelSwitch: allow; effect_started call_provider gpt-6.1-terra; effect_finished end_turn, 1 item; warning opaque_reasoning_dropped: opaque anthropic reasoning cannot be sent to openai.
  • A model without images. A missing capability is worked around, never guessed. Without image support the request carries a note where the image was, and new images are refused. The log keeps the image for a model that takes it. The model is sent: user: what is wrong here?; model: I’ll take a screenshot.; tool: [image image/png, 48213 bytes, not shown: this session's model doesn't take images]. The log gains: effect_started call_provider qwen3-coder; effect_finished end_turn, 1 item.
  • Replay. A replayed session calls no model. Everything a fold needs is in the log: the model, its capabilities, each response and each switch. Nothing leaves the machine. The model is sent: nothing, no provider is called. The log gains: session_started claude-sonnet-5-5, anthropic, images, reasoning, 200k window; command_accepted user_message “fix the test”; effect_started call_provider claude-sonnet-5-5; effect_finished end_turn, 1 item; command_accepted set_model gpt-6.1-terra, openai, images, reasoning, 400k window.

A provider has three methods:

  • Stream sends a request and returns its stream of events. Text and reasoning arrive as small pieces, and the stream ends with one done event holding the assembled response. Cancelling the context ends the call.
  • Capabilities says what a model supports: its context window, its output limit, whether it takes images, and a few more.
  • Family names the wire format of the raw payloads the provider puts on the items it returns.

The request is plain data: the model, the rendered system prompt, the tools in a fixed order so the prompt’s prefix stays cacheable, the history, the output limit, the reasoning effort, and a cache key that stays the same for every call of one conversation. A provider must not change it, since the loop may share the history with its own state.

The response is what the loop records: the items the model produced, why it stopped, and the tokens used. The stop reason is normalized, so end_turn, tool_use, max_tokens, refusal and pause_turn mean the same from any vendor. A reason zelk doesn’t know is kept as unknown, with the vendor’s own word beside it, and the loop never guesses.

Four adapters live under core/provider:

Package API Family
provider/anthropic Anthropic’s Messages API anthropic
provider/openai OpenAI’s Responses API openai
provider/openaicompat Chat Completions, which OpenAI, OpenRouter, vLLM, Ollama, LM Studio and many others speak openaicompat
provider/gemini The Gemini API gemini

provider/openai/chatgpt is a sign-in for the Responses adapter, to use a ChatGPT plan instead of an API key. provider/replay is a scripted provider for tests, which answers each call with the next turn it was given.

In the CLI, -provider picks one: openai-compat (the default), openai, anthropic, gemini, chatgpt, opencode, or a server on your machine, ollama, lmstudio or llamacpp. The terminal UI sets providers up and switches between them with /provider. In Go you build the provider yourself and own it. zelk never opens a connection you didn’t give it.

WithModel names the model new sessions start with. The choice is recorded in the session’s first record, session_started, with the family, the settings (the output limit and the reasoning effort) and the model’s capabilities as they were then. After that the session’s calls use the recorded model. Reopening a session later behaves as it did, whatever the agent’s options are by then, and a driver refuses a log whose family it has no provider for.

A model’s capabilities are never discovered over the network. The host declares them: WithModels on an adapter lists each model it offers with its own capabilities, and WithCapabilities gives the default for any other. The same declarations feed the model picker. In the CLI, -context-window declares a window for a model whose provider reports none, as OpenAI’s API and OpenCode don’t.

Agent.SetModel turns a switch into a command. zelk resolves the new model’s family, settings and capabilities and puts them in the command, and the command is recorded when it is accepted. That is why replay never asks the provider anything. The terminal UI’s /model does the same. A switch is refused up front if the session has a hard token budget and the new model has no known window or output limit, since the budget could no longer be bounded.

Accepting is not applying. The switch waits until no model call or tool call is open, and a switch to another family waits until the run ends. Then, with hooks:

  1. A PreModelSwitch hook decides first. It can block the switch. If it does, the queued switch is dropped and the session records a model_switch_blocked warning. A Go hook that fails or times out blocks.
  2. The model changes. The configuration takes the new model, family, settings and capabilities.
  3. A PostModelSwitch hook runs, and may add context for the new model.

Each hook’s decision is recorded as hook_decided, and replay reads it back instead of running the hook again (Hooks).

What a switch doesn’t do is convert the history. The history keeps every item as it was recorded, each tagged with the family that produced it. The conversion happens when a request is built, by the adapter in use:

  • An item of the adapter’s own family goes back exactly as recorded, raw payload and all.
  • An item of another family is rebuilt from its normalized parts: its text, its tool calls and results, its images.
  • Reasoning that only its own family can read, such as a signed or encrypted block, has no form in another API. It is left out, and the next response carries an opaque_reasoning_dropped warning into the log. The log still holds it.

A switch also costs the prompt cache, since the next request has a new prefix, and measured token counts from the old model are discarded. A switch to a smaller model lowers the compaction threshold with it (Context and compaction).

A capability is a fact about a model, recorded in the log. What zelk does with each is narrower than the list suggests.

Images. This is the one zelk acts on. When the session’s model takes no images, each image in the request, whether in a message or in a tool result, becomes a text note giving its media type and size. The model is told that an image was there and wasn’t shown. Only the request changes. The image stays in the log, so a switch back to a model that takes images sends it again. A new image, from the user or from Read, is refused at once, before anything is recorded.

Context window and output limit. The window sets when a session compacts, and the output limit caps what a request asks for: a model’s limit wins over the agent’s default. A model with no known window gets none of that protection until the host declares one.

Reasoning and tool calling. The effort you set, low, medium or high, goes to the adapter, which renders it in its API’s terms. zelk doesn’t check a flag first. The capability snapshot has a reasoning flag and a parallel-tool-calls flag, and nothing in the loop acts on either. There is no tool-calling capability at all: zelk assumes the model can call tools. A model that can’t gets the vendor’s refusal, which comes back as an invalid_request or unsupported error and isn’t retried, so another model or provider is the fix.

A provider’s errors carry a class, so the driver acts on what a failure is:

  • Transient failures, such as a server error or a dropped connection, and rate limits are retried with backoff. By default a call gets ten attempts, waiting a second at first, doubling, with 20% jitter and at most 30 seconds a wait. A rate limit that says how long to wait is obeyed, unless it asks for more than ten minutes, and then the call fails. A stream that sends no bytes for five minutes counts as transient.
  • A malformed response, one that broke the vendor’s protocol, gets three attempts: enough to outlast one corrupted stream, not enough to pay ten times for a decoder at odds with its vendor.
  • A context overflow, a prompt too long for the model, makes the loop compact and try again.
  • An invalid request, a failed authentication, a spent quota and an unsupported capability are never retried. Each is its own class so a host can say which recovery applies: new credentials, more credit, or another model.

Each retry is recorded as an attempt_ended and a provider_retry warning. A model call has no side effects, so one lost to a crash is simply made again: the next process records the lost attempt and starts a new one.

Before every attempt the driver reserves a conservative maximum of tokens and records it with the call’s effect_started. The adapter bounds it from the request it actually renders when it can, and otherwise reserves the whole context window. That is what lets a hard token budget refuse a call before it is paid for (Durability and recovery).

A session’s log records what the model said, not how to ask it again. The call’s intent is effect_started. Its answer is effect_finished, which holds the items the model produced, with their raw payloads, the stop reason and the usage. The deltas that streamed in between are live only and are never recorded.

Replay folds a log with effects suppressed. It never dispatches a call, reads no clock and calls no port, so it never calls a provider. The model, its family and its capabilities come from session_started. Each response comes from its effect_finished, and each switch from its recorded command. The folded session is what it was, and nothing left the machine.

The provider/replay package is not this. It is a scripted provider for testing code that calls a provider, and it never reads a log.