# OpenAI-compatible endpoint

|  | Endpoint |
| --- | --- |
| Serves | Tools that speak OpenAI Chat Completions and use an OpenProgram Agent as their model |
| Routes | `GET /v1/models`, `POST /v1/chat/completions` on the Worker's port |
| Code | `openprogram/interfaces/server/routes/openai_compatible.py`; the turn starts through `runtime/conversation/turn_admission/owner_turn.py`; `/v1` refusals in `owner_auth.py` |
| User docs | [OpenAI-compatible API](https://fzkuji.github.io/OpenProgram/docs/interfaces/openai-api.html) |
| Tests | `tests/component/webui/runtime/test_openai_compatible.py`: the real turn path with a scripted model, streaming, client leaving, auth refusal |

## Peers

Of the 21 harnesses compared, two serve this endpoint: Hermes Agent (`/v1/chat/completions` and `/v1/responses`) and OpenClaw (`/v1/chat/completions`, off by default). Claude Code and Codex offer their own SDK and app-server protocols instead, which OpenProgram answers with ACP and the MCP server. The choices below follow Hermes and OpenClaw where they agree, and say which one where they differ.

## Decisions

| Question | Choice, followed peer and reason |
| --- | --- |
| Port and key | The Worker's own port and its owner token as `Authorization: Bearer`; no separate key, switch or permission gate. Both peers put the endpoint behind the server's existing bearer key. The token already opens every owner API of the Worker, a chat turn included, so a second key or an off switch would add a gate without narrowing what a token holder can do. |
| Model | The `model` is an Agent's id; `/v1/models` lists the Agents, the default first. OpenClaw: its model string selects the agent. Hermes advertises one fixed model because it has one agent; OpenProgram has several. |
| Conversation | Each request starts a conversation of that Agent; `conversation_id` in the body continues one, and every answer names its conversation in the body and the `X-OpenProgram-Conversation-Id` header. Both peers are stateless per request with an opt-in key for continuity (Hermes a header, OpenClaw the `user` field). A body field travels through the OpenAI SDKs' `extra_body`, which most tools expose. |
| Earlier messages | The last user message is the turn's message; the messages before it are folded into the same input, labelled by role, instructions first. OpenClaw builds its agent prompt this way. The conversation store holds only turns that happened, so the caller's history is not written as stored turns the Agent never gave. |
| Turn | The conversation's normal turn: it takes the run slot, runs the Agent's own model, tools and permission mode, is stored and shows in the App. Source `api`, the bearer's owner authority marked non-interactive and the unattended run note, as `openprogram --print`: no one can answer an approval or a question, so such a step is refused instead of waiting. Sampling and `tools` fields are ignored, as both peers do: the Agent's settings apply. |
| Streaming | The reply text as `chat.completion.chunk` deltas, a final chunk with `finish_reason`, an optional usage chunk, then `data: [DONE]`. Tool activity is not written into `content`: clients show it as the reply. A client that closes the stream cancels the turn, as OpenAI's API stops generating. |
| Errors | OpenAI's `{"error": {message, type, param, code}}` for every refusal, including the auth middleware's on `/v1` paths, so OpenAI clients show the message; a `/v1` path this server does not serve is a 404 in the same shape, not the App's page. A failure after a stream began is an error event before `[DONE]`. |

## Implementation status

| Item | State |
| --- | --- |
| Chat Completions, models, streaming, continuation, cancellation | Implemented |
| A key that survives a Worker restart | Open: the owner token is minted per Worker process, so a tool reads the token file at call time |
| `/v1/responses`, image input, caller-defined tools | Not built; Hermes serves the Responses API |
| A model request resent mid-stream | Open: after a transport failure the Agent's model request is sent again and the text it had streamed is void; a client cannot take back chunks it has received, so a stream that met a resend repeats the start of the reply |
| Browser callers on another origin | Not built: the owner middleware refuses a foreign Origin and there is no CORS |
