Cache dei prompt
NordRouter usa il prompt caching con i provider che lo supportano. I prefissi ripetuti possono essere memorizzati automaticamente o marcati con cache_control; usage mostra i token letti e scritti nella cache.
- Mantieni invariati prompt di sistema e contesto lungo, cambiando l'ultimo messaggio.
- Gestisci la cache automatica dal pannello e usa i marcatori manuali per un controllo preciso.
- La cache riduce il costo del solo input riutilizzato, non dei token di output.
Esempi completi
Tutti gli esempi eseguibili della guida originale sono conservati qui sotto. Esegui solo i blocchi adatti al tuo strumento e sistema operativo.
json
"usage": {
"prompt_tokens": 21364,
"prompt_tokens_details": {
"cached_tokens": 21361 // ← read from cache at ×0.1
},
"completion_tokens": 40
}bash
curl https://nordrouter.com/v1/messages \
-H "x-api-key: sk-nr-YOUR-KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "anthropic/claude-fable-5",
"max_tokens": 512,
"system": [
{
"type": "text",
"text": "You are an assistant for our documentation. Here is the reference: <...lots of text...>",
"cache_control": { "type": "ephemeral" }
}
],
"messages": [{ "role": "user", "content": "First question about the reference?" }]
}'python
from anthropic import Anthropic
client = Anthropic(base_url="https://nordrouter.com", api_key="sk-nr-YOUR-KEY")
resp = client.messages.create(
model="anthropic/claude-fable-5",
max_tokens=512,
system=[{
"type": "text",
"text": "You are an assistant for our documentation. Here is the reference: <...>",
"cache_control": {"type": "ephemeral"}, # cache the reference
}],
messages=[{"role": "user", "content": "First question about the reference?"}],
)
print(resp.usage) # cache_creation_input_tokens on the 1st request, cache_read on the nextDocumentazione ufficiale e collegamenti
| Operation | Multiplier | When it's charged |
|---|---|---|
| Write (cache-write) | ×1.25 | the first time the context enters the cache |
| Read (cache-read) | ×0.1 (−90%) | every repeat of the same context |
| No cache | With cache | |
|---|---|---|
| What's billed | 10 × 30k input | 1 write + 9 reads |
| Input cost | ~$0.46 | ~$0.10 |
Guide correlate
Verifica prima la connessione con una richiesta testuale minima, quindi aggiungi streaming, chiamate agli strumenti, immagini e altre funzioni avanzate. Non inserire mai la chiave API in codice pubblico o nel frontend del browser.