v2/docs/wiki/stream-chain-command.md
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.
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 - Execute Custom Stream ChainsRun a sequence of prompts through connected Claude instances:
stream-chain run "prompt1" "prompt2" "prompt3" [...]
Requirements:
Example:
./claude-flow stream-chain run \
"Analyze the user authentication system" \
"Identify security vulnerabilities" \
"Generate fixes for the vulnerabilities"
demo - Run Demonstration ChainExecute a pre-configured 3-step demonstration:
stream-chain demo [options]
The demo chain performs:
Example:
# Run demo in foreground
./claude-flow stream-chain demo
# Run demo in background
./claude-flow stream-chain demo --background
pipeline - Execute Predefined PipelinesRun 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 |
refactor | Refactoring pipeline | 1. Analyze refactoring opportunitiestest | Test generation pipeline | 1. Analyze code coverageoptimize | Performance optimization | 1. Profile performanceExamples:
# 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
test - Test Stream ConnectionVerify that stream chaining is working correctly:
stream-chain test [options]
Performs two tests:
Example:
./claude-flow stream-chain test --verbose
monitor - Monitor Background ChainsView all background stream chains and their status:
stream-chain monitor
Output includes:
stream_1234567890)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
kill - Terminate Background ChainStop 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:
stream_1234567890).claude-flow/stream-chains.jsonstream-chain monitorstream-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 initializationmessage - Assistant/user messagestool_use - Tool invocationstool_result - Tool execution resultsresult - 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:
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
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
# Train agents first
./claude-flow train-pipeline run
# Use trained agents in stream chain
./claude-flow stream-chain run \
"Apply conservative strategy from training" \
"Apply balanced strategy from training" \
"Apply aggressive optimization" \
--bg
# 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
Symptom: Command hangs or times out immediately
Checks:
which claudeclaude --version--timeout 5stream-chain testSymptom: monitor doesn't show expected process
Checks:
ls -la .claude-flow/stream-chains.jsonps aux | grep claude-flowSymptom: Chain marked as "Stopped" prematurely
Checks:
top or htop--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
| 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 |
To contribute to the stream-chain command:
git checkout -b feature/stream-chain-enhancement/src/cli/simple-commands/stream-chain.jsFor issues or questions:
Last updated: August 2025 Claude Flow Version: Alpha 89