Back to Ruflo

Stream Chain Command

v2/docs/wiki/stream-chain-command.md

3.6.3013.5 KB
Original Source

Stream Chain Command

Overview

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.

Installation

The stream-chain command is included in Claude Flow and registered in the command registry:

bash
# Access stream-chain command
npx claude-flow stream-chain help

# Or with the local CLI
./claude-flow stream-chain help

Command Structure

bash
stream-chain <subcommand> [options]

Subcommands

run - Execute Custom Stream Chains

Run a sequence of prompts through connected Claude instances:

bash
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:

bash
./claude-flow stream-chain run \
  "Analyze the user authentication system" \
  "Identify security vulnerabilities" \
  "Generate fixes for the vulnerabilities"

demo - Run Demonstration Chain

Execute a pre-configured 3-step demonstration:

bash
stream-chain demo [options]

The demo chain performs:

  1. Requirements analysis for a todo list application
  2. Data model and API endpoint design
  3. Core functionality implementation

Example:

bash
# Run demo in foreground
./claude-flow stream-chain demo

# Run demo in background
./claude-flow stream-chain demo --background

pipeline - Execute Predefined Pipelines

Run specialized pipelines for common development tasks:

bash
stream-chain pipeline <type> [options]

Available Pipeline Types:

PipelineDescriptionSteps
analysisCode analysis pipeline1. Read and analyze codebase
  1. Identify improvements
  2. Generate report | | refactor | Refactoring pipeline | 1. Analyze refactoring opportunities
  3. Create refactoring plan
  4. Apply changes | | test | Test generation pipeline | 1. Analyze code coverage
  5. Identify missing tests
  6. Generate tests | | optimize | Performance optimization | 1. Profile performance
  7. Identify bottlenecks
  8. Apply optimizations |

Examples:

bash
# 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 Connection

Verify that stream chaining is working correctly:

bash
stream-chain test [options]

Performs two tests:

  1. Simple echo test
  2. Stream chaining test

Example:

bash
./claude-flow stream-chain test --verbose

monitor - Monitor Background Chains

View all background stream chains and their status:

bash
stream-chain monitor

Output includes:

  • Process ID (e.g., stream_1234567890)
  • Original command
  • System PID
  • Start time
  • Current status (🟢 Running / šŸ”“ Stopped)

Example:

bash
$ ./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 Chain

Stop a specific background stream chain:

bash
stream-chain kill <process_id>

Example:

bash
./claude-flow stream-chain kill stream_1755021020133

Options

Global Options

OptionShortDescription
--background--bgRun the stream chain in background
--verboseShow detailed output during execution
--jsonKeep JSON format for final output
--timeout <sec>Set timeout for each step (in seconds)

Background Execution

The --background or --bg flag enables background execution for any stream chain:

bash
# 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-JSON Format

Stream chains use newline-delimited JSON (NDJSON) for communication:

json
{"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

Performance Characteristics

MetricValueDescription
Latency<100msPer handoff between agents
Context Preservation100%Full conversation history maintained
Memory UsageO(1)Constant memory via streaming
Speed Improvement40-60%Compared to file-based approaches

Integration with Background Commands

The stream-chain command fully integrates with Claude Code's background command system:

Using with /bashes Command

Background stream chains appear in the /bashes interactive menu:

bash
# 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)

Programmatic Control

You can manage stream chains programmatically through Claude:

markdown
# 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

Practical Examples

Example 1: Full Development Pipeline

bash
# 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

Example 2: Automated Code Review

bash
# 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

Example 3: Test-Driven Development

bash
# 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

Example 4: Documentation Generation

bash
# 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

Files and Storage

Process Tracking File

Background processes are tracked in:

.claude-flow/stream-chains.json

File Structure:

json
{
  "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"
  }
}

Error Handling

The stream-chain command includes comprehensive error handling:

Common Errors and Solutions

ErrorCauseSolution
"Stream chain requires at least 2 prompts"Running run with <2 promptsProvide at least 2 prompts
"Unknown pipeline: [type]"Invalid pipeline typeUse: analysis, refactor, test, or optimize
"Process [id] not found"Trying to kill non-existent processCheck monitor for valid IDs
"Failed to kill process: kill ESRCH"Process already stoppedNo action needed
Command timeoutClaude CLI not available or slowInstall Claude CLI or use shorter timeout

Best Practices

1. Use Background for Long Chains

For chains with 3+ steps or expected runtime >30 seconds:

bash
stream-chain run "step1" "step2" "step3" "step4" --background

2. Monitor Critical Chains

For important workflows, monitor actively:

bash
# Start critical chain
stream-chain pipeline refactor --bg

# Monitor in another terminal
watch -n 5 './claude-flow stream-chain monitor'

3. Set Appropriate Timeouts

Prevent hanging chains with timeouts:

bash
# 30 second timeout per step
stream-chain run "analyze" "implement" --timeout 30

4. Clean Up Old Processes

Periodically check and clean up stopped processes:

bash
# Check all processes
stream-chain monitor

# Kill stopped processes
stream-chain kill stream_xxx

5. Use Verbose for Debugging

When chains fail, use verbose mode to diagnose:

bash
stream-chain test --verbose
stream-chain run "task1" "task2" --verbose

Advanced Usage

Combining with Other Claude Flow Features

With Hive Mind

bash
# 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

With Training Pipeline

bash
# 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

With MCP Tools

bash
# 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

Troubleshooting

Chain Not Starting

Symptom: Command hangs or times out immediately

Checks:

  1. Verify Claude CLI is installed: which claude
  2. Check Claude is authenticated: claude --version
  3. Try with shorter timeout: --timeout 5
  4. Run test command: stream-chain test

Background Process Not Found

Symptom: monitor doesn't show expected process

Checks:

  1. Check process file exists: ls -la .claude-flow/stream-chains.json
  2. Verify process started: Check terminal output for process ID
  3. Check system processes: ps aux | grep claude-flow

Chain Stops Unexpectedly

Symptom: Chain marked as "Stopped" prematurely

Checks:

  1. Check system resources: top or htop
  2. Review timeout settings
  3. Check Claude CLI logs
  4. Run with verbose flag for more details

Performance Optimization

Tips for Faster Chains

  1. Minimize Context: Keep prompts concise
  2. Use Specific Instructions: Avoid ambiguous prompts
  3. Parallel When Possible: Run independent chains simultaneously
  4. Cache Results: Store intermediate results for reuse
  5. Profile Performance: Use --verbose to identify slow steps

Resource Management

bash
# 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 History

VersionDateChanges
1.0.02025-08-12Initial implementation
1.1.02025-08-12Added background execution support
1.2.02025-08-12Added monitor and kill commands

Contributing

To contribute to the stream-chain command:

  1. Fork the repository
  2. Create feature branch: git checkout -b feature/stream-chain-enhancement
  3. Make changes to /src/cli/simple-commands/stream-chain.js
  4. Update tests and documentation
  5. Submit pull request

Support

For issues or questions:


Last updated: August 2025 Claude Flow Version: Alpha 89