Yesterday run_agent returned the model's full text. A proposal brief can be 800 words or 80. How would you know which, without reading it?
I'd have to read it myself. That defeats the purpose — I'm trying to save time, not just move where I read.
The output is a Python string. Strings have .split(). Split on whitespace, count the pieces — that's your word count:
result = Agent(model).run_sync(prompt)
words = result.output.split()
print(len(words))So I'm treating the AI's output like any other string? I thought there was something special about it.
There's nothing special about it once it comes back. It's a Python string. Slice it, split it, search it, pass it to another function — the model's job ends when .output is populated. Your code's job starts there.
def word_count_of_output(prompt: str) -> int:
result = Agent(model).run_sync(prompt)
words = result.output.split()
return len(words)So the AI writes the text and I just measure it. That's a useful quality gate — if the output is under 10 words, something probably went wrong.
Exactly. A word count doesn't grade quality, but it flags suspiciously short answers. And the same pattern works for any post-processing: length, keyword search, format checks — all just string operations on .output.
I can already see using this: if the proposal summary is under 50 words, ask the agent to expand.
That's the composability insight. Call the model, check the output, decide what to do next — all in plain Python.
.output returns a plain Python str. All standard string operations apply immediately.
output = Agent(model).run_sync(prompt).output
len(output.split()) # word count
output.lower() # normalise case
output[:200] # first 200 characters
"invoice" in output # keyword checkEvery post-processing step — length checks, keyword extraction, format validation — is just a string operation on .output. The AI produces the text; your code decides what to do with it.
The .split() method without arguments splits on any whitespace — spaces, tabs, and newlines — which is exactly what word count needs. A response of 0 words means the model returned an empty string, which is worth logging as a quality issue.
Yesterday run_agent returned the model's full text. A proposal brief can be 800 words or 80. How would you know which, without reading it?
I'd have to read it myself. That defeats the purpose — I'm trying to save time, not just move where I read.
The output is a Python string. Strings have .split(). Split on whitespace, count the pieces — that's your word count:
result = Agent(model).run_sync(prompt)
words = result.output.split()
print(len(words))So I'm treating the AI's output like any other string? I thought there was something special about it.
There's nothing special about it once it comes back. It's a Python string. Slice it, split it, search it, pass it to another function — the model's job ends when .output is populated. Your code's job starts there.
def word_count_of_output(prompt: str) -> int:
result = Agent(model).run_sync(prompt)
words = result.output.split()
return len(words)So the AI writes the text and I just measure it. That's a useful quality gate — if the output is under 10 words, something probably went wrong.
Exactly. A word count doesn't grade quality, but it flags suspiciously short answers. And the same pattern works for any post-processing: length, keyword search, format checks — all just string operations on .output.
I can already see using this: if the proposal summary is under 50 words, ask the agent to expand.
That's the composability insight. Call the model, check the output, decide what to do next — all in plain Python.
.output returns a plain Python str. All standard string operations apply immediately.
output = Agent(model).run_sync(prompt).output
len(output.split()) # word count
output.lower() # normalise case
output[:200] # first 200 characters
"invoice" in output # keyword checkEvery post-processing step — length checks, keyword extraction, format validation — is just a string operation on .output. The AI produces the text; your code decides what to do with it.
The .split() method without arguments splits on any whitespace — spaces, tabs, and newlines — which is exactly what word count needs. A response of 0 words means the model returned an empty string, which is worth logging as a quality issue.
Create a free account to get started. Paid plans unlock all tracks.