# Messages (Anthropic)

> POST /v1/messages nel formato Anthropic Messages, usato dagli SDK Anthropic e da Claude Code.


Richiesta e risposta di Anthropic Messages sui modelli NotHoly. Gli SDK Anthropic prendono `https://api.notholy.com` come URL base, senza `/v1`. Funzionano sia `x-api-key` sia `Authorization: Bearer`.

```endpoint
POST https://api.notholy.com/v1/messages
```

## Richiesta

| Campo | Tipo | Obbligatorio | Note |
|---|---|---|---|
| `model` | string | Sì | Un ID di modello, per esempio `notholy-specter` |
| `max_tokens` | number | Sì | Maggiore di 0. Limitato all'output massimo del modello |
| `messages` | array | Sì | Turni `user` e `assistant` con blocchi `text`, `image`, `tool_use` e `tool_result`. Sono accettati anche turni `system` tra i messaggi |
| `system` | string o array | No | Solo blocchi di testo |
| `tools` | array | No | I tuoi strumenti con `input_schema`. Gli strumenti lato server, come la ricerca web, vengono saltati |
| `tool_choice` | object | No | `auto`, `any`, `none` o `{"type": "tool", "name": …}` |
| `thinking` | object | No | `{"type": "enabled", "budget_tokens": N}` o `{"type": "adaptive"}` attiva il ragionamento |
| `output_config.effort` | string | No | Livello di ragionamento; prevale su `thinking`. Viene portato al livello più vicino che il modello ha |
| `stop_sequences`, `temperature`, `top_p` | | No | Passati al modello |
| `stream` | boolean | No | `true` restituisce eventi di stream Anthropic |

`POST /v1/messages/count_tokens` accetta lo stesso corpo e restituisce `{"input_tokens": n}`, una stima fatta senza chiamare il modello.

## Risposta senza streaming

```json
{
  "id": "msg_3Jd8a",
  "type": "message",
  "role": "assistant",
  "model": "notholy-specter",
  "content": [
    { "type": "thinking", "thinking": "…", "signature": "nh" },
    { "type": "text", "text": "A Sigma rule is a generic detection rule in YAML…" }
  ],
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": { "input_tokens": 24, "output_tokens": 131, "cache_read_input_tokens": 0, "cache_creation_input_tokens": 0 }
}
```

- `stop_reason` è `end_turn`, `max_tokens` o `tool_use`.
- I blocchi `thinking` che rimandi nella cronologia vengono scartati prima che il modello li veda.
- `cache_control` viene accettato e ignorato.
- Le fonti `document` PDF e file restituiscono `400 unsupported_parameter`. Manda i file tramite la chat o `POST /v1/files`.
- In uno stream, le chiamate di strumenti arrivano intere: un solo `input_json_delta` con gli argomenti completi. Le pause lunghe ricevono eventi `ping`.

## Esempio

```bash test
curl https://api.notholy.com/v1/messages \
  -H "x-api-key: $NOTHOLY_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{"model": "notholy-specter", "max_tokens": 512, "messages": [{"role": "user", "content": "What is a Sigma rule?"}]}'
```

```python test
import os
from anthropic import Anthropic

client = Anthropic(base_url="https://api.notholy.com", api_key=os.environ["NOTHOLY_API_KEY"])

with client.messages.stream(
    model="notholy-specter",
    max_tokens=512,
    messages=[{"role": "user", "content": "Write a Sigma rule title for encoded PowerShell."}],
) as s:
    for text in s.text_stream:
        print(text, end="")
print()
```

Conta i token prima di inviare:

```bash test
curl https://api.notholy.com/v1/messages/count_tokens \
  -H "x-api-key: $NOTHOLY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "notholy-specter", "messages": [{"role": "user", "content": "What is a Sigma rule?"}]}'
```

## Errori

Gli errori usano la forma di Anthropic, `{"type": "error", "error": {"type", "message"}}`: `invalid_request_error` (400) · `authentication_error` (401) · `billing_error` (402) · `permission_error` (403) · `not_found_error` (404) · `rate_limit_error` (429) · `request_too_large` (413) · `api_error` (500, 502, 504) · `overloaded_error` (503). Vedi [Errori](/it/guides/errors).
