> ## Documentation Index
> Fetch the complete documentation index at: https://docs.arcium.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Usage and errors

> How blackthorn serve reports token usage, why an answer stopped, and errors, in OpenAI's and Anthropic's APIs.

`blackthorn serve` reports token usage and errors the way each API it serves does, so an OpenAI or Anthropic SDK reads them as it would from OpenAI or Anthropic.

## Token usage

Every answer carries its API's usage object. The part of the prompt the engine already held, from earlier turns of the same conversation, is reported as cached.

| API | Prompt | Of it, cached | Answer | Total |
| - | - | - | - | - |
| Chat Completions | `usage.prompt_tokens` | `usage.prompt_tokens_details.cached_tokens` | `usage.completion_tokens` | `usage.total_tokens` |
| Responses | `usage.input_tokens` | `usage.input_tokens_details.cached_tokens` | `usage.output_tokens` | `usage.total_tokens` |
| Anthropic's Messages | `usage.input_tokens` plus `usage.cache_read_input_tokens` | `usage.cache_read_input_tokens` | `usage.output_tokens` | Not reported |

* In Chat Completions and Responses, the prompt's count includes its cached tokens. In Anthropic's Messages, `input_tokens` counts only the tokens that weren't cached, as Anthropic's API does.
* `cache_creation_input_tokens` and `reasoning_tokens` are always `0`.
* `POST /v1/messages/count_tokens` returns a prompt's exact count as `input_tokens`, without running it.

### In a stream

| API | Where the usage is |
| - | - |
| Chat Completions | In the last chunk, the one with `finish_reason`. With `stream_options: {"include_usage": true}` it comes in a chunk of its own after that one, with empty `choices`, as OpenAI sends it |
| Responses | In the response of the final `response.completed` event |
| Anthropic's Messages | The prompt's tokens in `message_start`, the answer's `output_tokens` in `message_delta` |

The [portal](https://www.arcium.com/blackthorn/portal)'s usage page shows the same three counts for each API key: input, cached input and output.

## Why an answer stopped

| Why | Chat Completions `finish_reason` | Responses | Anthropic's Messages `stop_reason` |
| - | - | - | - |
| The model finished | `stop` | `status` `completed` | `end_turn` |
| The model called a tool | `tool_calls` | `status` `completed`, with the calls in `output` | `tool_use` |
| A stop string matched | `stop` | Not applicable | `stop_sequence`, with the string in `stop_sequence` when the request gave only one |
| The answer reached its token limit, or the context is full | `length` | `status` `incomplete`, with `incomplete_details.reason` `max_output_tokens` | `max_tokens` |
| `serve` ran 10 tool calls of its own in one request | Not applicable | Not applicable | `pause_turn`: send the conversation back as it is to go on |

## Errors

A request that fails gets an HTTP status and an error in its API's own format:

```json Chat Completions theme={null}
{"error": {"message": "prompt is too long: 300000 tokens > 262143 maximum", "type": "invalid_request_error"}}
```

```json Responses theme={null}
{"error": {"message": "...", "type": "...", "param": null, "code": "context_length_exceeded"}}
```

```json Anthropic's Messages theme={null}
{"type": "error", "error": {"type": "invalid_request_error", "message": "..."}}
```

The `message` says what went wrong. The status tells you whether sending the request again can help:

| Status | Meaning | Send it again? | Responses `code` | Messages `type` |
| - | - | - | - | - |
| `400` | The request is invalid, asks for something [the API doesn't support](https://docs.arcium.com/blackthorn/quickstart.md#differences-from-openais-and-anthropics-apis), or its prompt is longer than the context (`prompt is too long`) | No: change the request | `invalid_prompt`, or `context_length_exceeded` for a prompt that is too long | `invalid_request_error` |
| `402` | Your API key's account is out of credits | Not until you top up in the [portal](https://www.arcium.com/blackthorn/portal). The same key works again afterwards | `insufficient_quota` | `billing_error` |
| `404` | No such route | No | `not_found` | `not_found_error` |
| `411`, `413`, `431` | The body has no `Content-Length`, the body is too large, or the headers are too large | No: change the request | `invalid_prompt` | `invalid_request_error`, or `request_too_large` for `413` |
| `502` | `serve` couldn't get an answer from the host or the engine: your API key was refused, the host is busy or unreachable, or the session was lost. The message says which | Yes, unless the message says the key was refused | `server_error` | `api_error` |
| `503` | The engine had no room for the conversation, or `serve` has too many connections open | Yes | `server_is_overloaded` | `overloaded_error` |

The `402` needs `blackthorn` 0.8.2 or newer.

### In a stream

A stream that has already started has sent its `200`, so a failure after that arrives as an event, and the stream ends:

| API | Event |
| - | - |
| Chat Completions | A `data:` line holding `{"error": {"message": ..., "type": ...}}`, then `data: [DONE]` |
| Responses | `response.failed`, whose response has `status` `failed` and an `error` with `code` and `message` |
| Anthropic's Messages | An `error` event with the same body as above |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.