# OpenAI-compatible API

1. **Your tool** any OpenAI client, the Web token as its API key
2. **Worker** `http://127.0.0.1:18100/v1`, model: an Agent's id
3. **Agent turn** stored as a conversation; answered whole or streamed

## Connect a tool

| Setting | Value |
| --- | --- |
| Base URL | `http://127.0.0.1:18100/v1`, or `localhost`; the Worker's own port if you changed it |
| API key | The Web token in `~/.openprogram/web/token` (`~/.openprogram-<profile>/web/token` for a profile). A new one is made each time the Worker starts, so read the file at call time. |
| Model | An Agent's id, as `GET /v1/models` lists them, the default Agent first |

```
curl http://127.0.0.1:18100/v1/chat/completions \
  -H "Authorization: Bearer $(cat ~/.openprogram/web/token)" \
  -H "Content-Type: application/json" \
  -d '{"model": "main", "messages": [{"role": "user", "content": "Summarise README.md"}]}'
```

```
from pathlib import Path
from openai import OpenAI

client = OpenAI(base_url="http://127.0.0.1:18100/v1",
                api_key=(Path.home() / ".openprogram/web/token").read_text())
for chunk in client.chat.completions.create(
        model="main", stream=True,
        messages=[{"role": "user", "content": "What changed today?"}]):
    print(chunk.choices[0].delta.content or "", end="")
```

## What a request does

| Part | Behaviour |
| --- | --- |
| Turn | One turn of the Agent, as if typed in the App: its model, tools, memory and permission mode. It shows in the App while it runs and stays there as a conversation. |
| Messages | The last message must be the user's; it is the turn's message. Earlier messages, including `system`, go with it as the calling tool's context. Content is text, or a list of text parts. |
| Conversation | Each request starts a new one. To add a turn to an earlier one instead, send its id in `conversation_id`; then only the last message is sent. Every answer names its conversation in `conversation_id` and the `X-OpenProgram-Conversation-Id` header. |
| Streaming | `"stream": true` sends the reply text as `chat.completion.chunk` events, then a chunk with `finish_reason`, then `data: [DONE]`. `stream_options.include_usage` adds a usage chunk. |
| Approvals | No one is there to answer, so a step that would ask for approval or ask you a question is refused at once, as with `openprogram --print`. |
| Stopping | Closing a streamed request stops its turn. |
| Ignored | `temperature`, `max_tokens`, `tools` and other sampling or function-calling fields: the Agent's own settings and tools apply. `n` other than 1 is refused. |

## Errors

Every refusal has OpenAI's body, `{"error": {"message", "type", "param", "code"}}`.

| Status | When |
| --- | --- |
| 401 `invalid_api_key` | No token, or a token from an earlier Worker: read the token file again |
| 400 | A malformed request; `param` names the field |
| 404 `model_not_found` | No Agent has that id |
| 404 `unknown_url` | A `/v1` endpoint this server does not serve, such as `/v1/responses` |
| 409 `conversation_busy` | The named conversation is running a turn; send again when it ends |
| 429, 502 | The Agent's model failed; `code` carries the reason. In a stream the error is the last event before `[DONE]`. |

## Scope

- The token is the owner's: whoever holds it acts as you. Keep the Worker on loopback unless you set up [access from other machines](https://fzkuji.github.io/OpenProgram/docs/server/configuration.html#who-can-reach-the-server).
- Callers outside a browser only. A web page on another origin is refused.
- Chat Completions only; no `/v1/responses`, embeddings or images.

[Editor (ACP)](https://fzkuji.github.io/OpenProgram/docs/interfaces/acp.html)
