aiti-model-gateway (Model Gateway)
aiti-model-gateway (Model Gateway)
Configure the model once, use it everywhere.
Models configured inside pi-web (providers, API keys, combo models — all in models.json) used to be reachable only from this web app. The gateway exports that configuration as standard APIs: an external CLI, a script or another AI tool only swaps a base URL and shares the same models — while no real provider key ever leaves the machine.
What it solves
- One model subscription, many tools: Claude Code / Codex in the terminal, a small script calling the API, the web UI itself — no re-configuring per tool, and no copying keys into third-party tools.
- Credentials stay home: external callers only hold gateway-issued tokens (revocable, dead immediately after revocation); real keys stay inside pi-web.
- One usage ledger: who spent what, on which model, lands in the same AI usage report (deduplicated by source — never counted twice).
Two protocols
| Endpoint | Purpose |
|---|---|
POST /v1/chat/completions | OpenAI-compatible: streaming / non-streaming, full tools passthrough, multimodal image input |
POST /v1/messages | Anthropic Messages: Claude Code-style clients that only understand ANTHROPIC_BASE_URL connect directly |
GET /v1/models | Model catalog (?all=1 for the full list; by default only credentialed models) |
POST /v1/responses | OpenAI Responses (the wire Codex ≥0.5x speaks, function_call round trips and streaming events included) |
POST /v1/images/generations | Host image models (b64_json or a temporary url) |
Model names accept three forms: provider/modelId (exact), a bare model id (same-name collisions resolved by credential availability — measured CLIs rewrite model strings, and both glm-5.3-flash and opencode/glm-5.3-flash land correctly), and aliases in gateway.json (for clients that hardcode names like gpt-4o); unknown names can also fall back to a configured fallbackModel.
Wiring an external agent
Option 1: pick a "pi model" in the model picker (recommended)
When creating a new session on an external agent backend, the model picker gains a "pi: model" group; selecting one routes that agent's LLM traffic through the gateway — no config file to write. A pi model can also be switched mid-session: hot switch where the backend carries the model per request, with a "restart to apply" hint for backends that only read config at startup.
Only backends that really honour an OpenAI/Anthropic base URL are offered (offering a model a CLI cannot consume would just fail on selection):
| Backend | Status |
|---|---|
| Claude Code (native + ACP) | ✅ end to end, tool calls included, mid-session model switch (hot) |
| opencode (ACP) | ✅ end to end (inline OPENCODE_CONFIG_CONTENT); model switch needs a session restart |
| qwen (ACP) | ✅ end to end (--auth-type openai + OPENAI_*; it names its own provider, the gateway routes it via fallbackModel) |
| qoder (intl / CN) | ✅ end to end (isolated QODER_CONFIG_DIR with a providers.pi-gateway table plus customModels, launched with -m pi-gateway/<ref>; the intl CLI must be logged in) |
| codex (native + ACP) | ✅ end to end over the OpenAI Responses protocol (isolated CODEX_HOME; tool calls verified with the CLI) |
| omp (Oh My Pi) | ✅ end to end (isolated PI_CODING_AGENT_DIR plus a gateway models.json; the fork only reads that dir and caches the catalog) |
| pi-acp | — not needed: pi-acp uses pi's own models.json + credentials, the same configuration we run |
| gemini / qwen / iflow / cline / copilot / cursor / codebuddy and 10 more | not wired: the binaries expose no base-URL override at all (own endpoints or hosted services) |
| droid / goose / iflow / kimi / devin / auggie | measured, not viable: droid needs Factory auth, goose needs config.yaml, kimi/iflow base URLs are for their own cloud, devin answers through its hosted backend, auggie's shape is undocumented |
| cline / codebuddy | support custom providers but gate on their own account: cline via CLINE_PROVIDER_SETTINGS_PATH, codebuddy via customModels + CODEBUDDY_CONFIG_DIR; both answered 401 asking to sign in. Once signed in, the generated provider config applies |
| gemini / cursor-agent | no provider-config entry (gemini only has a generic baseUrl; cursor's CURSOR_API_URL is its own controller) |
Option 2: three environment variables
# Claude Code (anthropic protocol; base WITHOUT /v1)
ANTHROPIC_BASE_URL=https://<host>/api/extensions/aiti-model-gateway \
AUTH_TOKEN=<网关 token> \
ANTHROPIC_MODEL=<provider>/<model> \
claude# Any OpenAI-compatible client
OPENAI_BASE_URL=https://<host>/api/extensions/aiti-model-gateway/v1
OPENAI_API_KEY=<gateway token>Three measured caveats: ① Claude Code reads the
envblock of~/.claude/settings.jsonand overrides process env (give it an isolatedCLAUDE_CONFIG_DIRwhen needed); ② its base URL must not include/v1(the SDK appends/v1/messagesitself); ③ the window and output cap must be declared explicitly —CLAUDE_CODE_AUTO_COMPACT_WINDOWplusCLAUDE_CODE_MAX_OUTPUT_TOKENSis the only correct path (otherwise the CLI assumes 200k/8k and silently degrades). Picking a "pi model" in the picker injects both automatically; with hand-written env, add them yourself.
Tokens and usage
- Issue: browser panel (toolbar "Gateway") → Tokens tab; the plaintext is shown once, only
sha256(salt+token)is persisted. The AI can also issue one in a session on your behalf. - Carry:
Authorization: Bearer/x-api-key/?token=. - Revoke: immediate; revoking every token does not reopen the endpoint (returning to open mode requires deleting
tokens.json). - Accounting: each token has a
subject—external(clients without their own stats, e.g. scripts or qoderclicn) counts into the AI usage totals, whileagent(claude / zcode and friends, which journal their own tokens) only shows in the gateway dimension, so the same spend is never counted twice. - Admin needs no token: the panel and
/admin/*trust the pi-web login itself (the proxy/tunnel already authenticated the operator), so nothing has to be pasted repeatedly.
Typical use
| Scenario | How |
|---|---|
| Share a subscription across several CLIs | Issue one token per tool; read usage per token |
| Point Claude Code at a cheap pi model | Issue a token in the panel → three env vars → claude |
| Batch inference from your own script | OPENAI_BASE_URL + gateway token, model ids from /v1/models |
| Rescue a tool that hardcodes model names | Aliases tab: gpt-4o → provider/model; or fallbackModel |
Deployment and status
🟡 Beta: not shipped with releases and not on npm yet — currently source-deployed from the experimental area (dev instance recommended). The API surface is stable; it joins releases once published.
- Data:
<config>/gateway.json(aliases / concurrency / timeout / fallback model),<config>/tokens.json(hashes only),<state>/usage.jsonl(fallback ledger when the AI usage extension is unavailable). - Panel: toolbar "Gateway" → overview / tokens / aliases / usage; also openable from the right dock.
- Dependencies: none beyond the host; models come from the host
models.json, images from the host image catalog.