# Claude Code Workflows: script subagents in loops and sequences

> Claude Code Workflows lets a workflow.js script invoke subagents in loops, sequences, or parallel, with structured output schemas to chain agents reliably.

Published: 2026-06-08
URL: https://daniliants.com/insights/claude-code-workflows-calls-subagents-programmatically/
Tags: claude-code, workflows, subagents, ai-agents, llm-orchestration, developer-tools, automation

---

## Summary

Claude Code's Workflows feature lets you write a `workflow.js` script that programmatically invokes subagents in loops, sequences, or parallel, without routing all orchestration through the main agent context. The script accepts arguments, supports structured output schemas for chaining agent outputs to inputs, and persists run metadata in `.claude/` for auditability.

## Key Insight

- **What it solves:** Previously, having the main Claude Code agent orchestrate multiple subagents consumed significant tokens for coordination logic. Workflows offload that to a JS script, so the main agent just hands off to the script.
- **Core mechanic:** You write (or let Claude write) a `workflow.js` that calls subagents via loops or conditionals, standard `if/else`, `while`, `for`, with pass-through arguments (e.g. "process up to 20 PRs").
- **Chaining:** Subagents can emit structured JSON schemas, enabling the output of one agent to pipe cleanly into the input of the next. This is what makes multi-step pipelines reliable.
- **Concrete example:** Pass a GitHub repo URL, the workflow calls subagents sequentially to review each PR, each subagent rates the PR, implements fixes, and loops up to a configurable limit.
- **Activation:** Run `/config` in Claude Code chat and enable "dynamic workflows". Then type the keyword `workflow` (renders with a rainbow indicator) and describe the workflow you want. Claude writes `workflow.js` to `.claude/`.
- **Observability:** Every run's metadata and outputs are stored persistently alongside the workflow file, useful for auditing agent runs.
- **Token efficiency:** Because structured control flow lives in the script, not in the main agent's reasoning, you avoid paying token overhead for orchestration logic.