Each agent automatically gets its own context-isolated runtime and tool resolver:
import asynciofrom praisonaiagents import Agentasync def run_agents(): # Each agent gets its own context automatically agent1 = Agent(name="Agent1", instructions="Help with coding") agent2 = Agent(name="Agent2", instructions="Help with writing") # These run safely in parallel - no state bleed tasks = [ asyncio.create_task(agent1.astart("Write a Python function")), asyncio.create_task(agent2.astart("Write a blog post")) ] results = await asyncio.gather(*tasks) print("Both agents completed safely")asyncio.run(run_agents())
2
Multi-Project Tool Resolution
Agents in different working directories get their own tool resolver anchored to their CWD:
import osfrom praisonaiagents import Agentfrom praisonai.tool_resolver import reset_default_resolver# Project A with its own tools.pyos.chdir("/project_a")agent_a = Agent(name="ProjectA", instructions="Use project A tools")agent_a.start("Use my_custom_tool") # Uses /project_a/tools.py# Switch to project Bos.chdir("/project_b")reset_default_resolver() # Force re-anchor for new CWDagent_b = Agent(name="ProjectB", instructions="Use project B tools")agent_b.start("Use my_custom_tool") # Uses /project_b/tools.py
import asynciofrom praisonaiagents import Agentasync def parallel_analysis(): """Run multiple agents concurrently without interference.""" # Each agent gets isolated context automatically agents = [ Agent(name="DataAnalyst", instructions="Analyze data patterns"), Agent(name="SecurityReviewer", instructions="Check for security issues"), Agent(name="CodeReviewer", instructions="Review code quality") ] tasks = [ "Analyze the sales data for trends", "Audit the authentication system", "Review the new API endpoints" ] # All run safely in parallel results = await asyncio.gather(*[ agent.astart(task) for agent, task in zip(agents, tasks) ]) return results# Each agent uses its own runtime/resolver contextresults = asyncio.run(parallel_analysis())
When switching working directories in long-lived processes, explicitly reset the tool resolver to pick up the new directory’s tools.py.
import osfrom praisonai.tool_resolver import reset_default_resolver# Good - explicit reset after chdiros.chdir("/new/project")reset_default_resolver()agent = Agent(name="Agent", instructions="Use local tools")# Bad - may use cached resolver from old directory os.chdir("/new/project")agent = Agent(name="Agent", instructions="Use local tools")
Trust automatic context isolation
Don’t manually manage context variables - the framework handles isolation automatically. Each async task or thread gets its own context.
# Good - automatic isolationasync def worker(agent_name: str): agent = Agent(name=agent_name, instructions="Work independently") return await agent.astart("Do the task")# Run multiple workers - each gets isolated contexttasks = [worker(f"Agent{i}") for i in range(5)]results = await asyncio.gather(*tasks)# Bad - manual context management (unnecessary)import contextvarsctx_var = contextvars.ContextVar('agent_state')# Don't do this - framework handles it
Use context-local cleanup functions
When cleaning up resources, use the context-local versions that only affect the current agent’s context.
from praisonai.cli.features.interactive_tools import cleanup_runtimeasync def agent_task(): agent = Agent(name="TempAgent", instructions="Temporary task") try: result = await agent.astart("Do something") return result finally: cleanup_runtime() # Only cleans up this context's runtime
Leverage CWD-per-context for multi-project tools
Each context anchors its tool resolver to the working directory when first called. Use this for clean project separation.
# Project structure:# /project_a/tools.py - has custom_tool_a()# /project_b/tools.py - has custom_tool_b()import osfrom praisonaiagents import Agent# Agent A in project A contextos.chdir("/project_a")agent_a = Agent(name="AgentA", instructions="Use project A tools")# Automatically resolves to /project_a/tools.py# Agent B in project B context os.chdir("/project_b")agent_b = Agent(name="AgentB", instructions="Use project B tools")# Automatically resolves to /project_b/tools.py# Each agent uses its own project's tools