Providers and models
A provider is the client for one model API. You build it, own its
lifetime and pass it to zelk.New; zelk never opens a connection you
didn’t give it.
| Package | API |
|---|---|
provider/openaicompat |
Chat Completions: OpenAI, OpenRouter, vLLM, Ollama, LM Studio and others |
provider/openai |
OpenAI’s Responses API |
provider/openai/chatgpt |
Responses on a ChatGPT plan, with its sign-in |
provider/anthropic |
Anthropic’s Messages API |
provider/gemini |
the Gemini API |
provider/replay |
scripted turns, for tests |
Each takes a base URL and options for the key, the HTTP client and timeouts:
c, err := anthropic.New("https://api.anthropic.com/v1", anthropic.WithAPIKey("host-supplied-key"))if err != nil { fmt.Println(err) return}defer c.Close()fmt.Println(c.Family())// Output: anthropicAn OpenAI-compatible server is a base URL away; here a test server stands in for one:
// A stand-in for a server such as Ollama or vLLM.srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "text/event-stream") fmt.Fprint(w, "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hello.\"},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n")}))defer srv.Close()
c, err := openaicompat.New(srv.URL + "/v1")if err != nil { panic(err)}stream, err := c.Stream(context.Background(), &provider.Request{ Model: "llama3", MaxOutputTokens: 1000, History: []history.Item{{Role: history.RoleUser, Status: history.StatusCompleted, Parts: []history.Part{{Kind: history.KindText, Text: "Hi"}}}},})if err != nil { panic(err)}for ev, err := range stream { if err != nil { panic(err) } if ev.Kind == provider.EventDone { fmt.Println(ev.Response.StopReason, ev.Response.Items[0].Parts[0].Text) }}// Output: end_turn Hello.Models
Section titled “Models”WithModel names the model sessions start with. A session records it, and
Agent.SetModel builds the command that switches it, with the new model’s
limits resolved:
p, err := replay.New(provider.Caps{MaxOutputTokens: 100})if err != nil { panic(err)}a, err := zelk.New(context.Background(), p, zelk.WithPermissionMode(permission.ModeBypass), zelk.WithModel("first"))if err != nil { panic(err)}// Resolve once, then pass this command to WithCommands or Execution.Send.cmd, err := a.SetModel(context.Background(), "second")if err != nil { panic(err)}fmt.Println(cmd.Kind, cmd.SetModel.Model, cmd.SetModel.Settings.MaxOutputTokens)// Output: set_model second 100To switch between providers too, WithProviderResolver returns the
provider for a family and model when a session asks. WithEffort sets how
hard reasoning models think; WithContextWindow declares a window for a
model whose provider doesn’t report one.
Your own provider
Section titled “Your own provider”A provider implements the small interface in
provider: stream a request, report its
capabilities. Its errors carry a class (transient, rate_limited,
context_overflow, auth, quota, …), so the driver retries what can be
retried and compacts on an overflow.
Custom tools gives the model functions of your own.