From c37771442002f2ceb0101cb2d3bd47e30368cd85 Mon Sep 17 00:00:00 2001 From: rUv Date: Tue, 5 Aug 2025 12:36:39 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=96=20Checkpoint:=20Edit=20/workspaces?= =?UTF-8?q?/claude-code-flow/docs/stream-chaining.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .claude/checkpoints/task-1754397364.json | 1 + docs/stream-chaining.md | 71 +++++++++++++++++++++--- 2 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 .claude/checkpoints/task-1754397364.json diff --git a/.claude/checkpoints/task-1754397364.json b/.claude/checkpoints/task-1754397364.json new file mode 100644 index 0000000000..8b5de00a38 --- /dev/null +++ b/.claude/checkpoints/task-1754397364.json @@ -0,0 +1 @@ +{"checkpoint": "task-20250805-123603", "task": "{{user_prompt}} ", "timestamp": "2025-08-05T12:36:04Z", "commit": "c60e843554eb6e05fcb641dd77c1ff0df38de5b0", "github_release": "true"} diff --git a/docs/stream-chaining.md b/docs/stream-chaining.md index a891eea278..acf1c53aaa 100644 --- a/docs/stream-chaining.md +++ b/docs/stream-chaining.md @@ -1,22 +1,75 @@ # Stream-JSON Chaining in Claude Flow -## Overview +## 🔁 Overview: Stream Chaining in Claude Code + +**Stream chaining** in Claude Code is the technique of connecting multiple `claude -p` (non-interactive) processes using real-time **JSON streams**, allowing you to build **modular, recursive, multi-agent pipelines**. Stream-JSON chaining enables Claude instances to pipe their outputs directly to other Claude instances, creating a seamless workflow where agents can build upon each other's work without intermediate storage. -## How It Works +## 🧱 How It Works + +### Core Claude Code Flags + +Claude Code supports two key flags that enable stream chaining: + +* `--output-format stream-json`: emits newline-delimited JSON (`NDJSON`) with every token, turn, and tool interaction +* `--input-format stream-json`: accepts a stream of messages in NDJSON format, simulating a continuous conversation + +By combining them: + +```bash +claude -p --output-format stream-json "First task" \ + | claude -p --input-format stream-json --output-format stream-json "Process results" \ + | claude -p --input-format stream-json "Final report" +``` + +Each agent processes input, emits structured responses, and hands them off to the next agent in the chain. + +### Automatic Chaining in Claude Flow When tasks have dependencies and stream-json output format is enabled, Claude Flow automatically: -1. Captures the stdout from the first agent -2. Pipes it directly to the stdin of the dependent agent -3. Adds `--input-format stream-json` flag to the receiving agent -4. Maintains the stream connection throughout execution +1. Detects task dependencies from workflow definitions +2. Captures the stdout stream from the dependency task +3. Pipes it directly to stdin of the dependent task +4. Adds `--input-format stream-json` flag to the receiving agent +5. Maintains the stream connection throughout execution -## Example Flow +## 🔄 What You Can Do With Stream Chaining -``` -Agent A (analyzer) → stdout (stream-json) → stdin → Agent B (processor) → stdout → stdin → Agent C (reporter) +* **Subagent orchestration**: planner → executor → reviewer +* **Recursive pipelines**: refinement loops, ablation agents, iterative optimization +* **Live feedback systems**: feed Claude output into a scoring or mutation agent +* **Task decomposition**: outer loop breaks work into subtasks, inner loop completes each +* **Multi-stage analysis**: data analysis → feature engineering → model training → validation +* **Complex workflows**: research → design → implementation → testing → documentation + +## 🧠 Key Features + +### Structured Message Types + +Stream-JSON format includes structured message types: +- `init`: Session initialization +- `message`: Assistant/user messages +- `tool_use`: Tool invocations with parameters +- `tool_result`: Tool execution results +- `result`: Final task completion status + +### Advanced Control Options +- Supports `--session` for session management +- `--max-turns` for granular conversation control +- Works seamlessly with shell scripts, `jq`, and Python SDKs +- Can simulate multi-turn conversation without REPL +- Preserves full context including reasoning and tool usage + +### Example Stream-JSON Output + +```json +{"type":"init","session_id":"abc123","timestamp":"2024-01-01T00:00:00Z"} +{"type":"message","role":"assistant","content":[{"type":"text","text":"Analyzing data..."}]} +{"type":"tool_use","name":"Bash","input":{"command":"ls -la"}} +{"type":"tool_result","output":"total 64\ndrwxr-xr-x 10 user staff 320 Jan 1 00:00 ."} +{"type":"result","status":"success","duration_ms":1234} ``` ## Enabling Stream Chaining