概述
它不只是提示語的措辭。這很重要,因為輸出品質高度取決於視窗內的內容;在代理人與長時間任務中,這些上下文是由程式在每次呼叫時組裝,而不是由人一次性輸入。
深入探討
Prompt engineering focuses on wording a single request. Context engineering takes a wider view. The model receives one sequence of tokens, and everything in it competes for space and for the model's attention. That sequence usually includes a system prompt, tool definitions, retrieved passages, prior turns, tool outputs and sometimes long-term memory. The term became common in 2025, when teams building agents found that many failures came from the model having the wrong information, too much of it or conflicting information, rather than from poor phrasing. Several principles follow. First, relevance beats volume. Some models accept hundreds of thousands of tokens, but they do not use every token equally well. The 'lost in the middle' study (Liu et al., 2023) found that models often use information at the start and end of a long input better than information buried in the middle. Second, context is a budget. Every token costs money and adds latency, and irrelevant material can distract the model. Third, structure helps. Clear sections, labeled documents and consistent formats make it easier for the model to find what matters. Common techniques include: - retrieval-augmented generation, which fetches only the relevant passages - just-in-time loading, where an agent holds lightweight references such as file names or IDs and uses tools to pull full content when needed - structured note-taking that keeps state outside the window - compaction of long histories - sub-agents that explore in their own context and return condensed findings One misconception is that context engineering is just a new name for prompting. Wording is one part of it, but most of the work is systems design: deciding what gets retrieved, summarized or dropped, and in what order it appears. Another misconception is that a bigger window removes the need for this. A bigger window lowers the pressure, but cost and attention limits remain.
現實世界的實施
客戶支援機器人只會檢索最相關的三篇說明中心文章以及客戶的方案等級。它不會把整個知識庫貼到每個請求中。
程式編寫代理人先得到檔案路徑與函式簽名清單,然後在需要時使用讀檔工具載入完整檔案。
會議助理將決策與待辦事項儲存到結構化筆記檔,並在下次會議時重新載入該檔,而不是重播所有過去的文字記錄。
團隊修改資料庫工具,使其回傳帶有列數的過濾摘要,而非 5,000 列原始 JSON。這可防止代理人的視窗被永遠不會使用的資料塞滿。
Context Engineering 的未來
上下文窗口迅速擴大,模型在使用長輸入方面也越來越好。成本、延遲和注意力限制仍使得選擇輸入內容變得重要。越來越多的工作正轉向框架和平台,幾家供應商已經提供內建記憶工具、自動壓縮和管理檢索。這項技能很可能會從手動組裝上下文轉向設計代理應該記住、檢索或忘記的政策,並測試這些政策。未來的模型設計是否需要更少的精心策劃仍是個未解之謎。現在,精心設計的上下文是提升模型輸出最可靠的方法之一。
付諸實踐
為什麼這很重要
- 速度與規模. 語言工作流程可以在不犧牲一致性的情況下更快地移動。
- 交通與覆蓋範圍. 它擴展了跨語言和溝通方式的訪問。
- 更明確的決策. 團隊可以花更多時間進行判斷,而自動化則可以處理重複。
留意
- 幻覺的事實可以悄悄地進入報告、支持流程或研究成果。
- 及時的敏感性可能會在類似的請求中產生不一致的結果。
- 如果存取控制薄弱,敏感文字資料可能會暴露。
後續步驟
- 在推出之前定義輸出格式、語氣和品質標準。
- 當準確性很重要時,請使用可信任來源進行地面回應。
- 為高風險輸出保留人工審查檢查點。
- 追蹤故障模式並定期重新訓練提示或工作流程。
不斷探索
常見問題
什麼是上下文工程?
上下文工程是決定語言模型在每一步看到什麼(指令、檢索文件、工具定義和結果、對話歷史和記憶)並將其放入有限上下文窗口的實踐。它比提示措辭更進一步。它很重要,因為輸出質量高度依賴於窗口中的內容,在代理和長期任務中,這些上下文是通過代碼在每次調用時組裝的,而不是由人一次性輸入。
什麼最能區分上下文工程和提示工程?
上下文工程將模型接收的整個 token 序列視為設計對象:指令、檢索段落、工具定義和輸出、歷史和記憶。提示措辭只是其中的一部分。
「在中間遺失」的研究對長輸入有何啟示?
Liu 等人(2023)發現,當相關資訊位於長上下文的中間時,效能往往會下降。這也是為什麼相關性與位置比單純的資訊量更重要的原因之一。
為什麼指南將上下文描述為預算?
視窗中的 token 在價格與回應時間上都有實際成本。無關的 token 也會與相關的 token 競爭模型的注意力,因此應該謹慎使用。
什麼是即時載入(just‑in‑time loading)?
代理人不是一次性載入全部內容,而是保留指標,僅在步驟需要時才拉取完整內容。這樣可以讓視窗保持精簡。
為什麼將穩定的內容(如系統指示與工具定義)放在開頭且以固定順序排列會有用?
支援提示快取的提供者可以重複使用在多次呼叫中保持位元相同的前綴。將穩定內容放在最前、變動的對話放在最後,就能實現這點。