昨天我們談到 Context 過長 會讓 AI 忘規則、失焦,輸出結果不一致。但就算解決了 Context 問題,還有另一個挑戰:性能。
如果規範過於冗長、缺乏重點排序,AI 不僅回應慢,還可能忽略部分規則。下面我整理了幾個常用的調整方法。
錯誤:
Always remember to try your best to avoid SQL injection.
改進:
Validate inputs. Prevent SQL injection.
Token 減半,語意更直接。
舊寫法:
新寫法:
PRIORITY_SYSTEM:
- P1_SECURITY: Validate input (SQLi safe).
- P2_CONSISTENCY: snake_case naming.
- P3_TEST: Unit test for all functions.
用代號管理規則,縮短內容又保持一致性,團隊共享代碼表即可。
以我們的多技術棧專案結構為例:
frontend/.cursorrules
# === P1 關鍵規則(最前面)===
SECURITY: Input validation required.
CONSISTENCY: JavaScript camelCase only.
# === 載入模組 ===
@import ../shared/prompts/lang/javascript.md
@import ../shared/prompts/testing/jest.md
# === P1 關鍵規則重申(最後面)===
REMINDER: All variables must use camelCase.
backend/.cursorrules
# === P1 關鍵規則(最前面)===
SECURITY: SQL injection prevention.
CONSISTENCY: PHP snake_case only.
# === 載入模組 ===
@import ../shared/prompts/lang/php.md
@import ../shared/prompts/testing/phpunit.md
# === P1 關鍵規則重申(最後面)===
REMINDER: All identifiers must follow snake_case.
測試顯示這樣更能強化規則遵守率。
shared/prompts/base/style.md(精簡版)
<!-- version: 2.0 | optimized for performance -->
STYLE_CORE:
- P1: Input validation
- P2: Consistent naming
- P3: Test coverage
FORMAT:
- 2-space indent
- No trailing spaces
- UTF-8 encoding
shared/prompts/lang/javascript.md(代碼化版)
JS_RULES:
- NAMING: camelCase (vars/functions), PascalCase (classes)
- SYNTAX: ES6+, const>let>var
- MODULES: import/export only
- DOM: Native API preferred
# 載入順序
@import ../shared/prompts/base/style.md # 基礎規則
@import ../shared/prompts/lang/javascript.md # 技術棧特定
@import ./project-specific.md # 專案特殊需求(如果有)
執行相同的指令,使用不同的System Prompt,並進行測試。
/my-app
├── shared/prompts/
│ ├── base/
│ │ └── core.md # 核心規則
│ ├── lang/
│ │ ├── js-optimized.md # JS精簡版
│ │ ├── php-optimized.md # PHP精簡版
│ │ └── flutter-optimized.md # Flutter精簡版
│ └── testing/
│ ├── jest-compact.md # Jest精簡版
│ └── phpunit-compact.md # PHPUnit精簡版
│
├── frontend/
│ └── .cursorrules
│
├── backend/
│ └── .cursorrules
│
└── mobile/
└── .cursorrules
JavaScript專案:
PHP專案:
跨技術棧任務:
性能調校在多技術棧環境下更加關鍵,因為錯誤的模組載入會導致規範混用和性能下降。