Complete CRUD operations for agents, environments, and sessions
Managed agent lifecycle provides full control over agent definitions, environments, and running sessions with comprehensive CRUD operations.
from praisonaiagents import Agentagent = Agent(name="ops-agent", instructions="Manage hosted agent resources.")agent.start("List active sessions and archive the stale ones.")
The user manages agents through CRUD APIs; definitions, environments, and sessions move through create → run → archive flows.
from praisonai import AnthropicManagedAgent, ManagedConfigmanaged = AnthropicManagedAgent(config=ManagedConfig( model="claude-sonnet-4-6", system="You are a helpful coding assistant.",))# Create agent and environmentagent_id = await managed.create_agent(managed._cfg)env_id = await managed.create_environment(managed._cfg)# Create sessionsession_id = await managed.create_session(agent_id, env_id)# Use session...await managed.send_event(session_id, {"type": "user.message", "content": [{"type": "text", "text": "Hello!"}]})# Clean up when doneawait managed.archive_session(session_id)await managed.archive_agent() # Clears cached IDs
2
Agent Version Pinning
Pin agents to specific versions for consistent behavior.
# Create agent and set version preferencemanaged = AnthropicManagedAgent(config=config)agent_id = await managed.create_agent(config)# Pin to specific versionmanaged.agent_version = 3# Sessions will use version 3session_id = await managed.create_session(agent_id, env_id)# List all versions of this agentversions = await managed.list_agent_versions()# [{"version": 1, "created_at": "..."}, {"version": 2, "..."}, {"version": 3, "..."}]
# Create agent (version 1)agent_id = await managed.create_agent(config)print(f"Created agent {agent_id} v{managed.agent_version}")# Update agent (creates version 2)updated_config = ManagedConfig( model="claude-haiku-4-5", # Changed model system="Updated system prompt")agent_id = await managed.create_agent(updated_config)print(f"Updated agent {agent_id} v{managed.agent_version}")# Pin to specific versionmanaged.agent_version = 1session_id = await managed.create_session(agent_id, env_id) # Uses version 1# List all versionsversions = await managed.list_agent_versions()for v in versions: print(f"Version {v['version']} created at {v['created_at']}")
# Pin to specific versions in productionproduction_agent_version = 5managed.agent_version = production_agent_version# Test new versions in stagingstaging_managed = AnthropicManagedAgent(config=new_config)test_agent_id = await staging_managed.create_agent(staging_managed._cfg)# If tests pass, promote to production
Handle failures gracefully with proper resource tracking:
try: agent_id = await managed.create_agent(config) env_id = await managed.create_environment(config) session_id = await managed.create_session(agent_id, env_id)except Exception as e: # Clean up any partially created resources if hasattr(managed, 'agent_id') and managed.agent_id: await managed.archive_agent() if hasattr(managed, 'environment_id') and managed.environment_id: await managed.delete_environment() raise e
A shared AnthropicManagedAgent or LocalManagedAgent is safe to execute() from multiple threads; agent / environment / session creation is guarded by per-instance locks, so only one of any resource is ever created. Each execute() call takes a per-call timeout= and stream_live= kwarg since PR #5774 — see Managed Runtime Protocol → Per-call execute() kwargs. On timeout the backend’s interrupt() is called on a daemon thread and asyncio.TimeoutError re-raises.
from concurrent.futures import ThreadPoolExecutorfrom praisonai import AnthropicManagedAgent, ManagedConfigmanaged = AnthropicManagedAgent(config=ManagedConfig(name="shared-agent"))with ThreadPoolExecutor(max_workers=8) as pool: for _ in range(8): pool.submit(managed.execute, "Summarise today's news")# Exactly one agent / environment / session was created in Anthropic's cloud.
The locks wrap only check + create + assign, so a stream reader is never blocked by another caller’s create. Long-running stream reads hold no lock.