Context Engineering cho AI Agents: Buoc Tiep Sau Prompt Engineering

Ky luat phan biet he thong AI agents production voi demo co ten goi: context engineering. Neu ban da xay dung LLM applications, ban biet rang prompt engineering giup ban bat dau. Context engineering moi dua ban den production.

Bang chung ro rang nhat den tu he thong PRINCE cua Bayer, duoc Martin Fowler ghi lai vao thang 6/2026. Bayer xay dung he thong AI giup nha nghien cuu truy cap du lieu tien lam sang. Kham pha quan trong nhat cua team: kien truc retrieval quan trong hon chat luong prompt. Khong phai viet prompt tot hon — ma la kiem soat chinh xac thong tin nao moi agent nhan duoc.


Context Engineering La Gi?

Context engineering la ky luat kiem soat thong tin nao den voi moi agent trong mot he thong multi-agent. 4 thanh phan:

  1. Retrieval control — tai lieu nao, DB rows nao, API results nao vao context window
  2. Memory management — agent nho gi qua cac turns, voi TTL bao lau
  3. Tool selection — tools nao co san cho agent nay o buoc nay, khong phai tat ca
  4. Handoff design — agent A truyen thong tin gi cho agent B, va giu lai gi

Kien truc PRINCE ket hop RAG cho documents va Text-to-SQL cho database. Quan trong nhat: moi agent chi nhan context can thiet. Agent summarization khong thay raw SQL results. Agent validation khong thay toan bo corpus. Day la context engineering.


3 Failure Modes Context Engineering Ngan Chan

Context Pollution

Agent nhan thong tin khong lien quan lam hallucinate connections. Fix: filter tai retrieval layer, khong phai prompt level.

# Sai: gui tat ca, hy vong agent bo qua phan khong lien quan
context = fetch_all_documents(query)  # 50 documents

# Dung: filter truoc khi gui den agent
relevant_docs = semantic_search(query, k=5, threshold=0.75)
context = format_for_agent(relevant_docs)

Context Overflow

Vuot token limit, model loi hoac silent truncation. Fix: budget context windows explicit.

def build_agent_context(docs, history, tools, max_tokens=12000):
    docs_budget = max_tokens - 2000 - 3000 - 1000
    compressed_docs = truncate_to_budget(docs, docs_budget)
    return AgentContext(docs=compressed_docs, history=history, tools=tools)

Context Stale

Agent nho thong tin cu, quyet dinh sai. Fix: memory management voi TTL va relevance scoring.

class AgentMemory:
    def store(self, key, value, ttl_seconds=300):
        self.memory[key] = {
            "value": value,
            "expires_at": time.time() + ttl_seconds,
            "relevance": self.score_relevance(value)
        }
    def retrieve(self, query):
        return [m["value"] for m in self.memory.values()
                if m["expires_at"] > time.time() and m["relevance"] > 0.6]

Harness Engineering: Nua Con Lai

Song song voi context engineering la harness engineering — infrastructure orchestrate agents, xu ly loi, va cung cap observability.

class AgentHarness:
    def run_with_retry(self, agent, context, max_retries=3):
        for attempt in range(max_retries):
            try:
                result = agent.run(context)
                self.log_context_and_result(context, result)
                if self.validate(result):
                    return result
                context = self.reduce_context(context)
            except Exception as e:
                self.log_failure(e, context)
                if attempt == max_retries - 1:
                    raise
        return self.fallback_result(context)

Trach nhiem harness: error recovery, observability (log context nhan vao, khong chi output), va guardrails (validate truoc khi pass downstream).


3 Nguyen Tac Tu PRINCE

Nguyen tac 1: Route truoc khi retrieve. Xac dinh loai query truoc khi pull context. Path retrieval sai lang phi tokens va them noise.

Nguyen tac 2: Cho agents context hep hon ban nghi can. Bat dau minimal va them khi agent fail, thay vi bat dau rong va hy vong agent bo qua noise. LLMs dung focused context tot hon la bo qua noise.

Nguyen tac 3: Log context, khong chi output. Khi agent cho ket qua sai, answer hiem khi noi tai sao. Context no nhan duoc moi lam dieu do. Observability stack can capture input cua agent, khong chi output.


Ung Dung Cho Team

  1. Treat context nhu first-class engineering concern — assign ownership cho retrieval va memory architecture.
  2. Instrument context windows — alert khi budget vuot 80%.
  3. Design cho 95th percentile query — cai breaks production khong phai average case.
  4. Test harness, khong chi agents — unit tests bo qua harness-level failures.

Context engineering la ky luat phan biet production AI voi demo AI. Cac team ship reliable agent systems trong 2026 khong phai nhung nguoi co prompts tot nhat — ma la nhung nguoi da engineer information flow.

Xuất nội dung

Bình luận