IzziAPI
TipsApr 5, 20267 min read

Prompt Caching giảm chi phí API 90%

Triển khai prompt caching để giảm mạnh chi phí input token. Ví dụ Python và Node.js với so sánh giá thực tế.

Izzi API Team
Engineering & DevRel
prompt cachingtối ưupythonnode.jschi phí
Prompt Caching giảm chi phí API 90%

Prompt caching là gì?

Prompt caching cho phép cache phần system prompt và context không thay đổi giữa các request. Kết quả: giao dịch giảm tới 90% chi phí input token.

Cách hoạt động

  1. Request đầu tiên: gửi system prompt đầy đủ → cache
  2. Các request sau: chỉ gửi phần mới → đọc từ cache
  3. Tiết kiệm: 90% input token cho phần cached

Ví dụ Python

Python
import openai

client = openai.OpenAI(
    base_url="https://api.izziapi.com/v1",
    api_key="izzi-xxx-your-key"
)

# System prompt dài (cached sau request đầu)
system_prompt = """You are a Python expert.
[... 2000 tokens system context ...]"""

# Request 1: Cache system prompt
response = client.chat.completions.create(
    model="claude-sonnet-4.5",
    messages=[
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": "Fix this bug"}
    ]
)

# Request 2+: System prompt đã cached → 90% rẻ hơn
response = client.chat.completions.create(
    model="claude-sonnet-4.5",
    messages=[
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": "Now add tests"}
    ]
)

So sánh chi phí

ScenarioKhông cacheCó cacheTiết kiệm
10 requests, 2K system tokens$0.066$0.01085%
100 requests, 5K system tokens$1.65$0.1889%
1000 requests, 10K system tokens$33.00$3.6089%

Kết luận

Prompt caching là cách đơn giản nhất để giảm chi phí API AI. Izzi API hỗ trợ cache tự động — bạn chỉ cần giữ system prompt nhất quán.

Sẵn sàng bắt đầu?

Truy cập 38+ model AI qua một API duy nhất. Gói miễn phí — không cần thẻ tín dụng.

MORE

Bài viết liên quan