Prompt & Context 101

Viết rõ việc cần làm. Chọn kỹ thứ được vào context. Say exactly what you want. Be choosy about what enters the context.

Context là một ngân sách hữu hạn: mỗi token phải xứng đáng với chỗ của nó. Trang này gom tám kỹ thuật viết prompt và quản lý context vào một chỗ, kèm cách chọn trong 30 giây. Context is a finite budget: every token has to earn its place. This page puts eight techniques for writing prompts and managing context in one place, with a way to pick in 30 seconds.

Context window System prompt, documents and examples sit at the start; conversation turns accumulate; the question sits at the end. When history crosses the 80% threshold, turns 1 to 4 are compacted into one summary and the freed space becomes room for new turns. 0 40k 80k 120k 160k 200k tokens system tài liệu documents ví dụ examples 1 2 3 4 Σ 1–4 chỗ cho lượt mới room for new turns 5 6 câu hỏi question ngưỡng 80% 80% threshold tĩnh, cache được static, cacheable lịch sử hội thoại conversation history

Phần tĩnh ở đầu, câu hỏi luôn ở cuối. Khi lịch sử vượt ngưỡng, bốn lượt cũ được nén thành một bản tóm tắt thay vì bị cắt mất. Static content first, the question always last. When history crosses the threshold, four old turns are compacted into one summary instead of being cut.

Chọn cái nàoWhich one

Tám kỹ thuật này phần lớn không loại trừ nhau. Hãy bắt đầu từ triệu chứng bạn đang thấy, không phải từ tên kỹ thuật. These techniques mostly aren't alternatives to each other. Start from the symptom you're seeing, not from a technique's name.

Trước hết: có thể bạn không cần gì cảFirst: you may need nothing at all

Bỏ qua mọi kỹ thuật khi cả ba điều sau đúng:Skip every technique when all three hold:

  • việc làm một lần và người đọc đầu ra là bạn;it's a one-off and you're the one reading the output;
  • bạn thấy kết quả ngay và hỏi lại được;you see the result right away and can ask again;
  • hội thoại còn xa giới hạn context.the conversation is nowhere near the context limit.

Cách rẻ nhất: một câu hỏi thẳng, thêm một câu nói mục đích, đọc kết quả, nói rõ chỗ sai rồi hỏi lại. Chỉ dùng kỹ thuật khi prompt chạy lặp lại mà không ai đọc từng đầu ra, hoặc cùng một kiểu lỗi cứ quay lại.The cheapest path: one direct question plus one sentence of purpose, read the result, say exactly what's wrong and ask again. Only use techniques when the prompt runs repeatedly with nobody reading each output, or the same failure keeps returning.

Chưa rõ nguyên nhân thì thử theo thứ tự 1 → 3 → 4 → 2: rẻ trước, đắt sau.Can't tell the cause? Try 1 → 3 → 4 → 2: cheapest first.

Tám khái niệmEight concepts

ViếtWriting nói gì, sắp xếp thế nàowhat to say and how to arrange it

1

Chỉ dẫn rõ ràngClear instructions

ViếtWriting

Giao việc như cho một đồng nghiệp giỏi nhưng mới: mục tiêu, lý do, giới hạn, định dạng, tiêu chí xong.Brief it like a capable new colleague: goal, reasons, limits, format, done criteria.

Chọn chỉ dẫn đầy đủ là đổi độ dài prompt và thời gian viết lấy việc model không phải đoán.Full instructions trade prompt length and writing time for a model that doesn't have to guess.

Dùng choUse for
Đầu ra cho người đọc cụ thể, trích xuất theo schema, pipeline chạy tự động.Output for a specific reader, schema extraction, automated pipelines.
ĐượcGain
Rẻ nhất, thường chỉ thêm 50–300 token; chữa phần lớn lỗi trả lời chung chung.Cheapest fix, usually 50–300 extra tokens; cures most generic answers.
MấtCost
Nhiều ràng buộc dễ xung đột ngầm; giọng văn và gu thẩm mỹ khó tả bằng lời.Many constraints conflict silently; voice and taste resist description.

Dấu hiệu:Signal: lần nào chạy xong bạn cũng phải sửa lại giọng văn.you re-edit the tone after every run.

2

Few-shot

ViếtWriting

Cho xem 3–5 cặp vào/ra mẫu để model tự suy ra quy luật.Show 3–5 sample input/output pairs so the model infers the pattern.

Chọn few-shot là đổi token và độ đa dạng của đầu ra lấy tính nhất quán.Few-shot trades tokens and output variety for consistency.

Dùng choUse for
Phân loại nhãn ranh giới mờ, định dạng lạ, giọng văn thương hiệu.Fuzzy-boundary labels, unusual formats, brand voice.
ĐượcGain
Định dạng và giọng văn nhất quán, chính xác hơn hẳn mô tả bằng lời.Format and voice stay consistent, far more precisely than a written description.
MấtCost
Model chép cả đặc điểm bề mặt như độ dài và cấu trúc; tốn token ở mọi lần gọi.The model copies surface features like length and structure; tokens on every call.

Dấu hiệu:Signal: mô tả định dạng dài hơn chính ví dụ.describing the format takes longer than showing it.

3

Phân tách bằng XML / delimiterXML / delimiter separation

ViếtWriting

Dùng thẻ hoặc delimiter để model biết đâu là lệnh, đâu là dữ liệu.Use tags or delimiters so the model knows what's an instruction and what's data.

Chọn phân tách tường minh là đổi một chút độ gọn và vài chục token lấy ranh giới rõ giữa lệnh và dữ liệu.Explicit separation trades a little brevity and a few dozen tokens for a clear line between instructions and data.

Dùng choUse for
Tài liệu do người dùng đưa vào, nhiều tài liệu cùng lúc, đầu ra cần parse.User-supplied documents, many documents at once, output that gets parsed.
ĐượcGain
Ranh giới rõ, parse ổn định, diff gọn khi quản lý version.Clear boundaries, reliable parsing, clean diffs under version control.
MấtCost
Không phải hàng rào bảo mật: prompt injection vẫn có thể vượt thẻ.Not a security boundary: prompt injection can still cross tags.

Dấu hiệu:Signal: bạn đang viết regex để cắt lấy câu trả lời.you're writing regex to cut out the answer.

4

Thứ tự tài liệu và câu hỏiOrdering documents and the question

ViếtWriting

Phần tĩnh và dài đặt đầu, câu hỏi đặt cuối, nơi model chú ý nhất.Static, long content first; the question last, where attention is strongest.

Chọn tĩnh trước, câu hỏi cuối là đổi thói quen viết tự nhiên lấy sự chú ý của model và tỷ lệ cache trúng.Static-first, question-last trades the natural writing habit for model attention and cache hits.

Dùng choUse for
Hỏi đáp trên tài liệu dài, RAG, system prompt dài gọi lặp lại.Long-document Q&A, RAG, long system prompts called repeatedly.
ĐượcGain
Tới ~30% chất lượng với đầu vào dài nhiều tài liệu; cache trúng giảm từ 90% chi phí phần được cache trở lên.Up to ~30% better on long multi-document input; cache hits cut the cost of the cached part by 90% or more.
MấtCost
Gần như vô nghĩa với prompt ngắn; phần động đặt sai chỗ là phá cache.Negligible for short prompts; dynamic content in the wrong place breaks the cache.

Dấu hiệu:Signal: model bỏ sót chi tiết nằm giữa tài liệu.the model misses details in the middle of the document.

Suy luậnReasoning khi model tự nghĩwhen the model thinks for itself

5

Prompt cho reasoning modelPrompting reasoning models

Suy luậnReasoning

Nói mục tiêu và tiêu chí, để model tự nghĩ; chỉnh mức effort thay vì dạy cách nghĩ.State the goal and criteria, let the model think; tune effort instead of teaching how to think.

Chọn reasoning model là đổi độ trễ và token suy nghĩ lấy độ chính xác ở bài toán nhiều bước.A reasoning model trades latency and thinking tokens for accuracy on multi-step problems.

Dùng choUse for
Toán, logic, gỡ lỗi khó, lập kế hoạch nhiều ràng buộc, chấm điểm đầu ra.Math, logic, hard debugging, constrained planning, grading output.
ĐượcGain
Chính xác hơn rõ ở bài nhiều bước; prompt ngắn hơn vì bỏ phần dạy cách nghĩ.Clearly more accurate on multi-step work; shorter prompts without "how to think".
MấtCost
Chậm, token suy nghĩ tính tiền; thừa cho phân loại hay trích xuất đơn giản.Slow, and thinking tokens are billed; overkill for simple classification or extraction.

Dấu hiệu:Signal: người làm cũng phải nháp mới giải được.a person would need scratch paper too.

Bộ nhớMemory khi lịch sử quá dài, quên cái gìwhen history grows too long, what to forget

6

Cắt bớtTruncation

Bộ nhớMemory

Giữ system prompt và N lượt gần nhất, bỏ phần cũ.Keep the system prompt and the last N turns; drop the rest.

Chọn cắt bớt là đổi trí nhớ dài hạn lấy sự đơn giản và chi phí có trần.Truncation trades long-term memory for simplicity and capped cost.

Dùng choUse for
Chatbot với câu hỏi độc lập, agent có kết quả tool lớn đã dùng xong.Chatbots with independent questions, agents with large spent tool outputs.
ĐượcGain
Vài dòng code, chi phí có trần, không thêm độ trễ.A few lines of code, capped cost, no added latency.
MấtCost
Quên trắng ràng buộc cũ; cắt giữa cặp tool call và tool result có thể làm API báo lỗi.Old constraints vanish; cutting between a tool call and its result can make the API error.

Dấu hiệu:Signal: lượt mới hiếm khi nhắc tới lượt cũ.new turns rarely refer to old ones.

7

Tóm tắt (compaction)Summarization (compaction)

Bộ nhớMemory

Nén lượt cũ thành bản tóm tắt có cấu trúc, giữ nguyên văn vài lượt gần nhất.Compact old turns into a structured summary; keep the latest few verbatim.

Chọn tóm tắt là đổi độ trung thực nguyên văn và một lần gọi model lấy trí nhớ dài hạn trong ngân sách cố định.Summarization trades verbatim fidelity and one extra model call for long-term memory within a fixed budget.

Dùng choUse for
Agent chạy nhiều giờ, trợ lý nhớ sở thích, tư vấn nhiều bước.Hours-long agents, assistants that remember preferences, multi-step advising.
ĐượcGain
40k token còn vài nghìn; phiên làm việc gần như không giới hạn.40k tokens down to a few thousand; practically unlimited sessions.
MấtCost
Model chọn thứ bị bỏ; một lỗi trong bản tóm tắt lan sang mọi lượt sau.The model chooses what's dropped; one error in the summary spreads to every later turn.

Dấu hiệu:Signal: quyết định ở lượt đầu vẫn chi phối lượt cuối.decisions from the first turns still govern the last.

Vận hànhOperations biết chắc thay đổi là tốt lênknowing a change is an improvement

8

Quản lý version prompt như codeVersioning prompts like code

Vận hànhOperations

Prompt nằm trong git, đi qua review, chạy eval trước khi triển khai, ghim model id.Prompts live in git, go through review, run evals before deploy, and pin the model id.

Chọn quản lý prompt như code là đổi tốc độ sửa nhanh lấy khả năng biết chắc thay đổi tốt hơn hay tệ hơn.Versioning prompts like code trades quick edits for certainty about whether a change is better or worse.

Dùng choUse for
Prompt chạy ở production, nhiều người cùng sửa, chuẩn bị nâng cấp model.Production prompts, several editors, upcoming model upgrades.
ĐượcGain
Bắt lỗi hồi quy trước khi người dùng thấy; quay lui trong vài phút.Regressions caught before users see them; rollback in minutes.
MấtCost
Xây eval tốn công nhất; eval kém tạo cảm giác an toàn giả.Building evals is the hardest part; weak evals create false confidence.

Dấu hiệu:Signal: có người hỏi "ai đổi prompt hôm qua?".someone asks "who changed the prompt yesterday?".

Bảng đánh đổiTrade-off table

Những thứ bạn thực sự phải trả khi dùng một kỹ thuật.What you actually pay when you use each technique.

Khái niệmConcept Token thêm / requestExtra tokens / request Độ trễ thêmExtra latency Công thiết lậpSetup effort Rủi ro đặc trưngCharacteristic risk
1Chỉ dẫn rõ ràngClear instructionsThấp (50–300)Low (50–300)KhôngNoneThấpLowRàng buộc xung đột ngầmHidden constraint conflicts
2Few-shotTrung bình đến caoMedium to highNhẹSlightTrung bìnhMediumHọc nhầm đặc điểm bề mặtLearning surface features
3Phân táchSeparationRất thấpVery lowKhôngNoneThấpLowTưởng là đã an toànFalse sense of security
4Thứ tựOrderingKhôngNoneGiảm (nhờ cache)Lower (via cache)ThấpLowPhần động phá cacheDynamic content breaks the cache
5Reasoning modelCao (token suy nghĩ)High (thinking tokens)CaoHighThấpLowChi phí đội khi dùng sai việcCost blowout on the wrong tasks
6Cắt bớtTruncationGiảmLowerKhôngNoneThấpLowQuên ràng buộc cũForgetting old constraints
7Tóm tắtSummarizationGiảm, cộng một lần gọi khi nénLower, plus one call per compactionTăng lúc nénHigher at compactionTrung bìnhMediumTóm tắt sai lan truyềnSummary errors propagate
8Version + evalVersioning + evalsKhôngNoneKhôngNoneCaoHighEval không đại diệnUnrepresentative evals

Bốn dòng đầu gần như miễn phí, nên luôn làm. Dòng 5 chỉ đáng khi bài toán thật sự nhiều bước. Dòng 6 và 7 chỉ cần khi hội thoại dài. Dòng 8 là chi phí cố định, đáng khi prompt đã vào production.The first four rows are nearly free, so always do them. Row 5 is only worth it for genuinely multi-step problems. Rows 6 and 7 matter only in long conversations. Row 8 is a fixed cost that pays off in production.

Anti-patterns

Sai lầmMistake Vì sao saiWhy it's wrong Sửa thế nàoHow to fix it
Viết hoa, "CRITICAL" khắp promptCAPS and "CRITICAL" everywhereModel đời mới làm quá tay; mọi thứ đều quan trọng thì không gì quan trọngNewer models overreact; when everything is important, nothing isNói lý do đằng sau ràng buộcExplain the reason behind the constraint
Chỉ viết chỉ thị phủ định "đừng làm X"Negative instructions only: "don't do X"Lệnh cấm khiến model phải đoán nên làm gì thay thế, có khi còn bị kéo về phía XA ban leaves the model guessing what to do instead, and can even nudge it toward XNói điều cần làm thay thếSay what to do instead
Nhồi mọi tài liệu vào "cho chắc"Stuffing in every document "just in case"Context rot, thông tin lạc ở giữa, tốn tiềnContext rot, lost-in-the-middle, higher costChỉ đưa thứ cần; truy xuất có chọn lọcInclude only what's needed; retrieve selectively
Chỉ có một ví dụA single exampleModel chép gần như y nguyênThe model copies it almost verbatim3–5 ví dụ khác nhau về độ dài, chủ đề, độ khó3–5 examples varying in length, topic, difficulty
"Think step by step" trên reasoning model"Think step by step" on a reasoning modelThừa, có khi kéo chất lượng xuốngRedundant, sometimes lowers qualityBỏ đi; tăng effort nếu cầnRemove it; raise effort if needed
Timestamp ở dòng đầu system promptTimestamp on the first line of the system promptPhá cache ở mọi requestBreaks the cache on every requestĐẩy phần động xuống sau phần tĩnhMove dynamic content below static content
Dán thẳng input người dùng vào promptPasting user input straight into the promptLẫn lệnh với dữ liệu, mở đường cho injectionInstructions blur with data; opens the door to injectionBọc thẻ, nói rõ đó là dữ liệuWrap it in tags and say it's data
Cắt lịch sử bỏ luôn mục tiêu ban đầuTruncating away the original goalAgent đi lạc đềThe agent drifts off taskGhim lượt đầu và các ràng buộcPin the first turn and the constraints
Tóm tắt tự do, không templateFree-form summaries, no templateRàng buộc và quyết định bị rơi mấtConstraints and decisions fall outTemplate với các mục cố địnhA template with fixed sections
Prompt hardcode, sửa thẳng trên production, model alias "latest"Hardcoded prompts edited live, "latest" model aliasKhông quay lui được, hành vi đổi mà không ai biếtNo rollback; behavior changes without anyone knowingFile riêng, git, eval trong CI, ghim model idSeparate files, git, evals in CI, pinned model id

Ba câu cần nhớThree things to remember

  • Viết prompt như giao việc cho một người giỏi nhưng mới: mục tiêu, lý do, tiêu chí xong. Mọi kỹ thuật khác chỉ là phần bổ sung.Brief the model like a capable new colleague: goal, reasons, done criteria. Every other technique is an add-on.
  • Context là ngân sách có hạn: chỉ đưa vào thứ cần, đặt câu hỏi ở cuối, và chủ động quyết định quên cái gì.Context is a finite budget: include only what's needed, put the question last, and decide deliberately what to forget.
  • Một thay đổi prompt chưa qua eval chỉ là một giả thuyết.A prompt change that hasn't been through evals is only a hypothesis.