AI Zero Token:本地 OpenAI 兼容网关,接入 ChatGPT/Codex OAuth 账号
fchangjun/AI-Zero-Token
AI Zero Token 是一个本地优先的 OpenAI 兼容网关,通过 ChatGPT/Codex OAuth 账号暴露 OpenAI 风格端点,提供本地 CLI、Web 控制台和 HTTP 网关。
Local-first OpenAI-compatible gateway for ChatGPT/Codex OAuth accounts.
AI Zero Token provides a local CLI, web console, and HTTP gateway that expose saved ChatGPT/Codex account capabilities through OpenAI-style endpoints. It is designed for local single-user workflows, prototyping, automation, and OpenAI-compatible clients.
This is an experimental local tool. It is not an official OpenAI product and is not recommended as a production multi-tenant gateway.
Features
- OpenAI-compatible local API:
GET /v1/modelsPOST /v1/responsesPOST /v1/chat/completionsPOST /v1/images/generationsPOST /v1/images/edits
- ChatGPT/Codex OAuth login with local token refresh.
- Multi-account management in the web console.
- Account JSON import/export, including ZIP batch import, selected batch export, and export audit indicators.
- Apply a saved account to local Codex by backing up and updating
~/.codex/auth.json. gpt-image-2image generation and JSON image editing. Paid plans use the Codex Responses image tool; Free plans can optionally use the ChatGPT web image path under Serve API → Models.- Optional quota-exhaustion auto switch to the next saved API account, including accounts whose quota has not been synced yet, plus configurable quota refresh concurrency.
- Optional upstream proxy configuration for OAuth, model refresh, and gateway forwarding.
- Local model discovery from the Codex model cache, with manual network sync from the Codex backend.
- OpenClaw-oriented chat compatibility for streaming, tool calls, tool result messages, and request-log diagnostics.
- Persistent local usage statistics for known token usage, images, success/failure counts, latency, and account/model/endpoint breakdowns.
Architecture
AI Zero Token keeps account tokens and gateway settings in local state, exposes OpenAI-compatible HTTP endpoints, and forwards requests to the selected ChatGPT/Codex OAuth account. The web console reads the same local state, so account switching, Codex auth application, proxy settings, and automatic switching are all controlled from one local place.
Quick Start
npm install -g ai-zero-token azt start
The command starts the local web console and gateway:
http://127.0.0.1:8787
http://127.0.0.1:8787/v1
API access is unauthenticated by default; clients may use local as a placeholder. After enabling an access key under Models & Services → Serve API → Overview, all model endpoints require Authorization: Bearer <access-key>.
Desktop Preview
This repository now includes an Electron desktop preview. It starts the existing local gateway from the desktop main process and loads the current web console:
npm run desktop
Build desktop release artifacts:
npm run dist:mac npm run dist:win
See docs/DESKTOP_RELEASE.md for release notes. Desktop installers will be published on GitHub Releases after each tagged release:
The first desktop version keeps the current default listener:
0.0.0.0:8787
For local clients, keep using:
http://127.0.0.1:8787/v1
On macOS, the desktop app also adds a menu-bar panel for quick account switching. The panel can switch the active gateway account, apply an account to local Codex, refresh quota status, copy the API Base URL, and restart the local gateway.
Web Console
The web console is the recommended entry point:
- Log in with OpenAI Codex OAuth.
- Import one or more account JSON files, or validate and import a ZIP archive that contains multiple account JSON files.
- Switch the active account.
- Export one account or selected accounts, with export status shown on account cards.
- Delete selected accounts in batches.
- Apply a saved account to local Codex.
- Configure the default text model and upstream proxy.
- Enable automatic account switching when the active API account has exhausted its recorded quota.
- Tune global quota refresh concurrency for larger account pools.
- Test
models,responses,chat.completions,images.generations, andimages.edits. - Review local usage statistics for today, the current gateway process, and lifetime totals.
- Check two release channels independently: desktop builds are compared only with same-platform GitHub Release artifacts, while npm/CLI is compared only with the npm registry; a newer local desktop build is labeled development/unreleased instead of being reported as an update.
API Usage
Models
curl http://127.0.0.1:8787/v1/models
Responses
curl http://127.0.0.1:8787/v1/responses \ -H "content-type: application/json" \ -d '{"model":"gpt-6-luna","input":"Reply with OK only."}'
Chat Completions
curl http://127.0.0.1:8787/v1/chat/completions \ -H "content-type: application/json" \ -d '{ "model": "gpt-6-luna", "messages": [ { "role": "user", "content": "Reply with OK only." } ] }'
For OpenClaw or other OpenAI-compatible coding clients, configure:
Provider: OpenAI compatible
Base URL: http://127.0.0.1:8787/v1
API Key: local
Model: gpt-6-luna
Streaming: enabled
Tools / function calling: enabled
/v1/chat/completions accepts stream=true, tools, tool_choice, parallel_tool_calls, assistant tool_calls, tool-role result messages, and reasoning_effort. OpenClaw requests also appear in the management console request log with safe summaries.
Codex CLI/Desktop can also use the gateway as a custom Responses provider:
Models & Services now has two directions: Connect External API and Serve API. Serve API contains the service overview, existing accounts, model catalog, default model, Free-account image setting, and rotation policy. Existing account files are reused, and old #accounts links redirect to the account pool.
The overview provides local/LAN client configurations and a local Codex connection. API serving and Codex selection are independent: switching Codex to an external provider or disconnecting it leaves the account-pool API running. Connect another machine's AI Zero Token gateway from Connect External API → Remote AI Zero Token.
Access keys are optional, take effect immediately, and are stored with 0600 permissions. When enabled, model endpoints require a Bearer key. Management operations always require loopback access, including when API authentication is disabled. LAN clients can call model APIs. An active local Codex connection is updated to the azt_gateway provider when the key changes; restart Codex to load that configuration. External and remote Codex connections remain independent. Other clients must update their keys after rotation.
Without an access key, advanced compatibility settings offer the native openai or separate AI Zero Token provider. Manual configuration example:
Manage company relays and other OpenAI-compatible APIs on the dedicated Models & Services page (模型与服务). Add a service with its name, Base URL, and token to fetch every model returned by /models, or enter model IDs manually when discovery is unavailable. Saving a service does not change the current Codex connection.
Run individual or batch capability checks from the service details page. Checks cover streamed Responses, tool calls, tool results, reasoning levels, and image input. They send real requests and consume upstream quota; background checks continue when you leave the page. Temporary network or busy errors preserve previously confirmed capabilities and identify them as earlier results.
Choose 接入 Codex, select the models to display, and pick a default. Unchecked, busy, and incompatible models remain selectable; tags provide guidance. The app writes the selected service and its exact model catalog, with one active service at a time. Restart Codex to apply the selection. Reapply the connection after editing a service. Legacy external API settings and inspection history are imported automatically. Switching keeps other service records; deleting a service removes its configuration, which existing conversations may still need.
model = "gpt-6-luna" model_provider = "openai" openai_base_url = "http://127.0.0.1:8787/codex/v1"
This keeps Codex on its native openai provider id and only replaces openai_base_url, so local conversation history remains in the same Codex history view.
If you want Codex to show a separate AI Zero Token provider, choose that mode under Serve API → Overview → Local Codex → Advanced. It writes a [model_providers.ai-zero-token] block instead of openai_base_url.
When a Codex request includes the image_generation tool, Plus, Team, Pro, and other paid plans keep using the Codex Responses image tool. If Free account image generation is enabled under Serve API → Models, Free plans use the ChatGPT web image path and receive a synthetic Codex-compatible Responses SSE stream. This handles the upstream service split where a Free account may still have ChatGPT image quota but not the Codex image_generation tool.
Image Generation
curl http://127.0.0.1:8787/v1/images/generations \ -H "content-type: application/json" \ -d '{ "model": "gpt-image-2", "prompt": "A clean product photo of a red apple on a white background.", "size": "1024x1024", "quality": "low", "response_format": "b64_json" }'
The gateway returns an OpenAI-style response with data[].b64_json.
Image Editing
/v1/images/edits currently supports JSON requests with URL, data URL, or raw base64 image references:
curl http://127.0.0.1:8787/v1/images/edits \ -H "content-type: application/json" \ -d '{ "model": "gpt-image-2", "prompt": "Use this reference image and create a cleaner product advertisement version.", "images": [ { "image_url": "data:image/png;base64,REPLACE_WITH_IMAGE_BASE64" } ], "size": "1024x1024", "quality": "low", "response_format": "b64_json" }'
More examples are available in docs/API_USAGE.md.
Account Management
AI Zero Token stores account state locally under:
~/.ai-zero-token/.state
The web console supports:
- OAuth login.
- JSON import from a single profile, an array, or a
profilesbundle. - ZIP batch import for archives containing multiple account JSON files, with validation before import.
- Single-account export.
- Selected batch export using checkboxes.
- Export audit status, including export count, latest export time, and export type.
- Single-account and selected batch deletion.
- Active-account switching.
Exported account JSON includes authentication tokens and should be treated as a secret.
Apply to Codex
The Apply to Codex action writes the selected account to:
~/.codex/auth.json
Before writing, the existing file is backed up as auth.json.azt-backup-*. New Codex sessions will use the applied account.
Configuration
Default listener:
0.0.0.0:8787
Local gateway base URL:
http://127.0.0.1:8787/v1
Restrict browser CORS origins:
AZT_CORS_ORIGIN=http://127.0.0.1:8124 azt start
Multiple origins can be separated by commas:
AZT_CORS_ORIGIN=http://127.0.0.1:8124,http://localhost:3000 azt start
The persistent state directory can be overridden with:
AI_ZERO_TOKEN_HOME=/path/to/home azt start
The web console settings are persisted in the same local state directory. The quota auto-switch option is stored as autoSwitch.enabled; when enabled, the gateway uses the latest saved quota snapshot and moves API traffic away from the active account once that snapshot shows a quota window is exhausted. Accounts listed in autoSwitch.excludedProfileIds are excluded from automatic rotation and remain available for manual selection.
The global quota refresh concurrency can be configured under Serve API → Rotation. The default is 3; lower it if upstream rate limits increase, or raise it for larger local account pools.
The default request body limit is 128 MiB, which is intended to make JSON base64 image references and Codex context-compaction requests practical for local workflows. The dedicated /codex/v1/responses/compact route is allowed at least 256 MiB. You can override the default with:
AZT_BODY_LIMIT_MB=256 azt start
Image Limits
ChatGPT Images availability and limits are controlled by the upstream account. Plus, Team, Pro, and other paid plans use the Codex Responses image_generation tool. Free accounts only use the ChatGPT web image path when Free account image generation is enabled under Serve API → Models; otherwise they keep the original Codex image-tool path. Free limits are stricter than paid plans and are not published as fixed public numbers; if the web image path is also exhausted, the gateway surfaces the upstream response.
Paid-plan image requests use gpt-5.4-mini as the internal orchestration model and pass the requested image model, such as gpt-image-2, to the image_generation tool. Free-plan web image requests convert the same input to a ChatGPT web image task.
For JSON image editing, base64 payloads are about 33% larger than the original image. With the default 128 MiB body limit, a raw image around 96 MiB is the practical upper bound before JSON overhead. For larger images or batch workflows, prefer a reachable image URL.
Limitations
- This project is intended for local single-user use.
/v1/chat/completionssupports OpenAI-style SSE forstream=true; Codex custom providers use the dedicated/codex/v1/responsesand/codex/v1/responses/compactroutes for upstream Responses SSE passthrough./v1/chat/completionssupports common tool/function-calling fields, butn > 1is not supported./v1/images/generationscurrently returnsb64_json; hosted image URLs are not supported./v1/images/generationsdoes not supportn > 1./v1/images/editscurrently supports JSON only.multipart/form-data,mask, andfile_idare not yet supported.- Very large base64 JSON requests are constrained by
AZT_BODY_LIMIT_MBand local memory. - OpenAI Responses API compatibility is partial and focused on common local client workflows.
Security
- Access tokens, refresh tokens, and ID tokens are equivalent to login credentials.
- Do not expose the gateway to untrusted networks.
- Do not share exported account JSON outside trusted environments.
- Review files under
~/.ai-zero-token/.stateand~/.codex/auth.jsonbefore copying or publishing local data.
Development
Install dependencies:
npm install
Run from source:
bun src/cli.ts start
Validate:
npm run typecheck npm run build
CLI packaging and release notes are documented in BUILD_CLI.md. User-facing changes are tracked in CHANGELOG.md.
Project Status
AI Zero Token is evolving quickly. The current focus is a reliable local gateway, account transfer workflows, and image generation/editing compatibility for OpenAI-style clients.
Issues and feature requests: GitHub Issues
来源:GitHub · github.com


