Advanced patterns for orchestrating Gemini CLI effectively from Claude Code.
The most reliable pattern for quality code generation.
# Step 1: Generate code
gemini "Create [code description]" --yolo -o text
# Step 2: Have Gemini review its own work
gemini "Review [generated file] for bugs and security issues" -o text
# Step 3: Fix identified issues
gemini "Fix these issues in [file]: [list from review]. Apply now." --yolo -o text- Different "mindset" for generation vs review
- Self-correction catches common mistakes
- Security vulnerabilities often caught in review phase
# Generate
gemini "Create a user authentication module with bcrypt and JWT" --yolo -o text
# Review
gemini "Review auth.js for security vulnerabilities" -o text
# Output: "Found XSS risk, missing input validation, weak JWT secret"
# Fix
gemini "Fix in auth.js: XSS risk, add input validation, use env var for JWT secret. Apply now." --yolo -o textUse JSON output when you need to process results programmatically.
gemini "[prompt]" -o json 2>&1// In Node.js or with jq
const result = JSON.parse(output);
const content = result.response;
const tokenUsage = result.stats.models["gemini-2.5-flash"].tokens.total;
const toolCalls = result.stats.tools.byName;- Extracting specific data from responses
- Monitoring token usage
- Tracking tool call success/failure
- Building automation pipelines
For long-running tasks, execute in background and continue working.
# Start in background
gemini "[long task]" --yolo -o text 2>&1 &
# Get process ID for later
echo $!
# Monitor output incrementally with BashOutput tool- Code generation for large projects
- Documentation generation
- Running multiple Gemini tasks in parallel
# Run multiple tasks simultaneously
gemini "Create frontend" --yolo -o text 2>&1 &
gemini "Create backend" --yolo -o text 2>&1 &
gemini "Create tests" --yolo -o text 2>&1 &Choose the right model for the task.
Is the task complex (architecture, multi-file, deep analysis)?
├── Yes → Use default (Gemini 3 Pro)
└── No → Is speed critical?
├── Yes → Use gemini-2.5-flash
└── No → Is it trivial (formatting, simple query)?
├── Yes → Use gemini-2.5-flash-lite
└── No → Use gemini-2.5-flash
# Complex: Architecture analysis
gemini "Analyze codebase architecture" -o text
# Quick: Simple formatting
gemini "Format this JSON" -m gemini-2.5-flash -o text
# Trivial: One-liner
gemini "What is 2+2?" -m gemini-2.5-flash -o textStrategies for working within rate limits.
Default behavior - CLI retries automatically with backoff.
# High priority: Use Pro
gemini "[important task]" --yolo -o text
# Lower priority: Use Flash (different quota)
gemini "[less critical task]" -m gemini-2.5-flash -o textCombine related operations into single prompts:
# Instead of multiple calls:
gemini "Create file A" --yolo
gemini "Create file B" --yolo
gemini "Create file C" --yolo
# Single call:
gemini "Create files A, B, and C with [specs]. Create all now." --yoloFor automated scripts, add delays:
gemini "[task 1]" --yolo -o text
sleep 2
gemini "[task 2]" --yolo -o textProvide rich context for better results.
gemini "Based on @./package.json and @./src/index.js, suggest improvements" -o textCreate project context that's automatically included:
# .gemini/GEMINI.md
## Project Overview
This is a React app using TypeScript.
## Coding Standards
- Use functional components
- Prefer hooks over classes
- All functions need JSDocgemini "Given this context:
- Project uses React 18 with TypeScript
- State management: Zustand
- Styling: Tailwind CSS
Create a user profile component." --yolo -o textAlways validate Gemini's output before using.
-
Syntax Check
# For JavaScript node --check generated.js # For TypeScript tsc --noEmit generated.ts
-
Security Scan
- Check for innerHTML with user input (XSS)
- Look for eval() or Function() calls
- Verify input validation
-
Functional Test
- Run any generated tests
- Manual smoke test
-
Style Check
eslint generated.js prettier --check generated.js
# Generate
gemini "Create utility functions" --yolo -o text
# Validate
node --check utils.js && eslint utils.js && npm testBuild complex outputs in stages.
# Stage 1: Core structure
gemini "Create basic Express server with routes for /api/users" --yolo -o text
# Stage 2: Add feature
gemini "Add authentication middleware to the Express server in server.js" --yolo -o text
# Stage 3: Add another feature
gemini "Add rate limiting to the Express server in server.js" --yolo -o text
# Stage 4: Review all
gemini "Review server.js for issues and optimize" -o text- Easier to debug issues
- Each stage validates before continuing
- Clear audit trail
Use both AIs for highest quality.
# 1. Claude writes code (using normal Claude Code tools)
# 2. Gemini reviews
gemini "Review this code for bugs and security issues: [paste code]" -o text# 1. Gemini generates
gemini "Create [code]" --yolo -o text
# 2. Claude reviews the output (in conversation)
# "Review this code that Gemini generated..."- Claude: Strong on reasoning, following complex instructions
- Gemini: Strong on current web knowledge, codebase investigation
Use sessions for multi-turn workflows.
# Initial task
gemini "Analyze this codebase architecture" -o text
# Session saved automatically
# List sessions
gemini --list-sessions
# Continue with follow-up
echo "What patterns did you find?" | gemini -r 1 -o text
# Further refinement
echo "Focus on the authentication flow" | gemini -r 1 -o text- Iterative analysis
- Building on previous context
- Debugging sessions
YOLO mode doesn't prevent planning. Gemini may still present plans.
Do: Use forceful language ("Apply now", "Start immediately")
Hammering the API wastes time on retries.
Do: Use appropriate models, batch operations
Gemini can make mistakes, especially with security.
Do: Always validate generated code
Extremely long prompts can confuse the model.
Do: Use incremental refinement for complex tasks
Even with 1M tokens, context can overflow.
Do: Use .geminiignore, be specific about files