Your run_agent returns a string. That string is just Python — you can slice it, measure it, pass it to another function. What would you measure first?
Length maybe? Or word count. If I'm paying per token I want to know how much the model writes.
Word count is exactly right. run_agent(prompt) returns a string — call .split() on it to get a list of words, then len() for the count. Two standard library operations on a plain Python string:
output = Agent(model).run_sync(prompt).output
words = output.split()
return len(words)So .split() with no arguments splits on any whitespace? Tabs, newlines, multiple spaces?
Yes — str.split() without an argument splits on all whitespace and collapses multiple spaces, tabs, and newlines. It's the safest way to count words in free-form AI output. Here's the full function:
def word_count_of_output(prompt: str) -> int:
output = Agent(model).run_sync(prompt).output
words = output.split()
count = len(words)
print(f"Word count: {count}")
return countI'm measuring the model's verbosity like a line item on my P&L. Useful.
Every AI output is data you can process. After word count, the pattern continues: call the agent, get a string, do something with it — summarise, classify, count, store. The tools build on each other.
The agent output is just a Python string — I can do everything I'd do with any other string. Filter it, count it, store it in a dict.
That's the key insight. AI is not a black box you query — it's a function that returns data. Once you see the output as just a string, you stop being intimidated and start building. Every string method you already know — split(), [:200], lower(), replace() — works on agent output.
Agent(model).run_sync(prompt).output returns a str. Every string method works on it:
output = Agent(model).run_sync(prompt).output
len(output.split()) # word count
output[:200] # truncate to 200 chars
output.lower() # normalise case
output.replace('\n', ' ') # flatten newlines.split() with no argumentstr.split() (no argument) splits on any whitespace — spaces, tabs, newlines — and collapses multiple consecutive spaces. It is the safest way to count words in free-form AI output.
.split(' ') (with a space argument) only splits on single spaces and creates empty strings from consecutive spaces — not what you want for word counting.
Your run_agent returns a string. That string is just Python — you can slice it, measure it, pass it to another function. What would you measure first?
Length maybe? Or word count. If I'm paying per token I want to know how much the model writes.
Word count is exactly right. run_agent(prompt) returns a string — call .split() on it to get a list of words, then len() for the count. Two standard library operations on a plain Python string:
output = Agent(model).run_sync(prompt).output
words = output.split()
return len(words)So .split() with no arguments splits on any whitespace? Tabs, newlines, multiple spaces?
Yes — str.split() without an argument splits on all whitespace and collapses multiple spaces, tabs, and newlines. It's the safest way to count words in free-form AI output. Here's the full function:
def word_count_of_output(prompt: str) -> int:
output = Agent(model).run_sync(prompt).output
words = output.split()
count = len(words)
print(f"Word count: {count}")
return countI'm measuring the model's verbosity like a line item on my P&L. Useful.
Every AI output is data you can process. After word count, the pattern continues: call the agent, get a string, do something with it — summarise, classify, count, store. The tools build on each other.
The agent output is just a Python string — I can do everything I'd do with any other string. Filter it, count it, store it in a dict.
That's the key insight. AI is not a black box you query — it's a function that returns data. Once you see the output as just a string, you stop being intimidated and start building. Every string method you already know — split(), [:200], lower(), replace() — works on agent output.
Agent(model).run_sync(prompt).output returns a str. Every string method works on it:
output = Agent(model).run_sync(prompt).output
len(output.split()) # word count
output[:200] # truncate to 200 chars
output.lower() # normalise case
output.replace('\n', ' ') # flatten newlines.split() with no argumentstr.split() (no argument) splits on any whitespace — spaces, tabs, newlines — and collapses multiple consecutive spaces. It is the safest way to count words in free-form AI output.
.split(' ') (with a space argument) only splits on single spaces and creates empty strings from consecutive spaces — not what you want for word counting.
Create a free account to get started. Paid plans unlock all tracks.