-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Stream Chain Command
The stream-chain
command enables you to connect multiple Claude instances via stream-json format, creating powerful multi-agent workflows with seamless context preservation. This command supports both foreground and background execution, allowing you to run complex chains while continuing other work.
# Basic usage
./claude-flow stream-chain run "Analyze code" "Generate tests" "Run tests"
# Run in background
./claude-flow stream-chain demo --background
# Monitor background chains
./claude-flow stream-chain monitor
The stream-chain command is included in Claude Flow and registered in the command registry:
# Access stream-chain command
npx claude-flow stream-chain help
# Or with the local CLI
./claude-flow stream-chain help
stream-chain <subcommand> [options]
Run a sequence of prompts through connected Claude instances:
stream-chain run "prompt1" "prompt2" "prompt3" [...]
Requirements:
- Minimum of 2 prompts required
- Each prompt is executed sequentially
- Output from each step feeds into the next
Example:
./claude-flow stream-chain run \
"Analyze the user authentication system" \
"Identify security vulnerabilities" \
"Generate fixes for the vulnerabilities"
Execute a pre-configured 3-step demonstration:
stream-chain demo [options]
The demo chain performs:
- Requirements analysis for a todo list application
- Data model and API endpoint design
- Core functionality implementation
Example:
# Run demo in foreground
./claude-flow stream-chain demo
# Run demo in background
./claude-flow stream-chain demo --background
Run specialized pipelines for common development tasks:
stream-chain pipeline <type> [options]
Available Pipeline Types:
Pipeline | Description | Steps |
---|---|---|
analysis |
Code analysis pipeline | 1. Read and analyze codebase 2. Identify improvements 3. Generate report |
refactor |
Refactoring pipeline | 1. Analyze refactoring opportunities 2. Create refactoring plan 3. Apply changes |
test |
Test generation pipeline | 1. Analyze code coverage 2. Identify missing tests 3. Generate tests |
optimize |
Performance optimization | 1. Profile performance 2. Identify bottlenecks 3. Apply optimizations |
Examples:
# Run analysis pipeline
./claude-flow stream-chain pipeline analysis
# Run refactoring pipeline in background
./claude-flow stream-chain pipeline refactor --bg
# Run test generation with verbose output
./claude-flow stream-chain pipeline test --verbose
Verify that stream chaining is working correctly:
stream-chain test [options]
Performs two tests:
- Simple echo test
- Stream chaining test
Example:
./claude-flow stream-chain test --verbose
View all background stream chains and their status:
stream-chain monitor
Output includes:
- Process ID (e.g.,
stream_1234567890
) - Original command
- System PID
- Start time
- Current status (🟢 Running / 🔴 Stopped)
Example:
$ ./claude-flow stream-chain monitor
📊 Background Stream Chains
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🔗 stream_1755021020133
Command: npx claude-flow stream-chain demo
PID: 366567
Started: 2025-08-12T17:50:20.135Z
Status: 🟢 Running
Stop a specific background stream chain:
stream-chain kill <process_id>
Example:
./claude-flow stream-chain kill stream_1755021020133
Option | Short | Description |
---|---|---|
--background |
--bg |
Run the stream chain in background |
--verbose |
Show detailed output during execution | |
--json |
Keep JSON format for final output | |
--timeout <sec> |
Set timeout for each step (in seconds) |
The --background
or --bg
flag enables background execution for any stream chain:
# Run any command in background
stream-chain run "task1" "task2" --background
stream-chain demo --bg
stream-chain pipeline analysis --background
Background Features:
- Process runs detached from terminal
- Unique process ID generated (e.g.,
stream_1234567890
) - Process information stored in
.claude-flow/stream-chains.json
- Continues running after terminal is closed
- Monitor with
stream-chain monitor
- Kill with
stream-chain kill <id>
Stream chains use newline-delimited JSON (NDJSON) for communication:
{"type":"init","session_id":"abc123","timestamp":"2024-01-01T00:00:00Z"}
{"type":"message","role":"assistant","content":[{"type":"text","text":"Processing..."}]}
{"type":"tool_use","name":"Bash","input":{"command":"ls -la"}}
{"type":"tool_result","output":"total 64\ndrwxr-xr-x 10 user staff 320"}
{"type":"result","status":"success","duration_ms":1234}
Message Types:
-
init
- Session initialization -
message
- Assistant/user messages -
tool_use
- Tool invocations -
tool_result
- Tool execution results -
result
- Final completion status
Metric | Value | Description |
---|---|---|
Latency | <100ms | Per handoff between agents |
Context Preservation | 100% | Full conversation history maintained |
Memory Usage | O(1) | Constant memory via streaming |
Speed Improvement | 40-60% | Compared to file-based approaches |
The stream-chain command fully integrates with Claude Code's background command system documented in background-commands.md:
Background stream chains appear in the /bashes
interactive menu:
# In Claude Code interactive mode
/bashes
# Shows all background processes including stream chains
Background Bash Shells
Select a shell to view details
1. npm run dev (running)
2. stream_1234567890: stream-chain demo (running)
3. docker-compose up (running)
You can manage stream chains programmatically through Claude:
# Ask Claude to start a stream chain in background
"Run a stream chain analysis pipeline in the background"
# Claude will execute:
./claude-flow stream-chain pipeline analysis --background
# Ask Claude to check status
"Check the status of background stream chains"
# Claude will execute:
./claude-flow stream-chain monitor
# Create a complete development workflow
./claude-flow stream-chain run \
"Analyze the requirements in docs/requirements.md" \
"Design the system architecture based on requirements" \
"Generate the API specification" \
"Create implementation plan" \
"Write the initial code structure" \
--background
# Monitor progress
./claude-flow stream-chain monitor
# Run code review pipeline in background
./claude-flow stream-chain run \
"Analyze code quality in src/" \
"Identify code smells and anti-patterns" \
"Suggest refactoring improvements" \
"Generate code review report" \
--bg --verbose
# Check when complete
./claude-flow stream-chain monitor
# TDD workflow
./claude-flow stream-chain run \
"Write test specifications for user authentication" \
"Generate unit tests based on specifications" \
"Implement code to pass the tests" \
"Refactor for code quality" \
--timeout 60
# Generate comprehensive documentation
./claude-flow stream-chain pipeline analysis --background
# After analysis completes, generate docs
./claude-flow stream-chain run \
"Based on the codebase analysis, create API documentation" \
"Generate user guide based on features" \
"Create developer setup guide" \
--bg
The stream-chain command works seamlessly with the Training Pipeline:
# Train agents first
./claude-flow train-pipeline run --complexity medium --iterations 3
# Use trained agent strategies in stream chain
./claude-flow stream-chain run \
"Apply conservative strategy for analysis" \
"Apply balanced strategy for implementation" \
"Apply aggressive strategy for optimization" \
--background
The trained agent profiles inform strategy selection:
- Conservative: 49.9% success rate, 1909ms avg time - Best for critical analysis
- Balanced: 50.0% success rate, 1887ms avg time - Good for general tasks
- Aggressive: 50.0% success rate, 1670ms avg time - Fastest execution
Background processes are tracked in:
.claude-flow/stream-chains.json
File Structure:
{
"stream_1234567890": {
"command": "npx claude-flow stream-chain demo",
"pid": 12345,
"startTime": "2025-08-12T17:50:20.135Z",
"status": "running"
},
"stream_9876543210": {
"command": "npx claude-flow stream-chain pipeline analysis",
"pid": 67890,
"startTime": "2025-08-12T18:00:00.000Z",
"status": "killed",
"endTime": "2025-08-12T18:05:00.000Z"
}
}
The stream-chain command includes comprehensive error handling:
Error | Cause | Solution |
---|---|---|
"Stream chain requires at least 2 prompts" | Running run with <2 prompts |
Provide at least 2 prompts |
"Unknown pipeline: [type]" | Invalid pipeline type | Use: analysis, refactor, test, or optimize |
"Process [id] not found" | Trying to kill non-existent process | Check monitor for valid IDs |
"Failed to kill process: kill ESRCH" | Process already stopped | No action needed |
Command timeout | Claude CLI not available or slow | Install Claude CLI or use shorter timeout |
For chains with 3+ steps or expected runtime >30 seconds:
stream-chain run "step1" "step2" "step3" "step4" --background
For important workflows, monitor actively:
# Start critical chain
stream-chain pipeline refactor --bg
# Monitor in another terminal
watch -n 5 './claude-flow stream-chain monitor'
Prevent hanging chains with timeouts:
# 30 second timeout per step
stream-chain run "analyze" "implement" --timeout 30
Periodically check and clean up stopped processes:
# Check all processes
stream-chain monitor
# Kill stopped processes
stream-chain kill stream_xxx
When chains fail, use verbose mode to diagnose:
stream-chain test --verbose
stream-chain run "task1" "task2" --verbose
# Start hive mind coordination
npx claude-flow hive-mind spawn "coordinator"
# Run stream chain managed by hive
./claude-flow stream-chain run \
"Coordinate with hive mind for task distribution" \
"Execute distributed tasks" \
"Aggregate results" \
--background
# Initialize swarm with MCP
npx claude-flow swarm init --topology mesh
# Run stream chain with swarm coordination
./claude-flow stream-chain run \
"Initialize swarm agents" \
"Distribute tasks across swarm" \
"Collect and synthesize results" \
--background
# Run stream chain with verification
./claude-flow stream-chain run \
"Implement feature with truth verification" \
"Run verification checks" \
"Apply corrections if needed" \
--verbose
- Minimize Context: Keep prompts concise
- Use Specific Instructions: Avoid ambiguous prompts
- Parallel When Possible: Run independent chains simultaneously
- Cache Results: Store intermediate results for reuse
-
Profile Performance: Use
--verbose
to identify slow steps
# Limit concurrent chains
MAX_CHAINS=3
CURRENT=$(./claude-flow stream-chain monitor | grep "🟢 Running" | wc -l)
if [ $CURRENT -lt $MAX_CHAINS ]; then
./claude-flow stream-chain demo --background
else
echo "Maximum chains running, waiting..."
fi
Symptom: Command hangs or times out immediately
Checks:
- Verify Claude CLI is installed:
which claude
- Check Claude is authenticated:
claude --version
- Try with shorter timeout:
--timeout 5
- Run test command:
stream-chain test
Symptom: monitor
doesn't show expected process
Checks:
- Check process file exists:
ls -la .claude-flow/stream-chains.json
- Verify process started: Check terminal output for process ID
- Check system processes:
ps aux | grep claude-flow
Symptom: Chain marked as "Stopped" prematurely
Checks:
- Check system resources:
top
orhtop
- Review timeout settings
- Check Claude CLI logs
- Run with verbose flag for more details
-
Command Handler:
/src/cli/simple-commands/stream-chain.js
-
Registry Entry:
/src/cli/command-registry.js
-
Process Tracking:
.claude-flow/stream-chains.json
-
streamChainCommand()
- Main command router -
runStreamChain()
- Execute custom chains -
runBackgroundStreamChain()
- Background execution handler -
monitorBackgroundChains()
- Display running chains -
killBackgroundChain()
- Terminate specific chain -
executeStreamStep()
- Execute single chain step
- Stream-Chaining - Detailed stream-json documentation
- background-commands - Background execution system
- Training-Pipeline - Agent training system
- Truth-Verification-System - Verification integration
- Hive-Mind-Intelligence - Multi-agent coordination
Version | Date | Changes |
---|---|---|
1.0.0 | 2025-08-12 | Initial implementation |
1.1.0 | 2025-08-12 | Added background execution support |
1.2.0 | 2025-08-12 | Added monitor and kill commands |
1.3.0 | 2025-08-12 | Integration with training pipeline |
To contribute to the stream-chain command:
- Fork the repository
- Create feature branch:
git checkout -b feature/stream-chain-enhancement
- Make changes to
/src/cli/simple-commands/stream-chain.js
- Update tests and documentation
- Submit pull request
For issues or questions:
- GitHub Issues: claude-flow/issues
- Stream Chaining Docs: Stream-Chaining.md
- Wiki Home: Home.md
Last updated: August 12, 2025 Claude Flow Version: Alpha 89 Issue #642: Resolved - stream-chain command fully implemented