---
title: "How chat turns work"
description: "Hold a conversation with the assistant from code: say something and read its answer, stop a turn, and approve or deny the changes it asks to make. It is the same chat the app shows, and the REST API and the MCP server run it the same way."
url: "https://nextmail.sh/docs/api/chats/overview"
area: "API"
category: "Mailbox"
---

# How chat turns work

Hold a conversation with the assistant from code: say something and read its answer, stop a turn, and approve or deny the changes it asks to make. It is the same chat the app shows, and the REST API and the MCP server run it the same way.

## A turn

`POST /chats/{id}/messages` says something in a chat and answers once the assistant has finished its reply, with one `chat_turn`. An id that names no chat yet starts one, with up to 64 characters.

**curl**

```
curl -X POST "$API/chats/8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65/messages" -H "$AUTH" \
  -H "Content-Type: application/json" \
  -d '{ "content": "Send Ana the September report from billing@acme.com", "messageId": "msg-ana-report" }'
```

**Response**

```
{
  "object": "chat_turn",
  "chatId": "8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65",
  "turnId": "3b7e1c9a-4d2f-4e8b-a6c5-0f9d8e7c6b5a",
  "status": "awaiting_approval",
  "stopReason": null,
  "messages": [
    {
      "object": "chat_message",
      "id": "msg-ana-report",
      "chatId": "8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65",
      "position": 12,
      "role": "user",
      "createdAt": "2026-10-25T09:12:40.000Z",
      "text": "Send Ana the September report from billing@acme.com",
      "parts": [{ "type": "text", "text": "Send Ana the September report from billing@acme.com" }]
    },
    {
      "object": "chat_message",
      "id": "a91f2c4e7b",
      "chatId": "8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65",
      "position": 13,
      "role": "assistant",
      "createdAt": "2026-10-25T09:12:46.000Z",
      "text": "Here is the email. Shall I send it?",
      "parts": [
        { "type": "text", "text": "Here is the email. Shall I send it?" },
        {
          "type": "tool_call",
          "toolCallId": "call_5d1e8b3f",
          "toolName": "sendEmail",
          "args": { "from": "billing@acme.com", "to": [{ "email": "ana@acme.com" }], "subject": "The September report", "message": "…" },
          "result": null,
          "status": "pending_approval"
        }
      ]
    }
  ],
  "pendingToolCalls": [
    {
      "toolCallId": "call_5d1e8b3f",
      "toolName": "sendEmail",
      "args": { "from": "billing@acme.com", "to": [{ "email": "ana@acme.com" }], "subject": "The September report", "message": "…" }
    }
  ],
  "error": null
}
```

> The answer comes back once the turn has ended, so give this call a long timeout. A turn runs for at most 240 seconds. Past that it stops with `stopReason` `time_limit` and keeps what it had written. One turn runs in a chat at a time.

| `status` | What it means |
| --- | --- |
| `completed` | The assistant has answered. |
| `awaiting_approval` | It waits for an answer to each call in `pendingToolCalls`. |
| `stopped` | It was stopped and kept what it had written. `stopReason` says why: `stop_requested` when somebody stopped it, `superseded` when a new message in the app took its place, and `time_limit` when it ran too long. |
| `failed` | It could not finish, and `error` says why. |

- `content` is what you say. `messageId` names your message, and one the chat already holds is a 422 `invalid_parameter`, so a retry with the same id never says it twice.
- `regenerate` true, sent without `content` or `messageId`, answers your last message again and replaces the reply it had.
- `attachments` takes up to 4 files for the assistant to read: `{ "fileId": … }` for a file in Files, or a text file of at most 256 KiB with its content in base64. `threadId` pins a conversation to the chat with its first message, and the assistant reads it before it answers.
- `messages` holds your message and every reply to it, as the chat now holds them. Each save shows straight away in the app, wherever the chat is open.
- A send or a regenerate uses one AI action of the workspace, charged before the turn starts. Answering a tool call uses none.

## One turn at a time

- A call that would start a turn while one runs in the chat, whether that one was started over the API or in the app, is a 409 `chat_busy`, and nothing is saved.
- A new message in the app stops the running turn instead, which then ends `stopped` with `stopReason` `superseded`.
- `POST /chats/{id}/stop` stops the turn that runs. It keeps the finished steps and the text so far, and marks each tool call that had not returned `interrupted`. It waits up to 20 seconds for the turn to end, then answers `stopped` with its `turnId`, and the send that waited on that turn answers `stopped` with `stopReason` `stop_requested`.
- With nothing running, or with a `turnId` that is not the turn that runs, a stop answers `idle` and changes nothing. It is never a 409.

## Approving and denying tool calls

A tool that changes something waits for an answer, as it waits for the person in the app: the turn ends `awaiting_approval` and names each call in `pendingToolCalls`, with its `toolCallId`, `toolName` and `args`. `POST /chats/{id}/tool-calls/{toolCallId}/approve` lets it run, `POST /chats/{id}/tool-calls/{toolCallId}/deny` refuses it, and each answers with the turn once the assistant has carried on.

**curl**

```
curl -X POST "$API/chats/8f3c2a71-5e9d-4b06-a1c4-7d2e9f0b3a65/tool-calls/call_5d1e8b3f/approve" -H "$AUTH" \
  -H "Content-Type: application/json" -d '{ "args": { "subject": "September report" } }'
```

- `args` on an approval is laid over the arguments the assistant chose, so a call can be corrected before it runs. Left out, the call runs as asked.
- With several calls waiting, the turn carries on once every one of them is answered.
- A new message while a call waits is a 409 `approval_pending`. Answer the call first.
- A call you asked for in so many words runs without waiting, as it does in the app, unless its tool always asks, as `sendEmail` does. The server answers those calls itself, for up to 4 rounds in one request.
- A call id that is not waiting is a 404. Approving a call whose tool this key or app may not run in a chat, such as one asked for in the app that needs a scope the key does not hold, is a 403 `tool_not_offered`: nothing runs and the call keeps waiting, so deny it, or approve it in the app.

## The tools a turn may use

A turn over the API runs under the tool policy of task runs, never with the whole role the chat in the app gets.

- Only the tools whose permissions the key or app holds are offered, and for an app a member connected, only those their role holds as well.
- No tool that asks for a verification code is offered, and neither are the chat tools themselves.
- The open thread is read only with `threads:read`, an open template with `templates:read`, attached files with `files:read`, the knowledge base with `knowledge:read` and the tasks of the chat with `tasks:read`.

## Reading the messages

`GET /chats/{id}/messages` reads a chat a page at a time, from its first message, or from its newest with `order` `desc`. `limit` defaults to 20 and goes up to 50, and `cursor` takes the `nextCursor` of the page before.

- Each message has its `position`, a `role` of `user` or `assistant`, its `text`, and its `parts` in order: `text`, `tool_call` with the call, its `args`, `result` and `status`, and `file`.
- A tool call reads `pending_approval` while it waits, `done` once it ran, `denied` when it was refused and `interrupted` when the turn stopped before it returned.
- A chat also holds what tools answered in turns run in the app, with the whole role of the person, so the `result` of a call reads `Redacted: this key or app may not read what this tool answered.` unless the key or app holds every permission its tool needs. The result of a tool that needs none is shown only to a key, or to an app the workspace owner connected. The tool, its arguments and its status stay.
- A tool answer that held a secret, such as an app password or the directory sync token, is not kept in the chat at all. Its `result` reads `Redacted: this answer held a secret, which is shown once, when the tool runs, and is not kept in the chat. If it was not saved, make a new one.`, and the secret in the text of a reply reads `[redacted secret]`.
- The app rewrites the whole chat on every turn, so a cursor whose message moved is a 400 `invalid_cursor`. Start again without one.

## Whose chats, and what refuses a turn

- Reading the messages needs `threads:read`. Sending, stopping, approving and denying need `threads:write`.
- Each person reaches only their own chats: an API key reaches the chats of the workspace owner, and an app reaches the chats of the person who connected it. A key or an app limited to particular addresses or domains is a 422 `capability_unsupported`.
- Every refusal comes before anything is saved. A chat that does not exist is a 404 on every call but a send, which starts it, and so is an id that belongs to a task run. 429 `ai_quota_exceeded` means the workspace has used all of its AI actions for the day, 409 `ai_not_configured` that AI is unavailable, and 503 `assistant_unavailable` that the assistant could not be reached.

## Over MCP

The MCP server has the same five calls as tools: `listChatMessages`, `sendChatMessage`, `stopChatTurn`, `approveChatToolCall` and `denyChatToolCall`. They run the same code, so every rule above holds, and a refusal such as a busy chat comes back as a tool result that starts `Refused (chat_busy):`.

- A turn lists each call it waits for, and an agent should ask the person before it approves one.
- The assistant in the app never has these tools, and the ChatGPT endpoint does not offer them.

- [REST API](https://nextmail.sh/docs/api/reference/chats.md): Every chat operation, with what each takes and returns.
- [TypeScript SDK](https://nextmail.sh/docs/sdk/reference/chats.md): The methods of `chats`, from `sendMessage` to `denyToolCall`.
- [CLI](https://nextmail.sh/docs/cli/reference/chats): The `chats` commands.
- [MCP tools](https://nextmail.sh/docs/mcp/tools/chats.md): The chat tools an agent can call.
