The model just answered you in English. How do you count how many words the answer had?
Split the string on whitespace and count the items? Same as counting words in any other Python string?
Exactly that. The key insight: result.output is just a regular Python string. Nothing special about it. Once you have it, every string tool you know applies — .split(), len(), slicing, indexing:
output = "A language model generates tokens."
print(len(output.split()))The call to .split() with no argument splits on any run of whitespace and drops empties, so " lots of space ".split() gives three items, not seven.
So I make the same agent call, then treat the output as an ordinary string?
Exactly. The agent work is unchanged — Agent(model).run_sync(prompt) hands you a result, .output gives you the string. The new work is the one-line transform after that:
def word_count_of_output(prompt: str) -> int:
result = Agent(model).run_sync(prompt)
return len(result.output.split())Why use .split() with no argument instead of .split(" ")?
.split(" ") splits on each single space, so "a b" gives ["a", "", "b"] — the empty string is an artifact. .split() with no argument collapses any whitespace run and skips empties. For word counting this is always what you want.
So the same pattern works for a short factual answer and a multi-sentence explanation — one function, any prompt, a count out?
One function, any prompt, a number. The agent gives you text; Python counts it. That separation — model for language, Python for math — is the shape of almost every agent program you will write.
TL;DR: call the agent, read .output, apply len(s.split()).
Agent(model).run_sync(prompt) — still the base callresult.output.split() — whitespace split, no argumentlen(...) — returns an int| Input | .split() | len |
|---|---|---|
"hello world" | ["hello", "world"] | 2 |
" lots of space " | ["lots", "of", "space"] | 3 |
"" | [] | 0 |
Outputs vary across runs because sampling is non-deterministic — the schema checks shape (int, min 1), not exact count.
The model just answered you in English. How do you count how many words the answer had?
Split the string on whitespace and count the items? Same as counting words in any other Python string?
Exactly that. The key insight: result.output is just a regular Python string. Nothing special about it. Once you have it, every string tool you know applies — .split(), len(), slicing, indexing:
output = "A language model generates tokens."
print(len(output.split()))The call to .split() with no argument splits on any run of whitespace and drops empties, so " lots of space ".split() gives three items, not seven.
So I make the same agent call, then treat the output as an ordinary string?
Exactly. The agent work is unchanged — Agent(model).run_sync(prompt) hands you a result, .output gives you the string. The new work is the one-line transform after that:
def word_count_of_output(prompt: str) -> int:
result = Agent(model).run_sync(prompt)
return len(result.output.split())Why use .split() with no argument instead of .split(" ")?
.split(" ") splits on each single space, so "a b" gives ["a", "", "b"] — the empty string is an artifact. .split() with no argument collapses any whitespace run and skips empties. For word counting this is always what you want.
So the same pattern works for a short factual answer and a multi-sentence explanation — one function, any prompt, a count out?
One function, any prompt, a number. The agent gives you text; Python counts it. That separation — model for language, Python for math — is the shape of almost every agent program you will write.
TL;DR: call the agent, read .output, apply len(s.split()).
Agent(model).run_sync(prompt) — still the base callresult.output.split() — whitespace split, no argumentlen(...) — returns an int| Input | .split() | len |
|---|---|---|
"hello world" | ["hello", "world"] | 2 |
" lots of space " | ["lots", "of", "space"] | 3 |
"" | [] | 0 |
Outputs vary across runs because sampling is non-deterministic — the schema checks shape (int, min 1), not exact count.
Create a free account to get started. Paid plans unlock all tracks.