You have a list of 30 ticket subjects and you want the agent to bucket them into high, medium, low — then return the counts per bucket. How do you keep that clean?
A loop that calls the agent per item and counts? Or do I need a fancier aggregator?
A comprehension plus a counter dict is all you need. One Literal-typed agent, one loop, one dict tally. Build the agent once, reuse it per item:
from typing import Literal
agent = Agent(model, result_type=Literal["high", "medium", "low"])
labels = [agent.run_sync(s).output for s in subjects]
print(labels)So the agent is created once outside the loop and called 30 times inside — not a fresh agent per item?
Exactly. Reuse saves allocation; the agent is stateless between calls. Counting is a plain dict update. The complete batch:
def triage_and_count(subjects: list) -> dict:
agent = Agent(model, result_type=Literal["high", "medium", "low"])
labels = [agent.run_sync(s).output for s in subjects]
counts = {"high": 0, "medium": 0, "low": 0}
for lbl in labels:
counts[lbl] = counts.get(lbl, 0) + 1
return countsWhy seed the dict with zero counts instead of building it fresh from the labels?
Because the caller wants all three keys present — even when zero. A missing key forces defensive .get(k, 0) everywhere downstream. Seeding the dict guarantees the output shape is always {high, medium, low}, no surprises.
So batch classification plus a clean counter is a three-line contract — one Literal type, one comprehension, one dict tally?
Three lines, one pass, typed output. This pattern generalizes to any closed-set label space — priorities, statuses, categories — the shape stays the same.
TL;DR: reuse one Literal agent over the list; seed the counter dict so every label appears.
Agent(model, result_type=Literal[...]) — validated labels, no .strip().lower() neededsubjects — one agent call per item| Placement | Allocations |
|---|---|
| Inside comprehension | N agents |
| Outside comprehension | 1 agent |
Stateless agents make reuse free — batch at scale without overhead.
You have a list of 30 ticket subjects and you want the agent to bucket them into high, medium, low — then return the counts per bucket. How do you keep that clean?
A loop that calls the agent per item and counts? Or do I need a fancier aggregator?
A comprehension plus a counter dict is all you need. One Literal-typed agent, one loop, one dict tally. Build the agent once, reuse it per item:
from typing import Literal
agent = Agent(model, result_type=Literal["high", "medium", "low"])
labels = [agent.run_sync(s).output for s in subjects]
print(labels)So the agent is created once outside the loop and called 30 times inside — not a fresh agent per item?
Exactly. Reuse saves allocation; the agent is stateless between calls. Counting is a plain dict update. The complete batch:
def triage_and_count(subjects: list) -> dict:
agent = Agent(model, result_type=Literal["high", "medium", "low"])
labels = [agent.run_sync(s).output for s in subjects]
counts = {"high": 0, "medium": 0, "low": 0}
for lbl in labels:
counts[lbl] = counts.get(lbl, 0) + 1
return countsWhy seed the dict with zero counts instead of building it fresh from the labels?
Because the caller wants all three keys present — even when zero. A missing key forces defensive .get(k, 0) everywhere downstream. Seeding the dict guarantees the output shape is always {high, medium, low}, no surprises.
So batch classification plus a clean counter is a three-line contract — one Literal type, one comprehension, one dict tally?
Three lines, one pass, typed output. This pattern generalizes to any closed-set label space — priorities, statuses, categories — the shape stays the same.
TL;DR: reuse one Literal agent over the list; seed the counter dict so every label appears.
Agent(model, result_type=Literal[...]) — validated labels, no .strip().lower() neededsubjects — one agent call per item| Placement | Allocations |
|---|---|
| Inside comprehension | N agents |
| Outside comprehension | 1 agent |
Stateless agents make reuse free — batch at scale without overhead.
Create a free account to get started. Paid plans unlock all tracks.