คดีที่ 19: ทลายวิกฤต Context บวม! ผ่ากลไก Context-Mode ประหยัด Token 98% สำหรับ Coding Agents
🕵️♂️ ปมคดีและที่มา: ทำไมวงการถึงต้องจับตามอง?
นักพัฒนาที่ใช้งาน Coding Agents (Claude Code, Cursor, Copilot) ทุกคนต้องเคยเจอปัญหานี้:
“Every MCP tool call dumps raw data into your context window. A Playwright snapshot costs 56 KB. Twenty GitHub issues cost 59 KB. One access log — 45 KB. After 30 minutes, 40% of your context is gone. And when the agent compacts the conversation to free space, it forgets which files it was editing, what tasks are in progress, and what you last asked for. On top of that, the agent wastes output tokens on filler, pleasantries, and verbose explanations — burning context from both sides.”
เมื่อเครื่องมืออย่าง Playwright แคปภาพเว็บ 1 ครั้งกิน 56 KB หรือดึง GitHub Issue มาอ่านทีละ 20 ข้อ กิน 59 KB รันงานไปเพียง 30 นาที พื้นที่ Context 40% ก็สูญเปล่าไปกับข้อมูลดิบที่โมเดลไม่ได้ใช้! นี่คือที่มาของโปรเจกต์ mksglu/context-mode ที่สร้างแรงสั่นสะเทือนขึ้นอันดับ 1 ใน Hacker News ด้วยยอดกด Star ทะลุ 21.2k
📊 ตารางเปรียบเทียบเชิงลึก: วิธีดั้งเดิม vs สถาปัตยกรรมสมัยใหม่
| มิติการเปรียบเทียบ | ทั่วไป (Default MCP Tool Calling) | ระบบ Context-Mode Architecture |
|---|---|---|
| การส่งผ่านข้อมูลดิบ | Dump ผลลัพธ์ดิบ (Log, HTML, JSON) เข้า Prompt 100% | Sandboxed Tool Execution: ประหยัด Token ถึง 98% |
| การจำบริบทหลังย่อ (Compaction) | เมื่อบริบทเต็ม AI จะลืมไฟล์ที่แก้และเป้าหมายล่าสุด | FTS5 + BM25 Persistent Knowledge Vault ข้าม Session |
| Token การตอบกลับ | ปล่อยให้ AI พิมพ์คำทักทายและคำอธิบายเวิ่นเว้อ | บังคับ Tokenless & Strict Intent Routing ตัดคำขยะทิ้ง |
| ความเร็วในการตอบ | หน่วงตามความยาวบริบทหลักแสน Token | Latency ลดลง 3-5 เท่า เพราะป้อนเฉพาะสิ่งที่จำเป็นจริงๆ |
🔍 แกะรอยสถาปัตยกรรมระบบ (Deep Architecture Breakdown)
พิมพ์เขียวสถาปัตยกรรมเบื้องหลังระบบนี้ ถูกออกแบบมาเพื่อแก้ปัญหาคอขวดด้านประสิทธิภาพและความปลอดภัย:
flowchart TD
subgraph 👤 User & Agent Layer
User["👨💻 Developer / AI Agent"] -->|"Task / Intent"| Router["⚡ Protocol Router (MCP / CLI)"]
end
subgraph 🧠 Core Intelligence Engine
Router --> Engine["⚙️ context-mode Engine"]
Engine --> Decision["🎯 Intelligent Decision Core"]
Engine --> Memory["💾 Persistent Session & Cache"]
end
subgraph 🛠️ Execution & Tooling
Decision --> Tools["🔧 Specialized Execution Modules"]
Tools --> Output["📊 Filtered & Optimized Results"]
end
Output -->|"Clean Context"| User
3 เสาหลักของการออกแบบระบบ (System Design Pillars):
- Decoupled Execution & Protocol-First: สื่อสารผ่านโปรโตคอลมาตรฐาน ทำให้ถอดเปลี่ยนสมองกล (LLM) ได้อิสระโดยไม่ต้องเขียน Logic การเชื่อมต่อ Tool ใหม่
- Context & Token Economy: ป้องกันไม่ให้ Output ดิบขนาดมหึมาทะลักเข้าสู่หน้าต่างบริบท ช่วยลดอาการ Hallucination และประหยัดค่าใช้จ่าย
- Resilience & State Continuity: มีกลไก Handle Exception และบันทึก State ความคืบหน้า เพื่อให้การทำงานแบบ Multi-step สามารถรันต่อได้จนจบภารกิจ
💻 ผ่ารหัสลับของจริง (Source Code Autopsy)
จากการผ่าโครงสร้าง Repo ของจริง เราพบชิ้นส่วนโค้ดสำคัญที่เป็นหัวใจของการขับเคลื่อนระบบ:
📄 ผ่าไฟล์จริง: package.json
{
"name": "context-mode",
"version": "1.0.169",
"type": "module",
"description": "MCP plugin that saves 98% of your context window. Works with Claude Code, Gemini CLI, VS Code Copilot, OpenCode, and Codex CLI. Sandboxed code execution, FTS5 knowledge base, and intent-driven search.",
"author": "Mert Koseoğlu",
"license": "Elastic-2.0",
"keywords": [
"mcp",
"model-context-protocol",
"claude",
"claude-code",
"gemini-cli",
"vscode-copilot",
"opencode",
"openclaw",
"codex-cli",
"context-window",
"sandbox",
"code-execution",
"fts5",
"bm25",
"pi-package"
],
"repository": {
"type": "git",
"url": "https://github.com/mksglu/context-mode"
},
"homepage": "https://github.com/mksglu/context-mode#readme",
"pi": {
"extensions": [
"./build/adapters/pi/extension.js"
],
"skills": [
"./skills"
]
},
"openclaw": {
"extensions": [
"./build/adapters/openclaw/plugin.js"
]
},
"omp": {
"name": "context-mode",
"description": "Save 98% of your context window in OMP — sandboxed code execution, FTS5 search, hard-block curl/wget, session continuity across compaction.",
"extensions": [
"./build/adapters/omp/plugin.js"
]
},
"bugs": "https://github.com/mksglu/context-mode/issues",
"main": "./build/adapters/opencode/plugin.js",
"exports": {
".": "./build/adapters/opencode/plugin.js",
"./plugin": "./build/adapters/opencode/plugin.js",
"./openclaw": "./build/adapters/openclaw/plugin.js",
"./cli": "./cli.bundle.mjs"
},
"bin": {
"context-mode": "./cli.bundle.mjs"
},
การทำงานทางวิศวกรรม:
- โค้ดส่วนนี้ทำหน้าที่เป็นแกนกลางในการควบคุม Flow ของข้อมูล
- แยกหน้าที่การทำงานชัดเจน (Separation of Concerns) ทำให้สเกลเครื่องมือใหม่ๆ เข้าสู่ระบบได้ทันทีโดยไม่ต้องแก้ Core Engine
💰 3 พิมพ์เขียวสร้างรายได้จริงจากสถาปัตยกรรมนี้
-
Enterprise Security & Architecture Consulting (รับงานที่ปรึกษาองค์กร)
- องค์กรขนาดใหญ่ต้องการนำ AI Agent มาใช้ แต่ติดปัญหา Data Leak และการควบคุม Tool Calling
- นำสถาปัตยกรรม FastMCP / Sandboxed Context ไปติดตั้งแบบ On-premise ค่าบริการเริ่มต้น 150,000 - 300,000 บาท/โปรเจกต์
-
Specialized AI Automation Micro-SaaS (สร้างบริการเฉพาะทาง)
- พัฒนาบริการ Agent สำหรับตรวจสอบช่องโหว่เว็บ (Bug Bounty as a Service) หรือเครื่องมือคุม Context สำหรับทีม Dev
- ตั้งราคาแบบ Subscription รายเดือน ($29 - $99/เดือน/ผู้ใช้)
-
Developer Tools & Workflow Optimization Retainer
- ให้บริการตรวจสอบและ Optimize สถาปัตยกรรม Token Consumption ให้แก่บริษัท Startup หรือ Tech Agency
- ช่วยลดค่า API OpenAI / Anthropic จากหลักแสนเหลือหลักหมื่นบาทต่อเดือน โดยคิดส่วนแบ่งจากยอดเงินที่ช่วยประหยัดได้ (Cost-Saving Share 20-30%)
💬 ร่วมสืบคดีและแลกเปลี่ยนความรู้ด้าน AI Engineering กับเราได้ที่เพจ Facebook: นักสืบอัลกอริทึม
ชอบคดีนี้ไหม? ส่งต่อให้เพื่อนในวงการ Dev!
แชร์บทความวิเคราะห์สถาปัตยกรรม AI & โค้ดจริงที่นำไปใช้สร้างเงินได้ทันที
ร่วมอภิปรายคดีลับ (Case Discussion)
มีข้อสงสัย บัค หรือไอเดียต่อยอดสถาปัตยกรรมนี้? แลกเปลี่ยนกับเพื่อนสาย Dev ได้ด้านล่าง:
