相信用過 Claude Code 的朋友,在 Gemini CLI 剛問世時,都迫不及待地去嚐鮮了吧?但不得不說,與 Claude Code 相比,Gemini 的表現確實不盡如人意。它不僅速度慢,工具調用也讓人一言難盡。
不過話說回來,誰能抗拒 Gemini 那長達百萬的上下文窗口呢?只可惜,它目前的表現又不足以獨挑大梁。
下面分享我的一些經驗:
如何解決 Gemini「難用」與 Claude Code「上下文太短」的問題?
於是,我最近一直在探索一些協作方法,希望能讓它們互補短板。接下來,就給大家展示一下我近期的工作場景:

強強聯手:Claude Code 與 Gemini CLI 的夢幻聯動
我們可以清楚看到,我的 Claude Code 在處理超長內容時,會「識趣」地選擇調用 Gemini CLI 來進行處理。然後,它會根據 Gemini 的分析結果,再親自操刀,幫我完成任務。
這樣一來,你瞬間就擁有了一個具備超長上下文處理能力的「滿血版」Claude Code!是不是感覺賺大了?
這種協作模式的實現方式非常簡單,我的靈感最初來自 Anthropic 的官方文檔:


這就是我們想要的東西!
管道工具能直接幫我們實現標準格式的輸入與輸出,而這一切,只需使用 -p
這個簡單的參數。
因此,Claude Code 的實現邏輯是:它基於調用 Bash 工具來啟動 Gemini CLI,CLI 會將結果打印到標準輸出(stdout),而 Claude Code 再通過 Bash 工具捕獲這個輸出。整個過程的示意圖如下:

這種設計的巧妙之處在於:
- Gemini:擁有超大的上下文窗口,完美適配分析整個代碼庫的需求。
- Claude Code:具備更強的代碼理解和交互能力,擅長提供精準建議和執行具體任務。
- 組合效果:Claude Code 可以智能調用 Gemini 處理那些它自己無法處理的大型分析任務,然後基於 Gemini 返回的分析結果,提供更精準、更深入的建議和執行。
本質上,這是兩個 AI 系統的協作:Gemini 負責「看」海量的代碼,而 Claude Code 則負責「理解」這些代碼並進行「交互」。
CLAUDE.md 中的提示詞配置
將以下內容(或根據你的實際需求修改後)添加到你的 CLAUDE.md
文件中,然後告訴 Claude 手動使用 Gemini
,或者讓它根據需要自行調用:
# Using Gemini CLI for Large Codebase Analysis
When analyzing large codebases or multiple files that might exceed context limits, use the Gemini CLI with its massive
context window. Use `gemini -p` to leverage Google Gemini's large context capacity.
## File and Directory Inclusion Syntax
Use the `@` syntax to include files and directories in your Gemini prompts. The paths should be relative to WHERE you run the
gemini command:
### Examples:
**Single file analysis:**
gemini -p "@src/main.py Explain this file's purpose and structure"
Multiple files:
gemini -p "@package.json @src/index.js Analyze the dependencies used in the code"
Entire directory:
gemini -p "@src/ Summarize the architecture of this codebase"
Multiple directories:
gemini -p "@src/ @tests/ Analyze test coverage for the source code"
Current directory and subdirectories:
gemini -p "@./ Give me an overview of this entire project"
# Or use --all_files flag:
gemini --all_files -p "Analyze the project structure and dependencies"
Implementation Verification Examples
Check if a feature is implemented:
gemini -p "@src/ @lib/ Has dark mode been implemented in this codebase? Show me the relevant files and functions"
Verify authentication implementation:
gemini -p "@src/ @middleware/ Is JWT authentication implemented? List all auth-related endpoints and middleware"
Check for specific patterns:
gemini -p "@src/ Are there any React hooks that handle WebSocket connections? List them with file paths"
Verify error handling:
gemini -p "@src/ @api/ Is proper error handling implemented for all API endpoints? Show examples of try-catch blocks"
Check for rate limiting:
gemini -p "@backend/ @middleware/ Is rate limiting implemented for the API? Show the implementation details"
Verify caching strategy:
gemini -p "@src/ @lib/ @services/ Is Redis caching implemented? List all cache-related functions and their usage"
Check for specific security measures:
gemini -p "@src/ @api/ Are SQL injection protections implemented? Show how user inputs are sanitized"
Verify test coverage for features:
gemini -p "@src/payment/ @tests/ Is the payment processing module fully tested? List all test cases"
When to Use Gemini CLI
Use gemini -p when:
- Analyzing entire codebases or large directories
- Comparing multiple large files
- Need to understand project-wide patterns or architecture
- Current context window is insufficient for the task
- Working with files totaling more than 100KB
- Verifying if specific features, patterns, or security measures are implemented
- Checking for the presence of certain coding patterns across the entire codebase
Important Notes
- Paths in @ syntax are relative to your current working directory when invoking gemini
- The CLI will include file contents directly in the context
- No need for --yolo flag for read-only analysis
- Gemini's context window can handle entire codebases that would overflow Claude's context
- When checking implementations, be specific about what you're looking for to get accurate results
好了,以上就是今天的文章內容。
小弟創建了一個有關ClaudeCode以及寫程式碼的DC群組,目前還處於起步狀態,希望各位能多多支持!每天都會在群組裏分享ClaudeCode的使用技巧。如果大家感興趣,請點按ClaudeCode台灣學習小組加入!感激不盡。