GC-201c · Module 1

Batch Processing Patterns

3 min read

Batch processing applies Gemini CLI to multiple inputs systematically. The pattern: iterate over a list of files, directories, or data items, run gemini -p on each one, and collect the results. This scales AI-assisted work from "one file at a time" to "every file in the project." Common batch operations include documentation generation, code review across modules, test stub creation, and migration file analysis.

Shell scripting is the natural batch orchestration layer. A for loop iterates over target files. Each iteration calls gemini -p with the file content piped in or referenced via @ syntax. Results are collected in an output directory or aggregated into a single report. Parallelization with xargs or GNU parallel speeds up large batches — but respect API rate limits.

#!/bin/bash
# Batch documentation generation
OUTDIR="docs/api"
mkdir -p "$OUTDIR"

for file in src/api/*.ts; do
  name=$(basename "$file" .ts)
  echo "Documenting $name..."
  gemini -p "Generate JSDoc documentation for every exported function in this file. Output only the documented source code.\n\n$(cat $file)" \
    > "$OUTDIR/$name.md"
done

echo "Documentation generated for $(ls $OUTDIR | wc -l) files"

# Parallel batch with rate limiting (4 concurrent)
find src/api -name "*.ts" | \
  xargs -P 4 -I {} bash -c '
    name=$(basename "{}" .ts)
    gemini -p "Review {} for security issues" --output-format json > "reviews/$name.json"
  '
  1. Identify the batch target What files or data items need processing? Use find, glob, or git diff to build the input list.
  2. Design the per-item prompt Write and test the gemini -p prompt on a single item. Get the output quality right before scaling.
  3. Build the iteration loop Wrap the prompt in a for loop or xargs pipeline. Collect output in a structured directory.
  4. Add rate limiting and error handling Add sleep between calls if hitting rate limits. Check exit codes. Log failures for retry.