# OpenAI / Anthropic-compatible API

The Task Manager can stand in for the OpenAI or Anthropic API. Point any app, script or tool built for either API at the base URL below and give it a Task Manager API key. Its requests then run on the AI provider chosen for that key (your default provider unless you pick another), whichever vendor that is. A tool written for OpenAI can run on your Claude subscription, and the reverse, with no code changes.

- OpenAI base URL: `http://localhost:3000/api/llm/v1`
- Anthropic base URL: `http://localhost:3000/api/llm`
- Auth: A `tm_live_…` key from **Settings → API**, sent as `Authorization: Bearer …` or `x-api-key: …`.

## Setup

### 1. Create an API key

Go to **Settings → API** and create a key named after the service that will use it. These are the same keys the [REST API](https://aitaskmanager.orangeball.dev/docs/api/) uses. The key is shown once, so copy it straight away. Send it wherever the service expects its OpenAI or Anthropic key. Both `Authorization: Bearer …` and `x-api-key: …` are accepted.

### 2. Check which provider will answer

Each key has a **Provider** setting, chosen when you create it and changeable later in the key list in **Settings → API**, just like a task's AI Provider. **Default provider** runs requests on the same provider a new task would get: if the Smart Provider Picker is on, it chooses; otherwise your default connection in **Settings → AI** answers. **Smart Provider** always picks the best available connection on each request. Picking **one connection** pins the key to it, so different services can run on different accounts. If the chosen connection is disabled, deleted or rate-limited, the request falls back to the default chain and any other enabled connection takes over. The client doesn't need to change.

### 3. Point your client at it

Replace `tm_live_YOUR_KEY` with your key. Swap `http://localhost:3000` for your own deployment's address.

**OpenAI · Python**

```python
from openai import OpenAI

client = OpenAI(base_url="http://localhost:3000/api/llm/v1", api_key="tm_live_YOUR_KEY")

reply = client.chat.completions.create(
    model="default",
    messages=[{"role": "user", "content": "Summarise the plot of Hamlet in one line."}],
)
print(reply.choices[0].message.content)
```

**OpenAI · TypeScript**

```ts
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "http://localhost:3000/api/llm/v1", apiKey: "tm_live_YOUR_KEY" });

const stream = await client.chat.completions.create({
  model: "default",
  stream: true,
  messages: [{ role: "user", content: "Write a haiku about queues." }],
});
for await (const chunk of stream) process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
```

**Anthropic · Python**

```python
import anthropic

client = anthropic.Anthropic(base_url="http://localhost:3000/api/llm", api_key="tm_live_YOUR_KEY")

message = client.messages.create(
    model="default",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Summarise the plot of Hamlet in one line."}],
)
print(message.content[-1].text)
```

**Anthropic · TypeScript**

```ts
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({ baseURL: "http://localhost:3000/api/llm", apiKey: "tm_live_YOUR_KEY" });

const stream = client.messages.stream({
  model: "default",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Write a haiku about queues." }],
});
stream.on("text", (text) => process.stdout.write(text));
await stream.finalMessage();
```

**cURL**

```bash
# OpenAI format
curl http://localhost:3000/api/llm/v1/chat/completions \
  -H "Authorization: Bearer tm_live_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"default","messages":[{"role":"user","content":"Hello"}]}'

# Anthropic format
curl http://localhost:3000/api/llm/v1/messages \
  -H "x-api-key: tm_live_YOUR_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{"model":"default","max_tokens":1024,"messages":[{"role":"user","content":"Hello"}]}'
```

**Environment variables**

```bash
# Many tools and SDKs read these instead of taking a base URL in code.
export OPENAI_BASE_URL="http://localhost:3000/api/llm/v1"
export OPENAI_API_KEY="tm_live_YOUR_KEY"

export ANTHROPIC_BASE_URL="http://localhost:3000/api/llm"
export ANTHROPIC_API_KEY="tm_live_YOUR_KEY"
```

- The OpenAI base URL ends in `/v1` and the Anthropic one doesn't, because the Anthropic SDKs add `/v1/messages` themselves.
- The client must be able to reach this server. Use HTTPS when it connects across a network, since the key travels in a header.

## Choosing a model

Clients keep sending the model name they were built for. The gateway decides whether to use it:

| Model sent | What runs |
| --- | --- |
| `default` | The provider's configured model (also for `auto`, or no model at all). |
| A model from the provider's vendor | Used as sent, e.g. `claude-haiku-4-5` on a Claude provider or `gpt-5.5` on Codex. |
| A model from the other vendor | Replaced by the provider's configured model, so an app hard-wired to `gpt-4o` still works on Claude. |
| An unrecognised name | Used as sent on OpenAI-compatible connections and connections with a custom base URL (self-hosted models, gateway aliases). Otherwise the provider's model is used. |

Where the provider reports it, the `model` field in each response names the model that actually ran.

## What works on each provider

When the request format matches the provider's own API, it is passed straight through. Otherwise it is translated, in both directions, including streaming.

| Provider type | Streaming | Tool calling | Images / PDFs | Notes |
| --- | --- | --- | --- | --- |
| Anthropic API / Claude PKI | Yes | Yes | Yes | Anthropic-format requests pass through unchanged, including `anthropic-beta` headers. |
| OpenAI-compatible | Yes | Yes | Yes | OpenAI-format requests pass through unchanged. |
| Claude Code (subscription) | Yes, token by token (in one chunk when your own tools are sent) | Yes, emulated, plus web search/fetch and MCP | Yes | Runs `claude -p`. See "Subscription CLIs" below. |
| Codex (subscription) | Sent in one chunk at the end | Yes, emulated, plus MCP | No | Runs `codex exec` read-only. No web search/fetch or images. |

### Translation notes

- **OpenAI → Claude:** `temperature` and `top_p` are dropped, because current Claude models reject them. `tool_choice: "required"` and a named function become `auto` for the same reason. `max_tokens` is raised to at least 8,192, because Claude spends part of the limit thinking before it answers. `response_format` becomes a JSON instruction in the system prompt, and `reasoning_effort` maps to effort. `n` greater than 1 isn't supported.
- **Claude → OpenAI clients:** thinking blocks are left out, because the OpenAI format has no place for them. Cached input tokens are reported in `prompt_tokens_details.cached_tokens`.
- **Subscription CLIs:** a conversation with more than one turn is passed to the CLI as a transcript. It must end with a user message. Images and PDFs are passed to the Claude CLI as real attachments. The `web_search` and `web_fetch` server tools run on the Claude CLI's own WebSearch and WebFetch tools (`allowed_domains`, `blocked_domains` and `max_uses` are passed on as instructions), and the reply comes back as text with the sources cited, not as `server_tool_use` blocks. Note that web fetch requests come from the Task Manager's host. Every enabled server in Settings → MCP Servers is attached to each request, as for task runs, and the model can call its tools by itself. Other server tools, such as code execution, get a 400.
- **Tool calling on subscription CLIs:** the CLIs can't take a tool list, so tools are emulated. Your tool definitions go into the system prompt, the model writes each call in a set format, and the gateway turns it into a normal `tool_use` block (Anthropic) or `tool_calls` entry (OpenAI) with the matching stop reason. Your client runs the tool and sends the result back as usual. When tools are sent, the reply streams in one chunk at the end, because the whole reply has to be read before the calls can be found. `tool_choice` is followed, but only through the prompt, so it's less reliable than on the API. A call to a tool you didn't define, or with malformed arguments, comes back as text.

## Endpoints

| Endpoint | Format | Description |
| --- | --- | --- |
| `POST /api/llm/v1/chat/completions` | OpenAI | Chat Completions. Set `stream: true` for SSE chunks ending in `[DONE]`; `stream_options.include_usage` adds a final usage chunk. |
| `POST /api/llm/v1/messages` | Anthropic | Messages API, streaming or not. Streams use the standard event types (`message_start`, `content_block_delta`, …). |
| `POST /api/llm/v1/messages/count_tokens` | Anthropic | Exact on Anthropic API providers. An estimate (about 4 characters per token) on other providers. |
| `GET /api/llm/v1/models` | Both | The models the current provider can run, starting with `default`. Each entry has the fields both SDKs read. |

## Errors

Errors use the same shape as the API the client called, so SDKs raise their usual exception types. Errors from an upstream API keep their original status. If an error happens after a stream has started, it arrives as an error event in the stream.

| Status | Meaning |
| --- | --- |
| 400 | The request can't run on this provider, e.g. code execution sent to a subscription CLI, images sent to Codex, or a conversation that doesn't end with a user message. |
| 401 | Missing or invalid API key. MCP tokens (`tm_mcp_…`) don't work here. |
| 429 | The provider is rate-limited or out of plan usage, and no other provider is available. Retry later. |
| 502 | The provider failed to answer (the CLI crashed, a network error, …). |
| 503 | No provider is configured or enabled, the CLI isn't installed, or its login has expired — or the gateway has been temporarily disabled (the message says until when, and timed pauses send `Retry-After`). |

## Good to know

- Requests use the same subscription or API account as your tasks. Heavy use here leaves less capacity for task runs, and the reverse.
- Requests are not tasks. They don't appear on the board, aren't logged, and never touch a repository.
- Each request's token usage (not its content) is recorded per API key. **Analytics → API Proxy** shows when the gateway spends tokens (hourly for the last day, daily, and by hour of day), how many, and the estimated cost by model, API key and request; subscription usage is also priced at API rates there so you can see what it would cost. **Analytics** compares it with what task runs spend, under "Token Spend by Source".
- You can disable the gateway temporarily — for 15 minutes up to 7 days, or until you turn it back on — from **Analytics → API Proxy** or **Settings → API**. While it's disabled every request with a valid key gets a 503; a timed pause ends on its own.
- The key's "Last used" time in Settings → API updates on every request, which is an easy way to confirm a service is connected. Use one key per service so you can revoke one without breaking the others.
- Requests to a subscription CLI start a CLI process, which adds a few seconds before the first token.
