Embedding zelk in Go
The Go library is the module github.com/barevoidco/zelk/core, package zelk.
It is the loop and nothing else: your program owns the process, the
storage, the isolation, the transport and the identity, and passes each to
zelk explicitly. The zelk command is a program built this way. Hosts
import the package under the name zelk:
zelk "github.com/barevoidco/zelk/core".
New here? The Go quickstart gets a first agent running, and How zelk works explains the model behind it. The guides below are in reading order; each ends with a link to the next.
By what you want to do
Section titled “By what you want to do”Run an agent
- One task, nothing wired: Zero-config Run.
- Build an agent from a provider, a workspace and options: Wiring an agent.
- Start work, wait for the result, follow it live, steer it: Sessions and executions.
Decide what it may do
- Answer approvals and questions, now or later: Approvals and questions.
- Allow, ask or refuse tool calls, and choose the modes a session may use: Permissions from Go.
- Confine the commands it runs: Sandboxing from Go.
- Bound turns, time and tokens: Budgets and limits.
- Define when a run is done: Goals and verification.
Keep it running
- Keep sessions on disk, resume after a restart, recover from a crash, list and delete them: Stores, leases and recovery.
- Run it inside an HTTP service or a worker: Running zelk in a service.
- Keep long sessions inside the model’s window: Compaction.
Extend it
- Pick a model API, or switch models: Providers and models.
- Give the model your own functions: Custom tools.
- Connect MCP servers, and run Go code at fixed points of the loop: MCP and hooks from Go.
- Skills and subagents: Skills and subagents.
See and check it
- Traces, metrics and logs: Observability.
- Test an agent without a network or a key: Testing your agent.
The reference for every exported name is generated from the source:
the zelk package and the component packages beside
it.
Advanced embedding
Section titled “Advanced embedding”Most hosts use the agent. zelk also keeps its parts usable on their own, so a host that must own more of the execution can. These are the entry points; the embedding contract lists every public package and who owns what.
Your own driver. The agent runs a session through a driver that
supervises the lease, adopts the log and dispatches effects. A host with its
own executor, such as one that runs effects in another process or fits them
into an existing job system, can use the same parts. driver.Ensure adopts a
log and initializes it only when it is empty:
ctx := context.Background()logs := mem.New()auth, err := driver.Acquire(ctx, &lease.Local{}, "job")if err != nil { panic(err)}defer func() { _ = auth.Release(context.Background()) }()model, err := replay.New(provider.Caps{}, replay.Text("Hello."))if err != nil { panic(err)}d, err := driver.New(logs, model)if err != nil { panic(err)}resolved := 0configure := func(context.Context) (event.Config, []event.Warning, error) { resolved++ return event.Config{Provider: history.FamilyOpenAICompat, ReducerVersion: loop.Version, Model: "m1", Cwd: "."}, nil, nil}for range 2 { if err := d.Ensure(ctx, auth, configure); err != nil { panic(err) }}fmt.Println("configurations resolved:", resolved)// Output: configurations resolved: 1The log’s record groups are protocol, not an implementation detail. Before
an append, loop.Records prepares the whole group, including the decisions
its payloads cause. Append the complete group, and dispatch effects only
after it is durable:
st := loop.New("session")records, err := loop.Records(st, time.Time{}, event.SessionStarted{Config: event.Config{ReducerVersion: loop.Version}})if err != nil { panic(err)}// Append this complete group atomically; fold only after the append succeeds.for _, ev := range records { fmt.Println(ev.Seq, ev.Kind())}fmt.Println("acknowledged sequence:", st.Seq())// Output:// 1 session_started// acknowledged sequence: 0examples/external-driver
in the repository is a complete executor written against the public API
only. It acquires the lease, folds the log, drives fenced attempts, and
recovers after its process is killed. Read the
durability contract before writing one.
Scheduling that you own. zelk starts no timer and no goroutine for
sessions that aren’t running. A host schedules a message or a cron expression
on a session through a catalog, and calls Agent.DispatchWakes from its own
tick, while it has the workspace open, to admit what is due. The agent offers
the scheduling tools to the model only with zelk.WithScheduling. Creating a
schedule starts nothing:
ctx := context.Background()logs := mem.New()leases, _ := lease.NewLocal()p, _ := replay.New(provider.Caps{}, replay.Reported("met", "Ready."))a, err := zelk.New(ctx, p, zelk.WithModel("example"), zelk.WithPermissionMode(permission.ModePlan), zelk.WithStore(logs), zelk.WithLease(leases), zelk.WithScheduling())if err != nil { panic(err)}defer a.Close(ctx)s, _ := a.Session(ctx, "conversation")x, err := s.Start(ctx, zelk.WithCommands(zelk.UserMessage("Say ready.")))if err != nil { panic(err)}if _, err := x.Wait(ctx); err != nil { panic(err)}catalog, err := zelk.NewCatalog(logs, leases)if err != nil { panic(err)}due := time.Date(2030, 1, 1, 3, 30, 0, 0, time.UTC)if err := catalog.ScheduleMessage(ctx, s.ID(), "inspect-worker", due, "Inspect the worker."); err != nil { panic(err)}jobs, err := s.ScheduledMessages(ctx)if err != nil { panic(err)}fmt.Println(jobs[0].ID, jobs[0].State)if err := catalog.CancelScheduledMessage(ctx, s.ID(), jobs[0].ID); err != nil { panic(err)}jobs, err = catalog.ScheduledMessages(ctx, s.ID())if err != nil { panic(err)}fmt.Println(jobs[0].State)// A host that keeps the workspace open calls a.DispatchWakes(ctx,// workspace) on its tick to admit due messages. Creating a schedule// starts nothing.// Output:// inspect-worker pending// cancelledObserver sources. A source is something outside the conversation that
the host watches, such as a price, a queue or a build. The host creates it
with zelk.CreateSource, and its observer records each occurrence with
Session.RecordOccurrence. Recording appends to the session’s inbox without
the lease and wakes the session, so the model reads it:
ctx := context.Background()p, _ := replay.New(provider.Caps{}, replay.Text("Observed."))a, err := zelk.New(ctx, p, zelk.WithModel("m"), zelk.WithPermissionMode(permission.ModeDefault), zelk.WithConversationalCompletion())if err != nil { panic(err)}defer func() { _ = a.Close(ctx) }()s, _ := a.Session(ctx, "page")x, err := s.Start(ctx, zelk.WithCommands(zelk.CreateSource(event.Source{ID: "price", Kind: "watch", Description: "Price changes"})))if err != nil { panic(err)}if _, err := x.Wait(ctx); err != nil { panic(err)}if err := s.RecordOccurrence(ctx, "price", event.OccurrencePayload{Text: "Price changed to 12."}); err != nil { panic(err)}// Recording pokes admission, which wakes the conversation.if x, ok := a.Execution("page"); ok { if _, err := x.Wait(ctx); err != nil { panic(err) }}x, ack, err := s.Send(ctx, zelk.EndSource("price"))if err != nil { panic(err)}if _, err := ack.Wait(ctx); err != nil { panic(err)}if _, err := x.Wait(ctx); err != nil { panic(err)}sources, err := s.Sources(ctx)if err != nil { panic(err)}fmt.Println(sources[0].State, sources[0].Cursor)// Output: ended 1An accepted occurrence means it is durable, not that the model has run.