Day 4 counted the words in one response. Day 19 batched a classifier over a list. Can you guess what happens if you compose them?
A list of prompts and a list of integer word counts back — one count per response, same length as the input?
Exactly. The list comprehension holds the repetition and the word-count chain does the transform per item. Readable left-to-right if you walk each piece:
results = [len(agent.run_sync(p).output.split()) for p in prompts]agent.run_sync(p) is the result object. .output is the string. .split() is the list of words. len(...) is the integer count. One expression, one integer per prompt.
All four chained calls happen inside the comprehension for every prompt automatically? No manual loop bookkeeping at all?
The comprehension handles the iteration and the result list. Your job is writing the per-item chain. And same rule as yesterday — create the agent once, outside the comprehension, so you don't pay the setup cost per item:
def batch_word_counts(prompts: list) -> list:
agent = Agent(model)
return [len(agent.run_sync(p).output.split()) for p in prompts]And the return type is list[int] because len(...) returns an int per iteration?
Right. The comprehension always produces a list; each element is the type of the inner expression. Chain returning a string gives list[str]; chain returning an int gives list[int]. The container shape is fixed, the element type is yours.
So this is how you measure a prompt's verbosity — feed it five different phrasings and see which one produces the shortest answers?
That is one real use. Verbosity testing, answer-length regression, evaluating prompt variants — they all want a numeric signal per input. Write batch_word_counts(prompts) now: one agent, one comprehension, chained len(.output.split()) per item.
TL;DR: [len(agent.run_sync(p).output.split()) for p in prompts] — four steps per item, one list out.
agent.run_sync(p) — result object.output — string.split() — list of wordslen(...) — integer count| Pattern | Signal |
|---|---|
| Word counts per prompt | Verbosity profiling |
sum(counts) / len(counts) | Average response length |
max(counts) | Longest response |
Always build the agent once above the comprehension — reuse is always safe.
Day 4 counted the words in one response. Day 19 batched a classifier over a list. Can you guess what happens if you compose them?
A list of prompts and a list of integer word counts back — one count per response, same length as the input?
Exactly. The list comprehension holds the repetition and the word-count chain does the transform per item. Readable left-to-right if you walk each piece:
results = [len(agent.run_sync(p).output.split()) for p in prompts]agent.run_sync(p) is the result object. .output is the string. .split() is the list of words. len(...) is the integer count. One expression, one integer per prompt.
All four chained calls happen inside the comprehension for every prompt automatically? No manual loop bookkeeping at all?
The comprehension handles the iteration and the result list. Your job is writing the per-item chain. And same rule as yesterday — create the agent once, outside the comprehension, so you don't pay the setup cost per item:
def batch_word_counts(prompts: list) -> list:
agent = Agent(model)
return [len(agent.run_sync(p).output.split()) for p in prompts]And the return type is list[int] because len(...) returns an int per iteration?
Right. The comprehension always produces a list; each element is the type of the inner expression. Chain returning a string gives list[str]; chain returning an int gives list[int]. The container shape is fixed, the element type is yours.
So this is how you measure a prompt's verbosity — feed it five different phrasings and see which one produces the shortest answers?
That is one real use. Verbosity testing, answer-length regression, evaluating prompt variants — they all want a numeric signal per input. Write batch_word_counts(prompts) now: one agent, one comprehension, chained len(.output.split()) per item.
TL;DR: [len(agent.run_sync(p).output.split()) for p in prompts] — four steps per item, one list out.
agent.run_sync(p) — result object.output — string.split() — list of wordslen(...) — integer count| Pattern | Signal |
|---|---|
| Word counts per prompt | Verbosity profiling |
sum(counts) / len(counts) | Average response length |
max(counts) | Longest response |
Always build the agent once above the comprehension — reuse is always safe.
Create a free account to get started. Paid plans unlock all tracks.