# Agent Drivers

A Driver decides how one Invocation (one call to an Agent) runs. Pick the Driver that fits the work:

| Choose | Use it when |
| --- | --- |
| Model-backed | ViteHub runs a model and the tools from your Capabilities (reusable tool and policy pieces). |
| Provider-backed | Codex or Claude Code runs its own loop, tools, approvals, and session. |
| Custom run | Your code does the work. |
| Ask | A fast decision model answers a fixed set of typed questions. |

`codexDriver()` and `claudeCodeDriver()` from `vite-hub/agent` create provider-backed Drivers. Other Drivers use exactly one of `{ model }`, `{ run }`, or `{ ask }`. For a full setup with a route, credentials, and deployment, read [Run the Claude Agent SDK in production](https://vitehub.dev/docs/guides/claude-agent-sdk) and [Run the Codex SDK in production](https://vitehub.dev/docs/guides/codex-sdk).

## Use a model-backed Driver

Use it for support answers, classification, extraction, structured output, and limited tool use.

```ts [server/agents/support.ts]
import { defineAgent } from 'vite-hub/agent'

export default defineAgent({
  driver: {
    model: 'openai/gpt-5.1-mini',
    instructions: 'Answer support requests from inspected evidence.',
    execution: {
      callSettings: { temperature: 0.2 },
      stepLimit: 8,
    },
  },
})
```

Model strings run through AI Gateway, with `AI_GATEWAY_API_KEY` from the process or the Cloudflare Server Env. To give the Agent its own key, pass `{ id, apiKey }`:

```ts [server/agents/support.ts]
import { defineAgent } from 'vite-hub/agent'

const apiKey = process.env.SUPPORT_AI_GATEWAY_API_KEY
if (!apiKey) throw new Error('SUPPORT_AI_GATEWAY_API_KEY is required')

export default defineAgent({
  driver: {
    model: { id: 'zai/glm-5v-turbo', apiKey },
  },
})
```

`model` can also be an AI SDK model or a callback that picks the model per Invocation. Use callbacks and instrumentation for routing and call settings. Put authorization in Access or a Capability policy.

### Model options

| Option | Purpose |
| --- | --- |
| `model` | Required model id, `{ id, apiKey }`, AI SDK model, or callback. With `gateway`, a model id. |
| `gateway` | `chatgpt()` runs the model on the [ChatGPT plan](https://vitehub.dev/docs/connections/chatgpt#use-a-model-backed-driver). For another gateway, set `model: languageModel(gateway, id)`. |
| `instructions` | String, string array, or callback parts. Defaults to the `instructions.md` file next to the Agent, if any. |
| `maxRetries` | Retry count for model calls. Do not also set `execution.callSettings.maxRetries`. |
| `execution.callSettings` | Provider and AI SDK call settings. |
| `execution.stepLimit` | Maximum tool-loop steps. Defaults to `20`. |
| `execution.instrumentation` | Wraps the model or overrides call settings per Invocation. |
| `execution.workspaceFallback` | Writes an answer from Workspace evidence when a run has tool results but no text. |

## Use a provider-backed Driver

To use a signed-in user's ChatGPT Plus or Pro plan, define a [ChatGPT Connection](https://vitehub.dev/docs/connections/chatgpt) and set `driver: codexDriver({ gateway: chatgpt() })`, with `chatgpt` from `vite-hub/gateways`. The CLI handles sign-in. Connections handles registration and refresh. This flow never falls back to an API key. Codex needs a POSIX Node.js host. A model-backed Driver uses the same grant on every host, including Cloudflare Workers: `driver: { model: 'gpt-5.5', gateway: chatgpt() }`.

The built-in Drivers reuse T3 Code's Codex and Claude Code runtime. ViteHub owns Agents, Capabilities, Workspaces, Invocations, and lifecycle events. Install only the provider packages you use:

```sh
pnpm add @openai/codex@0.149.1
pnpm add @anthropic-ai/claude-agent-sdk@0.3.246
```

```ts [server/agents/review/agent.ts]
import { codexDriver, defineAgent } from 'vite-hub/agent'
import { loadServerEnv } from '#vitehub/env/server'

export default defineAgent({
  driver: codexDriver({
    credentialProfile: 'review',
    credentials: async ({ abortSignal }) => (await loadServerEnv(undefined, { signal: abortSignal })).codexAuthJson,
    instructions: 'Review the exact pull request head before changing code.',
    model: 'gpt-5.5',
    permissions: 'ask',
    reasoningEffort: 'high',
    reasoningSummary: 'detailed',
  }),
  workspace: { mode: 'write' },
})
```

### Give Codex its credentials

`credentials` accepts a Codex `auth.json` string, a sealed Server Env value, or a resolver that runs per Invocation. So a Kubernetes secret or an [Env provider](https://vitehub.dev/docs/env/server-api#read-external-env-storage) can supply auth without startup files. ViteHub checks that the value is a JSON object and writes it as a `0600` file in a `0700` Codex Home, the directory where Codex keeps auth, config, sessions, and logs.

> **Warning**

Written credentials need a POSIX host. ViteHub rejects them on Windows, where these file modes cannot guarantee owner-only access. The value never enters the provider environment or inspection output.

`credentialProfile` names one writable Home that Drivers in the process share, at `.vitehub/data/codex/<credentialProfile>`. Only one Codex process uses it at a time. Codex can refresh `auth.json` there. ViteHub fingerprints the last resolver value: it keeps the refreshed file while that value is unchanged, and replaces it on the next Invocation when the value rotates.

> **Warning**

The profile is a full Codex Home. Treat its auth, config, sessions, and logs as sensitive. Only one ViteHub process may write a profile volume, so in Kubernetes give each replica its own persistent volume. ViteHub does not write refreshed credentials back to the Env provider.

Without `credentialProfile`, each Invocation gets a temporary Home that ViteHub removes when Codex stops.

### Host requirements

Provider Drivers need a local Node.js host whose process has the credentials.

- ViteHub finds an installed `@openai/codex` or `@anthropic-ai/claude-agent-sdk` without copy scripts.
- Self-hosted Node builds on macOS and Linux copy only the build host's native optional package, including the Linux libc variant, into `.output/server/node_modules`. Build on the host type you deploy to.
- Without the Codex package, ViteHub uses `codex` from `PATH`.
- Claude needs the Agent SDK. If its native package is missing at runtime, T3 falls back to the host `claude` command.
- ViteHub never downloads provider packages at build or startup.
- Provider Workspaces need a POSIX host.

Each Invocation gets a temporary working directory, any Workspace files, Codex instructions or a Claude prompt file, and Capability tools through a private loopback MCP server. Only successful write-mode runs commit, through the Workspace rules.

### Resume threads after a restart

Provider cursors resume a thread while the process runs. Set `sessionStorePath` to keep them in a local SQLite file across restarts.

- With `credentials`, you also need `credentialProfile`, because ViteHub removes a private Codex Home after each run.
- A relative path resolves from the process working directory.
- Use one file per provider Agent on one persistent host. The store does not stop two workers from using one thread.
- Chat cursors stay separate by origin, invoker, and Chat Session, so a new session cannot inherit an old one's context.
- If the provider cannot resume, for example after its session files were deleted, ViteHub sends the full history to a new thread and records `agent.provider.session.resume-failed`.

### Set the environment and wrap the launch

Resolve `env` per Invocation for short-lived credentials. Use `launch` to start the provider through your own wrapper, such as a remote execution CLI:

```ts [server/agents/review/agent.ts] fragment
import { codexDriver, defineAgent } from 'vite-hub/agent'

export default defineAgent({
  driver: codexDriver({
    env: async ({ abortSignal }) => ({
      SANDBOX_TOKEN: await issueSandboxToken({ signal: abortSignal }),
    }),
    launch: ({ command }) => ({
      args: ['exec', '--', command],
      command: 'sandboxctl',
    }),
    permissions: 'allow-all',
  }),
})
```

ViteHub resolves `env` once, then calls `launch` with the environment, `command` (the executable to start), `providerCommand` (the original provider executable), the temporary working directory, and an abort signal. Build wrapper arguments from `command` and pick a provider runner from `providerCommand`. They are equal during Invocations. The wrapper gets its own arguments, then the provider arguments. ViteHub writes an owner-only temporary launcher outside the Workspace and removes it when the provider stops.

Launch wrappers need a POSIX host. They change only process startup. Your app owns authorization, remote isolation, and wrapper credentials. Agent inspection shows whether `env` and `launch` are static or dynamic, without resolving them.

### Check the commands the provider needs

Set `requirements` to the commands the provider's shell needs, such as `['git', 'gh', 'unzip', 'apply_patch']`. `agent.status()` checks them where the Driver runs, with `command -v`, or `where.exe` for a local Windows Driver. Missing commands make the status `unavailable` and appear in `status.missingCommands` and `status.reason`. Invocations do not repeat the check.

With `launch`, requirements need a resolver. Status inspection resolves it once with `command: 'sh'`, and each provider probe checks the commands in that shell before it starts the provider, so no extra runner starts. A probe can run the launcher more than once. If the launcher does not report the check results, readiness stays unknown.

A resolver that routes by executable must read `providerCommand`, because `command` is `sh` during the check:

```ts fragment
launch: ({ command, providerCommand }) => ({
  args: ['exec', '--', command],
  command: selectRunnerForProvider(providerCommand),
})
```

### Limit the Codex Home size

Codex keeps every session in its Home (rollout files, `logs_N.sqlite`, `thread_history_N.sqlite`, `state_N.sqlite`, shell snapshots, `.tmp` Git directories) and never removes them. Set `retention` to bound the Home:

```ts [server/agents/review/agent.ts]
import { codexDriver, defineAgent } from 'vite-hub/agent'

export default defineAgent({
  driver: codexDriver({
    retention: { maxAge: '14d', maxSize: '45%' },
  }),
})
```

- `maxAge` takes a duration such as `'14d'`. It applies to sessions, log rows, and shell snapshots.
- `maxRecords` keeps the newest sessions.
- `maxSize` takes bytes, a size such as `'2GB'`, or a percent of the Home's volume, such as `'45%'`. It counts only the Home's files, live SQLite pages, and WAL files.

Every limit you set applies. Retention removes, in order: `.tmp` entries older than 24 hours, data older than `maxAge`, the oldest sessions past `maxRecords`, then the oldest sessions and hourly log rows until the Home fits `maxSize`. A removed session also loses its thread history and state rows. ViteHub runs `PRAGMA incremental_vacuum` so the files shrink. It never runs a full `VACUUM` or removes a database file.

Retention never removes a thread running in this process or a file changed in the last 10 minutes. Sessions that a resume cursor still references, in memory or in `sessionStorePath`, and their fork parents, go last, only if a limit still fails. That conversation's next Invocation then sends the full history, as for any failed resume.

Retention runs in the background on the first Codex Invocation, then at most every 10 minutes, and never fails an Invocation. It logs one `[vitehub] Codex home retention` line when it removes data or the Home stays over budget. The lock file `<home>/.vitehub-retention.lock` keeps processes apart.

Retention needs a persistent Home: a `credentialProfile`, `providerSettings.homePath`, `CODEX_HOME` in `driver.env` or the process environment, or a gateway with one of these. Plain `credentials` without a profile use a temporary Home, so `defineAgent()` rejects `retention` with `AGENT_R1006`. ViteHub does not prune the implicit `~/.codex` and warns once. With `launch`, the app must reach the Home at the same path, for example on a shared volume, or ViteHub warns once and skips it. Agents that share a Home get the strictest value of each limit.

Inspect and prune a Home from the CLI:

```sh
vitehub agent codex-home status --home /var/lib/app/codex --max-size 45%
vitehub agent codex-home prune --home /var/lib/app/codex --max-age 14d --max-size 45% --session-store .vitehub/provider-sessions.sqlite --dry-run
```

`prune` uses the same rules and lock file. Without `--dry-run`, it removes data. `--json` prints the report.

### Route model requests through a gateway

Set `gateway` to send provider model requests through an LLM proxy. An [AI gateway](https://vitehub.dev/docs/agents/ai-gateways) preset from `vite-hub/gateways` reads its URL and key from the environment:

```ts [server/agents/review/agent.ts]
import { codexDriver, defineAgent } from 'vite-hub/agent'
import { cliproxy } from 'vite-hub/gateways'

export default defineAgent({
  driver: codexDriver({
    model: 'gpt-5.5',
    // Reads CLIPROXY_URL and CLIPROXY_API_KEY.
    gateway: cliproxy(),
  }),
})
```

[AI gateways](https://vitehub.dev/docs/agents/ai-gateways) lists the presets, their variables, and `defineAiGateway()` for other endpoints. A gateway without a base URL for the Driver fails at definition time with `AGENT_R0976`.

ViteHub maps the gateway to each provider:

- Codex gets a generated `vitehub` model provider through `-c` overrides: `base_url`, `wire_api = "responses"`, `stream_max_retries = 10`, `env_key`, and `env_http_headers`. A gateway can answer 200 and then relay an upstream error, which only Codex can retry. 10 retries back off for about 3.5 minutes, against 6 seconds for the default 5. The key and headers go in `VITEHUB_GATEWAY_API_KEY` and `VITEHUB_GATEWAY_HEADER_<n>`, and launch arguments hold only the names. These overrides come after `providerSettings.launchArgs`, so they win.
- Claude Code gets `ANTHROPIC_BASE_URL`, `ANTHROPIC_CUSTOM_HEADERS`, and the key in `ANTHROPIC_AUTH_TOKEN` (bearer) or `ANTHROPIC_API_KEY` (x-api-key), with the other one empty.

The gateway owns these variables. Setting one in `driver.env`, or `T3CODE_CODEX_LAUNCH_ARGS` for Codex, fails with `AGENT_R0979`. A gateway replaces `credentials` and `credentialProfile`, also those from `defineAgent({ extends })`. A `launch` wrapper gets the variable names in `requiredEnvironment`, so SSH runners forward them. Inspection shows the gateway name and `executionAuthority.credentials: "provisioned"`. `agent.status()` does not cache results for an Agent with a gateway.

ViteHub does not forward host variables such as `OPENAI_API_KEY`, `ANTHROPIC_BASE_URL`, or `CLIPROXY_API_KEY`. Pass them through a gateway or `driver.env`.

A gateway changes where requests go, not the protocol. It must accept OpenAI Responses requests from Codex and Anthropic Messages requests from Claude Code, and `model` must be an ID it accepts.

> **Warning**

The key and headers are in the provider environment, so the provider's commands can read them. Treat a gateway key as visible to the Agent. Use API keys: [Anthropic's terms](https://code.claude.com/docs/en/legal-and-compliance) do not permit routing a Claude subscription sign-in through a proxy.

### Run in an existing directory

Each Invocation gets a new temporary working directory by default. Set `cwd` to run in a directory that your app prepares, such as a throwaway checkout that is also the Workspace's local store root:

```ts [server/agents/review/agent.ts]
import { codexDriver, defineAgent } from 'vite-hub/agent'

export default defineAgent({
  driver: codexDriver({
    cwd: '/srv/review/checkout',
    permissions: 'allow-edits',
  }),
})
```

`cwd` takes an absolute or relative path, or a per-Invocation resolver. Relative paths resolve from the process working directory. If the result is not an existing directory, the Invocation fails with `AGENT_R0939` before the provider starts. A `launch` resolver gets it as `cwd`.

The directory is the working copy. ViteHub materializes Workspace Sources (outside file sets a Workspace mounts) but starts no Workspace session: no file copy, Git baseline, diff, write-back, or removal. It restores generated instruction and Skill files when the provider stops. In a Git repository root, Git ignores those files during the run, so provider commits skip them. Title and progress summary runs ignore `cwd`. Your app owns the directory's contents, cleanup, and isolation between concurrent Invocations. Inspection shows whether `cwd` is static or dynamic, without resolving it.

### Temporary files

Each Invocation on this host gets its own temporary directory in `TMPDIR`, `TMP`, and `TEMP` for the provider, its commands, and a `launch` wrapper. Test fixtures, tool caches, and the Node.js compile cache stay out of the shared host directory. ViteHub removes it when the Invocation ends, even after a failure, abort, or timeout, or a later Invocation removes it once the dead owner process is confirmed. Set `TMPDIR` in `env` to use another directory, which ViteHub does not remove. A Sandbox Invocation uses the Sandbox's temporary directory.

### Provision the project toolchain

Set `toolchain` when the project pins its own Node.js and package manager. It uses the same declaration as [`sandbox.toolchain`](https://vitehub.dev/docs/agents/sandboxes#provision-nodejs-and-the-package-manager):

```ts [server/agents/review/agent.ts]
import { codexDriver, defineAgent } from 'vite-hub/agent'

export default defineAgent({
  driver: codexDriver({
    permissions: 'allow-all',
    toolchain: 'project',
  }),
})
```

After ViteHub prepares the pull request checkout, the Workspace, or `cwd`, it reads the pins there, installs them under `$XDG_CACHE_HOME/vitehub/toolchains`, and checks `node -v` and the package manager version. The toolchain `bin` directories come first in `PATH` for the provider, its commands, and Workspace commands. With a pull request mounted below the Workspace root, ViteHub reads that mount. A `launch` launcher gets the same `PATH`, so it must run on this host. Provider CLIs that start through `#!/usr/bin/env node`, such as the npm Codex package, also use the project's Node.js. Title and progress summary runs install no toolchain.

With `toolchain`, `status()` does not report `node`, `npm`, `npx`, `pnpm`, `pnpx`, `yarn`, or `yarnpkg` as missing, because the toolchain exists only after an Invocation prepares its checkout. With `sandbox`, use `sandbox.toolchain`. The Agent rejects `driver.toolchain` with `AGENT_R0972`.

### Provider options

Threads resume with the provider's opaque cursor. ViteHub turns provider output into Invocation events: text, reasoning, native and Capability tool activity, approvals, provider questions, usage, warnings, errors, and the final state.

| Option | Purpose |
| --- | --- |
| `credentialProfile` | Codex only. Stable Home name. Requires `credentials`. Stored under `.vitehub/data/codex/<credentialProfile>`. |
| `credentials` | Codex only. `auth.json` string, sealed Server Env value, or per-Invocation resolver. Inspection shows that it is set, without resolving it. |
| `reasoningEffort` | Codex only. A non-empty effort the model supports, such as `"low"`, `"high"`, or `"xhigh"`. Requires `model`. |
| `reasoningSummary` | Codex only. `"auto"`, `"concise"`, `"detailed"`, or `"none"`. Requires `model`. |
| `model` | Optional provider model id. |
| `env` | Values or a per-Invocation resolver, resolved once. Otherwise the provider gets only host paths, locale, and user-directory variables, not app secrets. `TMPDIR`, `TMP`, and `TEMP` point to a [temporary directory for the Invocation](https://vitehub.dev/docs/agents/agent-drivers#temporary-files) unless `env` sets them. |
| `execution.attachments.maxBytes` | Positive image attachment budget per Invocation. Defaults to 25 MiB, shared by inline and lazy images. |
| `instructions` | Added to the colocated instructions. |
| `launch` | Provider command wrapper or per-Invocation resolver. Gets the executable, working directory, environment, and abort signal. |
| `cwd` | Existing directory or per-Invocation resolver. No Workspace session, write-back, or removal. See [Run in an existing directory](https://vitehub.dev/docs/agents/agent-drivers#run-in-an-existing-directory). |
| `permissions` | `"ask"`, `"allow-edits"`, `"allow-edits-unattended"`, or `"allow-all"`. Defaults to `"ask"`. `"allow-edits-unattended"` keeps edit mode and denies native escalation without a prompt. Host-bound MCP tools keep their own authorization. Only an explicit `"allow-all"` skips approval. |
| `providerSettings` | Advanced runtime settings. They override the installed Codex executable fallback. |
| `retention` | Codex only. `{ maxAge, maxRecords, maxSize }` Home limits. Needs a persistent Home. See [Limit the Codex Home size](https://vitehub.dev/docs/agents/agent-drivers#limit-the-codex-home-size). |
| `requirements` | Commands that `status()` checks, reported in `missingCommands`. In a child Agent, a function gets the inherited list: `inherited => [...inherited, 'kubectl']`. |
| `sessionStorePath` | SQLite file for session cursors, so threads continue after a restart on the same persistent volume. |
| `toolchain` | `"project"` or `{ node, packageManager, fallbackNode }`. Installs the project's pinned Node.js and package manager. See [Provision the project toolchain](https://vitehub.dev/docs/agents/agent-drivers#provision-the-project-toolchain). |
| `output` | Structured output contract. |
| `capacity` | Static or adaptive concurrency and queue limits for this process. |

Provider Drivers fail explicitly with model-specific Provider Tool contributions, Cloudflare Agents, Deno, or Workspaces on Windows. Workspace-scoped Skills and Capability tools work.

## Run a provider Driver in a Sandbox

Set `sandbox` to start the provider in a [Sandbox](https://vitehub.dev/docs/agents/sandboxes), an isolated command environment that supplies the working tree, private Home, environment, and boot checks per Invocation:

```ts [server/agents/review/agent.ts]
import { codexDriver, defineAgent } from 'vite-hub/agent'

export default defineAgent({
  sandbox: {
    runtime: 'trusted-host',
    requires: ['git'],
  },
  driver: codexDriver({ permissions: 'allow-all' }),
})
```

The runtime stays in the ViteHub process. Only the provider command runs in the Sandbox. ViteHub adds that command to the Sandbox requirements and writes instructions and Skills to the Sandbox Home. `driver.env` reaches the provider. The Driver rejects `launch`, `credentials`, and `credentialProfile`, and the Agent rejects `workspace`, so put credentials in the Sandbox. A Sandbox session lasts one Invocation, so threads do not resume.

### Cloudflare Workers

Provider Drivers start local processes, so they cannot run in a Cloudflare Worker. Deploy them to a Node.js host. Worker builds leave out the provider runtime and `@t3tools/provider-runtime` through the `workerd` and `worker` package conditions of `@vite-hub/agent`. Apps with only `{ model }`, `{ run }`, or `{ ask }` Drivers never bundle it.

Only `codexDriver()` and `claudeCodeDriver()` import the runtime, so the build finds every provider Driver, also from a variable, preset, layer, or the `title()` and `progressSummary()` Capabilities. A Worker build that contains one fails with `AGENT_B0019`. In development, a Worker Invocation that selects one fails with `AGENT_R0928`.

## Adaptive process capacity

Self-hosted Node apps can admit work by current machine load instead of a fixed limit. Share one capacity object between the Agents that use the same host:

```ts [server/agent-capacity.ts]
import { createProcessAgentCapacity } from 'vite-hub/agent/runtime/process'

export const agentCapacity = createProcessAgentCapacity({
  concurrency: 6,
  queue: { maxPending: 100, timeout: 30 * 60_000 },
})
```

`concurrency` is the hard maximum. Under CPU or memory pressure, ViteHub admits fewer new Invocations. Active ones continue, the queue stays FIFO, and admission resumes after recovery. Linux uses cgroup v2 limits, memory events, and pressure stall information when available. Other hosts use Node's available-memory and parallelism signals. A failed sample, or one slower than `sampleTimeoutMs` (default one second), uses `fallbackConcurrency` (default one). `vitehub agent info` shows the current limit, hard maximum, queue depth, and sampling reason.

## Use a custom run Driver

Use `driver.run` when your code makes the result without a model loop.

```ts [server/agents/router.ts]
import { defineAgent } from 'vite-hub/agent'

export default defineAgent({
  driver: {
    run({ input, invoker }) {
      return { text: `Accepted ${invoker.id}: ${String(input.prompt ?? '')}` }
    },
  },
})
```

The callback gets the input, messages, tools, Workspace access, Invocation context, and the Actor (who made the call) as `actor` and `invoker`. It can call a model, but ViteHub treats that work and usage as your app's.

ViteHub cannot stop this callback. On cancel, `input.abortSignal` aborts and the result shows `notEnforcedBy: 'run'`. Pass the signal to your I/O or throw `input.abortSignal.reason` to stop. Model and provider Drivers stop on cancel. See [Cancel an invocation](https://vitehub.dev/docs/agents/invocations#cancel-an-invocation).

Read [Instructions](https://vitehub.dev/docs/agents/instructions) for model-facing behavior and [Workspace context](https://vitehub.dev/docs/agents/workspace-context) for files and writeback.

## Use an ask Driver

Use `driver.ask` when the Agent only answers a few typed questions, such as a label or a score. It runs no chat model, tools, or instructions. It sends the questions to [TypeSafe Jev](https://typesafe.ai) through the `advocaat` client, in fixed-order sequential batches when one request is too large. For each question, the shared state, the question, and request overhead must fit below 30,000 bytes, or the Invocation fails. An empty question map skips Jev.

Install the client, an optional peer dependency:

```sh
pnpm add advocaat
```

It asks through the [`typesafe()` AI gateway](https://vitehub.dev/docs/agents/ai-gateways), which reads `TYPESAFE_API_KEY`. Set `gateway` to use Vercel or Cloudflare AI Gateway:

```ts [server/agents/labeller.ts]
import { defineAgent } from 'vite-hub/agent'
import { ask } from 'vite-hub/gateways'
import { cloudflare } from 'vite-hub/gateways'

export default defineAgent({
  driver: {
    ask: { spam: ask.if('Is it spam?') },
    // Reads TYPESAFE_API_KEY, CLOUDFLARE_ACCOUNT_ID, and CLOUDFLARE_AI_GATEWAY_ID.
    gateway: cloudflare(),
  },
})
```

Build the questions with `ask`:

```ts [server/agents/email-labeller.ts]
import { defineAgent } from 'vite-hub/agent'
import { ask } from 'vite-hub/gateways'

export default defineAgent({
  driver: {
    ask: {
      label: ask.choice('Which Gmail label fits this email?', {
        invoice: 'Bills, receipts, and payment requests.',
        none: 'No label clearly fits.',
      }),
      urgent: ask.if('Does the email need action today?'),
    },
  },
})
```

The answers are the Invocation output, and their type comes from the questions. `runAgent()` returns a tuple, so destructure it and read `result.output`:

```ts [server/label-email.ts]
import { runAgent } from 'vite-hub/agent'
import labeller from './agents/email-labeller'

const [error, result] = await runAgent(labeller, { prompt: 'Invoice 42' })
if (error) throw error
const answers = result.output!
answers.label.choice // 'invoice' | 'none'
answers.urgent // boolean
```

No schema or parsing is needed. Jev reads the Invocation `data`, else the prompt, else the latest user message. To build questions per Invocation, pass a function. It gets the `driver.run` context:

```ts [server/agents/labeller.ts]
import { useServerEnv } from '#vitehub/env/server'
import { defineAgent } from 'vite-hub/agent'
import { ask } from 'vite-hub/gateways'

export default defineAgent({
  driver: {
    ask: () => {
      const labels = useServerEnv().labeller.labels.split(',')
      return {
        label: ask.choice('Which label fits?', Object.fromEntries(labels.map(label => [label, null]))),
      }
    },
  },
})
```

| Builder | Answer |
| --- | --- |
| `ask.choice(instructions, criteria)` | `{ choice, confidence, probabilities }`. `criteria` maps labels to descriptions, or lists 2 to 255 labels. |
| `ask.switch(instructions, criteria)` | The label only. |
| `ask.score(instructions, levels)` | `{ score, ratio, confidence, legend, probabilities }` for 2 to 10 ordered levels. |
| `ask.chance(instructions, criteria?)` | `{ chance }`, the probability of yes. |
| `ask.if(instructions, { threshold? })` | `true` when the probability of yes is above `threshold`, between `0` and `1`, default `0.5`. |

State, instructions, and criteria accept JSON. Root numbers and booleans become text for the `advocaat` Entry contract. Nested ones stay native. Score legends keep the level descriptions.

`driver.ask` accepts only `ask`, `capacity`, and `gateway`. A gateway that cannot answer Jev, such as `cliproxy()`, fails at definition time with `AGENT_R1013`. The gateway retries transient failures. A missing `advocaat` or a missing or rejected key fails the Invocation with a `GATEWAYS_` code. See [AI gateway errors](https://vitehub.dev/docs/agents/ai-gateways#errors).

The Console and `vitehub agent info` show the Driver kind as `ask`. In an ask Agent, [`llmGate()`](https://vitehub.dev/docs/agents/capabilities/llm-gate) and [`llmRoute()`](https://vitehub.dev/docs/agents/capabilities/llm-route) without a `model` also decide through Jev, with their own `gateway` or `typesafe()`.

Each question is a trace step, so the journal and Console show what Jev was asked and answered:

| Observation | Attributes |
| --- | --- |
| `agent.tool.start` | `tool.input`: the question sent to Jev |
| `agent.tool.finish` | `tool.output`: the answer. `ask.answer`: the label, score, chance, or boolean. `ask.probabilities`, `ask.confidence`, `ask.model`, `tool.durationMs` |
| `agent.tool.error` | `tool.error`, `error.status`, `error.requestId` for each question of a failed request |
| `agent.usage.recorded` | One per Jev request: `usage.model`, `usage.inputTokens`, `usage.outputTokens`, `usage.totalTokens`, `ask.durationMs` |

Each Jev request is one call (`model`, `provider`, `usage`) in the usage record. `event.invocation.usage` in `agent:finish` and `usage.record` on `agent.invocation.finish` sum the tokens and name the model when all requests share it. The Console cost table reads it, as for Codex. To price it, add [`usage({ pricing })`](https://vitehub.dev/docs/agents/capabilities/usage) to the Agent.

`agent.tool.*` observations carry `tool.id` `ask:<key>`, `ask.key`, `ask.type`, `ask.batch.index`, `ask.batch.count`, and `ask.batch.size`. The shared state is `input.data` (or the prompt) on `agent.invocation.start`, and the answers are `result.output` on `agent.invocation.finish`. A metadata-only journal keeps `tool.input`, `tool.output`, `input.data`, and `result.output` only when `metadataContent` lists them, as the Console journal does. No credentials or request headers are recorded.

### Provider exit evidence

To save the final checkout HEAD in host-owned state, return `onExit({ cwd, abortSignal })` from a `launch` resolver. ViteHub calls it once after the provider and Workspace commands stop, before it restores generated files or deletes the working directory. Title generation and other auxiliary runs skip it. It also runs after a failed or cancelled turn once shutdown completes, and a cancel can return before that. Its signal has its own teardown deadline: stop all I/O when it aborts. A throw fails cleanup, but the directory is still removed.

ViteHub skips the callback when shutdown fails or passes the cleanup deadline, and during inspection. Treat missing evidence as unknown.

> **Warning**

The callback does not verify model claims, grant push authority, or survive a host crash. For durability, validate the checkout in host code and save the result before returning.

<!-- vitehub-docs:section-links -->
## In this section

- [Start](https://vitehub.dev/raw/docs/agents.md)
- Define and run
  - [Agent Definitions](https://vitehub.dev/raw/docs/agents/agent-definitions.md)
  - [Agent Drivers](https://vitehub.dev/raw/docs/agents/agent-drivers.md) (this page)
  - [Invocations](https://vitehub.dev/raw/docs/agents/invocations.md)
  - [AI gateways](https://vitehub.dev/raw/docs/agents/ai-gateways.md)
- [Instructions and tools](https://vitehub.dev/raw/docs/agents/instructions.md)
- [History and context](https://vitehub.dev/raw/docs/agents/chat-history-sessions.md)
- [Channels](https://vitehub.dev/raw/docs/agents/channels.md)
- [Inspect and evaluate](https://vitehub.dev/raw/docs/agents/observability.md)
- [Advanced](https://vitehub.dev/raw/docs/agents/agent-presets.md)
- [Recipes](https://vitehub.dev/raw/docs/agents/support-agent.md)
- [Presets](https://vitehub.dev/raw/docs/agents/presets.md)
- [Capabilities](https://vitehub.dev/raw/docs/agents/capabilities/access.md)

Next: [AGENT_R0011](https://vitehub.dev/raw/docs/agents/diagnostics/agent_r0011.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
