|
| 1 | +# Session Management |
| 2 | + |
| 3 | +Gemini CLI includes robust session management features that automatically save |
| 4 | +your conversation history. This allows you to interrupt your work and resume |
| 5 | +exactly where you left off, review past interactions, and manage your |
| 6 | +conversation history effectively. |
| 7 | + |
| 8 | +## Automatic Saving |
| 9 | + |
| 10 | +Every time you interact with Gemini CLI, your session is automatically saved. |
| 11 | +This happens in the background without any manual intervention. |
| 12 | + |
| 13 | +- **What is saved:** The complete conversation history, including: |
| 14 | + - Your prompts and the model's responses. |
| 15 | + - All tool executions (inputs and outputs). |
| 16 | + - Token usage statistics (input/output/cached, etc.). |
| 17 | + - Assistant thoughts/reasoning summaries (when available). |
| 18 | +- **Location:** Sessions are stored in `~/.gemini/tmp/<project_hash>/chats/`. |
| 19 | +- **Scope:** Sessions are project-specific. Switching directories to a different |
| 20 | + project will switch to that project's session history. |
| 21 | + |
| 22 | +## Resuming Sessions |
| 23 | + |
| 24 | +You can resume a previous session to continue the conversation with all prior |
| 25 | +context restored. |
| 26 | + |
| 27 | +### From the Command Line |
| 28 | + |
| 29 | +When starting the CLI, you can use the `--resume` (or `-r`) flag: |
| 30 | + |
| 31 | +- **Resume latest:** |
| 32 | + |
| 33 | + ```bash |
| 34 | + gemini --resume |
| 35 | + ``` |
| 36 | + |
| 37 | + This immediately loads the most recent session. |
| 38 | + |
| 39 | +- **Resume by index:** First, list available sessions (see |
| 40 | + [Listing Sessions](#listing-sessions)), then use the index number: |
| 41 | + |
| 42 | + ```bash |
| 43 | + gemini --resume 1 |
| 44 | + ``` |
| 45 | + |
| 46 | +- **Resume by ID:** You can also provide the full session UUID: |
| 47 | + ```bash |
| 48 | + gemini --resume a1b2c3d4-e5f6-7890-abcd-ef1234567890 |
| 49 | + ``` |
| 50 | + |
| 51 | +### From the Interactive Interface |
| 52 | + |
| 53 | +While the CLI is running, you can use the `/resume` slash command to open the |
| 54 | +**Session Browser**: |
| 55 | + |
| 56 | +```text |
| 57 | +/resume |
| 58 | +``` |
| 59 | + |
| 60 | +This opens an interactive interface where you can: |
| 61 | + |
| 62 | +- **Browse:** Scroll through a list of your past sessions. |
| 63 | +- **Preview:** See details like the session date, message count, and the first |
| 64 | + user prompt. |
| 65 | +- **Search:** Press `/` to enter search mode, then type to filter sessions by ID |
| 66 | + or content. |
| 67 | +- **Select:** Press `Enter` to resume the selected session. |
| 68 | + |
| 69 | +## Managing Sessions |
| 70 | + |
| 71 | +### Listing Sessions |
| 72 | + |
| 73 | +To see a list of all available sessions for the current project from the command |
| 74 | +line: |
| 75 | + |
| 76 | +```bash |
| 77 | +gemini --list-sessions |
| 78 | +``` |
| 79 | + |
| 80 | +Output example: |
| 81 | + |
| 82 | +```text |
| 83 | +Available sessions for this project (3): |
| 84 | +
|
| 85 | + 1. Fix bug in auth (2 days ago) [a1b2c3d4] |
| 86 | + 2. Refactor database schema (5 hours ago) [e5f67890] |
| 87 | + 3. Update documentation (Just now) [abcd1234] |
| 88 | +``` |
| 89 | + |
| 90 | +### Deleting Sessions |
| 91 | + |
| 92 | +You can remove old or unwanted sessions to free up space or declutter your |
| 93 | +history. |
| 94 | + |
| 95 | +**From the Command Line:** Use the `--delete-session` flag with an index or ID: |
| 96 | + |
| 97 | +```bash |
| 98 | +gemini --delete-session 2 |
| 99 | +``` |
| 100 | + |
| 101 | +**From the Session Browser:** |
| 102 | + |
| 103 | +1. Open the browser with `/resume`. |
| 104 | +2. Navigate to the session you want to remove. |
| 105 | +3. Press `x`. |
| 106 | + |
| 107 | +## Configuration |
| 108 | + |
| 109 | +You can configure how Gemini CLI manages your session history in your |
| 110 | +`settings.json` file. |
| 111 | + |
| 112 | +### Session Retention |
| 113 | + |
| 114 | +To prevent your history from growing indefinitely, you can enable automatic |
| 115 | +cleanup policies. |
| 116 | + |
| 117 | +```json |
| 118 | +{ |
| 119 | + "general": { |
| 120 | + "sessionRetention": { |
| 121 | + "enabled": true, |
| 122 | + "maxAge": "30d", // Keep sessions for 30 days |
| 123 | + "maxCount": 50 // Keep the 50 most recent sessions |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +- **`enabled`**: (boolean) Master switch for session cleanup. Default is |
| 130 | + `false`. |
| 131 | +- **`maxAge`**: (string) Duration to keep sessions (e.g., "24h", "7d", "4w"). |
| 132 | + Sessions older than this will be deleted. |
| 133 | +- **`maxCount`**: (number) Maximum number of sessions to retain. The oldest |
| 134 | + sessions exceeding this count will be deleted. |
| 135 | +- **`minRetention`**: (string) Minimum retention period (safety limit). Defaults |
| 136 | + to `"1d"`; sessions newer than this period are never deleted by automatic |
| 137 | + cleanup. |
| 138 | + |
| 139 | +### Session Limits |
| 140 | + |
| 141 | +You can also limit the length of individual sessions to prevent context windows |
| 142 | +from becoming too large and expensive. |
| 143 | + |
| 144 | +```json |
| 145 | +{ |
| 146 | + "model": { |
| 147 | + "maxSessionTurns": 100 |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +- **`maxSessionTurns`**: (number) The maximum number of turns (user + model |
| 153 | + exchanges) allowed in a single session. Set to `-1` for unlimited (default). |
| 154 | + |
| 155 | + **Behavior when limit is reached:** |
| 156 | + - **Interactive Mode:** The CLI shows an informational message and stops |
| 157 | + sending requests to the model. You must manually start a new session. |
| 158 | + - **Non-Interactive Mode:** The CLI exits with an error. |
0 commit comments