Skip to content
CI/CD & Automation Intermediate

Batch File Processing

Process multiple files independently with per-file budget caps and cost tracking

Command

$ "color:#7C5CFC">for file in src/**/*.ts; "color:#7C5CFC">do
    "color:#7C5CFC">claude -p "Add JSDoc comments to $file" \
      "color:#d97757">--output-format json \
      "color:#d97757">--max-budget-usd 0.10 \
      "color:#d97757">--no-session-persistence \
      "color:#d97757">--permission-mode bypassPermissions
  "color:#7C5CFC">done

Response

Processing: src/auth.ts
  Cost: $0.045 (Running total: $0.045)
Processing: src/api.ts
  Cost: $0.038 (Running total: $0.083)
=== Batch complete. Total: $0.083 ===

Parsing Code

059669">">let totalCost = 0;
059669">">for (059669">">const file 059669">">of files) {
  059669">">const data = JSON.parse(execFileSync(059669059669">">'claude', [
    059669059669">">'-p', 059669">`Add JSDoc to ${file}`,
    059669059669">">'--output-format', 059669059669">">'json', 059669059669">">'--max-budget-usd', 059669059669">">'0.10',
    059669059669">">'--no-session-persistence', 059669059669">">'--permission-mode', 059669059669">">'bypassPermissions'
  ], opts));
  totalCost += data.total_cost_usd;
  console.log(059669">`${file}: $${data.total_cost_usd}`);
}

Gotchas

! Each invocation is isolated — no session state leaks between files
! Use --no-session-persistence to avoid polluting the session store with thousands of one-off runs

Related Recipes