# Providers and models

> Connect zelk to a model API, and switch models in a session.

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`](/reference/go/provider-openaicompat/) | Chat Completions: OpenAI, OpenRouter, vLLM, Ollama, LM Studio and others |
| [`provider/openai`](/reference/go/provider-openai/) | OpenAI's Responses API |
| [`provider/openai/chatgpt`](/reference/go/provider-openai-chatgpt/) | Responses on a ChatGPT plan, with its sign-in |
| [`provider/anthropic`](/reference/go/provider-anthropic/) | Anthropic's Messages API |
| [`provider/gemini`](/reference/go/provider-gemini/) | the Gemini API |
| [`provider/replay`](/reference/go/provider-replay/) | scripted turns, for tests |

Each takes a base URL and options for the key, the HTTP client and
timeouts:

```go
// core/provider/anthropic/example_test.go
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: anthropic
```

An OpenAI-compatible server is a base URL away; here a test server stands
in for one:

```go
// core/provider/openaicompat/example_test.go
// 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

`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:

```go
// core/example_test.go
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 100
```

To 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

A provider implements the small interface in
[`provider`](/reference/go/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.

## Next

[Custom tools](/go/tools/) gives the model functions of your own.
