如何讓 Claude Code 也擁有百萬上下文窗口?

更新於 發佈於 閱讀時間約 11 分鐘

相信用過 Claude Code 的朋友,在 Gemini CLI 剛問世時,都迫不及待地去嚐鮮了吧?但不得不說,與 Claude Code 相比,Gemini 的表現確實不盡如人意。它不僅速度慢,工具調用也讓人一言難盡。

不過話說回來,誰能抗拒 Gemini 那長達百萬的上下文窗口呢?只可惜,它目前的表現又不足以獨挑大梁。

下面分享我的一些經驗:

如何解決 Gemini「難用」與 Claude Code「上下文太短」的問題?

於是,我最近一直在探索一些協作方法,希望能讓它們互補短板。接下來,就給大家展示一下我近期的工作場景:

raw-image

強強聯手:Claude Code 與 Gemini CLI 的夢幻聯動

我們可以清楚看到,我的 Claude Code 在處理超長內容時,會「識趣」地選擇調用 Gemini CLI 來進行處理。然後,它會根據 Gemini 的分析結果,再親自操刀,幫我完成任務。

這樣一來,你瞬間就擁有了一個具備超長上下文處理能力的「滿血版」Claude Code!是不是感覺賺大了?

這種協作模式的實現方式非常簡單,我的靈感最初來自 Anthropic 的官方文檔:

raw-image
raw-image

這就是我們想要的東西!

管道工具能直接幫我們實現標準格式的輸入與輸出,而這一切,只需使用 -p 這個簡單的參數。

因此,Claude Code 的實現邏輯是:它基於調用 Bash 工具來啟動 Gemini CLI,CLI 會將結果打印到標準輸出(stdout),而 Claude Code 再通過 Bash 工具捕獲這個輸出。整個過程的示意圖如下:

raw-image

這種設計的巧妙之處在於:

  • 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台灣學習小組加入!感激不盡。

留言
avatar-img
留言分享你的想法!
avatar-img
KoiCode的沙龍
1會員
5內容數
解鎖更多claude code/程式碼相關的玩法和黑科技
你可能也想看
Thumbnail
嗨!歡迎來到 vocus vocus 方格子是台灣最大的內容創作與知識變現平台,並且計畫持續拓展東南亞等等國際市場。我們致力於打造讓創作者能夠自由發表、累積影響力並獲得實質收益的創作生態圈!「創作至上」是我們的核心價值,我們致力於透過平台功能與服務,賦予創作者更多的可能。 vocus 平台匯聚了
Thumbnail
嗨!歡迎來到 vocus vocus 方格子是台灣最大的內容創作與知識變現平台,並且計畫持續拓展東南亞等等國際市場。我們致力於打造讓創作者能夠自由發表、累積影響力並獲得實質收益的創作生態圈!「創作至上」是我們的核心價值,我們致力於透過平台功能與服務,賦予創作者更多的可能。 vocus 平台匯聚了
Thumbnail
透過 No Code 工具 Glide 打造專屬 APP,Glide 支援無縫串接資料庫(如 Google Sheets, Airtable) 、即時預覽的簡易介面設計、串接第三方工具的自動化流程和強大 AI 功能,還有免費豐富範本供直接套用,無需程式和設計經驗,也能將電子表格數據轉換為 APP!
Thumbnail
透過 No Code 工具 Glide 打造專屬 APP,Glide 支援無縫串接資料庫(如 Google Sheets, Airtable) 、即時預覽的簡易介面設計、串接第三方工具的自動化流程和強大 AI 功能,還有免費豐富範本供直接套用,無需程式和設計經驗,也能將電子表格數據轉換為 APP!
Thumbnail
基於大型語言模型的 AI 聊天服務可以協助完成多項工作,為了提升 AI 聊天的互動便利性,許多開發者打造了不同性質的擴充功能,Ask Screenshot for Gemini 就是一款能快速將網頁文字與截圖傳送至 Gemini 對話框的實用工具。
Thumbnail
基於大型語言模型的 AI 聊天服務可以協助完成多項工作,為了提升 AI 聊天的互動便利性,許多開發者打造了不同性質的擴充功能,Ask Screenshot for Gemini 就是一款能快速將網頁文字與截圖傳送至 Gemini 對話框的實用工具。
Thumbnail
Relay.app 是簡單強大的自動化 No Code 工具,支援約百種數位工具,操作介面簡單易上手。內建串接 OpenAI,只要簡單輸入指令,即可獲得 AI 生成回覆!更棒的是,可以整合真人決策進入自動化工作流程,使人力介入不再中斷自動化,打造更全面完整工作流程,提升團隊協作,專案管理更有效率!
Thumbnail
Relay.app 是簡單強大的自動化 No Code 工具,支援約百種數位工具,操作介面簡單易上手。內建串接 OpenAI,只要簡單輸入指令,即可獲得 AI 生成回覆!更棒的是,可以整合真人決策進入自動化工作流程,使人力介入不再中斷自動化,打造更全面完整工作流程,提升團隊協作,專案管理更有效率!
Thumbnail
近日,Google Chrome 發佈最新版本,新版瀏覽器多了「與 Gemini 對話」,在網址列輸入「@」可以選擇與 Gemini 對話,接著輸入對話指令即可傳送至 Gemini 聊天服務並獲得回覆。
Thumbnail
近日,Google Chrome 發佈最新版本,新版瀏覽器多了「與 Gemini 對話」,在網址列輸入「@」可以選擇與 Gemini 對話,接著輸入對話指令即可傳送至 Gemini 聊天服務並獲得回覆。
Thumbnail
透過專案管理工具 ClickUp,一站整合專案任務、內容協作、資源管理、自動化流程,有效提升生產力!ClickUp 支援多元檢視模式,讓你自由切換甘特圖、心智圖、樹狀圖、月曆等形式,還提供可視化圖表功能,協助掌握團隊成員負荷程度及專案耗費時間。另有文件共編功能和自動化模組供直接套用,省下重複瑣碎工作
Thumbnail
透過專案管理工具 ClickUp,一站整合專案任務、內容協作、資源管理、自動化流程,有效提升生產力!ClickUp 支援多元檢視模式,讓你自由切換甘特圖、心智圖、樹狀圖、月曆等形式,還提供可視化圖表功能,協助掌握團隊成員負荷程度及專案耗費時間。另有文件共編功能和自動化模組供直接套用,省下重複瑣碎工作
Thumbnail
Gemini Pro 1.0 和 Gemini Pro 1.5 都是強大的工具,但它們適用於不同的需求並提供不同的功能。以下是它們的主要區別: 模態性 Gemini Pro 1.0 主要基於文字,對圖片和視頻的支持有限(1.0-pro-vision)。 Gemini Pro 1.5 真正的多模
Thumbnail
Gemini Pro 1.0 和 Gemini Pro 1.5 都是強大的工具,但它們適用於不同的需求並提供不同的功能。以下是它們的主要區別: 模態性 Gemini Pro 1.0 主要基於文字,對圖片和視頻的支持有限(1.0-pro-vision)。 Gemini Pro 1.5 真正的多模
Thumbnail
Gamma 是一款基於人工智慧的設計工具,幫助用戶快速生成精美的簡報、文件以及網頁、及社群貼文。
Thumbnail
Gamma 是一款基於人工智慧的設計工具,幫助用戶快速生成精美的簡報、文件以及網頁、及社群貼文。
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News