1How do I read an input longer than one context without losing the thread?
The input is cut into segments that each fit one call. A chain of workers reads them in order. Each worker gets its segment, the question and the note left by the worker before it, and rewrites that note. A manager reads only the last note and answers. Nobody ever holds the whole input.
You may need it if
- The document, transcript or code base is longer than one context window.
- Search keeps returning passages that look relevant and miss the ones the answer needs.
- Later sections only make sense in the light of earlier ones, such as a story, a case file or a long thread.
- Readers working on separate chunks each report something true and nobody connects them.
2When is a sequential relay the wrong way to read?
Use it when
- The input is several times longer than one context, and the whole of it may matter.
- Meaning builds up in reading order, so a fact found late has to be joined to one found early.
- The task is a question or a summary over the input, with no tools needed along the way.
- You can wait for one call per segment, one after the other.
Avoid it when
- Each part can be judged without the others and elapsed time matters. Independent workers can then read all the parts at once and one call merges them.
Use Map-Reduce instead - Only a few passages matter and a search can find them. Reading every segment in order is then wasted spend.
Use Agentic RAG instead - The work needs different specialists, each given its own subtask, and a coordinator that merges their results. Here every worker has the same job.
Use Supervisor & Specialists instead - An early detail only turns out to matter much later. If the note dropped it, no later worker can go back for it.
- The input fits comfortably in one context. A single call reads it whole and the relay only adds places to lose detail.
What it costs
- Model calls
- segments + 1 manager call
- Latency
- high
- Complexity
- low
3What exactly is passed from one worker to the next?
A relay in reading order. Code cuts the input into segments. The first worker reads segment 1 and writes a note. Each following worker reads its segment with the previous note and rewrites the note. After the last segment, a manager answers from the final note and the question alone.
- Step 1 of 6
Code cuts the report in reading order
A 240-page audit of a hospital software migration arrives with a question. Why was the go-live postponed, and who decided? Plain code cuts it into 8 segments of 30 pages and keeps their order.
- Step 2 of 6
The first worker starts the note
Worker 1 reads pages 1 to 30 and the question. Its note says that go-live was planned for March and depended on a data migration test. It does not try to answer yet.
- Step 3 of 6
The next worker reads with the note in hand
Worker 2 gets pages 31 to 60 and that note. It finds that the test failed in February. Because the note told it the test mattered, it keeps the fact and rewrites the note. Workers 3 to 7 do the same in turn.
- Step 4 of 6
The note is rewritten, not appended
Each worker hands on one note of bounded length. It merges what it found with what it received and drops what no longer bears on the question. The note never grows into the document.
- Step 5 of 6
The last worker closes the reading
Worker 8 reads the final pages. The steering committee minutes record the decision to postpone and name its chair. The final note now links the plan, the failed test and the decision.
- Step 6 of 6
The manager answers from the note
The manager sees the question and the final note, about a page long, and writes the answer. It has not read any of the 240 pages, and it says so if the note leaves part of the question open.
What each part does
- Long input
- The long input, with the question or the instruction to carry out on it.
- Splitter
- Plain code. Cuts the input, in reading order, into segments that each fit one call with room for the note.
- First worker
- Reads segment 1 and the question. Starts the note with what bears on the question.
- Next worker
- Reads its segment, the question and the previous note. Rewrites the note. One such worker follows another for every middle segment.
- Last worker
- Same job on the last segment. Its note stands for the reading of the whole input.
- Manager
- Appears once, at the end. Reads the question and the final note, never the segments, and writes the answer.
- Answer
- The answer or summary, with the final note it was written from.
- Splitter → First worker: segment 1
- Splitter → Next worker: next segment
- Splitter → Last worker: last segment
- First worker → Next worker: note
- Next worker → Last worker: rewritten note
- Last worker → Manager: final note
Where it connects
A question or an instruction such as a summary, with an input longer than one context whose parts are meant to be read in order.
When: The last worker has passed on its note and the manager has written from it.
The answer or summary, the final note it rests on, the number of segments read, and any segment whose worker returned nothing.
4What do I ask my assistant to set up?
The agents to launch
Worker, one per segment, run one after the other in reading order
Read your segment with the note from the previous worker. Write a new note of at most 300 words that merges what the old note held with what your segment adds towards the question. Keep names, figures and where they were found. Drop what no longer matters. Do not answer the question.
Sees: The question, its own segment and the previous worker's note. Not the other segments, not earlier notes, and no tools.
Manager
Answer the question from the final note alone. Use only what the note states. If the note leaves part of the question open, say which part and do not fill the gap from general knowledge.
Sees: The question and the final note. Not the segments and not the intermediate notes.
A prompt you can paste
Works with any assistant that can launch sub-agents. It contains the agents, the procedure and the stop rule above.
You are the orchestrator of a multi-agent workflow that follows the "Chain of Agents" architecture. Launch every agent listed below as a separate sub-agent with its own context. Give each agent only the context stated for it. Do not do an agent's work yourself. If you cannot launch sub-agents, play each agent in a clearly separated turn and never let it use what is withheld from it. # Task [Describe your task here] # Chain of Agents Workers read a long input in order and pass one running note. Input: the task above. It must provide: A question or an instruction such as a summary, with an input longer than one context whose parts are meant to be read in order. If something essential is missing, ask for it before starting. Agents to launch: - Worker (one per segment, run one after the other in reading order): Read your segment with the note from the previous worker. Write a new note of at most 300 words that merges what the old note held with what your segment adds towards the question. Keep names, figures and where they were found. Drop what no longer matters. Do not answer the question. Context for this agent: The question, its own segment and the previous worker's note. Not the other segments, not earlier notes, and no tools. - Manager: Answer the question from the final note alone. Use only what the note states. If the note leaves part of the question open, say which part and do not fill the gap from general knowledge. Context for this agent: The question and the final note. Not the segments and not the intermediate notes. Procedure: 1. Split the input with plain code, in reading order, into segments that each fit one call with room for the question and a 300-word note. 2. Launch the first worker on segment 1 and the question, with an empty note. 3. For each following segment, launch a fresh worker with the question, that segment and only the latest note. Wait for it before starting the next one. 4. If a worker returns an empty note, retry it once. If it fails again, pass the previous note on unchanged and record the segment as unread. 5. After the last segment, launch the manager once on the question and the final note. 6. For a summary with no question, use the instruction in place of the question. The note is then a running summary. Stop rule: Stop after one pass. That is one worker call per segment, in order, plus one retry at most for an empty note, then one manager call. No segment is read twice and the chain never goes back. Exit: - `answer`, when: The last worker has passed on its note and the manager has written from it. Deliver: The answer or summary, the final note it rests on, the number of segments read, and any segment whose worker returned nothing. # Final deliverable The answer, then the final note, the number of segments read, and the list of segments whose worker returned nothing.
The same thing in code
from typing import Literal, TypedDict
from langchain.chat_models import init_chat_model
from langgraph.graph import END, START, StateGraph
SEGMENT_CHARS = 24_000 # one segment must fit one call, with room for the question and the note
NOTE_WORDS = 300
WORKER = ("Read your segment with the note from the previous worker. Write a new note of at most "
f"{NOTE_WORDS} words that merges what the old note held with what your segment adds towards the "
"question. Keep names, figures and where they were found. Drop what no longer matters. Do not "
"answer the question.")
MANAGER = ("Answer the question from the final note alone. Use only what the note states. If the note leaves "
"part of the question open, say which part and do not fill the gap from general knowledge.")
class State(TypedDict):
question: str
segments: list[str]
done: int # segments read so far
note: str # the only thing that travels along the chain
unread: list[int]
answer: str
llm = init_chat_model("anthropic:claude-sonnet-5-5")
def split(document: str) -> list[str]:
# plain code, reading order kept
return [document[i:i + SEGMENT_CHARS] for i in range(0, len(document), SEGMENT_CHARS)]
async def worker(state: State) -> dict:
# a fresh context each time: the question, one segment and the latest note only
i, total = state["done"], len(state["segments"])
prompt = (f"{WORKER}\n\nQuestion: {state['question']}\n\n"
f"Note from the previous worker:\n{state['note'] or '(none, you are the first)'}\n\n"
f"Segment {i + 1} of {total}:\n{state['segments'][i]}")
for _ in range(2): # one retry for an empty note
note = (await llm.ainvoke(prompt)).text.strip()
if note:
return {"note": note, "done": i + 1}
return {"done": i + 1, "unread": state["unread"] + [i + 1]} # the previous note is passed on unchanged
def relay(state: State) -> Literal["worker", "manager"]:
return "worker" if state["done"] < len(state["segments"]) else "manager"
async def manager(state: State) -> dict:
# appears once: the question and the final note, never the segments
msg = await llm.ainvoke(f"{MANAGER}\n\nQuestion: {state['question']}\n\nFinal note:\n{state['note']}")
return {"answer": msg.text}
g = StateGraph(State)
g.add_node("worker", worker)
g.add_node("manager", manager)
g.add_edge(START, "worker")
g.add_conditional_edges("worker", relay, ["worker", "manager"]) # one worker after another, in order
g.add_edge("manager", END)
app = g.compile()
async def run(question: str, document: str) -> dict:
segments = split(document) or [""]
s = await app.ainvoke({"question": question, "segments": segments, "done": 0, "note": "", "unread": [], "answer": ""},
{"recursion_limit": len(segments) + 10})
return {"answer": s["answer"], "final_note": s["note"], "segments": len(segments), "unread": s["unread"]}
One node runs each worker in turn; the state carries only the latest note. The split is by character count for brevity: cut on section boundaries in a real system.
from claude_agent_sdk import ClaudeAgentOptions, ResultMessage, query
SEGMENT_CHARS = 24_000 # one segment must fit one call, with room for the question and the note
NOTE_WORDS = 300
WORKER = ("Read your segment with the note from the previous worker. Write a new note of at most "
f"{NOTE_WORDS} words that merges what the old note held with what your segment adds towards the "
"question. Keep names, figures and where they were found. Drop what no longer matters. Do not "
"answer the question. Reply with the note only.")
MANAGER = ("Answer the question from the final note alone. Use only what the note states. If the note leaves "
"part of the question open, say which part and do not fill the gap from general knowledge.")
def split(document: str) -> list[str]:
# plain code, reading order kept
return [document[i:i + SEGMENT_CHARS] for i in range(0, len(document), SEGMENT_CHARS)]
async def ask(system: str, prompt: str) -> str:
# each call is its own session: a fresh context with no tools
async for msg in query(prompt=prompt, options=ClaudeAgentOptions(system_prompt=system, tools=[])):
if isinstance(msg, ResultMessage):
return (msg.result or "").strip()
return ""
async def run(question: str, document: str) -> dict:
segments = split(document)
note = "" # the only thing that travels along the chain
unread: list[int] = []
for i, segment in enumerate(segments): # one worker after another, in reading order
prompt = (f"Question: {question}\n\nNote from the previous worker:\n{note or '(none, you are the first)'}\n\n"
f"Segment {i + 1} of {len(segments)}:\n{segment}")
new = await ask(WORKER, prompt) or await ask(WORKER, prompt) # one retry for an empty note
if new:
note = new
else:
unread.append(i + 1) # the previous note is passed on unchanged
# the manager appears once: the question and the final note, never the segments
answer = await ask(MANAGER, f"Question: {question}\n\nFinal note:\n{note}")
return {"answer": answer, "final_note": note, "segments": len(segments), "unread": unread}
Each worker is a separate session started by plain code, so it cannot see earlier segments or notes. Cut on section boundaries in a real system.
from agents import Agent, Runner
SEGMENT_CHARS = 24_000 # one segment must fit one call, with room for the question and the note
NOTE_WORDS = 300
worker = Agent(
name="Worker",
instructions="Read your segment with the note from the previous worker. Write a new note of at most "
f"{NOTE_WORDS} words that merges what the old note held with what your segment adds towards the "
"question. Keep names, figures and where they were found. Drop what no longer matters. Do not "
"answer the question. Reply with the note only.",
)
manager = Agent(
name="Manager",
instructions="Answer the question from the final note alone. Use only what the note states. If the note "
"leaves part of the question open, say which part and do not fill the gap from general knowledge.",
)
def split(document: str) -> list[str]:
# plain code, reading order kept
return [document[i:i + SEGMENT_CHARS] for i in range(0, len(document), SEGMENT_CHARS)]
async def write_note(prompt: str) -> str:
for _ in range(2): # one retry for an empty note
note = (await Runner.run(worker, prompt)).final_output_as(str).strip()
if note:
return note
return ""
async def run(question: str, document: str) -> dict:
segments = split(document)
note = "" # the only thing that travels along the chain
unread: list[int] = []
for i, segment in enumerate(segments): # a separate run per segment, one after another
new = await write_note(
f"Question: {question}\n\nNote from the previous worker:\n{note or '(none, you are the first)'}\n\n"
f"Segment {i + 1} of {len(segments)}:\n{segment}")
if new:
note = new
else:
unread.append(i + 1) # the previous note is passed on unchanged
# the manager appears once: the question and the final note, never the segments
out = await Runner.run(manager, f"Question: {question}\n\nFinal note:\n{note}")
return {"answer": out.final_output_as(str), "final_note": note, "segments": len(segments), "unread": unread}
No handoffs here: a handoff would pass the whole history along, and the point is that only the note travels. Each worker is a separate run.
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const SEGMENT_CHARS = 24_000; // one segment must fit one call, with room for the question and the note
const NOTE_WORDS = 300;
const model = anthropic('claude-sonnet-5-5');
const WORKER =
'Read your segment with the note from the previous worker. Write a new note of at most ' +
`${NOTE_WORDS} words that merges what the old note held with what your segment adds towards the ` +
'question. Keep names, figures and where they were found. Drop what no longer matters. Do not ' +
'answer the question. Reply with the note only.';
const MANAGER =
'Answer the question from the final note alone. Use only what the note states. If the note leaves ' +
'part of the question open, say which part and do not fill the gap from general knowledge.';
function split(document: string): string[] {
// plain code, reading order kept
const segments: string[] = [];
for (let i = 0; i < document.length; i += SEGMENT_CHARS) segments.push(document.slice(i, i + SEGMENT_CHARS));
return segments;
}
async function writeNote(prompt: string): Promise<string> {
for (let attempt = 0; attempt < 2; attempt++) { // one retry for an empty note
const { text } = await generateText({ model, instructions: WORKER, prompt });
if (text.trim()) return text.trim();
}
return '';
}
export async function run(question: string, document: string) {
const segments = split(document);
const unread: number[] = [];
let note = ''; // the only thing that travels along the chain
for (const [i, segment] of segments.entries()) { // one worker after another, in reading order
const next = await writeNote(
`Question: ${question}\n\nNote from the previous worker:\n${note || '(none, you are the first)'}\n\n` +
`Segment ${i + 1} of ${segments.length}:\n${segment}`,
);
if (next) note = next;
else unread.push(i + 1); // the previous note is passed on unchanged
}
// the manager appears once: the question and the final note, never the segments
const { text: answer } = await generateText({
model,
instructions: MANAGER,
prompt: `Question: ${question}\n\nFinal note:\n${note}`,
});
return { answer, finalNote: note, segments: segments.length, unread };
}
Each worker is a separate call with the question, one segment and the latest note. The split is by character count for brevity: cut on section boundaries.
Snippets are generated when the site is built and type-checked against the SDK versions listed in the repository. Treat them as a starting skeleton.
What goes wrong
- A note with no length limit grows at every step until it is the long context again. Fix a limit and have each worker rewrite, not append.
- Workers that are not told the question keep generic highlights. Put the question in every worker's prompt.
- Giving the manager the segments or every note looks safer and rebuilds the context problem. Give it the final note only.
- A cut in the middle of a table or a clause splits one fact across two workers. Cut on section boundaries where you can.
- Running the workers in parallel to save time removes the note they depend on. If the parts are independent, you want map-reduce.
Chain it
Its result can feed
- Reflection conditional
Condition: Use this link when the task asks for long-form text such as a summary or report, not a short answer to a question.
- Chain of Verification direct
Treat A's answer as an existing answer whose factual claims are to be turned into verification questions, checked independently and kept, corrected or removed.
- Debate conditional
Condition: Use this link when the original task is a question or decision with a contestable answer, not a request for a summary.
- Constitutional Critique direct
Treat A's answer or summary as the draft to check against each rule of the constitution, and revise only the rules it fails.
- Textual Backpropagation conditional
Condition: Use this link when the task asks for long-form text such as a summary or report, not a short answer to a question.