Sổ Tay AI
ky-thuat Cơ bản

API (AI) là gì? Cách dùng API LLM

Cách lập trình viên gọi mô hình AI từ code — Claude API, OpenAI API, Gemini API hoạt động thế nào.

Cập nhật: 2 tháng 5, 2026 · 3 phút đọc

API (Application Programming Interface) trong context AI là cách lập trình viên gọi mô hình AI (Claude, GPT, Gemini…) từ code thay vì qua giao diện chat. Đây là cách build sản phẩm AI tích hợp vào app/website của bạn.

Tại sao dùng API thay vì web?

Web (claude.ai, chatgpt.com)API
Cho người dùng cuốiCho developer
Trả tiền theo góiTrả tiền theo token dùng
1 user 1 lúcHàng nghìn request song song
Không tích hợp được vào appTích hợp dễ dàng

→ Build chatbot, automation, analysis tool → bắt buộc dùng API.

Ví dụ gọi API (Python)

Claude

from anthropic import Anthropic
client = Anthropic(api_key="sk-ant-...")

response = client.messages.create(
    model="claude-sonnet-4-7",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Giải thích RAG cho người mới"}]
)
print(response.content[0].text)

OpenAI

from openai import OpenAI
client = OpenAI(api_key="sk-...")

response = client.chat.completions.create(
    model="gpt-5",
    messages=[{"role": "user", "content": "Giải thích RAG cho người mới"}]
)
print(response.choices[0].message.content)

Gemini

from google import genai
client = genai.Client(api_key="...")

response = client.models.generate_content(
    model="gemini-2.5-pro",
    contents="Giải thích RAG cho người mới"
)
print(response.text)

→ Cú pháp khác nhau nhưng concept giống: gửi message, nhận response.

Pricing API các provider chính (2026)

ProviderModelInput / 1M tokensOutput / 1M tokens
AnthropicClaude Sonnet 4.7$3$15
AnthropicClaude Haiku 4.5$0.80$4
AnthropicClaude Opus 4.5$15$75
OpenAIGPT-5$2.50$10
OpenAIGPT-5 mini$0.15$0.60
GoogleGemini 2.5 Pro$1.25$5
GoogleGemini 2.5 Flash$0.10$0.40

→ Tier “small/flash” rẻ hơn 10-30× tier flagship — quy tắc vàng: dùng model nhỏ nhất đủ tốt.

Tính năng nâng cao của API

  • Streaming: nhận token từng phần (tốt cho UX chatbot)
  • Function calling / Tool use: cho LLM gọi function của bạn — xem Function Calling
  • Structured output: ép LLM trả JSON đúng schema
  • Vision: gửi ảnh kèm text
  • Caching: cache prompt cố định, giảm tới 90% chi phí
  • Batch API: gửi 1000 request 1 lúc, giảm 50% giá

Lưu ý khi dùng API ở Việt Nam

  • Thanh toán: cần thẻ Visa/Mastercard quốc tế. Một số ngân hàng có thẻ ảo cho mục đích này (Techcombank Visa Debit, VPBank).
  • Rate limits: tier mới mở thường thấp. Verify phone, top up tiền → tier cao hơn.
  • Latency: API call từ VN sang us-east có ~150-200ms. Anthropic, OpenAI có endpoint Asia (Singapore, Tokyo) cho phí tier cao.
  • Compliance: nếu xử lý data nhạy cảm khách hàng → đọc kỹ data policy provider.

Wrapper / SDK đáng dùng

  • LangChain — framework tổng hợp, hỗ trợ mọi provider (overkill với task đơn giản)
  • LlamaIndex — tốt cho RAG
  • Vercel AI SDK — tốt nhất cho web app TypeScript
  • LiteLLM — proxy nhiều provider qua 1 interface

Liên quan

Thẻ
#api#developer#production