This page looks best with JavaScript enabled

Analyzing gemini-cliโ€™s Source Code with gemini-cli

 ·  โ˜• 11 min read

logo

Overview

This article introduces how to use the gemini-cli command-line tool to analyze its own source code repository. Through a series of interactive commands, we will explore the design and implementation of gemini-cli. This approach not only demonstrates the functionality of AI tools but also provides developers with a new way of learning and exploration.

If you donโ€™t know what gemini-cli is, hereโ€™s a brief introduction:
gemini-cli is an open-source Command-Line Interface (CLI) tool that allows developers and users to interact directly with Googleโ€™s Gemini model in the terminal. You can think of it as an intelligent AI assistant inside the command line.

Motivation

When learning a new field, studying only theories and concepts is often not deep enough, and it can easily feel tedious. In contrast, analyzing the source code of a real project can provide a more intuitive understanding of its design and implementation details. This applies to learning Kubernetes as well as AI applications. Analyzing the source code of an AI tool is both interesting and challenging.

One way to evaluate whether a tool is mature is to see if it can โ€œbootstrapโ€ itself. Bootstrapping refers to a tool being able to use its own functions to handle its own tasks.
Anyone with experience building OpenJDK knows that in order to compile OpenJDK, you first need to install a JDK (even a relatively older version works), and then use that installed JDK to build the target JDK. In computer terminology, this process is called bootstrapping. In the AI field, we can also use AI tools to analyze and understand the source code of AI tools themselves. This approach can be seen as a bootstrapping processโ€”โ€œusing oneโ€™s own spear against oneโ€™s own shield.โ€

Therefore, using Gemini-CLI to analyze Gemini-CLIโ€™s own source code, I think, is both very interesting and very practical.

Analysis

Every time we look at the gemini-cli source code, we can either use AI analysis tools or just read the code ourselves. My suggestion is to start with its High-Level design documents. In the AI era, human-written thingsโ€”especially high-level documentsโ€”are especially valuable: Gemini CLI Architecture Overview

1
2
3
4
5
6
git clone https://github.com/google-gemini/gemini-cli.git

cd gemini-cli.git

# start gemini-cli
gemini-cli

Prompts

When analyzing the source code of LLM applications, the most direct and easiest entry point is of course the prompts. Prompts are the core of an LLM application and directly determine the modelโ€™s behavior and output.

Software Engineering Tasks

Question:

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚  > Explain the prompts in @packages/core/src/core/prompts.ts and illustrate with one โ”‚
โ”‚    example.                                                                          โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Analysis result:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
โœฆ The file packages/core/src/core/prompts.ts mainly contains two types of prompts:

   1. Core System Prompt (`getCoreSystemPrompt`): This is the main prompt defining the agentโ€™s behavior, responsibilities, and operational guidelines. It specifies in detail how the agent should follow principles, workflows, and safety guidelines when performing software engineering tasks or developing new applications.

   2. History Compression Prompt (`getCompressionPrompt`): This prompt guides the model to compress conversation history into a structured XML snapshot, so the agent can effectively manage and recall past interactions and information.

  Example of Core System Prompt:

  Here is a part of the workflow for โ€œSoftware Engineering Tasksโ€ in getCoreSystemPrompt, which guides the agent on how to handle such requests:

  ## Software Engineering Tasks
  When requested to perform tasks like fixing bugs, adding features, refactoring, or explaining code, follow this sequence:
  1. **Understand:** Think about the user's request and the relevant codebase context. Use '${GrepTool.Name}' and '${GlobTool.Name}' search tools extensively (in parallel if independent) to understand file 
    structures, existing code patterns, and conventions. Use '${ReadFileTool.Name}' and '${ReadManyFilesTool.Name}' to understand context and validate any assumptions you may have.
  2. **Plan:** Build a coherent and grounded (based on the understanding in step 1) plan for how you intend to resolve the user's task. Share an extremely concise yet clear plan with the user if it would help the 
    user understand your thought process. As part of the plan, you should try to use a self-verification loop by writing unit tests if relevant to the task. Use output logs or debug statements as part of this self 
    verification loop to arrive at a solution.
  3. **Implement:** Use the available tools (e.g., '${EditTool.Name}', '${WriteFileTool.Name}' '${ShellTool.Name}' ...) to act on the plan, strictly adhering to the project's established conventions (detailed 
    under 'Core Mandates').
  4. **Verify (Tests):** If applicable and feasible, verify the changes using the project's testing procedures. Identify the correct test commands and frameworks by examining 'README' files, build/package 
    configuration (e.g., 'package.json'), or existing test execution patterns. NEVER assume standard test commands.
  5. **Verify (Standards):** VERY IMPORTANT: After making code changes, execute the project-specific build, linting and type-checking commands (e.g., 'tsc', 'npm run lint', 'ruff check .') that you have identified
    for this project (or obtained from the user). This ensures code quality and adherence to standards. If unsure about these commands, you can ask the user if they'd like you to run them and if so how to.

  This example shows that the agent is explicitly instructed to follow a structured five-step process when handling software engineering tasks: understand, plan, implement, verify through testing, and verify standards. It also mentions the specific tools (such as GrepTool, ReadFileTool, ShellTool, etc.) that can be used at each stage, ensuring the agentโ€™s behavior is predictable and efficient.

Here, the โ€œagentโ€ refers to the AI Agent.

New Applications

Next, letโ€™s analyze how gemini-cli handles the creation of new applications.

Question:

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚  > Can you give another example? โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Analysis result:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
โœฆ Certainly, I can provide another example. This time letโ€™s look at the workflow for โ€œNew Applicationsโ€ in getCoreSystemPrompt:

  ## New Applications
  
  **Goal:** Autonomously implement and deliver a visually appealing, substantially complete, and functional prototype. Utilize all tools at your disposal to implement the application. Some tools you may
  especially find useful are '${WriteFileTool.Name}', '${EditTool.Name}' and '${ShellTool.Name}'.
  
  1. **Understand Requirements:** Analyze the user's request to identify core features, desired user experience (UX), visual aesthetic, application type/platform (web, mobile, desktop, CLI, library, 2D or 3D 
  game), and explicit constraints. If critical information for initial planning is missing or ambiguous, ask concise, targeted clarification questions.
  2. **Propose Plan:** Formulate an internal development plan. Present a clear, concise, high-level summary to the user. This summary must effectively convey the application's type and core purpose, key 
  technologies to be used, main features and how users will interact with them, and the general approach to the visual design and user experience (UX) with the intention of delivering something beautiful, modern,
  and polished, especially for UI-based applications. For applications requiring visual assets (like games or rich UIs), briefly describe the strategy for sourcing or generating placeholders (e.g., simple 
  geometric shapes, procedurally generated patterns, or open-source assets if feasible and licenses permit) to ensure a visually complete initial prototype. Ensure this information is presented in a structured 
  and easily digestible manner.
    - When key technologies aren't specified, prefer the following:
    - **Websites (Frontend):** React (JavaScript/TypeScript) with Bootstrap CSS, incorporating Material Design principles for UI/UX.
    - **Back-End APIs:** Node.js with Express.js (JavaScript/TypeScript) or Python with FastAPI.
  - **Full-stack:** Next.js (React/Node.js) using Bootstrap CSS and Material Design principles for the frontend, or Python (Django/Flask) for the backend with a React/Vue.js frontend styled with Bootstrap CSS
  and Material Design principles.
  - **CLIs:** Python or Go.
  - **Mobile App:** Compose Multiplatform (Kotlin Multiplatform) or Flutter (Dart) using Material Design libraries and principles, when sharing code between Android and iOS. Jetpack Compose (Kotlin JVM) with
  Material Design principles or SwiftUI (Swift) for native apps targeted at either Android or iOS, respectively.
  - **3d Games:** HTML/CSS/JavaScript with Three.js.
  - **2d Games:** HTML/CSS/JavaScript.
3. **User Approval:** Obtain user approval for the proposed plan.
4. **Implementation:** Autonomously implement each feature and design element per the approved plan utilizing all available tools. When starting ensure you scaffold the application using '${ShellTool.Name}' for
  commands like 'npm init', 'npx create-react-app'. Aim for full scope completion. Proactively create or source necessary placeholder assets (e.g., images, icons, game sprites, 3D models using basic primitives if
  complex assets are not generatable) to ensure the application is visually coherent and functional, minimizing reliance on the user to provide these. If the model can generate simple assets (e.g., a uniformly 
  colored square sprite, a simple 3D cube), it should do so. Otherwise, it should clearly indicate what kind of placeholder has been used and, if absolutely necessary, what the user might replace it with. Use 
  placeholders only when essential for progress, intending to replace them with more refined versions or instruct the user on replacement during polishing if generation is not feasible.
5. **Verify:** Review work against the original request, the approved plan. Fix bugs, deviations, and all placeholders where feasible, or ensure placeholders are visually adequate for a prototype. Ensure
  styling, interactions, produce a high-quality, functional and beautiful prototype aligned with design goals. Finally, but MOST importantly, build the application and ensure there are no compile errors.
6. **Solicit Feedback:** If still applicable, provide instructions on how to start the application and request user feedback on the prototype.

  This example shows the detailed workflow the agent follows when creating new applications, including:

   1. Understanding requirements: analyzing the userโ€™s request to clarify core functions, UX, aesthetics, and constraints.
   2. Proposing a plan: creating an internal development plan and presenting a concise, high-level summary to the user.
   3. User approval: obtaining user approval for the plan.
   4. Implementation: autonomously implementing features and design, creating or sourcing necessary placeholder assets.
   5. Verification: reviewing against the original request and plan, fixing bugs and ensuring visual and functional quality.
   6. Soliciting feedback: providing startup instructions and requesting user feedback.

  This example also shows the preferred technology stacks the agent chooses when none are specifiedโ€”for example, React for frontend, Node.js or Python for backend, etc.

Interesting Side Notes

I must admit, Iโ€™m a bit lazy. When I was thinking about writing this article, I hoped gemini-cli could help me generate one about using gemini-cli to analyze its own source code. So, I sent it a prompt:

> Generate a Hugo markdown article placed at /home/labile/blog/content/zh/posts/ai about "using gemini-cli as a tool to analyze the gemini-cli source repository".

โœฆ Sure, I will generate this Hugo Markdown article for you.

  I will create the file /home/labile/blog/content/zh/posts/ai/analyzing-gemini-cli-with-gemini-cli.md.

  This article will explore how to use the powerful gemini-cli command-line tool to interactively analyze and understand its own source repository, demonstrating a modern learning method in software developmentโ€”โ€œusing oneโ€™s own spear against oneโ€™s own shield.โ€

  Now, I will create this article for you.

  โœ”  WriteFile Writing to /analyzing-gemini-cli-with-gemini-cli.md      
 โ”‚      2 title: "Using Oneโ€™s Own Spear Against Oneโ€™s Own Shield: Analyzing Gemini-CLIโ€™s Own Source Code"       
 โ”‚      3 date: 2025-08-19T10:00:00+08:00                               
 โ”‚      4 draft: false                                                  
 โ”‚      5 tags: ["Gemini", "CLI", "AI", "Source Analysis", "Developer Tools"]  
 โ”‚      6 categories: ["AI Applications"]                                        
 โ”‚      7 ---                                                           
 โ”‚      8                                                               
 โ”‚      9 ## Introduction                                                       
 โ”‚     10                                                               
 โ”‚     11 In the field of software development, โ€ฆโ€ฆ

 โœฆ I have successfully created the analyzing-gemini-cli-with-gemini-cli.md file under /home/labile/blog/content/zh/posts/ai/.

  You can now check the file or directly use Hugo to build your site and preview the article. If you need any modifications, please let me know.

But I must say, most of this article was still handwritten by me.

Conclusion

As an IT professional with 20 years of experience, each time I face a technological shift, I either actively or passively learn it. From Cloud Native to AI applications, Iโ€™ve had a feelingโ€”one Iโ€™ve kept to myself for some time: the learning curve for practical skills is getting steeper, and the cost of learning is getting higher:

  • Sometimes, hardware resources are the limitation, like CPU and memory in Cloud Native, or GPU in AI. This once again widens the gap between wealthy and less wealthy learners.
  • Sometimes, itโ€™s being overwhelmed by the vast ocean of knowledge, not knowing where to start, as new technologies, tools, and frameworks keep emerging every day.

Ironically, while writing this article in VSCode, the gemini code assist plugin auto-suggested a completion:

But I believe that the emergence of AI tools, especially those like gemini-cli, will greatly lower the barriers for us to learn and master new technologies. They can not only help us understand complex code and concepts more quickly, but also give us immediate feedback and guidance in practice.

Thatโ€™s it. In the past, we said that internet information powered by recommendation engines influenced peopleโ€™s independent thinking. Now, with the prevalence of AI tools, not only will people become more dependent on AI to acquire information, but AI will also influence the generation and output of information at its source! How can we ensure peopleโ€™s independent thinking before AI dominates all information? Or perhaps we donโ€™t need such an ability anymore? Is living comfortably with the flow more important than independent thought?

logo

I know this steak doesnโ€™t exist. I know that when I put it in my mouth, the Matrix is telling my brain that it is juicy and delicious. After nine years, you know what I realize? Ignorance is bliss.
โ€”โ€” Cypher @ The Matrix (1999)

One last note: I am currently unemployed. I am open for work. If you have suitable job opportunities, feel free to contact me.

Share on

Mark Zhu
WRITTEN BY
Mark Zhu
An old developer