# Nulu API — integration reference for coding agents

You are helping a developer integrate with the Nulu API (Nulu AI). Use only what is stated here; it is the complete public documentation. If something is not covered, say so instead of guessing, and point to https://platform.nuluai.com/docs.

## Essentials
- Base URL: `https://api.nuluai.com/v1`
- Auth header on every request: `Authorization: Bearer <NULU_API_KEY>` (keys look like `sk_wat_...`).
- Keys are created in the dashboard at https://platform.nuluai.com → "API Key". The full secret is shown once. Read it from an environment variable (suggested name `NULU_API_KEY`); never hard-code or log it.
- The API is OpenAI-compatible: use the official OpenAI SDK (or any OpenAI-compatible client) with `baseURL` set to the base URL above. Do not call api.openai.com.
- Default chat model: `nulu-5-ultra`. Model ids are lowercase and case-sensitive; send them exactly as listed. Other model names are rejected (404).
- Prepaid balance: requests need at least $0.50 of balance, otherwise 402.

## Endpoints
| Endpoint | Purpose |
| --- | --- |
| `GET https://api.nuluai.com/v1/models` | List the model ids available to the key. |
| `POST https://api.nuluai.com/v1/chat/completions` | Chat completions (OpenAI format). `stream: true` returns SSE events. |
| `POST https://api.nuluai.com/v1/images/generations` | Image generation, JSON body. |
| `POST https://api.nuluai.com/v1/images/edits` | Image editing, `multipart/form-data` (image + prompt). |
| `GET https://api.nuluai.com/v1/pricing` | Public, no key: current prices and `peak.active_now` / `peak.next_change_at`. |

## Models
- `nulu-5-pro` (Nulu 5 Pro): chat, reasoning, tools, optional thinking. Per 1M tokens: input $1.98, cached input $0.066, output $5.94; peak hours ×2 (input $3.96, cached $0.132, output $11.88).
- `nulu-5-ultra` (Nulu 5 Ultra): chat, vision, reasoning, tools, optional thinking. Per 1M tokens: input $0.45, cached input $0.009, output $1.8; peak hours ×2 (input $0.9, cached $0.018, output $3.6).
- `nulu-image-3`: image generation and editing. $0.0353–$0.0662 per image (generate or edit), independent of size and quality.

## Chat completions
Request fields:
| Field | Required | Notes |
| --- | --- | --- |
| `model` | yes | A public model id from the list above. |
| `messages` | yes | Array of `{ role, content }` (system, user, assistant, tool). |
| `temperature` | no | Randomness, typically 0–2. |
| `top_p` | no | Nucleus sampling; use this or temperature. |
| `max_tokens` | no | Maximum generated tokens. |
| `stream` | no | `true` → OpenAI-compatible SSE. |
| `response_format` | no | `{ "type": "json_object" }` for JSON mode. |
| `reasoning_effort` | no | `low`, `medium`, `high` or `max`. Sending it turns thinking on. |
| `thinking` | no | `{ "type": "enabled" }` or `{ "type": "disabled" }` (in the OpenAI SDK pass it through `extra_body`). |
| `tools` | no | Function tools, on models that list tools. |

Minimal call (JavaScript, OpenAI SDK):
```js
import OpenAI from "openai";

const client = new OpenAI({ apiKey: process.env.NULU_API_KEY, baseURL: "https://api.nuluai.com/v1" });

const response = await client.chat.completions.create({
  model: "nulu-5-ultra",
  messages: [{ role: "user", content: "Say hello" }],
});
console.log(response.choices[0].message.content);
```

Same call with curl:
```bash
curl https://api.nuluai.com/v1/chat/completions \
  -H "Authorization: Bearer $NULU_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"nulu-5-ultra","messages":[{"role":"user","content":"Say hello"}],"max_tokens":500}'
```

Python (OpenAI SDK): `OpenAI(api_key=os.environ["NULU_API_KEY"], base_url="https://api.nuluai.com/v1")`, then `client.chat.completions.create(...)` with the same fields.

### Rules that matter
- **Stateless.** The API keeps no conversation. For multi-turn chat, append the assistant message you received and the new user message to `messages` and send the full history every time.
- **Thinking** is opt-in per request: send `reasoning_effort` or `thinking: { type: "enabled" }`; disable with `thinking: { type: "disabled" }`. Reasoning arrives in `message.reasoning_content`. It has no separate price, but reasoning tokens are billed as output tokens, so it costs more.
- **Thinking + tools:** when a thinking-mode assistant message contains tool calls, send that assistant message back with its `reasoning_content` intact in later turns, or the next request fails.
- **Tool calls** follow the OpenAI function-calling format: pass `tools: [{ type: "function", function: { name, description, parameters } }]`, run the function yourself when the model returns `tool_calls`, then send a `role: "tool"` message with the result.
- **JSON mode:** send `response_format: { "type": "json_object" }`, mention the word JSON in the prompt, show an example of the expected shape, and set `max_tokens` high enough that the output is not cut off.
- **Vision:** `nulu-5-ultra` accepts images in messages (JPEG, PNG, GIF, WebP), billed as input tokens.
- **Context cache** is automatic: a repeated prompt prefix can be billed at the cached-input rate. No code changes; `usage.prompt_tokens_details.cached_tokens` reports the cached part. Keep stable content (system prompt, long documents) at the start of the messages to benefit.
- **Usage:** every response includes `usage` (prompt, completion and cached tokens). Billing is computed from it.

## Images
- Model id: `nulu-image-3` only.
- Generate: `POST https://api.nuluai.com/v1/images/generations`, JSON: `model` (required), `prompt` (required), `size` (optional, e.g. `1024x1024`), `quality` (optional: `low`, `medium`, `high`), `n` (optional, 1–10).
- Edit: `POST https://api.nuluai.com/v1/images/edits`, `multipart/form-data`: `model` (required), `prompt` (required), `image` (required, PNG/JPEG/WebP file), `mask` (optional, transparent PNG), `size`, `n` (optional, 1–10).
- Response is OpenAI-shaped: `data[].url` or `data[].b64_json`.
```bash
curl https://api.nuluai.com/v1/images/generations \
  -H "Authorization: Bearer $NULU_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"nulu-image-3","prompt":"Modern wooden chair in a bright studio","size":"1024x1024","n":1}'
```
With the OpenAI SDK: `client.images.generate({ model: "nulu-image-3", prompt })` and `client.images.edit({ model: "nulu-image-3", image: fs.createReadStream("photo.png"), prompt })`.

## Billing
- Prepaid balance in USD, topped up in the dashboard. Chat is billed per token (input, cached input, output) at the rates in "Models"; images per image.
- **Peak hours:** Monday–Friday 01:00–04:00 and 06:00–10:00 UTC. During them, models marked "peak hours ×2" cost double. Weekends, Chinese public holidays and all other hours are off-peak. `GET https://api.nuluai.com/v1/pricing` tells you whether peak pricing is active right now and when it changes; schedule large batch jobs off-peak when cost matters.
- Estimate cost as `input_tokens × input rate + cached_tokens × cached rate + output_tokens × output rate` (per 1M), doubled in peak hours where it applies.

## Errors
| Status | Meaning | What to do |
| --- | --- | --- |
| 400 | Invalid JSON or invalid/unsupported parameters. | Fix the request; do not retry unchanged. |
| 401 | Missing, invalid or revoked API key. | Check the `Authorization` header and the key. |
| 402 | Insufficient balance (minimum $0.50). | Tell the user to add balance in the dashboard; do not retry. |
| 404 | Model id not available. | Use an id from `GET /models`. |
| 502 | The model request failed upstream. | Retry with exponential backoff (a few attempts). |
Error bodies are JSON: `{ "error": { "message": "..." } }`.

## Using Nulu API from other tools
Any tool that accepts an OpenAI-compatible provider works: set the base URL to `https://api.nuluai.com/v1`, the API key, and one of the model ids above. For opencode, add a provider that uses `@ai-sdk/openai-compatible` with `options.baseURL` and `options.apiKey`, and list the model ids under `models`; reasoning models expose the `low`, `medium`, `high` and `max` variants via `reasoningEffort`.

## Nulu Code (official terminal agent)
- Requires Node.js 18+. Install: `npm install -g nulu-code`; check: `nulu --version`. Lines starting with `npm warn` are not errors. If `nulu` answers "install script did not run" (Windows with recent npm): `npm install -g --allow-scripts=nulu-code nulu-code`.
- Start in a project folder with `nulu`. First run shows a sign-in link: open it, authorize on the platform, paste the one-time code (valid 10 minutes). No API key needed; usage is charged to the same Nulu AI balance.
- Commands: `nulu --continue`, `nulu run "..."`, `nulu login`, `nulu logout`, `nulu models`, `nulu usage`, `nulu doctor`, `nulu update`. Inside a session: `/help`, `/models`, `/new`, `/resume`, `/undo`, `/copy`, `/usage`, `/permissions`, `/language`, `/update`.

## How to work with this reference
1. Use the OpenAI SDK for the user's language with the base URL above unless they ask for raw HTTP.
2. Read the key from the environment; never print it or commit it.
3. Default to `nulu-5-ultra`; pick another id only from the Models list.
4. Handle 402 and 401 with a clear message to the user, and retry only 502 and network errors.
5. Answer the user in the language they write in.
