Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
docs(agent): using a gateway
locnguyen198696cb064c5643
diff --git a/docs/src/pages/docs/agent/cli.mdx b/docs/src/pages/docs/agent/cli.mdxindex 4f0aa5b4c..cad4ecb6a 100644--- a/docs/src/pages/docs/agent/cli.mdx+++ b/docs/src/pages/docs/agent/cli.mdx@@ -46,6 +46,7 @@ jan <COMMAND>| `--no-worktree` | Work in the project directory, overriding a persistent `worktree` setting || `--provider <ID>` | Target a specific provider for this run || `--api-key <KEY>` | API key for that provider |+| `--base-url <URL>` | Base URL for the `--provider` named, e.g. a gateway. Requires `--provider`. See [Using a gateway](/docs/agent/providers#using-a-gateway) |```bashjan # current directory@@ -107,7 +108,7 @@ jan cli agent run --max-turns 5 --max-session-tokens 20000 "triage the build fai| `--max-turns <N>` | Hard cap on agentic turns. If the model is still calling tools at N the run fails with `reached the N-turn limit...` and exits `53`; with `--output-format json` the partial answer is still in `result`. Bounds the parent run only - subagent turns are not counted. `0` means unbounded, which is the default || `--max-session-tokens <N>` | Advisory token ceiling for this run, overriding `[budget].max_tokens`. Triggers compaction and a recorded note when passed; it does not terminate the run. `0` means no ceiling || `--max-budget-usd <USD>` | Stop the run once it has spent this much, overriding `[budget].max_usd`. Priced from the provider's published rates, so a model with no published price is **refused** rather than run uncapped - including a subagent whose definition names its own model. Subagents inherit the ceiling. Compaction is billed against it like any other request. Stopping this way exits `0` with `stop_reason: budget_exceeded`; `0` as a limit stops at the first billed request |-| `--provider`, `--api-key` | Credential overrides for this run |+| `--provider`, `--api-key`, `--base-url` | Provider overrides for this run. `--base-url` requires `--provider`; see [Using a gateway](/docs/agent/providers#using-a-gateway) || `--output-format <FORMAT>` | `text` (default) streams the answer; `json` prints one result object; `stream-json` prints one JSON event per line || `--input-format <FORMAT>` | `text` (default) ignores stdin; `stream-json` reads client messages while the run is in flight. Requires `--output-format stream-json` || `--host-tools <FILE>` | JSON file declaring tools **the client executes**, as a list of `{"name", "description", "parameters", "capability"}`. The model calls them as `host__<name>` (see [name mapping](#host-tools)); each call arrives as a `tool_request` on stdout and must be answered with a `tool_result` on stdin, so this requires `--input-format stream-json`. Validated before the run starts |@@ -443,8 +444,14 @@ Print the resolved project config and available providers as JSON.```bashjan cli agent statusjan cli agent status --project ~/code/app+jan cli agent status --provider tokamak # as a launched session would resolve it```+It takes the same `--provider`, `--api-key` and `--base-url` flags as `run`. The top-level+`capabilities` array lists what this build supports, and each provider entry names its+`header_names` (never their values) and its `base_url_source`. See+[Checking what a build supports](/docs/agent/providers#checking-what-a-build-supports).+### `rpc`Run one long-lived Jan process with multiple addressable sessions over LF-delimited JSON-RPC on stdin/stdout. `jan cli agent rpc` prints **protocol lines only** to stdout. The first request is `initialize` with `protocolVersion: 1` and a `clientInfo` name/version; wait for its response, then send the `initialized` notification before any other method. Calling a method early returns `-32002`.@@ -758,6 +765,10 @@ doing anything. Re-run the installer with `--source` to rebuild.| --- | --- || `JAN_API_KEY` | API key for the target provider || `<PROVIDER>_API_KEY` | Per-provider key, e.g. `ANTHROPIC_API_KEY` |+| `JAN_BASE_URL` | Base URL for the provider named with `--provider`. Ignored without it |+| `JAN_CUSTOM_HEADERS` | Extra request headers for that provider, one `Name: Value` per line. Ignored without `--provider` |+| `TOKAMAK_BASE_URL` | Tokamak deployment for sign-in and account calls; `/v1` is appended to a bare origin |+| `TOKAMAK_WEB_URL` | Tokamak web app for the sign-in and API-keys pages, when it can't be derived from the API host || `JAN_INSTALL_DIR` | Install directory used by the install script |## Using it in CIdiff --git a/docs/src/pages/docs/agent/providers.mdx b/docs/src/pages/docs/agent/providers.mdxindex 3fde3dd0c..213d06eb0 100644--- a/docs/src/pages/docs/agent/providers.mdx+++ b/docs/src/pages/docs/agent/providers.mdx@@ -37,6 +37,20 @@ jan config set \| `--model` | Model to expose. Repeatable, and replaces any existing list || `--api-type` | Wire protocol, `openai` or `anthropic`. Defaults to OpenAI-compatible |+Extra request headers for a provider go in `~/.jan/config.toml` directly. They are sent with every+request to that provider, both inference and the `/models` listing:++```toml+[providers.my-gateway]+base_url = "https://gateway.example.com/v1"+headers = { "X-Team" = "infra" }+```++A few names are reserved and never sent from here: `Authorization`, `Content-Type`, `Accept`,+`Accept-Encoding`, `x-api-key`, `x-goog-api-key` and `anthropic-version`. Jan sets those itself.+Jan also owns `User-Agent`, `X-Client-Request-Id` and `X-Session-Id`, so a custom header with one+of those names loses to Jan's value.+## Inspecting```bash@@ -104,12 +118,108 @@ Four sources, each beating the ones above it:| 1 | `~/.jan/config.toml` | The base, and the only file `jan config set` writes || 2 | Jan Desktop's `settings.json` | Inherit-only. Adds providers you haven't configured here, never overwrites one you have, and is never written back to || 3 | `[provider]` in a project's `agent.toml` | An explicit per-project choice, so it wins over both |-| 4 | `--provider` / `--api-key`, or `JAN_API_KEY` / `<PROVIDER>_API_KEY` | The most explicit and most ephemeral signal |+| 4 | `--provider` / `--api-key` / `--base-url`, or `JAN_API_KEY` / `<PROVIDER>_API_KEY` / `JAN_BASE_URL` / `JAN_CUSTOM_HEADERS` | The most explicit and most ephemeral signal. See [Using a gateway](#using-a-gateway) |The useful consequence: Jan Desktop fills in providers for free without ever clobbering somethingyou set deliberately, a project can pin its own provider without touching your global setup, and asingle run can override everything.+## Using a gateway++A gateway is an OpenAI-compatible endpoint that sits in front of other providers. Tokamak is one;+so are OpenRouter-style routers and a company proxy that adds its own headers. You can point one+run at a gateway without touching `~/.jan/config.toml`, the same way `ANTHROPIC_BASE_URL` works+for Claude Code:++```bash+export TOKAMAK_API_KEY="sk_live_..."+export JAN_BASE_URL="https://api-stag.tokamak.sh/v1"+export JAN_CUSTOM_HEADERS=$'X-Client-Name: my-launcher\nX-Team: infra'++jan --provider tokamak --model tokamak/auto+jan cli agent run --provider tokamak "fix the failing test"+```++| Setting | Flag | Environment | Notes |+| --- | --- | --- | --- |+| Base URL | `--base-url <URL>` | `JAN_BASE_URL` | `https://`, or `http://` for localhost only. The flag wins |+| Key | `--api-key <KEY>` | `<PROVIDER>_API_KEY`, e.g. `TOKAMAK_API_KEY` | `JAN_API_KEY` still works as before |+| Headers | none | `JAN_CUSTOM_HEADERS` | One `Name: Value` per line, the format of Claude Code's `ANTHROPIC_CUSTOM_HEADERS`. Beats a configured header of the same name |++These apply **only to the provider named with `--provider`**. If you leave `--provider` out, Jan+falls back to the model Jan Desktop has selected, and redirecting that choice to a gateway would be+a surprise. So `JAN_BASE_URL` and `JAN_CUSTOM_HEADERS` are then ignored, and Jan warns once.+`JAN_API_KEY` keeps its old behaviour: it applies to the provider you name, or to every provider if+you name none.++The overrides last for the whole session: a reload after `/login` and the `/model` refresh keep+the same key and base URL. They cover the console, `jan cli agent run` and `jan cli agent step`.+`jan cli agent rpc` takes none, because an ADK host sets up its own providers.++### What stays in memory++A provider is *session-scoped* when you name it with `--provider` and its base URL or key comes+from a flag or the environment. Its models and prices describe the gateway, not whatever your+config was written for, so Jan keeps them in memory and never writes them to disk:++- At startup Jan lists the gateway's models once with `GET {base}/models`. The console waits about+ 5 seconds for that, or 20 if a spend cap is set; `run` and `step` wait 20 seconds. The list+ replaces the provider's configured models and prices for this session only. `/model` and+ <kbd>Ctrl</kbd>+<kbd>R</kbd> refresh it.+- If the listing fails, the session has no models and no prices for that provider. A+ `provider/model` id still routes by its prefix, but a spend cap cannot be priced.+- `~/.jan/config.toml`, `~/.jan/model_catalog.json` and the project's `agent.toml` are left alone.+ Picking a model the gateway serves applies to this session only.+- Signing in to another provider inside the session works as usual, and does not move the session+ off the gateway.++If that provider is `tokamak`, account commands (`/usage`, the key check) use the session's+endpoint and key. The console also refuses any change to the Tokamak sign-in, because the key in use+belongs to whoever started the session. That covers `/login` (including `--paste-token`),+`/logout tokamak`, and the `tokamak` row of the `/login` picker and of `/settings` > `providers`.+Other providers are unaffected.++### What the gateway sees++Every inference request from the CLI carries:++| Header | Value |+| --- | --- |+| `User-Agent` | `Jan-Agent/<version> (<os>; <arch>)`. Jan Desktop sends none |+| `X-Client-Request-Id` | `jan-<session id>`, searchable in Tokamak's usage records |+| `X-Session-Id` | `<session id>`, the same id without the prefix |++Your custom headers are sent too, and the `/models` listing carries them as well, since a proxy that+needs a header for inference usually needs it for the listing.++### Checking what a build supports++`jan cli agent status --provider <id>` reports what this build can do, so a launcher can check+before it relies on anything:++```json+{+ "capabilities": ["provider-overrides", "session-overrides", "custom-headers",+ "user-agent", "session-header", "session-scoped-providers"],+ "providers": [+ { "provider": "tokamak", "base_url": "https://api-stag.tokamak.sh/v1",+ "base_url_source": "env", "has_api_key": true, "models": 0,+ "header_names": ["X-Client-Name", "X-Team"] }+ ]+}+```++`base_url_source` is one of `flag`, `env`, `project`, `config` or `desktop`. `header_names` lists+names only, never values.++### Tokamak on another deployment++A native `jan login` (not a gateway session) follows `TOKAMAK_BASE_URL` for a dev or self-hosted+deployment. A bare origin such as `https://api-stag.tokamak.sh` gets `/v1` appended. The sign-in and+API-keys pages open on the matching web app: `api.` maps to the bare host and `api-<band>.` to+`<band>.`. Set `TOKAMAK_WEB_URL` when the web app can't be derived that way, for example a local+stack on `http://localhost:3001`.+## Your own hardwareAny OpenAI-compatible endpoint works, which includes a model running locally. Jan Desktop's