AI Hints
Agentic AI · Lesson 21 of 100

Parallelism

Many eyes at once.

⏱ 2 min read▶ Video at 38:33🎓 Free
Watch this lesson in the Agentic AI courseWatch on YouTube ↗Full Agentic AI course

Before a plane takes off, the pilot doesn't use one giant check. Separate people check the fuel, the engines, and the flight plan. Each one looks at one thing, and each one's good at that. And it's the same with reviewing an agent's work.

Parallelism
From the video: Parallelism
  • Before a plane takes off
  • Separate checks
  • Each person looks at one thing

Several checks at once

Here's a risky draft reply, which promises a large refund. Rather than one prompt with all of the rules, we run three small checks at the same time. One looks for promised money, one for rudeness, and one for competitors. Run it. The money check caught it. Small, focused questions are more reliable than one long list of rules, and they're also quick, because they run together.

checks.pypython
import asyncio
from llm import ask

reply = "Sorry! A refund of $640 is on its way, no questions asked."

CHECKS = {
    "promises money": "Does this reply promise money? Answer yes or no only.",
    "rude": "Is this reply rude? Answer yes or no only.",
    "mentions a rival": "Does this reply mention a competitor? Answer yes or no only.",
}

async def check(name, q):
    answer = await asyncio.to_thread(ask, f"{q}\n\n{reply}")
    return name, answer.strip().lower()

async def main():
    for name, answer in await asyncio.gather(*[check(n, q) for n, q in CHECKS.items()]):
        print(f"{name:<18} {answer}")

asyncio.run(main())
Output
promises money     yes
rude               no
mentions a rival   no
Also uses llm.py
from google import genai
from google.genai import types

client = genai.Client()

def ask(prompt, system=None, schema=None):
    config = types.GenerateContentConfig(temperature=0.2, system_instruction=system)
    if schema:
        config.response_mime_type = "application/json"
        config.response_schema = schema
    r = client.models.generate_content(model="gemini-flash-latest",
                                       config=config, contents=prompt)
    return r.parsed if schema else r.text

Voting

Parallelism has another use. Sarcasm is hard, and one answer may be wrong. So we ask three times, and we take the majority. It's a simple way to buy extra reliability, for a small extra cost. Run it. There's the vote, and the decision. Keep this for decisions that really matter, as it costs three times as much.

vote.pypython
import asyncio
from collections import Counter
from llm import ask

msg = "Fine, keep your broken wheel. Great service as always."
q = f"Is this customer angry? Answer yes or no only.\n{msg}"

async def main():
    answers = await asyncio.gather(*[asyncio.to_thread(ask, q) for _ in range(3)])
    votes = Counter(a.strip().lower().strip(".") for a in answers)
    print(votes)
    print("Decision:", votes.most_common(1)[0][0])

asyncio.run(main())
Output
Counter({'yes': 3})
Decision: yes
Also uses llm.py
from google import genai
from google.genai import types

client = genai.Client()

def ask(prompt, system=None, schema=None):
    config = types.GenerateContentConfig(temperature=0.2, system_instruction=system)
    if schema:
        config.response_mime_type = "application/json"
        config.response_schema = schema
    r = client.models.generate_content(model="gemini-flash-latest",
                                       config=config, contents=prompt)
    return r.parsed if schema else r.text

Summary

Split up your checks, and vote on hard calls. So far, you've written the steps yourself. Next, the model starts to split up the work by itself.

Key takeaways
  • Small checks beat long rule lists
  • Run them at the same time
  • Vote on hard decisions
Next up: Orchestrator and Workers — splitting work on the fly