A single query gives the engine's take on one phrasing. What do you get when you fan out across three related phrasings and merge the results?
Broader coverage — the engine ranks differently for each wording, so you surface snippets a single query would miss. Then the agent synthesizes all three sets into one paragraph?
Exactly. The shape is a loop of search() calls, each feeding a flat list, then one agent call over the merged snippets:
all_snippets = []
for q in queries:
results = search(q, count=3)
all_snippets.extend(r["snippet"] for r in results)
context = " ".join(all_snippets)
result = Agent(model).run_sync(f"Context: {context} Summarize.")
print(result.output)Notice .extend(...) — it flattens each round's snippets into the same flat list, not a list of lists.
Extend vs append — I can see why that matters here. If I used append, I'd get a list of lists, and joining would fail on the nested structure.
Exactly the right intuition. append(x) adds x as a single element; extend(iter) unrolls iter into individual elements. For accumulating from multiple sources, extend is almost always what you want. The full function:
def multi_query_brief(queries: list) -> str:
all_snippets = []
for q in queries:
results = search(q, count=3)
all_snippets.extend(r["snippet"] for r in results)
context = " ".join(all_snippets)
prompt = f"Context: {context} Summarize what these sources cover."
return Agent(model).run_sync(prompt).outputThree queries times three results each — nine snippets feeding one prompt. Does the agent actually read all nine, or does the prompt get truncated?
Modern models handle thousands of tokens; nine short snippets is well inside any window. But for longer content or more queries you'd chunk or rank first, then synthesize. Week 3's dedup and ranking become mandatory the moment your retrieval outgrows a single prompt. For this lesson, nine snippets is comfortably within the context window.
So the agent produces one coherent summary that touches every angle the three queries surfaced — richer than any single query could deliver.
Exactly. Multiple queries, merged retrieval, single synthesis. The agent is the glue between separate retrievals — which is exactly why fan-out RAG beats single-query RAG for broad topics.
TL;DR: loop search() across related queries, .extend() into one flat list, synthesize once.
extend not append — unrolls each round's snippetscount=3 per query — keeps the combined prompt short| Call | Result |
|---|---|
append([1,2]) | [..., [1,2]] |
extend([1,2]) | [..., 1, 2] |
For accumulating from many sources into a single flat input, extend is the right call.
A single query gives the engine's take on one phrasing. What do you get when you fan out across three related phrasings and merge the results?
Broader coverage — the engine ranks differently for each wording, so you surface snippets a single query would miss. Then the agent synthesizes all three sets into one paragraph?
Exactly. The shape is a loop of search() calls, each feeding a flat list, then one agent call over the merged snippets:
all_snippets = []
for q in queries:
results = search(q, count=3)
all_snippets.extend(r["snippet"] for r in results)
context = " ".join(all_snippets)
result = Agent(model).run_sync(f"Context: {context} Summarize.")
print(result.output)Notice .extend(...) — it flattens each round's snippets into the same flat list, not a list of lists.
Extend vs append — I can see why that matters here. If I used append, I'd get a list of lists, and joining would fail on the nested structure.
Exactly the right intuition. append(x) adds x as a single element; extend(iter) unrolls iter into individual elements. For accumulating from multiple sources, extend is almost always what you want. The full function:
def multi_query_brief(queries: list) -> str:
all_snippets = []
for q in queries:
results = search(q, count=3)
all_snippets.extend(r["snippet"] for r in results)
context = " ".join(all_snippets)
prompt = f"Context: {context} Summarize what these sources cover."
return Agent(model).run_sync(prompt).outputThree queries times three results each — nine snippets feeding one prompt. Does the agent actually read all nine, or does the prompt get truncated?
Modern models handle thousands of tokens; nine short snippets is well inside any window. But for longer content or more queries you'd chunk or rank first, then synthesize. Week 3's dedup and ranking become mandatory the moment your retrieval outgrows a single prompt. For this lesson, nine snippets is comfortably within the context window.
So the agent produces one coherent summary that touches every angle the three queries surfaced — richer than any single query could deliver.
Exactly. Multiple queries, merged retrieval, single synthesis. The agent is the glue between separate retrievals — which is exactly why fan-out RAG beats single-query RAG for broad topics.
TL;DR: loop search() across related queries, .extend() into one flat list, synthesize once.
extend not append — unrolls each round's snippetscount=3 per query — keeps the combined prompt short| Call | Result |
|---|---|
append([1,2]) | [..., [1,2]] |
extend([1,2]) | [..., 1, 2] |
For accumulating from many sources into a single flat input, extend is the right call.
Create a free account to get started. Paid plans unlock all tracks.