Concurrent Execution
Run multiple agents concurrently using asyncio.gather for parallel execution.
1"""2Concurrent Execution3=============================45Concurrent Agent Execution with asyncio.gather.6"""78import asyncio910from kern.agent import Agent11from kern.models.openai import OpenAIResponses12from kern.tools.duckduckgo import DuckDuckGoTools13from rich.pretty import pprint1415# ---------------------------------------------------------------------------16# Create Agent17# ---------------------------------------------------------------------------18providers = ["openai", "anthropic", "ollama", "cohere", "google"]19instructions = """20Your task is to write a well researched report on AI providers.21The report should be unbiased and factual.22"""2324# Create the agent ONCE outside the loop - this is the correct pattern25agent = Agent(26 model=OpenAIResponses(id="gpt-5-mini"),27 instructions=instructions,28 tools=[DuckDuckGoTools()],29)303132async def get_reports():33 """Run multiple research tasks concurrently using the same agent instance."""34 tasks = [35 agent.arun(f"Write a report on the following AI provider: {provider}")36 for provider in providers37 ]38 results = await asyncio.gather(*tasks)39 return results404142async def main():43 results = await get_reports()44 for result in results:45 print("************")46 pprint(result.content)47 print("************")48 print("\n")495051# ---------------------------------------------------------------------------52# Run Agent53# ---------------------------------------------------------------------------54if __name__ == "__main__":55 asyncio.run(main())Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/14_advanced45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python concurrent_execution.py