Skip to main content
ManagedAgent is deprecated as of PR #1550. New code should use HostedAgent for Anthropic-hosted runs (this page) or LocalAgent for local loops. Existing imports continue to work but emit a DeprecationWarning for non-Anthropic providers.
HostedAgent executes on Anthropic’s managed infrastructure while seamlessly persisting conversation history and session state to any of the registry-supported conversation and state backends. See the persistence overview for the full list.
from praisonaiagents import Agent, db
from praisonai import HostedAgent, HostedAgentConfig

hosted = HostedAgent(
    provider="anthropic",
    config=HostedAgentConfig(model="gpt-4o-mini", system="You are helpful"),
)
agent = Agent(name="assistant", backend=hosted)
agent.start("Remember my project name is Orion.")
The user chats with a hosted agent; each turn persists to your database so sessions resume after restarts.
When context compaction is enabled, managed persistence surfaces the compaction checkpoint automatically — resume replays the compacted working history (summary + tail). See Compacted Session Resume.

Quick Start

1

Basic Usage

Run gpt-4o-mini conversations with SQLite persistence in 5 lines:
from praisonaiagents import Agent, db
from praisonai import HostedAgent, HostedAgentConfig

hosted = HostedAgent(
    provider="anthropic",
    config=HostedAgentConfig(model="gpt-4o-mini", system="You are helpful")
)

agent = Agent(
    name="Assistant", 
    backend=hosted,
    db=db(database_url="conversation.db"),
    session_id="session-1"
)

agent.start("Remember: The sky is blue")
2

Session Resume

Continue conversations after restarts:
# Later process - same session_id resumes conversation
agent2 = Agent(
    name="Assistant",
    backend=hosted,
    db=db(database_url="conversation.db"), 
    session_id="session-1"  # Same ID = resume
)

response = agent2.start("What color is the sky?")
# Response: "The sky is blue" (remembers from previous session)

How It Works


Database Backends

Zero external dependencies, file-based storage:
from praisonaiagents import Agent, db
from praisonai import HostedAgent, HostedAgentConfig

# Phase 1: First session (teach facts)
hosted = HostedAgent(
    provider="anthropic",
    config=HostedAgentConfig(model="gpt-4o-mini", system="You are helpful")
)

agent = Agent(
    name="Assistant",
    backend=hosted,
    db=db(database_url="sqlite:///my_data.db"),
    session_id="learning-session"
)

agent.start("Remember: PraisonAI is an AI agent framework")
agent.start("Also remember: It supports multiple LLM providers")

# Phase 2: Direct verification
import sqlite3
conn = sqlite3.connect("my_data.db")
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM messages WHERE session_id = ?", ("learning-session",))
message_count = cursor.fetchone()[0]
print(f"Messages stored: {message_count}")
conn.close()

# Phase 3: Session resume (new instance)
hosted2 = HostedAgent(
    provider="anthropic", 
    config=HostedAgentConfig(model="gpt-4o-mini", system="You are helpful")
)

agent2 = Agent(
    name="Assistant",
    backend=hosted2,
    db=db(database_url="sqlite:///my_data.db"),
    session_id="learning-session"  # Same ID resumes
)

result = agent2.start("What did I tell you about PraisonAI?")
# Result: "You told me that PraisonAI is an AI agent framework and that it supports multiple LLM providers."
Prerequisites: None (built into Python)

Session Metadata Persistence

HostedAgent automatically persists both chat messages and session metadata to your database, ensuring complete session recovery after process restarts.

What Survives Process Restarts

When using database persistence, these session components automatically survive crashes, restarts, and deployments:
ComponentPurposeRestored After Restart
Chat MessagesFull conversation history✅ Yes
Token Countstotal_input_tokens, total_output_tokens✅ Yes
Session HistoryPer-turn audit trail✅ Yes
Agent Identityagent_id for session continuity✅ Yes
Compute InstanceSandbox/runner references✅ Yes

Complete Session Resume Example

from praisonaiagents import Agent, db
from praisonai import HostedAgent, HostedAgentConfig

# First process: run a turn, metadata persisted automatically
agent = Agent(
    name="Assistant",
    backend=HostedAgent(
        provider="anthropic",
        config=HostedAgentConfig(model="gpt-4o-mini")
    ),
    db=db(database_url="conversation.db"),
    session_id="session-1",
)
response = agent.start("Plan a 3-step research workflow on quantum computing")

# Check usage tokens (automatically tracked)
print(f"Tokens used: {agent.total_tokens}")

# Process exits. New process starts with same session_id.
agent2 = Agent(
    name="Assistant", 
    backend=HostedAgent(
        provider="anthropic",
        config=HostedAgentConfig(model="gpt-4o-mini")
    ),
    db=db(database_url="conversation.db"),
    session_id="session-1",  # Same ID → resumes with full metadata
)

# All metadata restored: token totals, session history, agent_id, compute instance
response = agent2.start("Continue from step 2.")
print(f"Total tokens across restarts: {agent2.total_tokens}")  # Cumulative count restored

Metadata Fields Preserved

The following metadata fields are automatically persisted and restored:
FieldTypeDescription
agent_idstrStable identity for session continuity
total_input_tokensintCumulative input tokens for cost tracking
total_output_tokensintCumulative output tokens for cost tracking
session_historyList[dict]Per-turn audit trail with actions and results
compute_instancestrSandbox/runner ID for compute reattachment
These fields enable cost tracking, usage analytics, and compute resource management across process boundaries.

Configuration Options

HostedAgent API Reference

Complete HostedAgent configuration options

PraisonDB Reference

Database adapter configuration options
ComponentPurposeKey Parameters
HostedAgentAnthropic execution backendprovider, config, api_key, timeout
HostedAgentConfigAgent definitionmodel, system, tools, packages
PraisonDBDatabase adapterdatabase_url, state_url, analytics_url
DbSessionAdapterSession bridgeAuto-configured based on database URL

Clearing & Deleting Sessions

The DbSessionAdapter now properly purges persisted messages from the database, not just the in-memory cache, ensuring that cleared sessions stay clear even after restarts.

Clear vs Delete Sessions

from praisonaiagents import Agent, db
from praisonai.integrations.db_session_adapter import DbSessionAdapter

store = DbSessionAdapter(db(database_url="conversation.db"))

# Clear the conversation — also wipes persisted rows in the DB
store.clear_session("session-1")
assert store.get_chat_history("session-1") == []

# A brand-new instance after a restart starts empty — no stale history
store2 = DbSessionAdapter(db(database_url="conversation.db"))
assert store2.get_chat_history("session-1") == []

# Delete session completely — removes all data and metadata
store.delete_session("session-1")
Privacy Guarantee: Cleared messages do not come back after a restart or when creating a new adapter instance. Both clear_session() and delete_session() now purge persisted messages from the underlying conversation store.
MethodPurposeBehavior
clear_session(session_id)Empty the conversation but keep it re-creatablePurges DB messages, resets cache to empty
delete_session(session_id)Remove the session entirelyPurges DB messages and removes all metadata

Common Patterns

The most common pattern for persistent managed agents:
from praisonaiagents import Agent, db
from praisonai import HostedAgent, HostedAgentConfig

def create_agent(session_id: str):
    hosted = HostedAgent(
        provider="anthropic",
        config=HostedAgentConfig(
            model="gpt-4o-mini",
            system="You are a helpful assistant with perfect memory"
        )
    )
    
    return Agent(
        name="PersistentAgent",
        backend=hosted,
        db=db(database_url="postgresql://localhost/agentdb"),
        session_id=session_id
    )

# First conversation
agent1 = create_agent("user-123")
agent1.start("My name is Alice and I live in Paris")

# Later conversation (different process/server restart)
agent2 = create_agent("user-123")  # Same session_id
response = agent2.start("What's my name and where do I live?")
# Response: "Your name is Alice and you live in Paris."
Use different backends for different data types:
from praisonaiagents import Agent, db
from praisonai import HostedAgent, HostedAgentConfig

hosted = HostedAgent(
    provider="anthropic",
    config=HostedAgentConfig(model="gpt-4o-mini")
)

agent = Agent(
    name="MultiBackendAgent",
    backend=hosted,
    db=db(
        database_url="postgresql://localhost/conversations",  # Conversations
        state_url="redis://localhost:6379",                  # Fast state
        analytics_url="clickhouse://localhost:8123/default"  # Analytics
    ),
    session_id="multi-backend-session"
)

# All data types are automatically stored in appropriate backends
agent.start("Analyze this data and remember the insights")
Best practices for session identification:
from praisonaiagents import Agent, db
from praisonai import HostedAgent, HostedAgentConfig
import hashlib
from datetime import datetime

def generate_session_id(user_id: str, conversation_type: str) -> str:
    """Generate deterministic session IDs"""
    # Per-user, per-type sessions
    base = f"{user_id}-{conversation_type}"
    return hashlib.md5(base.encode()).hexdigest()[:16]

def get_daily_session_id(user_id: str) -> str:
    """Daily session rotation"""
    today = datetime.now().strftime("%Y-%m-%d")
    return f"{user_id}-{today}"

# Usage examples
user_session = generate_session_id("user-456", "support")
daily_session = get_daily_session_id("user-456")

hosted = HostedAgent(
    provider="anthropic",
    config=HostedAgentConfig(model="gpt-4o-mini")
)

agent = Agent(
    name="SupportAgent",
    backend=hosted,
    db=db(database_url="sqlite:///support.db"),
    session_id=user_session  # Consistent across requests
)

Best Practices

  • Use meaningful session IDs (user-based, not random)
  • Implement session rotation for long conversations
  • Store session metadata for debugging
  • Concurrent metadata writes are safe with DefaultSessionStore (locked read-modify-write); for custom stores, implement update_session_metadata or your own equivalent to avoid stale-copy overwrites
  • SQLite: Development, single-user apps, file-based persistence
  • PostgreSQL: Production apps, complex queries, ACID compliance
  • MySQL: Existing MySQL infrastructure, compatibility requirements
  • Redis: High-speed state, session caching, temporary data
  • MongoDB: Document-based state, flexible schemas
  • ClickHouse: Analytics, large-scale logging, data warehousing
  • JSON Files: Prototyping, zero dependencies, simple use cases
  • Use connection pooling for database connections
  • Implement message compaction for long sessions
  • Cache frequently accessed session data
  • Use async database operations when possible
  • Monitor database performance metrics
  • Implement retry logic for transient database failures
  • Handle session corruption gracefully
  • Log database errors for debugging
  • Provide fallback behavior when persistence fails
  • Test database connection before agent creation

Hosted Agent

Run entire agent loops on Anthropic’s managed runtime

Local Agent

Run agent loops locally with any LLM

Managed CLI

Terminal commands for managing Anthropic-hosted resources

Session Management

Advanced session handling techniques