|
| 1 | +# Advanced Model Configuration |
| 2 | + |
| 3 | +This guide details the Model Configuration system within the Gemini CLI. |
| 4 | +Designed for researchers, AI quality engineers, and advanced users, this system |
| 5 | +provides a rigorous framework for managing generative model hyperparameters and |
| 6 | +behaviors. |
| 7 | + |
| 8 | +> **Warning**: This is a power-user feature. Configuration values are passed |
| 9 | +> directly to the model provider with minimal validation. Incorrect settings |
| 10 | +> (e.g., incompatible parameter combinations) may result in runtime errors from |
| 11 | +> the API. |
| 12 | +
|
| 13 | +## 1. System Overview |
| 14 | + |
| 15 | +The Model Configuration system (`ModelConfigService`) enables deterministic |
| 16 | +control over model generation. It decouples the requested model identifier |
| 17 | +(e.g., a CLI flag or agent request) from the underlying API configuration. This |
| 18 | +allows for: |
| 19 | + |
| 20 | +- **Precise Hyperparameter Tuning**: Direct control over `temperature`, `topP`, |
| 21 | + `thinkingBudget`, and other SDK-level parameters. |
| 22 | +- **Environment-Specific Behavior**: Distinct configurations for different |
| 23 | + operating contexts (e.g., testing vs. production). |
| 24 | +- **Agent-Scoped Customization**: Applying specific settings only when a |
| 25 | + particular agent is active. |
| 26 | + |
| 27 | +The system operates on two core primitives: **Aliases** and **Overrides**. |
| 28 | + |
| 29 | +## 2. Configuration Primitives |
| 30 | + |
| 31 | +These settings are located under the `modelConfigs` key in your configuration |
| 32 | +file. |
| 33 | + |
| 34 | +### Aliases (`customAliases`) |
| 35 | + |
| 36 | +Aliases are named, reusable configuration presets. Users should define their own |
| 37 | +aliases (or override system defaults) in the `customAliases` map. |
| 38 | + |
| 39 | +- **Inheritance**: An alias can `extends` another alias (including system |
| 40 | + defaults like `chat-base`), inheriting its `modelConfig`. Child aliases can |
| 41 | + overwrite or augment inherited settings. |
| 42 | +- **Abstract Aliases**: An alias is not required to specify a concrete `model` |
| 43 | + if it serves purely as a base for other aliases. |
| 44 | + |
| 45 | +**Example Hierarchy**: |
| 46 | + |
| 47 | +```json |
| 48 | +"modelConfigs": { |
| 49 | + "customAliases": { |
| 50 | + "base": { |
| 51 | + "modelConfig": { |
| 52 | + "generateContentConfig": { "temperature": 0.0 } |
| 53 | + } |
| 54 | + }, |
| 55 | + "chat-base": { |
| 56 | + "extends": "base", |
| 57 | + "modelConfig": { |
| 58 | + "generateContentConfig": { "temperature": 0.7 } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### Overrides (`overrides`) |
| 66 | + |
| 67 | +Overrides are conditional rules that inject configuration based on the runtime |
| 68 | +context. They are evaluated dynamically for each model request. |
| 69 | + |
| 70 | +- **Match Criteria**: Overrides apply when the request context matches the |
| 71 | + specified `match` properties. |
| 72 | + - `model`: Matches the requested model name or alias. |
| 73 | + - `overrideScope`: Matches the distinct scope of the request (typically the |
| 74 | + agent name, e.g., `codebaseInvestigator`). |
| 75 | + |
| 76 | +**Example Override**: |
| 77 | + |
| 78 | +```json |
| 79 | +"modelConfigs": { |
| 80 | + "overrides": [ |
| 81 | + { |
| 82 | + "match": { |
| 83 | + "overrideScope": "codebaseInvestigator" |
| 84 | + }, |
| 85 | + "modelConfig": { |
| 86 | + "generateContentConfig": { "temperature": 0.1 } |
| 87 | + } |
| 88 | + } |
| 89 | + ] |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## 3. Resolution Strategy |
| 94 | + |
| 95 | +The `ModelConfigService` resolves the final configuration through a two-step |
| 96 | +process: |
| 97 | + |
| 98 | +### Step 1: Alias Resolution |
| 99 | + |
| 100 | +The requested model string is looked up in the merged map of system `aliases` |
| 101 | +and user `customAliases`. |
| 102 | + |
| 103 | +1. If found, the system recursively resolves the `extends` chain. |
| 104 | +2. Settings are merged from parent to child (child wins). |
| 105 | +3. This results in a base `ResolvedModelConfig`. |
| 106 | +4. If not found, the requested string is treated as the raw model name. |
| 107 | + |
| 108 | +### Step 2: Override Application |
| 109 | + |
| 110 | +The system evaluates the `overrides` list against the request context (`model` |
| 111 | +and `overrideScope`). |
| 112 | + |
| 113 | +1. **Filtering**: All matching overrides are identified. |
| 114 | +2. **Sorting**: Matches are prioritized by **specificity** (the number of |
| 115 | + matched keys in the `match` object). |
| 116 | + - Specific matches (e.g., `model` + `overrideScope`) override broad matches |
| 117 | + (e.g., `model` only). |
| 118 | + - Tie-breaking: If specificity is equal, the order of definition in the |
| 119 | + `overrides` array is preserved (last one wins). |
| 120 | +3. **Merging**: The configurations from the sorted overrides are merged |
| 121 | + sequentially onto the base configuration. |
| 122 | + |
| 123 | +## 4. Configuration Reference |
| 124 | + |
| 125 | +The configuration follows the `ModelConfigServiceConfig` interface. |
| 126 | + |
| 127 | +### `ModelConfig` Object |
| 128 | + |
| 129 | +Defines the actual parameters for the model. |
| 130 | + |
| 131 | +| Property | Type | Description | |
| 132 | +| :---------------------- | :------- | :----------------------------------------------------------------- | |
| 133 | +| `model` | `string` | The identifier of the model to be called (e.g., `gemini-2.5-pro`). | |
| 134 | +| `generateContentConfig` | `object` | The configuration object passed to the `@google/genai` SDK. | |
| 135 | + |
| 136 | +### `GenerateContentConfig` (Common Parameters) |
| 137 | + |
| 138 | +Directly maps to the SDK's `GenerateContentConfig`. Common parameters include: |
| 139 | + |
| 140 | +- **`temperature`**: (`number`) Controls output randomness. Lower values (0.0) |
| 141 | + are deterministic; higher values (>0.7) are creative. |
| 142 | +- **`topP`**: (`number`) Nucleus sampling probability. |
| 143 | +- **`maxOutputTokens`**: (`number`) Limit on generated response length. |
| 144 | +- **`thinkingConfig`**: (`object`) Configuration for models with reasoning |
| 145 | + capabilities (e.g., `thinkingBudget`, `includeThoughts`). |
| 146 | + |
| 147 | +## 5. Practical Examples |
| 148 | + |
| 149 | +### Defining a Deterministic Baseline |
| 150 | + |
| 151 | +Create an alias for tasks requiring high precision, extending the standard chat |
| 152 | +configuration but enforcing zero temperature. |
| 153 | + |
| 154 | +```json |
| 155 | +"modelConfigs": { |
| 156 | + "customAliases": { |
| 157 | + "precise-mode": { |
| 158 | + "extends": "chat-base", |
| 159 | + "modelConfig": { |
| 160 | + "generateContentConfig": { |
| 161 | + "temperature": 0.0, |
| 162 | + "topP": 1.0 |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +### Agent-Specific Parameter Injection |
| 171 | + |
| 172 | +Enforce extended thinking budgets for a specific agent without altering the |
| 173 | +global default, e.g. for the `codebaseInvestigator`. |
| 174 | + |
| 175 | +```json |
| 176 | +"modelConfigs": { |
| 177 | + "overrides": [ |
| 178 | + { |
| 179 | + "match": { |
| 180 | + "overrideScope": "codebaseInvestigator" |
| 181 | + }, |
| 182 | + "modelConfig": { |
| 183 | + "generateContentConfig": { |
| 184 | + "thinkingConfig": { "thinkingBudget": 4096 } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + ] |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +### Experimental Model Evaluation |
| 193 | + |
| 194 | +Route traffic for a specific alias to a preview model for A/B testing, without |
| 195 | +changing client code. |
| 196 | + |
| 197 | +```json |
| 198 | +"modelConfigs": { |
| 199 | + "overrides": [ |
| 200 | + { |
| 201 | + "match": { |
| 202 | + "model": "gemini-2.5-pro" |
| 203 | + }, |
| 204 | + "modelConfig": { |
| 205 | + "model": "gemini-2.5-pro-experimental-001" |
| 206 | + } |
| 207 | + } |
| 208 | + ] |
| 209 | +} |
| 210 | +``` |
0 commit comments