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.
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:
2

Session Resume

Continue conversations after restarts:

How It Works


Database Backends

Zero external dependencies, file-based storage:
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:

Complete Session Resume Example

Metadata Fields Preserved

The following metadata fields are automatically persisted and restored: 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

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

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.

Common Patterns

The most common pattern for persistent managed agents:
Use different backends for different data types:
Best practices for session identification:

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