2025-12-01 11:38:48 -08:00
|
|
|
|
# Gemini CLI core
|
2025-05-15 20:04:33 -07:00
|
|
|
|
|
2025-10-09 08:17:37 -04:00
|
|
|
|
Gemini CLI's core package (`packages/core`) is the backend portion of Gemini
|
|
|
|
|
|
CLI, handling communication with the Gemini API, managing tools, and processing
|
|
|
|
|
|
requests sent from `packages/cli`. For a general overview of Gemini CLI, see the
|
|
|
|
|
|
[main documentation page](../index.md).
|
2025-06-07 10:47:30 -07:00
|
|
|
|
|
2025-06-23 14:07:03 -07:00
|
|
|
|
## Navigating this section
|
2025-06-07 10:47:30 -07:00
|
|
|
|
|
2026-01-27 18:24:37 +00:00
|
|
|
|
- **[Sub-agents (experimental)](./subagents.md):** Learn how to create and use
|
|
|
|
|
|
specialized sub-agents for complex tasks.
|
2026-03-04 12:16:33 -08:00
|
|
|
|
- **[Core tools reference](../reference/tools.md):** Information on how tools
|
|
|
|
|
|
are defined, registered, and used by the core.
|
2026-02-19 15:47:39 -08:00
|
|
|
|
- **[Memory Import Processor](../reference/memport.md):** Documentation for the
|
|
|
|
|
|
modular GEMINI.md import feature using @file.md syntax.
|
|
|
|
|
|
- **[Policy Engine](../reference/policy-engine.md):** Use the Policy Engine for
|
2025-10-31 13:23:56 -07:00
|
|
|
|
fine-grained control over tool execution.
|
2025-05-15 20:04:33 -07:00
|
|
|
|
|
2025-06-23 14:07:03 -07:00
|
|
|
|
## Role of the core
|
2025-05-15 20:04:33 -07:00
|
|
|
|
|
2025-10-09 08:17:37 -04:00
|
|
|
|
While the `packages/cli` portion of Gemini CLI provides the user interface,
|
|
|
|
|
|
`packages/core` is responsible for:
|
2025-05-15 20:04:33 -07:00
|
|
|
|
|
2025-10-09 08:17:37 -04:00
|
|
|
|
- **Gemini API interaction:** Securely communicating with the Google Gemini API,
|
|
|
|
|
|
sending user prompts, and receiving model responses.
|
|
|
|
|
|
- **Prompt engineering:** Constructing effective prompts for the Gemini model,
|
|
|
|
|
|
potentially incorporating conversation history, tool definitions, and
|
|
|
|
|
|
instructional context from `GEMINI.md` files.
|
2025-06-23 14:07:03 -07:00
|
|
|
|
- **Tool management & orchestration:**
|
2025-10-09 08:17:37 -04:00
|
|
|
|
- Registering available tools (e.g., file system tools, shell command
|
|
|
|
|
|
execution).
|
2025-05-15 20:04:33 -07:00
|
|
|
|
- Interpreting tool use requests from the Gemini model.
|
|
|
|
|
|
- Executing the requested tools with the provided arguments.
|
|
|
|
|
|
- Returning tool execution results to the Gemini model for further processing.
|
2025-10-09 08:17:37 -04:00
|
|
|
|
- **Session and state management:** Keeping track of the conversation state,
|
|
|
|
|
|
including history and any relevant context required for coherent interactions.
|
|
|
|
|
|
- **Configuration:** Managing core-specific configurations, such as API key
|
|
|
|
|
|
access, model selection, and tool settings.
|
2025-05-15 20:04:33 -07:00
|
|
|
|
|
2025-06-23 14:07:03 -07:00
|
|
|
|
## Security considerations
|
2025-05-15 20:04:33 -07:00
|
|
|
|
|
2025-05-30 18:25:47 -07:00
|
|
|
|
The core plays a vital role in security:
|
2025-05-15 20:04:33 -07:00
|
|
|
|
|
2025-10-09 08:17:37 -04:00
|
|
|
|
- **API key management:** It handles the `GEMINI_API_KEY` and ensures it's used
|
|
|
|
|
|
securely when communicating with the Gemini API.
|
|
|
|
|
|
- **Tool execution:** When tools interact with the local system (e.g.,
|
|
|
|
|
|
`run_shell_command`), the core (and its underlying tool implementations) must
|
|
|
|
|
|
do so with appropriate caution, often involving sandboxing mechanisms to
|
|
|
|
|
|
prevent unintended modifications.
|
2025-05-15 20:04:33 -07:00
|
|
|
|
|
2025-06-23 14:07:03 -07:00
|
|
|
|
## Chat history compression
|
2025-05-15 20:04:33 -07:00
|
|
|
|
|
2025-10-09 08:17:37 -04:00
|
|
|
|
To ensure that long conversations don't exceed the token limits of the Gemini
|
|
|
|
|
|
model, the core includes a chat history compression feature.
|
2025-06-07 10:47:30 -07:00
|
|
|
|
|
2025-10-09 08:17:37 -04:00
|
|
|
|
When a conversation approaches the token limit for the configured model, the
|
|
|
|
|
|
core automatically compresses the conversation history before sending it to the
|
|
|
|
|
|
model. This compression is designed to be lossless in terms of the information
|
|
|
|
|
|
conveyed, but it reduces the overall number of tokens used.
|
2025-06-07 10:47:30 -07:00
|
|
|
|
|
2025-10-09 08:17:37 -04:00
|
|
|
|
You can find the token limits for each model in the
|
|
|
|
|
|
[Google AI documentation](https://ai.google.dev/gemini-api/docs/models).
|
2025-06-07 10:47:30 -07:00
|
|
|
|
|
2025-06-23 14:07:03 -07:00
|
|
|
|
## Model fallback
|
2025-06-07 10:47:30 -07:00
|
|
|
|
|
2025-10-09 08:17:37 -04:00
|
|
|
|
Gemini CLI includes a model fallback mechanism to ensure that you can continue
|
|
|
|
|
|
to use the CLI even if the default "pro" model is rate-limited.
|
2025-06-07 10:47:30 -07:00
|
|
|
|
|
2025-10-09 08:17:37 -04:00
|
|
|
|
If you are using the default "pro" model and the CLI detects that you are being
|
|
|
|
|
|
rate-limited, it automatically switches to the "flash" model for the current
|
|
|
|
|
|
session. This allows you to continue working without interruption.
|
2025-06-07 10:47:30 -07:00
|
|
|
|
|
2026-01-20 14:43:43 -05:00
|
|
|
|
Internal utility calls that use `gemini-2.5-flash-lite` (for example, prompt
|
|
|
|
|
|
completion and classification) silently fall back to `gemini-2.5-flash` and
|
|
|
|
|
|
`gemini-2.5-pro` when quota is exhausted, without changing the configured model.
|
|
|
|
|
|
|
2025-06-23 14:07:03 -07:00
|
|
|
|
## File discovery service
|
2025-06-07 10:47:30 -07:00
|
|
|
|
|
2025-10-09 08:17:37 -04:00
|
|
|
|
The file discovery service is responsible for finding files in the project that
|
|
|
|
|
|
are relevant to the current context. It is used by the `@` command and other
|
|
|
|
|
|
tools that need to access files.
|
2025-06-07 10:47:30 -07:00
|
|
|
|
|
2025-06-23 14:07:03 -07:00
|
|
|
|
## Memory discovery service
|
2025-06-07 10:47:30 -07:00
|
|
|
|
|
2025-10-09 08:17:37 -04:00
|
|
|
|
The memory discovery service is responsible for finding and loading the
|
|
|
|
|
|
`GEMINI.md` files that provide context to the model. It searches for these files
|
|
|
|
|
|
in a hierarchical manner, starting from the current working directory and moving
|
|
|
|
|
|
up to the project root and the user's home directory. It also searches in
|
|
|
|
|
|
subdirectories.
|
2025-06-07 10:47:30 -07:00
|
|
|
|
|
2025-10-09 08:17:37 -04:00
|
|
|
|
This allows you to have global, project-level, and component-level context
|
|
|
|
|
|
files, which are all combined to provide the model with the most relevant
|
|
|
|
|
|
information.
|
2025-05-15 20:04:33 -07:00
|
|
|
|
|
2026-02-19 15:47:39 -08:00
|
|
|
|
You can use the [`/memory` command](../reference/commands.md) to `show`, `add`,
|
|
|
|
|
|
and `refresh` the content of loaded `GEMINI.md` files.
|
2025-10-08 14:00:44 -07:00
|
|
|
|
|
|
|
|
|
|
## Citations
|
|
|
|
|
|
|
2025-10-09 08:17:37 -04:00
|
|
|
|
When Gemini finds it is reciting text from a source it appends the citation to
|
|
|
|
|
|
the output. It is enabled by default but can be disabled with the
|
|
|
|
|
|
ui.showCitations setting.
|
2025-10-08 14:00:44 -07:00
|
|
|
|
|
2025-10-09 08:17:37 -04:00
|
|
|
|
- When proposing an edit the citations display before giving the user the option
|
|
|
|
|
|
to accept.
|
2025-10-08 14:00:44 -07:00
|
|
|
|
- Citations are always shown at the end of the model’s turn.
|
|
|
|
|
|
- We deduplicate citations and display them in alphabetical order.
|