ການ Cache Prompt
ອັດຕະໂນມັດHosted open-weight model ຈະ cache prefix ຂອງ prompt ທີ່ຊ້ຳກັນໂດຍອັດຕະໂນມັດ. ເມື່ອ request ເລີ່ມດ້ວຍ system prompt, tools ແລະ ຂໍ້ຄວາມກ່ອນໜ້າຄືກັນກັບ request ຫຼ້າສຸດໃນ model ດຽວກັນ, prefix ທີ່ໃຊ້ຮ່ວມກັນນັ້ນຈະຖືກອ່ານຈາກ cache ແລະ ຄິດໄລ່ 25% ຂອງລາຄາ input ຂອງ model (10% ໃນ model Heavy Duty). ບໍ່ຕ້ອງເປີດໃຊ້ຫຍັງ, ແລະ ການຂຽນລົງ cache ແມ່ນບໍ່ເສຍຄ່າ.
ຫຼັກການເຮັດວຽກ
- Prefix, ຕາມລຳດັບ — Prompt ຈະຖືກອ່ານຕາມລຳດັບ: system prompt, tool definitions, ຈາກນັ້ນຈຶ່ງເປັນຂໍ້ຄວາມ. Cache ຈະກົງກັນນັບຈາກຈຸດເລີ່ມຕົ້ນຂອງລຳດັບນັ້ນ ຈົນຮອດ token ທຳອິດທີ່ມີການປ່ຽນແປງ.
- ສິ່ງທີ່ນັບວ່າເປັນ hit — Request ທີ່ prompt ເລີ່ມຕົ້ນດ້ວຍເນື້ອຫາທີ່ຄືກັນກັບ request ຫຼ້າສຸດ — ໂດຍທົ່ວໄປແມ່ນການສົນທະນາຮອບກ່ອນໜ້ານັ້ນ ທີ່ມີຂໍ້ຄວາມໃໝ່ຖືກເພີ່ມເຂົ້າໄປ. Prefix ທີ່ກົງກັນແມ່ນ cached input; ສ່ວນທີ່ເຫຼືອຫຼັງຈາກນັ້ນແມ່ນ regular input.
- ລະດັບຄວາມລະອຽດ — cache ເກັບ prompt ເປັນ block ລະ 1,568 tokens, ດັ່ງນັ້ນ prompt ທີ່ສັ້ນກວ່າປະມານ 1,500 tokens ຈະບໍ່ຖືກ cache. ຈຳນວນ cached ໃນຄຳຕອບແມ່ນຈຳນວນ input ຂອງທ່ານຄູນດ້ວຍສ່ວນທີ່ຖືກ cache ຂອງ prompt, ປັດລົງ. ມັນບໍ່ຈຳເປັນຕ້ອງເປັນຈຳນວນເທົ່າຂອງຂະໜາດ block.
- ເມື່ອບໍ່ມີ hit — ຄຳຮ້ອງຂໍທີ່ສ່ວນເລີ່ມຕົ້ນບໍ່ຢູ່ໃນ cache ຖືກຄິດເງິນຕາມອັດຕາ input ປົກກະຕິ. ບໍ່ມີການປະກາດອາຍຸຂອງ prompt ທີ່ຖືກ cache ແລະ ບໍ່ຮັບປະກັນ hit: ອ່ານ
usageເພື່ອເບິ່ງວ່າຄຳຮ້ອງຂໍໃຊ້ຫຍັງຈາກ cache. - ບໍ່ມີສະວິດ — ຄຳຮ້ອງຂໍບໍ່ຕ້ອງເລືອກເຂົ້າ, ແລະ ບໍ່ມີຟີວໃດປິດ caching.
- Model ທີ່ຮອງຮັບ — ທຸກ hosted open-weight id. GET /v1/models ຈະລາຍງານ capabilities.prompt_caching: true ແລະ pricing.cached_input_per_million_usd ສໍາລັບ model ເຫຼົ່ານັ້ນ. Shannon models ຈະຄິດໄລ່ໃນອັດຕາຄົງທີ່.
ເບິ່ງ cache hit ໃນຄຳຕອບ
ສົ່ງສອງຄຳຮ້ອງຂໍທີ່ເລີ່ມດ້ວຍ system prompt ຍາວອັນດຽວກັນ ແລະ ພິມ usage ຂອງແຕ່ລະອັນ. ເລກທຳອິດແມ່ນ input ຂອງຄຳຮ້ອງຂໍ, ເລກທີສອງແມ່ນສ່ວນທີ່ອ່ານຈາກ 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 ລາຄາ
Cached input tokens ຈະຖືກຄິດໄລ່ 25% ຂອງອັດຕາ input ຂອງ model (10% ໃນ model Heavy Duty), ປັດເປັນ $0.001 ຕໍ່ 1M. ການຂຽນລົງ cache ບໍ່ມີຄ່າໃຊ້ຈ່າຍເພີ່ມເຕີມ, ແລະ output ຈະຖືກຄິດໄລ່ຕາມປົກກະຕິ. ອັດຕາ cached ຂອງແຕ່ລະ id ຢູ່ໃນຕາຕະລາງ Models & pricing. Model ແລະ ລາຄາ
input ຂອງການເອີ້ນຄັ້ງໜຶ່ງຖືກຄິດເງິນເປັນ (input − cached) × ອັດຕາ input + cached × ອັດຕາ cached. ຈຳນວນ cached ບໍ່ເຄີຍໃຫຍ່ກວ່າຈຳນວນ input.
| Model | Input / 1M | Input ທີ່ cache / 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 |
usage log ລະບຸ cached input ຂອງແຕ່ລະການເອີ້ນ. tokens ທີ່ຖືກຄິດເງິນ ແລະ ຄ່າໃຊ້ຈ່າຍຂອງມັນລວມອັດຕາ cached ແລ້ວ. ກະແຈ ແລະ ການໃຊ້ງານ
ຊ່ອງຂໍ້ມູນການນຳໃຊ້
| Endpoint | ຂໍ້ມູນ 입력 ທີ່ຖືກ Cache | ການຫາເຫດຜົນ (Reasoning) |
|---|---|---|
/v1/chat/completions | usage.prompt_tokens_details.cached_tokens — ສ່ວນໜຶ່ງຂອງ prompt_tokens | usage.completion_tokens_details.reasoning_tokens — ສ່ວນໜຶ່ງຂອງ completion_tokens |
/v1/responses | usage.input_tokens_details.cached_tokens — ສ່ວນໜຶ່ງຂອງ input_tokens | usage.output_tokens_details.reasoning_tokens — ສ່ວນໜຶ່ງຂອງ output_tokens |
/v1/messages | usage.cache_read_input_tokens — ລາຍງານແຍກກັນ: input_tokens ແມ່ນສ່ວນທີ່ບໍ່ໄດ້ cache; cache_creation_input_tokens ຈະເປັນ 0 ສະເໝີ | thinking ຖືກນັບລວມໃນ 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
}
} ຄຳຕອບແບບ stream ມີຟີວດຽວກັນໃນ usage ສຸດທ້າຍ. ທ່ານບໍ່ຕ້ອງຂໍມັນ:
| Endpoint | usage ມາຮອດບ່ອນໃດ |
|---|---|
/v1/chat/completions | usage ໃນ chunk ສຸດທ້າຍກ່ອນ data: [DONE]. ສົ່ງໃນທຸກ stream. |
/v1/responses | response.usage ຂອງ event response.completed. |
/v1/messages | usage ຂອງ event message_delta. usage ຂອງ message_start ເປັນສູນ. |
ວິທີເພີ່ມ cache hits
- ຮັກສາ system prompt ແລະ tool definitions ໃຫ້ຄົງທີ່ (stable) ລະຫວ່າງການເອີ້ນໃຊ້. ວາງຄ່າທີ່ປ່ຽນແປງໃນແຕ່ລະ call ເຊັ່ນ timestamps ຫຼື request ids ໄວ້ທີ່ຕອນທ້າຍຂອງຂໍ້ຄວາມຫຼ້າສຸດ, ບໍ່ແມ່ນໃນ system prompt.
- ໃຫ້ເພີ່ມຂໍ້ມູນ (append) ຕໍ່ທ້າຍ history ເທົ່ານັ້ນ. ການແກ້ໄຂ, ຕັດ ຫຼື ສະຫຼຸບຂໍ້ຄວາມໃນຮອບກ່ອນໜ້ານັ້ນ ຈະເຮັດໃຫ້ prefix ປ່ຽນແປງ, ແລະ ທຸກຢ່າງຫຼັງຈາກຈຸດທີ່ປ່ຽນແປງຈະຖືກຄິດໄລ່ເປັນ regular input.
- ຢ່າປ່ຽນລຳດັບຂອງ tools, ຂໍ້ຄວາມ ຫຼື content blocks ລະຫວ່າງການເອີ້ນໃຊ້, ແລະ ຈັດຮູບແບບ JSON (tool schemas, tool arguments ແລະ results) ໃຫ້ຄືກັນທຸກຄັ້ງ.
- ໃຊ້ model id ດຽວຕະຫຼອດບົດສົນທະນາ, ແລະ ສົ່ງການເອີ້ນຕໍ່ໄປໃຫ້ໄວຫຼັງຈາກການເອີ້ນກ່ອນໜ້າ.
API ຮັກສາສ່ວນເລີ່ມຕົ້ນຂອງບົດສົນທະນາໃຫ້ຄົງທີ່ໃນກໍລະນີເຫຼົ່ານີ້:
- ຂໍ້ຄວາມ
systemຫຼືdeveloperທີ່ສົ່ງຕອນຫຼັງໃນບົດສົນທະນາຢູ່ບ່ອນເດີມຂອງມັນ. ມັນບໍ່ປ່ຽນສ່ວນເລີ່ມຕົ້ນຂອງ prompt, ດັ່ງນັ້ນ turn ກ່ອນມັນຍັງຖືກ cache. - arguments ຂອງການເອີ້ນ tool ໃນ assistant turn ກ່ອນໜ້າຖືກປຽບທຽບດ້ວຍຄ່າ. ລຳດັບ key ແລະ ການເວັ້ນວັກຂອງ JSON ນັ້ນບໍ່ສຳຄັນ.
- ທັງສາມ endpoint ອ່ານບົດສົນທະນາແບບດຽວກັນ. ບົດສົນທະນາທີ່ສືບຕໍ່ໃນ endpoint ອື່ນຈະຮັກສາ prefix ຮ່ວມຂອງມັນໄວ້ ເມື່ອເນື້ອຫາຄືກັນ.
ຟິວຂອງຄຳຮ້ອງຂໍ (Request fields)
prompt_cache_key (ສຳລັບ Chat Completions ແລະ Responses) ແລະ cache_control ໃນ content blocks ຂອງ Messages ແມ່ນຖືກຮອງຮັບ, ດັ່ງນັ້ນ code ຂອງ client ເດີມຈຶ່ງເຮັດວຽກໄດ້ໂດຍບໍ່ຕ້ອງປ່ຽນແປງ. ທັງສອງບໍ່ໄດ້ຖືກບັງຄັບ: ການເຮັດ caching ແມ່ນເປັນອັດຕະໂນມັດ ແລະ ເຮັດວຽກໄດ້ຄືກັນໂດຍບໍ່ມີພວກມັນ.
| ຟີວ | ສົ່ງໄປຫາ | ມັນແມ່ນຫຍັງ |
|---|---|---|
prompt_cache_key | /v1/chat/completions, /v1/responses | ກຸນແຈສຳລັບກຳນົດເສັ້ນທາງ cache (cache routing key) ຂອງ OpenAI API. |
cache_control | /v1/messages | cache breakpoint ໃນ content block, ໃນ block system ຫຼື ໃນຂໍ້ຄວາມຂອງ Anthropic API. |
stream_options | /v1/chat/completions | include_usage ຂໍ usage ໃນ stream ຈາກ OpenAI API. ທີ່ນີ້ທຸກ stream ຈົບດ້ວຍ usage. |
ການນັບ tokens
ສອງ endpoint ທີ່ບໍ່ເສຍຄ່າ, POST /v1/tokenize ແລະ POST /v1/messages/count_tokens, ນັບ tokens ຂອງຂໍ້ຄວາມ ຫຼື ຂອງທັງຄຳຮ້ອງຂໍສຳລັບ hosted open-weight models ກ່ອນທີ່ທ່ານຈະສົ່ງ. ມັນມີໜ້າຂອງຕົນເອງ: ການນັບ token
