Skip to content

Commit 243436d

Browse files
authored
add a max tool round options (#63)
1 parent 5a4b2c8 commit 243436d

File tree

4 files changed

+50
-11
lines changed

4 files changed

+50
-11
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Process a single prompt and exit (useful for scripting and automation):
191191
grok --prompt "show me the package.json file"
192192
grok -p "create a new file called example.js with a hello world function"
193193
grok --prompt "run npm test and show me the results" --directory /path/to/project
194+
grok --prompt "complex task" --max-tool-rounds 50 # Limit tool usage for faster execution
194195
```
195196

196197
This mode is particularly useful for:
@@ -199,6 +200,27 @@ This mode is particularly useful for:
199200
- **Terminal benchmarks**: Perfect for tools like Terminal Bench that need non-interactive execution
200201
- **Batch processing**: Process multiple prompts programmatically
201202

203+
### Tool Execution Control
204+
205+
By default, Grok CLI allows up to 400 tool execution rounds to handle complex multi-step tasks. You can control this behavior:
206+
207+
```bash
208+
# Limit tool rounds for faster execution on simple tasks
209+
grok --max-tool-rounds 10 --prompt "show me the current directory"
210+
211+
# Increase limit for very complex tasks (use with caution)
212+
grok --max-tool-rounds 1000 --prompt "comprehensive code refactoring"
213+
214+
# Works with all modes
215+
grok --max-tool-rounds 20 # Interactive mode
216+
grok git commit-and-push --max-tool-rounds 30 # Git commands
217+
```
218+
219+
**Use Cases**:
220+
- **Fast responses**: Lower limits (10-50) for simple queries
221+
- **Complex automation**: Higher limits (500+) for comprehensive tasks
222+
- **Resource control**: Prevent runaway executions in automated environments
223+
202224
### Model Selection
203225

204226
You can specify which AI model to use with the `--model` parameter or `GROK_MODEL` environment variable:
@@ -244,6 +266,7 @@ Options:
244266
-u, --base-url <url> Grok API base URL (or set GROK_BASE_URL env var)
245267
-m, --model <model> AI model to use (e.g., grok-4-latest, grok-3-latest) (or set GROK_MODEL env var)
246268
-p, --prompt <prompt> process a single prompt and exit (headless mode)
269+
--max-tool-rounds <rounds> maximum number of tool execution rounds (default: 400)
247270
-h, --help display help for command
248271
```
249272

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@vibe-kit/grok-cli",
3-
"version": "0.0.18",
3+
"version": "0.0.19",
44
"description": "An open-source AI agent that brings the power of Grok directly into your terminal.",
55
"main": "dist/index.js",
66
"bin": {

src/agent/grok-agent.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ export class GrokAgent extends EventEmitter {
5151
private tokenCounter: TokenCounter;
5252
private abortController: AbortController | null = null;
5353
private mcpInitialized: boolean = false;
54+
private maxToolRounds: number;
5455

55-
constructor(apiKey: string, baseURL?: string, model?: string) {
56+
constructor(apiKey: string, baseURL?: string, model?: string, maxToolRounds?: number) {
5657
super();
5758
const manager = getSettingsManager();
5859
const savedModel = manager.getCurrentModel();
5960
const modelToUse = model || savedModel || "grok-4-latest";
61+
this.maxToolRounds = maxToolRounds || 400;
6062
this.grokClient = new GrokClient(apiKey, modelToUse, baseURL);
6163
this.textEditor = new TextEditorTool();
6264
this.bash = new BashTool();
@@ -179,7 +181,7 @@ Current working directory: ${process.cwd()}`,
179181
this.messages.push({ role: "user", content: message });
180182

181183
const newEntries: ChatEntry[] = [userEntry];
182-
const maxToolRounds = 10; // Prevent infinite loops
184+
const maxToolRounds = this.maxToolRounds; // Prevent infinite loops
183185
let toolRounds = 0;
184186

185187
try {
@@ -382,7 +384,7 @@ Current working directory: ${process.cwd()}`,
382384
tokenCount: inputTokens,
383385
};
384386

385-
const maxToolRounds = 30; // Prevent infinite loops
387+
const maxToolRounds = this.maxToolRounds; // Prevent infinite loops
386388
let toolRounds = 0;
387389
let totalOutputTokens = 0;
388390

src/index.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ function loadModel(): string | undefined {
108108
async function handleCommitAndPushHeadless(
109109
apiKey: string,
110110
baseURL?: string,
111-
model?: string
111+
model?: string,
112+
maxToolRounds?: number
112113
): Promise<void> {
113114
try {
114-
const agent = new GrokAgent(apiKey, baseURL, model);
115+
const agent = new GrokAgent(apiKey, baseURL, model, maxToolRounds);
115116

116117
// Configure confirmation service for headless mode (auto-approve all operations)
117118
const confirmationService = ConfirmationService.getInstance();
@@ -229,10 +230,11 @@ async function processPromptHeadless(
229230
prompt: string,
230231
apiKey: string,
231232
baseURL?: string,
232-
model?: string
233+
model?: string,
234+
maxToolRounds?: number
233235
): Promise<void> {
234236
try {
235-
const agent = new GrokAgent(apiKey, baseURL, model);
237+
const agent = new GrokAgent(apiKey, baseURL, model, maxToolRounds);
236238

237239
// Configure confirmation service for headless mode (auto-approve all operations)
238240
const confirmationService = ConfirmationService.getInstance();
@@ -322,6 +324,11 @@ program
322324
"-p, --prompt <prompt>",
323325
"process a single prompt and exit (headless mode)"
324326
)
327+
.option(
328+
"--max-tool-rounds <rounds>",
329+
"maximum number of tool execution rounds (default: 400)",
330+
"400"
331+
)
325332
.action(async (options) => {
326333
if (options.directory) {
327334
try {
@@ -340,6 +347,7 @@ program
340347
const apiKey = options.apiKey || loadApiKey();
341348
const baseURL = options.baseUrl || loadBaseURL();
342349
const model = options.model || loadModel();
350+
const maxToolRounds = parseInt(options.maxToolRounds) || 400;
343351

344352
if (!apiKey) {
345353
console.error(
@@ -355,12 +363,12 @@ program
355363

356364
// Headless mode: process prompt and exit
357365
if (options.prompt) {
358-
await processPromptHeadless(options.prompt, apiKey, baseURL, model);
366+
await processPromptHeadless(options.prompt, apiKey, baseURL, model, maxToolRounds);
359367
return;
360368
}
361369

362370
// Interactive mode: launch UI
363-
const agent = new GrokAgent(apiKey, baseURL, model);
371+
const agent = new GrokAgent(apiKey, baseURL, model, maxToolRounds);
364372
console.log("🤖 Starting Grok CLI Conversational Assistant...\n");
365373

366374
ensureUserSettingsDirectory();
@@ -390,6 +398,11 @@ gitCommand
390398
"-m, --model <model>",
391399
"AI model to use (e.g., gemini-2.5-pro, grok-4-latest) (or set GROK_MODEL env var)"
392400
)
401+
.option(
402+
"--max-tool-rounds <rounds>",
403+
"maximum number of tool execution rounds (default: 400)",
404+
"400"
405+
)
393406
.action(async (options) => {
394407
if (options.directory) {
395408
try {
@@ -408,6 +421,7 @@ gitCommand
408421
const apiKey = options.apiKey || loadApiKey();
409422
const baseURL = options.baseUrl || loadBaseURL();
410423
const model = options.model || loadModel();
424+
const maxToolRounds = parseInt(options.maxToolRounds) || 400;
411425

412426
if (!apiKey) {
413427
console.error(
@@ -421,7 +435,7 @@ gitCommand
421435
await saveCommandLineSettings(options.apiKey, options.baseUrl);
422436
}
423437

424-
await handleCommitAndPushHeadless(apiKey, baseURL, model);
438+
await handleCommitAndPushHeadless(apiKey, baseURL, model, maxToolRounds);
425439
} catch (error: any) {
426440
console.error("❌ Error during git commit-and-push:", error.message);
427441
process.exit(1);

0 commit comments

Comments
 (0)