Keg docs

Keg's assistant

Keg's assistant answers a person's questions about their node in words, from what it reads there, and proposes changes that a person confirms. It is in the console (the command box, and the Command page, #/command) and in the Mac app (Command, and Needs you).

It is off until a person sets it up. Nothing of it runs on a node where keg assistant setup was never run.

Setting it up

keg assistant setup --env-file ~/secrets/.env   # reads OPENAI_API_KEY (and ANTHROPIC_API_KEY) and nothing else
keg assistant setup                             # or: asks for each key here, unseen
keg assistant keys                              # which keys the vault holds for the gateway, never their values
keg assistant model                             # the models offered, their prices, and the one asked by default
keg assistant model <name>                      # sets it

Setup is a person's, on their own terminal, and it is the one way any node gets a model key: the owner's Mac, a Linux server and a stranger's laptop alike. It reads the .env once, prints the names it found and never a value, and puts each key in the vault (docs/VAULT.md) as the entry keg/model/<provider> (keg/model/openai, keg/model/anthropic), given to Keg's model gateway alone. The gateway is part of the node: there is nothing to install or start for it. Setup starts the assistant and makes one call to check it. The file is never read again and may be deleted. No part of Keg reads a project's .env by itself: not the assistant, not a piece of work (below), not an app's model binding. Each reaches models through the gateway, holds no key, and on a node with no key says the one command above.

keg assistant keys is a view of those entries: which is set, the provider it serves, and when the gateway last used it. keg vault ls lists them too. A key is replaced by running setup again (or keg vault set keg/model/openai) and removed with keg vault rm keg/model/openai. Nothing but Keg's own gateway may be granted an entry under keg/.

A node upgraded from 0.12 carries the keys it had into the vault at its first start, with nothing typed again.

OpenAI (its Responses API) is built in. Any other provider is one a person adds: see below. ANTHROPIC_API_KEY, which setup puts in the vault as keg/model/anthropic, is the key of a provider named anthropic once a person adds it (below).

Using another provider

A provider is a name, the address of its API, the shape it speaks, and its key (or none, for a server of your own). The gateway speaks openai-compatible (the Chat Completions shape, streamed with tool calls: what OpenRouter, Azure OpenAI, Together, Groq, vLLM, LM Studio and Ollama's OpenAI-compatible endpoint speak; also named openai-chat), openai (OpenAI's Responses API; also openai-responses) and anthropic (Anthropic's Messages API, its key in x-api-key). What each provider said it read from its cache is counted at the model's cached price.

keg assistant setup                                  # asks: OpenAI, or another provider, its model and price, then tests it
keg assistant provider add NAME --url URL [--kind openai-compatible|openai|anthropic] [--key-from-env VAR | --key-from-env-file PATH | --no-key]
keg assistant provider ls                            # each, its key set or not, what it was allowed
keg assistant provider default NAME                  # its first model is the one asked when a call names none
keg assistant provider test NAME [--model M]         # one small call, as you, said plainly
keg assistant provider rm NAME                       # its models, its key and what it was allowed go with it
keg assistant model add PROVIDER/MODEL --input-usd-per-mtok 0.15 --output-usd-per-mtok 0.60
keg assistant model add ollama/llama3.2 --free       # a model of your own: it costs nothing
keg assistant model ls | rm PROVIDER/MODEL
keg assistant model PROVIDER/MODEL                   # the default; --works for pieces of work

Everything downstream is unchanged: the assistant, a piece of work's agent through its door (Chat Completions, whatever the provider speaks), and the ledger, each through the gateway.

Setup reads another provider from a .env too, once: KEG_MODEL_URL, KEG_MODEL_KEY, KEG_MODEL_NAME (the model), and optionally KEG_MODEL_KIND, KEG_MODEL_PROVIDER (its name; the host's otherwise), KEG_MODEL_INPUT_USD_PER_MTOK, KEG_MODEL_OUTPUT_USD_PER_MTOK and KEG_MODEL_FREE. With --allow-private or --allow-plain-http when its address needs them.

OpenRouter

keg assistant provider add openrouter --url https://openrouter.ai/api/v1 --header HTTP-Referer=https://example.com --header X-Title=Keg
keg assistant model add openrouter/meta-llama/llama-4-scout --input-usd-per-mtok 0.15 --output-usd-per-mtok 0.60
keg assistant model openrouter/meta-llama/llama-4-scout
keg assistant provider test openrouter

Use the model's price as OpenRouter's page gives it, or more.

Anthropic

keg assistant provider add anthropic --kind anthropic --url https://api.anthropic.com/v1
keg assistant model add anthropic/MODEL --input-usd-per-mtok N --output-usd-per-mtok N

The key typed is the vault's keg/model/anthropic, the same entry setup writes from ANTHROPIC_API_KEY. Use the model's name and prices as Anthropic's pages give them.

Azure OpenAI

Azure's v1 API takes the deployment's name as the model, and its key in the api-key header:

keg assistant provider add azure --url https://RESOURCE.openai.azure.com/openai/v1 --key-header api-key
keg assistant model add azure/DEPLOYMENT --input-usd-per-mtok 2.50 --output-usd-per-mtok 10

An address with ?api-version= is not taken: use the v1 address.

Ollama on a Mac

Keg's node runs in a VM on a Mac; the Mac itself is reached from it as mac.keg.internal, which the node finds as its VM's way out (the address macOS's network for VMs gives the Mac, often 192.168.64.1). Ollama listens on the Mac's loopback alone unless told otherwise, so start it on every address of the Mac first, and note that anything on the networks the Mac is on can then reach it too:

OLLAMA_HOST=0.0.0.0:11434 ollama serve
keg assistant provider add ollama --url http://mac.keg.internal:11434/v1 --no-key --allow-private
keg assistant model add ollama/llama3.2 --free
keg assistant model ollama/llama3.2
keg assistant provider test ollama

If the Mac's address for its VMs changes, add the provider again.

Ollama on Linux

On a Linux node, name the computer Ollama is on by its address on your network, or the server itself by its loopback (the gateway is part of the node, so on a Linux server 127.0.0.1 is that server):

keg assistant provider add ollama --url http://127.0.0.1:11434/v1 --kind openai-compatible --no-key --allow-private
keg assistant model add ollama/llama3.2 --free

The gateway is part of the node

Since 0.15.0 the node answers model calls itself: it holds each call against its caller's allowance, reads the key of the call's provider from the vault for that one call, reaches the provider at the one address a person gave it, and settles what the provider counted. A node that ran an earlier Keg's models service stops and removes it at its first start, keeps its keys for the node, and says so once in its log, an event and keg assistant model. keg system install models says there is nothing to install.

Using a standard SDK, or an agent framework

An app's door (KEG_DOOR_URL, its token KEG_DOOR_TOKEN) and a work's door also answer in the shapes OpenAI's and Anthropic's SDKs speak, so an agent written with either, or with a framework on them, needs only its base URL and key changed:

Shape Path
OpenAI Chat Completions POST /v1/chat/completions
OpenAI Responses POST /v1/responses
Anthropic Messages POST /v1/messages
The models it may ask GET /v1/models
import OpenAI from "openai";
const openai = new OpenAI({ baseURL: `${process.env.KEG_DOOR_URL}/v1`, apiKey: process.env.KEG_DOOR_TOKEN });
const answer = await openai.chat.completions.create({ model: "gpt-5.6-luna", messages: [{ role: "user", content: "Hello" }] });
import anthropic, os
client = anthropic.Anthropic(base_url=os.environ["KEG_DOOR_URL"], api_key=os.environ["KEG_DOOR_TOKEN"])
answer = client.messages.create(model="claude-x", max_tokens=200, messages=[{"role": "user", "content": "Hello"}])

Pieces of work reach their model through the gateway

Once the gateway is set up, the agent of a piece of work (Pi, in the work's machine) asks its model through the gateway too, and is given no key: the project needs no .env, and the runner reads none.

keg assistant model --works openai/gpt-5.6-luna   # the model works ask (none set: the default)
keg assistant model                              # marks the default and the one works ask
keg assistant limits work:12 --usd-per-day 5     # one work's allowance; default:work for all

The gateway is the one thing on the node that holds a model's key and reaches a provider. It runs walled, with the public internet only, and besides it the one address and port of each provider a person allowed with --allow-private. The assistant runs walled with no network at all and nothing but its socket.

What it may spend

Every model call is held at its worst case against its caller's allowance and the node's before it is sent, and settled at what the provider counted. By default a day (UTC) allows:

Who Default
the assistant $5
each piece of work $2
each app environment $2
a person's own calls (as setup's check) $1
the node, in all $20
keg assistant limits                                   # every limit
keg assistant limits default:assistant --usd-per-day 10
keg assistant limits node --usd-per-day 50 --per-minute 30 --tokens-per-day 2000000
keg assistant limits service:assistant --resume        # after a call cost more than was held
keg assistant usage [--day YYYY-MM-DD] [--json]       # what each caller spent and has held

Input the provider read from its cache

A call is HELD, before it is sent, at the model's full input price: that is an upper bound and stays one. It is SETTLED at what the provider counted, and the input tokens the provider says it read from its cache are counted at the model's cached price, when it has one:

keg assistant model                                               # each model and its prices
keg assistant model openai/gpt-5.6-luna --cached-input-usd-per-mtok 0.02

A cached price is above nothing and at most the input price. A model with none counts cached input at the input price, as before. The models Keg ships with have OpenAI's published cached prices (standard tier, short context, 2026-11-25): gpt-5.6-luna $0.02, gpt-6-luna $0.01, gpt-5.6-terra $0.20, gpt-5.6-sol $0.40 a million tokens. A provider that says it read more from its cache than it was sent is believed for the input and no more. keg assistant usage says how many input tokens were read from the cache.

A node that kept its own table of models before cached prices were known (one upgraded from 0.13.0, where a person had set a model) has the shipped cached price for each model whose prices in and out are still the shipped ones. A model a person priced otherwise is left as it is, and keg assistant model says so under the list, with the command that sets its cached price. A cached price a person set is never replaced; to be charged in full for cached input, set it to the input price.

Not counted: a provider's prices for a context past 272K input tokens, and for writing to a cache it keeps 30 minutes or 12 hours.

Each organization of an app

A call an app's agent makes as a step of a thread carries a claim, signed by Keg's SQL in the app's database with a key the node put there for Keg's role alone, of which thread and organization it is for. The node checks it (403 if it is forged, stale, or contradicted by the call's own headers) and counts the call under that organization, always: keg assistant usage lists each organization under its app. An organization the app only names, with no claim, is counted under none.

keg assistant limits app:desk/local --per-organization-usd-per-day 1

holds EACH organization of that app's environment to that much a day. A call past it is refused (429 over_limit) in words the app can show that organization's administrator, which name no command of the node's; the app's other organizations are answered. keg assistant limits lists the ceiling beside the app's own, and keg assistant usage shows each organization's spend against it.

A call is held, before it is sent, for the most it may cost, and the hold counts toward the ceiling. So a ceiling below what the least call is held for would refuse every organization: it is refused when set, saying the least that lets a call through. One above that and below what a call is held for when it does not say how much it may answer (4,000 tokens of output) is taken, and keg assistant limits says under it that such calls are refused. A call outside any thread (keg.model()) carries no claim and is for no organization: it is held to the app's ceiling alone, and keg assistant usage shows such calls on a line of their own under the app whenever a ceiling for organizations is set. A call that names a thread and carries no claim is refused (403) while such a ceiling is set: the app's copy of the client predates claims, and keg app client brings it up to date. What an app's dry replays of its threads spent is said as evaluations.

Over a limit a call is refused (429 over_limit), saying which limit and when it resets. At most 16 calls run at once on a node, 4 kept for the assistant.

What it reads

What a person reads, through 28 tools whose requests the node builds itself from the tool's arguments: containers (state, ports, how each is made, the last lines each printed), volumes, images, networks, machines, builds, events and deliveries, pieces of work with their evidence and diffs, the services of an environment, Keg's own services, apps and their jobs, environments, change sets, backing services, what waits for a person, what the node uses, and what takes space on its disk. It can open the console's pages.

What it may change, and how

The assistant changes nothing by itself. It may propose 16 kinds of change: start, stop, restart, pause, resume or remove a container; start or stop a machine; retry or skip a delivery; fetch or remove an image; remove a volume; take a Compose project down; promote a release to an environment, or roll one back.

A proposal is a card in the node's own words. It names each thing by the node's name for it, says whether the change can be undone, what the person asked, and, in a box of its own, what a program wrote that the assistant read first ("A program wrote that, and it may be wrong or written to mislead."). Every value on it is one line, its line breaks and invisible characters shown escaped. The model's own words never reach a card.

The rules:

What it can never do

Whatever it reads or is asked: approve or reject a gate or a change set; install, replace or remove a service, or change what one is allowed; read or set a secret; set a model's key, limits or allowances; change what this computer reaches; run a cleanup or change what is kept; share or expose anything; begin a piece of work or hand it files; confirm or decline a proposal, its own or any. None of these is a tool, even as a proposal; a test fails if one becomes one. An agent that works on the node (Claude Code, Codex) is refused keg assistant altogether.

Who can see it

Anyone with the console's link can read the conversations and confirm the changes the assistant proposes. The link holds the console's token: treat it as the node's key. The console and the Command page say so.

Conversations are kept on the node: at most 200, for 30 days, and 256 MiB of steps, the oldest going first; a person can delete one. A run is held to 200 steps, 50 tool calls and 30 minutes.

For apps that read keg.my_events

The assistant does not read an app's own events. An app that does should read pages of them with @keg/app's db.events({ type, about, limit, before }) rather than SQL of its own over keg.my_events with ORDER BY … LIMIT: db.events() reads every page, the first or one far back, from the feed's index whatever Postgres's statistics say, where a query of one's own may be planned as a scan of the whole table.

Keg's documentation. Something wrong or missing? Say so.