Files
opencode-proxy/README.md
T
2026-07-21 11:17:44 -05:00

70 lines
2.5 KiB
Markdown

# OpenCode Go Proxy
A small, unauthenticated OpenAI-compatible proxy for OpenCode Go.
## Setup
Requires Node.js 18 or newer.
```sh
npm install
```
Create `keys.txt` in the project root. Put one OpenCode Go API key on each line. Blank lines and comments are ignored; inline comments are supported.
```text
go-first-key
go-second-key # optional comment
# disabled-key
```
For deployments such as Railway, keys can instead be supplied as a JSON environment variable. When set, `OPENCODE_API_KEYS` takes precedence over `keys.txt`:
```sh
OPENCODE_API_KEYS='["go-first-key","go-second-key"]'
```
To convert `keys.txt` into a copyable JSON value:
```sh
npm run keys:env
```
The global context limit defaults to 256k tokens and can be changed with `MAX_CONTEXT_TOKENS`.
Inference requests are limited per client IP to 2 requests per second, 100 requests per five hours, and $10 of reported upstream cost per five hours. Railway's `X-Real-IP` header is used to identify clients.
Start the proxy:
```sh
npm start
```
The proxy listens at `http://localhost:4005/oai/v1` and `http://localhost:4005/ant/v1`. It does not require authentication from clients. Keys are selected round-robin for each request and are sent upstream as Bearer tokens. If an upstream request fails, the proxy tries each remaining key once before returning the final failure response.
## Endpoints
```sh
curl http://localhost:4005/oai/v1/models
```
```sh
curl http://localhost:4005/oai/v1/chat/completions \
-H 'content-type: application/json' \
-d '{"model":"kimi-k3","messages":[{"role":"user","content":"Hello"}]}'
```
Codex and other OpenAI Responses API clients can use `POST /oai/v1/responses`. Responses requests are translated to the upstream Chat Completions API and responses, including streaming events, are translated back to the Responses format.
Streaming requests are supported with `"stream":true` and are passed through as Server-Sent Events. The model list is fetched from OpenCode Go, so it can include models whose upstream transport is not OpenAI Chat Completions.
The Anthropic-compatible adapter exposes `POST /ant/v1/messages` and `GET /ant/v1/models`. Messages, tools, JSON responses, and streaming events are translated to and from OpenAI Chat Completions. Anthropic clients should use their normal Messages API request format and can use the OpenCode model IDs returned by the models endpoint.
For Claude Code, either set `ANTHROPIC_BASE_URL=http://localhost:4005/ant` or use the displayed `http://localhost:4005/ant/v1` value; both forms are supported.
## Tests
```sh
npm test
```