Caching prompt
CNEUMae'r modelau open-weight a gynhelir yn storio rhagddodiaid prompt sy'n ailadrodd yn awtomatig. Pan fydd cais yn dechrau gyda'r un prompt system, offer a negeseuon blaenorol â chais diweddar ar yr un model, caiff y rhagddodiad a rennir ei ddarllen o'r storfa a'i godi ar 25% o bris mewnbwn y model (10% ar fodelau Heavy Duty). Does dim i'w alluogi, ac mae ysgrifennu i'r storfa am ddim.
Sut mae'n gweithio
- Prefix, yn ddyfyn — Mae'r prompt yn cael ei darllen yn ddyfyn: prompt system, diffiniadau tool,na yna'r negeseion. Mae'r cache yn cyfateb o ddechrau'r ddyfyn hwn hyd’r token cyntaf sy'n wahanol.
- Beth sy'n g counts as a hit — Cais y mae'n dechrau prompt ei wegesydd gyda'r un cynnwys â cais recent — fel arfer tro blaen o'r un conversations gyda negeseion newydd wedi'u dechrau. Mae'r prefix sy'n cyfateb yn mewnfa cached; mae popeth wedy hyn yn mewnfa regylair.
- Granularia — Mae'r cache yn dal prompt mewn blociau o 1,568 token, felly ni chaiff prompt sy'n fyrrach na thua 1,500 token ei gacheu. Cyfrif mewnbwn yw'r cyfrif cache mewn ateb wedi'i luosi â chyfran gacheu y prompt, wedi'i dalgrynnu i lawr. Nid yw o reidrwydd yn lluosrif o faint y bloc.
- Heb hit — Bilir cais nad yw ei ddechrau yn y cache ar y gyfradd fewnbwn arferol. Ni chyhoeddir oes ar gyfer promptau wedi'u cacheu ac nid yw hit wedi'i warantu: darllenwch
usagei weld beth gymerodd cais o'r cache. - Dim switsh — Nid yw cais yn optio i mewn, ac nid oes maes sy'n diffodd caching.
- Pa modelau — Pob id open-weight hosted. Mae GET /v1/models yn adrodd capabilities.prompt_caching: true a pricing.cached_input_per_million_usd ar gyfer nhw. Mae modelau Shannon yn codi toll unigol ar gyfradd fflat.
Gweld cache hit mewn ateb
Anfonwch ddau gais sy'n dechrau gyda'r un prompt system hir ac argraffwch ddefnydd pob un. Y rhif cyntaf yw mewnbwn y cais, yr ail yw'r rhan ohono a ddarllenwyd o'r cache.
from openai import OpenAI
client = OpenAI(api_key="YOUR_API_KEY", base_url="https://api.shannon-ai.com/v1")
handbook = open("handbook.txt").read() # a long text that stays the same
def ask(question):
response = client.chat.completions.create(
model="Kimi-K3-uncensored-3BIT-REAP",
messages=[
{"role": "system", "content": handbook},
{"role": "user", "content": question},
],
)
usage = response.usage
print(usage.prompt_tokens, usage.prompt_tokens_details.cached_tokens)
ask("What is the refund policy?")
ask("Who approves travel?") # same start: read the second number import { readFileSync } from "node:fs";
import OpenAI from "openai";
const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.shannon-ai.com/v1" });
const handbook = readFileSync("handbook.txt", "utf8"); // a long text that stays the same
async function ask(question) {
const response = await client.chat.completions.create({
model: "Kimi-K3-uncensored-3BIT-REAP",
messages: [
{ role: "system", content: handbook },
{ role: "user", content: question },
],
});
const usage = response.usage;
console.log(usage.prompt_tokens, usage.prompt_tokens_details.cached_tokens);
}
await ask("What is the refund policy?");
await ask("Who approves travel?"); // same start: read the second number # handbook.txt is a long text that stays the same. jq builds the JSON body from it
# and prints the usage object of the reply. Run it twice with different questions.
jq -Rs '{
model: "Kimi-K3-uncensored-3BIT-REAP",
messages: [
{role: "system", content: .},
{role: "user", content: "What is the refund policy?"}
]
}' handbook.txt \
| curl -s https://api.shannon-ai.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d @- \
| jq .usage Prisiau
Codir tocynnau mewnbwn wedi'u storio ar 25% o gyfradd mewnbwn y model (10% ar fodelau Heavy Duty), wedi'i dalgrynnu i $0.001 fesul 1M. Nid yw ysgrifennu i'r storfa yn costio dim ychwanegol, a chodir am allbwn fel arfer. Mae cyfradd wedi'i storio pob id yn y tabl Modelau a phrisiau. Modelau a phrisiau
Codir am fewnbwn galwad fel (mewnbwn − wedi'i gacheu) × cyfradd mewnbwn + wedi'i gacheu × cyfradd cache. Nid yw'r cyfrif cache byth yn fwy na'r cyfrif mewnbwn.
| Model | Mewnbwn / 1M | Mewnbwn wedi'i gacheu / 1M |
|---|---|---|
DeepSeek-V4-Pro-0813-uncensored-3BIT-REAP | $1.76 | $0.44 |
GLM-5.2-uncensored-3BIT-REAP | $1.47 | $0.368 |
Kimi-K3-uncensored-3BIT-REAP | $3.67 | $0.918 |
Nemotron3Ultra-uncensored-3BIT-REAP | $0.86 | $0.215 |
MiniMax-M3-uncensored-3BIT-REAP | $0.62 | $0.155 |
DeepSeek-V4-Flash-0731-uncensored-W4A16-AUTOROUND-REAP | $0.44 | $0.11 |
Kimi-K2.6-uncensored-W4A16-AUTOROUND-REAP | $1.11 | $0.278 |
Laguna-S-2.1-uncensored-W4A16-AUTOROUND-REAP | $0.41 | $0.103 |
inkling-uncensored-W4A16-AUTOROUND-REAP | $1.65 | $0.413 |
MiMo-V2.5-Pro-uncensored-W4A4 | $0.79 | $0.198 |
MiMo-V2.5-uncensored-W4A4 | $0.45 | $0.113 |
Hy3-uncensored-W4A4 | $0.44 | $0.11 |
DeepSeek-V4.1-Flash-1017-uncensored-W4A4 | $0.46 | $0.115 |
GLM-5.3-uncensored-W4A4 | $1.63 | $0.408 |
GLM-5.3-Flash-uncensored-W4A4 | $0.44 | $0.11 |
MiMo-V2.6-Pro-uncensored-W4A4 | $0.79 | $0.198 |
MiMo-V2.6-Flash-uncensored-W4A4 | $0.45 | $0.113 |
MiniMax-M3.1-Flash-uncensored-W4A4 | $0.46 | $0.115 |
Qwen3.8-2.4T-uncensored-W4A4 | $2.55 | $0.638 |
Qwen3.8-Flash-uncensored-W4A4 | $0.46 | $0.115 |
Hunyuan-4-Preview-uncensored-W4A4 | $1.23 | $0.308 |
Step-5-Preview-uncensored-W4A4 | $1.42 | $0.355 |
LongCat-2.0-uncensored-W4A4 | $1.14 | $0.285 |
Ling-3.1-Flash-uncensored-W4A4 | $0.38 | $0.095 |
Nemotron-3.5-Super-uncensored-W4A4 | $0.46 | $0.115 |
Muse-Spark-Open-uncensored-W4A4 | $1.70 | $0.425 |
Beam-1.0-uncensored-W4A4 | $0.75 | $0.188 |
Nex-N2.5-Pro-uncensored-W4A4 | $0.38 | $0.095 |
Kimi-K3.1-Pro-uncensored-NVFP4 | $4.02 | $0.402 |
GLM-5.3-uncensored-FP8 | $6.22 | $0.622 |
DeepSeek-V4.1-Flash-1017-uncensored-FP8 | $2.08 | $0.208 |
Kimi-K3-uncensored-NVFP4 | $5.15 | $0.515 |
MiMo-V2.6-Pro-uncensored-NVFP4 | $2.26 | $0.226 |
Qwen3.8-2.4T-uncensored-NVFP4 | $4.02 | $0.402 |
LongCat-2.0-uncensored-NVFP4 | $2.61 | $0.261 |
Mae'r log defnydd yn rhestru mewnbwn wedi'i gacheu pob galwad. Mae'r tokenau a filiwyd a'r gost eisoes yn cynnwys y gyfradd cache. Allweddi a defnydd
Meysydd defnydd
| Pwyntter | Mewnfa cached | Rhesoniad |
|---|---|---|
/v1/chat/completions | usage.prompt_tokens_details.cached_tokens — rhan o prompt_tokens | usage.completion_tokens_details.reasoning_tokens — rhan o completion_tokens |
/v1/responses | usage.input_tokens_details.cached_tokens — rhan o input_tokens | usage.output_tokens_details.reasoning_tokens — rhan o output_tokens |
/v1/messages | usage.cache_read_input_tokens — adroddedig ar wahanbol: input_tokens yw'r rhan heb cache; cache_creation_input_tokens yw bob amser 0 | cael meddwl yn cael ei nifoethu mewn output_tokens |
{
"usage": {
"prompt_tokens": 20000,
"completion_tokens": 812,
"total_tokens": 20812,
"prompt_tokens_details": {
"cached_tokens": 18000
},
"completion_tokens_details": {
"reasoning_tokens": 604
}
}
} {
"usage": {
"input_tokens": 20000,
"input_tokens_details": {
"cached_tokens": 18000
},
"output_tokens": 812,
"output_tokens_details": {
"reasoning_tokens": 604
},
"total_tokens": 20812
}
} {
"usage": {
"input_tokens": 2000,
"cache_read_input_tokens": 18000,
"cache_creation_input_tokens": 0,
"output_tokens": 812
}
} Mae ateb wedi'i ffrydio yn cario'r un meysydd yn ei ddefnydd terfynol. Does dim rhaid i chi ofyn amdano:
| Pwyntter | Lle mae'r defnydd yn cyrraedd |
|---|---|
/v1/chat/completions | usage ar y talp olaf cyn data: [DONE]. Fe'i hanfonir ar bob ffrwd. |
/v1/responses | response.usage y digwyddiad response.completed. |
/v1/messages | usage y digwyddiad message_delta. Mae usage message_start yn cynnwys sero. |
Cael mwy o cache hits
- Cadwch i'r prompt system a diffiniadau tool fod yn sefydlog byte-am-byte rhwng galwadau. Rhowch gwerthoedd per-galwad fel timestampiau neu id-iau cais ar ddiwedd y neges ddiweddar, nid yn y prompt system.
- Ymunwch yn unig â'r hanygof. Mae golygu, trimio neu crynodeb troeddau cyn ownhau yn newid y prefix, ac mae popeth wedy'r newid cyntaf yn cael tollu fel mewnfa regylair.
- Peidiwch a ail-drefnu toolau, negeseion neu blociau cynnwys rhwng galwadau, a serialise JSON (sceamas tool, argudion tool a canlyniadau) yn yr un ffordd bob tro.
- Arhoswch ar un id model drwy sgwrs, ac anfonwch yr alwad nesaf yn fuan ar ôl yr un o'i blaen.
Mae'r API yn cadw dechrau sgwrs yn sefydlog yn yr achosion hyn:
- Mae neges
systemneudevelopera anfonir yn hwyrach mewn sgwrs yn aros yn ei lle. Nid yw'n newid dechrau'r prompt, felly mae'r tro cyn hynny yn aros yn y cache. - Cymharir dadleuon galwadau offer mewn troeon asiant cynharach yn ôl gwerth. Nid yw trefn allweddi a bylchu'r JSON hwnnw o bwys.
- Mae'r tri phwynt terfyn yn darllen sgwrs yn yr un ffordd. Mae sgwrs a barheir ar bwynt terfyn arall yn cadw ei rhagddodiad a rennir pan fo'r cynnwys yr un fath.
Meysydd goestyn
Mae prompt_cache_key (Chat Completions a Responses) a cache_control ar flociau cynnwys Messages yn cael eu derbyn, felly mae cod clechsiennau presennol yn rhedeg heb nowysgau. Nid yw unrhyw un ohonyn nhw'n gofyn: mae caching yn awtomatig ac yn gweithio'r un fath heb nhw.
| Maes | Anfonir i | Beth ydyw |
|---|---|---|
prompt_cache_key | /v1/chat/completions, /v1/responses | Allwedd llwybro cache o'r API OpenAI. |
cache_control | /v1/messages | Toriad cache ar floc cynnwys, bloc system neu neges o'r API Anthropic. |
stream_options | /v1/chat/completions | Mae include_usage yn gofyn i'r API OpenAI am y defnydd ar ffrwd. Yma mae pob ffrwd yn gorffen gyda'r defnydd. |
Cyfrif tokens
Mae dau bwynt terfyn rhad ac am ddim, POST /v1/tokenize a POST /v1/messages/count_tokens, yn cyfrif tokenau testun neu gais cyfan ar gyfer y modelau pwysau agored a gynhelir cyn i chi ei anfon. Mae ganddynt eu tudalen eu hunain: Cyfrif tokenau
