Yesterday's batch_classify applied one function to a list. What if you need to run different prompts through an agent and find which one produced the shortest response?
A list comprehension to get all responses, then min(..., key=len) to find the shortest one. key=len means Python compares by length, not alphabetically.
Exactly right. min(responses, key=len) returns the string with the minimum len() value. The list comprehension runs the agent, the min reduces the results:
def shortest_response(prompts: list) -> str:
responses = [Agent(model).run_sync(p).output for p in prompts]
return min(responses, key=len)Why would I want the shortest response? Shorter isn't better in research writing.
Direct use case: comparing prompt phrasings. If you're trying to get a concise definition or a one-line summary, you want to know which prompt phrasing makes the model the most concise. The shortest response to a 'summarize in one sentence' prompt tells you which phrasing actually worked as instructed:
def shortest_response(prompts: list) -> str:
responses = [Agent(model).run_sync(p).output for p in prompts]
shortest = min(responses, key=len)
print(f"Shortest response: {len(shortest)} characters from {len(prompts)} prompts")
return shortestI could use this to optimise my abstract screening prompt — test ten phrasings, find which one produces the most concise 'relevant/not relevant' decision, use that phrasing in the batch run.
Prompt engineering is an iterative process. Running variants and measuring output length is a valid signal for conciseness. It's not the only signal — accuracy matters more — but length tells you whether the model understood 'be brief'.
My thesis advisor always writes back with one sentence. Maybe she's been doing min(responses, key=len) for years.
Efficient academics converge on concise answers through practice. The function codifies that instinct as a measurable process.
responses = [Agent(model).run_sync(p).output for p in prompts]
return min(responses, key=len)key=lenmin() with key=len compares elements by their length (number of characters) rather than lexicographically. The shortest string by character count is returned.
Each prompt in the list gets one API call. The comprehension collects all responses before min() selects the shortest. This is a sequential batch — calls happen one after another, not in parallel.
Quality audit: the shortest response in a batch often signals a refusal or a degenerate output. Use min to surface the weakest response for manual inspection before passing the batch downstream.
Yesterday's batch_classify applied one function to a list. What if you need to run different prompts through an agent and find which one produced the shortest response?
A list comprehension to get all responses, then min(..., key=len) to find the shortest one. key=len means Python compares by length, not alphabetically.
Exactly right. min(responses, key=len) returns the string with the minimum len() value. The list comprehension runs the agent, the min reduces the results:
def shortest_response(prompts: list) -> str:
responses = [Agent(model).run_sync(p).output for p in prompts]
return min(responses, key=len)Why would I want the shortest response? Shorter isn't better in research writing.
Direct use case: comparing prompt phrasings. If you're trying to get a concise definition or a one-line summary, you want to know which prompt phrasing makes the model the most concise. The shortest response to a 'summarize in one sentence' prompt tells you which phrasing actually worked as instructed:
def shortest_response(prompts: list) -> str:
responses = [Agent(model).run_sync(p).output for p in prompts]
shortest = min(responses, key=len)
print(f"Shortest response: {len(shortest)} characters from {len(prompts)} prompts")
return shortestI could use this to optimise my abstract screening prompt — test ten phrasings, find which one produces the most concise 'relevant/not relevant' decision, use that phrasing in the batch run.
Prompt engineering is an iterative process. Running variants and measuring output length is a valid signal for conciseness. It's not the only signal — accuracy matters more — but length tells you whether the model understood 'be brief'.
My thesis advisor always writes back with one sentence. Maybe she's been doing min(responses, key=len) for years.
Efficient academics converge on concise answers through practice. The function codifies that instinct as a measurable process.
responses = [Agent(model).run_sync(p).output for p in prompts]
return min(responses, key=len)key=lenmin() with key=len compares elements by their length (number of characters) rather than lexicographically. The shortest string by character count is returned.
Each prompt in the list gets one API call. The comprehension collects all responses before min() selects the shortest. This is a sequential batch — calls happen one after another, not in parallel.
Quality audit: the shortest response in a batch often signals a refusal or a degenerate output. Use min to surface the weakest response for manual inspection before passing the batch downstream.
Create a free account to get started. Paid plans unlock all tracks.