Building with the Claude API — Ghi chú & tóm tắt khoá (Anthropic)

  • Mọi tương tác với Claude API đi qua 5 bước: client → server của bạn → Anthropic API → model xử lý → response về server → response về client.
  • KHÔNG bao giờ gọi Anthropic API trực tiếp từ client: API key là bí mật, lộ ra client là lỗ hổng bảo mật. Server riêng giữ key và đứng giữa.
  • Một request bắt buộc có 4 field — API key, model, messages, max_tokens; Claude xử lý qua 4 giai đoạn tokenization → embedding → contextualization → generation.

Building with the Claude API là gì? Là khoá developer của Anthropic Academy về tích hợp Claude qua API: gọi API, tool calling, prompt engineering, RAG và agentic workflow. Đây là ghi chú tiếng Việt tóm tắt khoá — thuật ngữ giữ nguyên tiếng Anh kèm chú thích. (Ghi chú đang được cập nhật dần theo tiến độ học khoá ~8 giờ.)

TL;DR — Mỗi lần người dùng bấm "send", request đi qua 5 bước: client → server của bạnAnthropic APImodel xử lý → response về server → response về client. Bạn luôn cần một server riêng vì API key là bí mật, không được lộ ra client. Request bắt buộc có 4 field (API key · model · messages · max_tokens). Bên trong, Claude xử lý qua 4 giai đoạn: tokenization → embedding → contextualization → generation.

Vòng đời một request (5 bước)

Hiểu trọn vòng đời một request giúp bạn ra quyết định kiến trúc tốt hơn và debug nhanh hơn — biết lỗi nằm ở khúc nào trong pipeline.

# Bước Chuyện gì xảy ra
1 Request to server Web/mobile app gửi input người dùng tới server của bạn (không gọi thẳng Anthropic).
2 Request to Anthropic API Server gắn API key + tham số rồi gọi Anthropic API (qua SDK hoặc HTTP thuần).
3 Model processing Claude xử lý input qua 4 giai đoạn (xem dưới) và sinh response.
4 Response to server Anthropic trả về một response có cấu trúc (message · usage · stop_reason).
5 Response to client Server chuyển text sinh ra về app, hiển thị cho người dùng.

Vì sao bắt buộc có server riêng

KHÔNG bao giờ gọi Anthropic API trực tiếp từ client-side code.

  • Mỗi request cần một secret API key để xác thực.
  • Nhúng key vào code client = lỗ hổng bảo mật nghiêm trọng — ai cũng trích xuất được.
  • Kẻ xấu lấy key xong có thể gọi API trái phép, đốt tiền trên tài khoản của bạn.

Thay vào đó: app gửi request tới server của bạn, server giữ key an toàn (biến môi trường / secret manager) rồi mới nói chuyện với Anthropic API.

Gọi API: SDK hay HTTP, và các field bắt buộc

Server có thể gọi Anthropic API bằng SDK chính thức hoặc HTTP request thuần. Anthropic cung cấp SDK cho Python, TypeScript, JavaScript, Go, Ruby.

Mọi request đều phải có 4 field cốt lõi:

Field Mục đích
API Key Xác thực request của bạn với Anthropic.
Model Tên model cần dùng (vd một model id họ claude-…; id tiến hoá theo thời gian nên luôn tra id hiện hành).
Messages Danh sách "messages" chứa input người dùng. Text người dùng được bọc trong một message role: "user", rồi đặt vào list messages.
Max Tokens Giới hạn số token tối đa Claude được sinh ra.

Bên trong Claude xử lý — 4 giai đoạn

Khi Anthropic nhận request, Claude chạy qua 4 giai đoạn: tokenization → embedding → contextualization → generation.

1. Tokenization — Claude cắt input thành các mẩu nhỏ gọi là token (có thể là cả từ, một phần từ, dấu cách, hay ký hiệu). Để đơn giản, cứ hình dung mỗi từ ≈ một token. Ví dụ "What is quantum computing?"What · is · quantum · computing · ?.

2. Embedding — Mỗi token được đổi thành một embedding: một danh sách dài các con số (vector) biểu diễn mọi nghĩa khả dĩ của từ đó — như một "định nghĩa bằng số" nắm bắt quan hệ ngữ nghĩa.

Một từ thường đa nghĩa. Ví dụ "quantum" có thể là: (1) đơn vị rời rạc của một đại lượng vật lý; (2) khái niệm cơ học/vật lý lượng tử; (3) thứ cực nhỏ ở mức hạ nguyên tử; (4) liên quan tới quantum computing. Embedding ban đầu mang theo tất cả các nghĩa này.

3. Contextualization — Claude tinh chỉnh từng embedding dựa trên các từ xung quanh để xác định nghĩa khả dĩ nhất trong ngữ cảnh. Cùng từ "quantum" nhưng đứng cạnh "computing" sẽ được kéo về nghĩa "quantum computing", loại bớt các nghĩa kia.

4. Generation — Các embedding đã ngữ cảnh hoá đi qua một output layer tính xác suất cho mỗi từ kế tiếp khả dĩ. Claude không phải lúc nào cũng chọn từ xác suất cao nhất — nó pha trộn xác suất với một chút ngẫu nhiên có kiểm soát để câu trả lời tự nhiên, đa dạng. Chọn xong một từ, nó thêm vào chuỗi và lặp lại toàn bộ quá trình cho từ tiếp theo.

Khi nào Claude dừng sinh

Sau mỗi token, Claude kiểm tra để quyết định có sinh tiếp không:

  1. Max tokens reached — đã chạm giới hạn max_tokens bạn đặt chưa?
  2. Natural ending — đã sinh ra token kết thúc tự nhiên (end-of-sequence) chưa?
  3. Stop sequence — có gặp một cụm dừng (stop sequence) bạn định nghĩa trước không?

Response trả về

Khi sinh xong, API trả về một response có cấu trúc:

Field Nội dung
Message Text Claude sinh ra.
Usage Số token input và output (cơ sở để tính chi phí).
Stop Reason Vì sao dừng (xem 3 điều kiện trên).

Server nhận response này, lấy text và chuyển về client để hiển thị.

Góc Delivery Manager. Cái sơ đồ "5 bước" này không chỉ là kiến thức học thuộc — nó là bản đồ rủi ro để mình thiết kế và vận hành. (1) Bước "server riêng" không thương lượng: với team mình, key luôn nằm ở secret manager phía server, không bao giờ ở repo/client — và đây cũng là chỗ đặt rate-limit + logging tập trung. (2) max_tokensusageđòn bẩy chi phí: đặt trần token đúng theo use case (chatbot ngắn vs tổng hợp tài liệu) tránh vừa đốt tiền vừa để câu trả lời lan man. (3) stop_reason phải được xử lý tường minh trong code: dừng vì max_tokens (output bị cắt giữa chừng — cần xử lý khác) hoàn toàn khác dừng tự nhiên — nuốt lỗi này là nguồn của những bug "câu trả lời cụt" khó tái hiện. Cuối cùng, biết pipeline 5 bước giúp khoanh vùng sự cố nhanh: lỗi ở server mình, ở request gửi đi, hay ở phía model.

Từ khoá cần thuộc

Thuật ngữ giữ nguyên tiếng Anh — đúng cách chúng xuất hiện trong tài liệu gốc và đề thi.

🔴 Core: 5-step request flow · server-side API key (không lộ client) · 4 required fields (API key · model · messages · max_tokens) · messages với role: "user" · Tokenization · Embedding · Contextualization · Generation · stop_reason (max_tokens / end-of-sequence / stop sequence) · usage (input/output tokens).

🟡 Important: SDK (Python · TypeScript · JavaScript · Go · Ruby) vs HTTP request · token (mẩu chữ) · output layer · controlled randomness · response structure (message · usage · stop_reason).

🟢 Good-to-know: đa nghĩa của từ ("quantum") · embedding = "định nghĩa bằng số" · pipeline như bản đồ debug.


Nguồn: Building with the Claude API (Anthropic Academy) — Copyright Anthropic. Phần đề thi thử cho khoá này nằm ở tab "Đề thi thử".

Câu hỏi thường gặp

Vì sao không nên gọi Anthropic API trực tiếp từ client (web/mobile app)?
Vì mỗi request cần một API key bí mật để xác thực. Nếu nhúng key vào code client, bất kỳ ai cũng trích xuất được và gọi API trái phép trên tài khoản của bạn. Cách đúng: app gửi request tới server riêng của bạn, server giữ key an toàn rồi mới gọi Anthropic API.
Một request tới Claude API bắt buộc gồm những field nào?
Bốn field cốt lõi: API key (xác thực request với Anthropic), model (tên model cần dùng), messages (danh sách tin nhắn chứa input của người dùng), và max_tokens (giới hạn số token Claude được sinh ra).
Claude xử lý một request qua mấy giai đoạn?
Bốn giai đoạn: tokenization (cắt input thành token), embedding (đổi mỗi token thành một vector số biểu diễn mọi nghĩa có thể của từ), contextualization (tinh chỉnh embedding theo các từ xung quanh để chọn đúng nghĩa trong ngữ cảnh), và generation (tính xác suất và chọn token kế tiếp, lặp lại cho tới khi dừng).
Claude dừng sinh văn bản khi nào?
Khi gặp một trong ba điều kiện: chạm giới hạn max_tokens đã đặt; sinh ra token kết thúc tự nhiên (end-of-sequence); hoặc gặp một stop sequence (cụm dừng) bạn định nghĩa trước. Lý do dừng được trả về trong field stop_reason.

Đề thi thử (14 câu)

Đề thi thử tự biên soạn, bám sát đề thi chứng chỉ thật. Bản tương tác — chấm điểm, đáp án & giải thích từng câu — nằm ở tab “Đề thi thử” trên trang.

  1. Request Lifecycle Why should you NOT call the Anthropic API directly from client-side code (web or mobile app)?

    • A. Client devices are too slow to handle the response
    • B. API requests require a secret API key that would be exposed and could be extracted for unauthorized use
    • C. The Anthropic API only accepts requests from servers, never browsers
    • D. Client code cannot parse JSON responses
  2. Request Lifecycle What is the correct order of the five-step request flow?

    • A. Request to Anthropic API → Request to server → Model processing → Response to client → Response to server
    • B. Request to server → Request to Anthropic API → Model processing → Response to server → Response to client
    • C. Model processing → Request to server → Request to Anthropic API → Response to client → Response to server
    • D. Request to server → Model processing → Request to Anthropic API → Response to server → Response to client
  3. Request Lifecycle (Select all that apply) Which fields are required in every request to the Claude API? (chọn nhiều đáp án)

    • A. API Key
    • B. Model
    • C. Messages
    • D. Max Tokens
  4. Request Lifecycle When a request is built, where is the user's input text placed?

    • A. Directly in the "model" field
    • B. Inside a "user" message, which is then placed in a list of messages
    • C. In the "max_tokens" parameter
    • D. In the API key header
  5. Request Lifecycle What does the "max_tokens" field control?

    • A. The maximum size of the input prompt
    • B. The maximum number of tokens Claude is allowed to generate
    • C. The number of SDK retries
    • D. The model's context window size
  6. Request Lifecycle (Select all that apply) For which languages does Anthropic provide official SDKs? (chọn nhiều đáp án)

    • A. Python
    • B. TypeScript / JavaScript
    • C. Go
    • D. Ruby
  7. Model Processing What is the correct order of the four stages Claude uses to process a request?

    • A. Embedding → Tokenization → Generation → Contextualization
    • B. Tokenization → Embedding → Contextualization → Generation
    • C. Tokenization → Contextualization → Embedding → Generation
    • D. Generation → Embedding → Contextualization → Tokenization
  8. Model Processing What is a "token"?

    • A. A secret key used to authenticate the request
    • B. A small chunk of text — a whole word, part of a word, a space, or a symbol
    • C. A complete sentence
    • D. A unit of GPU compute time
  9. Model Processing What does an embedding represent?

    • A. The final answer text
    • B. A list of numbers capturing all the possible meanings of a token and its semantic relationships
    • C. The number of tokens remaining before max_tokens
    • D. A compressed copy of the API key
  10. Model Processing What is the purpose of the contextualization stage?

    • A. To split the input into tokens
    • B. To refine each embedding based on surrounding words to determine the most likely meaning in context
    • C. To count input and output tokens for billing
    • D. To pick the final word with the highest probability
  11. Model Processing During generation, does Claude always pick the single highest-probability next word?

    • A. Yes, it always selects the most probable word
    • B. No — it mixes probability with controlled randomness to produce natural, varied responses
    • C. No — it always picks the least probable word
    • D. Yes, unless extended thinking is enabled
  12. Model Processing (Select all that apply) Which conditions cause Claude to stop generating? (chọn nhiều đáp án)

    • A. It reaches the max_tokens limit
    • B. It generates a natural end-of-sequence token
    • C. It encounters a predefined stop sequence
    • D. The user's API key expires mid-response
  13. API Response (Select all that apply) Which fields are included in the structured API response? (chọn nhiều đáp án)

    • A. Message — the generated text
    • B. Usage — count of input and output tokens
    • C. Stop Reason — why generation ended
    • D. API Key — echoed back for verification
  14. API Response Your application receives a response and the output text appears cut off mid-sentence. Which stop reason most likely explains this?

    • A. Natural end-of-sequence
    • B. The max_tokens limit was reached
    • C. A connector was disconnected
    • D. The embedding stage failed