Async agents with astart

Run multiple prompts concurrently with asyncio

Calling agent.start(…) blocks until one LLM response finishes. When you need to process many prompts at the same time — such as in a FastAPI web server or batch job — use await agent.astart(…) with asyncio.gather(…).

Expected output: both prompts run concurrently and print their results in roughly the time of a single call.

Example

import asyncio
from praisonai import Agent

async def main():
    agent = Agent(instructions='Reply in five words or fewer.')
    results = await asyncio.gather(
        agent.astart('Name one ocean.'),
        agent.astart('Name one continent.'),
    )
    for reply in results:
        print(reply)

asyncio.run(main())

Try it Yourself

Example explained

  • await agent.astart(…) — Non-blocking async version of start() for concurrent Python apps.
  • asyncio.gather(…) — Runs multiple agent calls in parallel instead of waiting one by one.

When to use astart() vs start()

Use agent.start() in simple scripts and CLI jobs. Switch to await agent.astart() inside FastAPI/AgentOS routes, Telegram/Discord bots, or asyncio.gather() batches so waiting on LLM network I/O never blocks other users.

Common pitfall: calling blocking agent.start() inside an async def endpoint freezes the entire event loop for concurrent visitors.

Reference: documentation