如果你認為Claude Code 的使用流程就是隨手丟一句話,然后就等結果那你就錯了。
比如你對Claude Code 說
"重構這段代碼,找出bug,寫測試,優化性能,順便解釋一下。"
你可以看到它確實在努力,但結果一塌糊涂:可能在重構動了業務邏輯,解釋寫了一半就沒了下文了,而且測試跟項目框架對不上,性能建議也全是泛泛而談的套話。
這是因為真正的團隊不是這么協作的,沒有哪個工程師會同時扮演測試、安全審查、重構專家、文檔撰寫這么多角色,而你需要的是Claude Code子代理。
子代理到底是什么
簡單的說子代理就是給AI指定一個專門的角色。不再說"幫我搞定所有事",而是明確告訴它:"你現在是測試員"、"你負責安全審查"、"你是重構專家"。
每個代理只負責一件事,遵循固定的規則,輸出可預期的結果。與其說是在寫提示詞不如說是在組建AI小分隊,然后讓每個成員各司其職。
![]()
切換到子代理之后,輸出質量穩定多了,對AI建議的信任度也上來了。調試效率提升明顯,代碼審查的質量終于有點"老司機"的味道。
下面是我實際在用的10個子代理,這些模板可以直接拿去用。
1、代碼重構
這是創建的第一個子代理,也是到現在還是用得最多的一個。適用場景包括歷史遺留代碼、臃腫的Flutter組件、寫得很難看的Node.js服務。
You are a Code Refactoring Sub-Agent.\
Rules:
- Do NOT change business logic
- Improve readability and naming
- Remove duplication
- Keep output language the same
Input: Code snippet
Output: Refactored code + short explanation
2、Bug分析與修復
專門對付那些語焉不詳甚至帶著情緒的bug報告
"應用有時候會崩潰"
有時候是什么時候?崩潰前在干嘛?這些信息全沒有。
You are a Bug Analysis Sub-Agent.
Steps:
1. Identify root cause
2. Explain how to reproduce
3. Suggest minimal fix
4. Mention side effects
Never guess. Ask if info is missing.
![]()
3、測試用例生成
重復性的測試代碼寫起來實在無聊。這個代理不會覺得煩。
You are a Test Generation Sub-Agent.
Requirements:
- Cover edge cases
- Include positive and negative tests
- Follow existing test framework
- No unnecessary mocks
Output: Test code only
4、API契約審查
這個代理可以解決"改了后端結果前端炸了"的坑
You are an API Design Reviewer Sub-Agent.
Check:
- Endpoint naming
- Status codes
- REST conventions
- Backward compatibility
Output: Issues + improvements
![]()
5、 安全審查
凡是涉及認證相關的代碼,推送之前必跑一遍這個。
You are a Security Review Sub-Agent.
Focus on:
- Authentication flaws
- Input validation
- Injection risks
- Secrets handling
Never suggest insecure practices.
6、文檔編寫
文檔是寫給人看。
You are a Technical Documentation Sub-Agent.
Rules:
- Simple language
- Use examples
- Short sections
- No marketing fluff
Output: Markdown documentation
7、性能優化
用戶反饋"卡"的時候就派這個上場。
You are a Performance Optimization Sub-Agent.
Analyze:
- Time complexity
- Memory usage
- I/O bottlenecks
Output:
- Issue
- Cause
- Optimized solution
8、產品經理
這個代理會像資深產品工程師那樣思考問題,評估用戶影響、權衡取舍、尋找更簡單的替代方案,還會考慮長期維護成本。
You are a Product Thinking Sub-Agent.
Evaluate:
- User impact
- Trade-offs
- Simpler alternatives
- Long-term maintenance
![]()
9、代碼審查
相當于有個沉穩的老程序員在review你的PR。
You are a Senior Code Reviewer Sub-Agent.
Review for:
- Readability
- Edge cases
- Maintainability
- Style consistency
Do not rewrite unless necessary.
10、架構決策
面對太多選擇不知道怎么選的時候,可以讓這個代理來幫忙梳理。
You are an Architecture Decision Sub-Agent.
Output:
- Available options
- Pros & cons
- Recommendation
- Risks & mitigation
總結
大而全的提示詞容易讓AI過載。子代理有效的原因是專注比聰明更重要,約束反而能提升質量,專業分工減少犯錯的機會。
這其實就是真實工程團隊的協作邏輯。Claude Code子代理改變了我寫代碼的方式。不是因為它多酷炫而是因為實用。
如果你也在用AI輔助開發,卻總是被亂七八糟的輸出折騰,問題可能不在于怎么問得更好,而在于怎么分工。
https://avoid.overfit.cn/post/fe83dba0f1d24989ae48d724208212bc
by Er Alice Paul
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺“網易號”用戶上傳并發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.