docs · start
Concepts#
Hekma treats each AI agent you run as a service: it starts and stops the agent's process, watches it, counts the tokens it spends, and acts when a budget is crossed. This page defines the handful of words the rest of the docs use. Read the first diagram and the bold terms to get the whole model in two minutes; each section links to the page that goes deeper.Diagram source (Mermaid)
flowchart TD
You["You: hekma CLI<br/>or your own program"] --> Engine["Hekma engine"]
Engine --> Adapter["Adapter<br/>(built-in kind or adapter.toml)"]
Engine --> State[("State directory:<br/>fleet, usage ledger, Agent Homes")]
Adapter --> Agent["Agent process<br/>(OpenCode, Hermes, your script)"]
Agent -->|model calls| Provider["Model provider"]
Agent -.->|usage| Engine
You register an agent once. Hekma stores its settings and gives it a private folder. From then on, any hekma command, or a program that embeds Hekma's engine, can start it, stop it, read its usage and enforce its budget. The engine does that work only while some process holds it open, which is the most important limit to understand in Hekma 0.11.0 (see supervision). On Hekma's main branch, a Service Host holds it open for the CLI (Unreleased: after 0.11.0).
Agents and adapters#
Agent#
An agent is a program you already run that calls a language model on your behalf: a coding agent such as OpenCode, a personal agent such as Hermes Agent, or a script of your own. Hekma does not help you write agents. It runs them as operating-system processes and governs them from the outside.
Each registered agent has a unique name (lowercase letters, digits, - and _, starting with a letter or digit). The docs and the CLI also call a registered agent an Agent Instance; it is the same thing. You can register the same program several times under different names, each with its own settings, budget and history.
Adapter#
An adapter tells Hekma how to run one kind of agent: the command that launches it, what it supports on each operating system, and where its token counts come from. There are two sorts:
| Adapter | How you register | Use it for |
|---|---|---|
| Built-in kind | --kind acp, --kind hermes, --kind mock | Agents Hekma knows natively. acp runs any Agent Client Protocol agent. |
| Manifest | --manifest /path/to/my-agent | Any other program. You describe it in an adapter.toml. |
Adapters follow a versioned adapter contract, currently 1.0.0; Hekma main is at 1.1.0, which adds optional readiness, liveness, exit-code, pause-command and drain sections to the manifest (Unreleased: after 0.11.0). A manifest names the contract version it was written for, and Hekma refuses a manifest whose major version it does not support.
Capability declaration#
Not every agent can do everything on every operating system. Pausing a process, for example, is reliable on macOS and Linux and only cooperative on Windows. Each adapter carries a capability declaration that rates every capability per OS as guaranteed, best-effort or unsupported. Hekma acts on it: a guaranteed pause suspends the process, a best-effort pause prints a note, and an unsupported one fails before anything happens. hekma agent show NAME prints the declaration for your OS.
Agent Home#
Every agent gets its own folder, its Agent Home, inside the state directory. It holds the agent's settings, logs, rendered config files and, if you attach one, its memory folder. The agent's process starts with the Agent Home as its working directory. The Agent Home separates agents' files from each other; it is not a security sandbox, so an agent can still read anything your user account can.
Fleet#
The fleet is every agent registered in one state directory. hekma agent list shows the fleet as a table, and hekma agent usage without a name adds up the fleet's token totals.
Running agents#
Lifecycle#
Every agent is always in exactly one state, and moves between states only through a fixed set of transitions:Diagram source (Mermaid)
flowchart TD
registered -->|start| starting
starting --> running
starting --> failed
running -->|pause| paused
paused -->|resume| running
running -->|stop| stopping
paused -->|stop| stopping
stopping --> stopped
running -->|crash| failed
stopped -->|start| starting
failed -->|"start, or restart policy"| starting
| State | Meaning |
|---|---|
registered | Known to Hekma, never started. |
starting | The process is being launched. |
running | The process is up. |
paused | The process is suspended (or asked to idle, on Windows). |
stopping | Hekma asked the process to exit and is waiting. |
stopped | The process exited after a stop. |
failed | The process crashed, failed to launch, or was gone when Hekma looked for it. The reason is recorded. |
You drive the arrows marked with commands (start, stop, pause, resume). Hekma drives the others when it sees the process start, exit or crash. A command that does not fit the current state, such as pausing a stopped agent, fails with exit code 4 and changes nothing.
Run#
A run is one life of an agent's process, from a start until it stops or crashes. Per-run budgets and "this run" usage numbers reset when a new run begins. In 0.11.0 a restart after a crash starts a new run; a detached agent stays in one run across all the commands that look at it.
On Hekma's main branch (Unreleased: after 0.11.0) a Run is a record of its own, the unit Hekma's newer verbs act on. Every supervised start creates one, with a run id such as run_01M76G4PAEY4VYBV7WPD99QCX9 (run_ and a ULID, so ids sort by creation time; older run-… ids stay valid), a Control Depth, a Run Status, Anomalies and labels. A restart policy relaunch continues the same Run, so per-run budgets keep counting across a crash; only start opens a new Run. hekma run show RUN_ID prints one. Runs for workloads Hekma does not launch are planned. See Runs, Control Depth, ps and top.
Control Depth#
The Control Depth (Unreleased: after 0.11.0) says how far Hekma controls a Run: supervised when Hekma owns the process, or supervised (no wire) when the agent's model calls do not pass through Hekma's metering listener (a self-reported agent). A child Run inside a supervised agent is announced. The planned depths wire and observed are for Runs Hekma did not start; Runs has the whole ladder and which verbs work at each depth.
Run Status#
A Run's Run Status (Unreleased: after 0.11.0) follows its agent's lifecycle: active while the agent is starting, running or stopping, held while it is paused, and ended once it stops. An ended Run records why it ended, such as stop-graceful or, for a child Run, closed.
Anomaly#
An Anomaly (Unreleased: after 0.11.0) is a condition Hekma makes loud on a Run without acting on it. failed marks a Run whose agent crashed and awaits its relaunch; it clears when the relaunch is running, and stays if the restart policy gives up. breach marks a Run whose token budget or dollar cap was crossed, whatever the breach action. Two more come from Policies: depth-fallback marks a Run whose Policy asked for a breach action its Control Depth cannot carry out, so it acts as warn, and unpriced-model marks a Run with a Cost Cap and no Rate for its model. One comes from Run Keys: credential-reuse marks a Run whose refresh credential was presented a second time, and hekma run revoke-keys clears it.
Child Run#
A child Run (Unreleased: after 0.11.0) is a Run a Host opens under an agent's Run for one unit of its own work, with hekma run open --parent NAME (or the Control API or the engine). Its usage counts on itself and every ancestor, every ancestor's ceilings apply to it, and its own ceilings take the warn breach action. Pausing or stopping the agent holds or ends its child Runs. Runs nest at most 8 levels. See Child Runs.
Labels#
Labels (Unreleased: after 0.11.0) are key=value pairs on a Run that record where they came from. Bound labels come from you: an agent's labels.<key> config key (origin config) or hekma label set (origin label-verb, which wins per key). Caller labels come from the agent or Host itself, such as the --label given to hekma run open. A caller cannot set a key that is already bound, so a workload cannot relabel itself out of the limits you set. Labels belong to the Run: a relaunch keeps them, and a new start opens a Run without the ones set with the verb. See Labels from configuration and Bound labels.
Selector#
A Selector (Unreleased: after 0.11.0) is a list of key=value pairs, such as team=core,env=prod, that matches every open Run carrying all those labels, bound or caller. hekma label, hekma ps, hekma budget and the lifecycle verbs (stop, pause, resume, drain and run close) take one; when it matches more than one Run, the set is listed and you confirm it (--yes skips the question, --dry-run only shows it). A Policy names its Runs with one, matching bound labels only. Selectors on the wire verbs are in development.
Restart policy#
When a supervised process exits on its own, Hekma records a crash and applies the agent's restart policy. The default, on-failure, starts it again after 1 second, then after 2, 4 and 8 seconds, and gives up at the fifth crash in a row, leaving the agent failed. The count goes back to 0 only when you start the agent yourself. The other policy, never, leaves a crashed agent failed. The policy can be changed through the embedding API; the CLI shows it in hekma agent show.
On Hekma's main branch (Unreleased: after 0.11.0), a crash after at least 30 seconds of continuous running also resets the count first, so it counts as the first failure, and an adapter can declare its own exit codes: a restart_requested code is relaunched without counting as a failure, and a clean code stops the agent as if you had stopped it. The Control API can set the policy (PUT /v0/instances/{name}/restart-policy).
Engine#
The engine is the part of Hekma that does the work: it keeps the fleet's records, launches and watches processes, meters usage and enforces budgets. It is a Rust library, hekma-engine. The hekma command opens the engine for the length of one command; your own program can embed it and keep it open as long as it likes.
Service Host#
The Service Host (Unreleased: after 0.11.0) is the one process that owns a state directory's data on Hekma's main branch: hekma serve, a long-running engine. Every hekma agent command that writes (register, remove, start, stop, pause, resume, drain, send, config set, memory attach and memory detach) is one call to it, over a loopback endpoint the host records in the state directory's control.json. When no host serves the directory, the first write starts one in the background (hekma serve --autostart, logging to serve.log), which leaves on its own once no agent is live and no command has reached it for 30 seconds. Commands that only read (list, show, usage, logs, config get) open the database read-only and never start a host. See How the CLI reaches the Service Host and Run Hekma as a service.
Supervision#
Supervision is the engine watching an agent: noticing crashes and restarting it, counting its tokens as they are reported, and acting on budget breaches. It happens only while a process holds the engine open.
In Hekma 0.11.0 that means:
- Each
hekmacommand supervises for as long as it runs. Most commands finish in a fraction of a second;hekma agent logs NAME --followkeeps running until you stop it, so it keeps supervision going. - A program that embeds the engine supervises for as long as it keeps the engine open.
- Between commands, nothing supervises a detached agent: no crash detection, no budget enforcement, and usage the agent reports in that time is not counted later.
hekma agent list and show have a Supervision column that reads detached, session, or — when no process is recorded. Supervision and detached runs explains the patterns that work in 0.11.0.
On Hekma's main branch (Unreleased: after 0.11.0), the Service Host holds the engine for the CLI, so an agent you start stays supervised, metered and held to its budget after the command and its terminal exit, until you stop it or the host shuts down.
Detached#
By default, an agent started by hekma agent start belongs to that command and stops when the command exits. hekma agent start NAME --detach leaves it running after the command exits. The next hekma command finds it again by its process id and start time, which Hekma calls adoption, and supervises it for as long as that command runs. A detached agent's stdin is closed, so you cannot send it input from the CLI.
On Hekma's main branch (Unreleased: after 0.11.0) a plain start already outlives the command, because the agent runs under the Service Host. --detach then means the agent also survives the host's exit, to be adopted by the next host; between hosts it has no crash detection and no budget enforcement. An engine-observed agent still cannot be detached.
Usage and money#
Metering source#
Every adapter declares where its token counts come from, its metering source:
| Source | How it works | Trust |
|---|---|---|
self-reported | The agent, or a small wrapper, prints one KTESIO_USAGE {...} line per model call. | You trust the agent to report. |
engine-observed | The agent sends its model traffic through a proxy the engine runs on 127.0.0.1, and the engine reads the token counts from the provider's responses. | The counts come off the wire, so the agent cannot under-report. |
Meter token usage shows how to set up each one.
Usage ledger#
The usage ledger is Hekma's durable record of every usage event: input, output and cached tokens, per agent and per run, with replays removed. Every token number Hekma shows, and every budget decision it makes, comes from the ledger.
Budget#
A budget is a ceiling on an agent's tokens or estimated dollars, either per run or cumulative over the agent's life. Budgets are ordinary config values such as budget.tokens.cumulative. Hekma checks them in the same step that writes new usage to the ledger, so the breach is acted on at the usage event that crossed the line. Budgets and cost covers them in full.
Breach action#
The breach action decides what happens when any budget is crossed: pause (the default in 0.11.0) suspends the agent, stop stops it, and warn only records the breach. block (Unreleased: after 0.11.0) changes no state either: it leaves the agent running and refuses its further model calls at the metering listener — the wire fence, for engine-observed agents only. A breach is acted on once per run. On main (Unreleased: after 0.11.0), changing the breach action after a breach fires it once more under the new action.
Unreleased: after 0.11.0. On main, warn is the default: a breach is recorded, marks the Run with the breach Anomaly and is published, and nothing is stopped. pause, stop and block are opt-in, and an agent that relied on the 0.11.0 default keeps pause. A Policy can set a breach action for every Run a Selector matches, and hekma budget set sets one on a single Run. See Policies and the warn default.
Rate#
A rate is a price per million tokens, set separately for input, output and cached tokens (cost.rate.input and so on). With rates set, Hekma derives an estimated dollar cost for every usage event and can enforce dollar budgets. Without a rate, dollar budgets do nothing. Costs are kept as whole micro-dollars and always shown as estimates, because only your provider's invoice is authoritative.
Settings and state#
Config layers#
Each agent's settings are key-value pairs such as model or budget.breach_action. They resolve from layers: the adapter kind's defaults, then the engine level, then the agent's own instance layer, where hekma agent config set writes. The most specific layer wins, and hekma agent config get shows each value with the layer it came from. Keys Hekma does not know must start with agent., and are passed to the agent through its adapter's config mapping. See Config keys.
Secrets#
A config value of the form secret:NAME is a secret reference. Hekma stores only the reference. When the agent starts, it resolves NAME from the environment, then from secrets.toml in the state directory, and hands the real value to the agent, while every Hekma output (tables, JSON, logs, events) keeps it masked. hekma agent config get NAME --reveal is the only way to see it. See Config, secrets and memory.
Memory backing#
A memory backing gives an agent durable memory that Hekma looks after. A filesystem backing is a memory/ folder inside the Agent Home that Hekma creates, keeps across restarts and never touches, and passes to the agent at each start. A native backing records that the agent manages its own memory. You attach or detach one while the agent is stopped.
State directory#
Everything Hekma knows lives in one state directory: a SQLite database (state.db) with the fleet and the usage ledger, an optional secrets.toml, and one Agent Home per agent under agents/. On Hekma's main branch (Unreleased: after 0.11.0), state.db also holds the Runs, the lifecycle transitions, the breaches and the export outbox, and a running Service Host adds control.json, host.lock and serve.log. Its first open migrates state.db one way, so an older Hekma refuses the directory afterwards.
| OS | Default location |
|---|---|
| macOS | ~/Library/Application Support/ktesio |
| Linux | ~/.local/share/ktesio (or $XDG_DATA_HOME/ktesio) |
| Windows | %APPDATA%\ktesio\data |
The folder keeps the name ktesio from before the product was renamed, so existing installs keep their data. Set HEKMA_STATE_DIR to an absolute path to use another folder, for example one per project or a throwaway one for experiments.Diagram source (Mermaid)
flowchart TD
Root["state directory"] --> DB[("state.db<br/>fleet + usage ledger")]
Root --> Secrets["secrets.toml<br/>(optional, owner-only)"]
Root --> Agents["agents/"]
Agents --> Home["NAME/ (the Agent Home)"]
Home --> Conf["config.toml<br/>effective-config.json"]
Home --> Logs["logs/<br/>output.log, agent.log,<br/>instance.log, breaches.log"]
Home --> Mem["memory/<br/>(if a filesystem backing is attached)"]
Read next#
- Quickstart: see all of this in five minutes with a real agent.
- Supervision and detached runs: how to keep agents running and governed.
- Architecture: how the engine implements these ideas.