Yesterday's run_agent returns a string. What if you need to know how detailed the model's response is before you pass it to the next step?
I could check len(output) for characters, but word count is more meaningful for research writing — you'd want at least 20 words for a real summary, not just punctuation.
Exactly. .split() on a string splits on whitespace and returns a list of words. len() of that list is the word count. Two Python operations on the output string from Day 3:
def word_count_of_output(prompt: str) -> int:
result = Agent(model).run_sync(prompt)
words = result.output.split()
return len(words)So the function doesn't return the text — just the count? When would I use a count instead of the text itself?
Quality checks on batch runs. If you loop over 40 abstracts and ask the model to summarise each one, you want to flag any response shorter than 10 words — those are likely failures or refusals. word_count_of_output becomes a diagnostic tool:
def word_count_of_output(prompt: str) -> int:
result = Agent(model).run_sync(prompt)
output = result.output
words = output.split()
count = len(words)
print(f"Response: {count} words")
return countI can use this to automatically flag responses under a threshold and re-prompt those cases. That's a quality gate for the whole pipeline.
That's exactly the pattern. Word count is the cheapest possible output validator. A 3-word response to "summarise this 200-word abstract" is a signal something went wrong — before you pass that output to the next step.
My thesis advisor gives the same feedback — if your response is under 100 words, you haven't actually answered the question.
The model has the same tendency. Prompts that are too short or ambiguous get short, vague responses. Word count is the first signal.
result = Agent(model).run_sync(prompt)
words = result.output.split()
return len(words).split() behaviourstr.split() with no argument splits on any whitespace and removes empty strings. "hello world".split() → ['hello', 'world'], len() = 2.
Quality gate for batch pipelines — flag responses under a threshold for re-prompting. A 3-word response to a 200-word abstract prompt is a signal something went wrong, not a valid summary.
Loop over 40 prompts, collect word counts as a list, and filter for any response under 10 words — those are candidates for re-prompting.
Yesterday's run_agent returns a string. What if you need to know how detailed the model's response is before you pass it to the next step?
I could check len(output) for characters, but word count is more meaningful for research writing — you'd want at least 20 words for a real summary, not just punctuation.
Exactly. .split() on a string splits on whitespace and returns a list of words. len() of that list is the word count. Two Python operations on the output string from Day 3:
def word_count_of_output(prompt: str) -> int:
result = Agent(model).run_sync(prompt)
words = result.output.split()
return len(words)So the function doesn't return the text — just the count? When would I use a count instead of the text itself?
Quality checks on batch runs. If you loop over 40 abstracts and ask the model to summarise each one, you want to flag any response shorter than 10 words — those are likely failures or refusals. word_count_of_output becomes a diagnostic tool:
def word_count_of_output(prompt: str) -> int:
result = Agent(model).run_sync(prompt)
output = result.output
words = output.split()
count = len(words)
print(f"Response: {count} words")
return countI can use this to automatically flag responses under a threshold and re-prompt those cases. That's a quality gate for the whole pipeline.
That's exactly the pattern. Word count is the cheapest possible output validator. A 3-word response to "summarise this 200-word abstract" is a signal something went wrong — before you pass that output to the next step.
My thesis advisor gives the same feedback — if your response is under 100 words, you haven't actually answered the question.
The model has the same tendency. Prompts that are too short or ambiguous get short, vague responses. Word count is the first signal.
result = Agent(model).run_sync(prompt)
words = result.output.split()
return len(words).split() behaviourstr.split() with no argument splits on any whitespace and removes empty strings. "hello world".split() → ['hello', 'world'], len() = 2.
Quality gate for batch pipelines — flag responses under a threshold for re-prompting. A 3-word response to a 200-word abstract prompt is a signal something went wrong, not a valid summary.
Loop over 40 prompts, collect word counts as a list, and filter for any response under 10 words — those are candidates for re-prompting.
Create a free account to get started. Paid plans unlock all tracks.