Skip to main content
Async DB hooks enable non-blocking database operations in async agents through automatic async/sync detection and asyncio.to_thread fallback.
Breaking Change in PR #1829: The async_* prefixed methods have been removed from async stores. The orchestrator now uses isinstance(store, AsyncConversationStore) for dispatch instead of runtime method introspection.
The user chats asynchronously; DB hooks persist messages without blocking the event loop.

Quick Start

1

Async Context Manager

Use async DB operations with the async context manager for automatic cleanup.
2

Wire to Async Agent

Connect async DB hooks to an async agent for seamless persistence.

How It Works

Sync hook against an async-mode store

Calling a sync hook against a store opened with mode="async" now fails clearly instead of crashing deep inside the store.
Calling the sync on_agent_start against a store opened with mode="async" from inside a running event loop now raises a clear RuntimeError telling you to use the async surface (previously it crashed with the opaque TypeError: 'coroutine' object is not iterable). Called outside a loop, sync now resolves the coroutine transparently.

State-store lifecycle key

The wrapper writes both start and end under agent:{session_id}, so the end transition updates the same record the start wrote instead of a disconnected one. Start previously used agent:{session_id}:{agent_id or name} and end wrote a separate record. Consumers who query the state store directly must update their key format to agent:{session_id}.

Configuration Options

Signature change in PR #3857: aon_agent_start(agent_name, session_id, user_id, metadata) -> List and aon_agent_end(session_id, metadata). user_id is now persisted (previously silently dropped) and aon_agent_start returns the resumed message list. Update any custom AsyncDbAdapter implementations to match.
All async hooks support these signatures from the DB adapter:

Common Patterns

Complete Async Lifecycle

Sync Store Compatibility

Native Async Store


Best Practices

Manual aclose() is now safe and idempotent — calling it twice does nothing harmful. Prefer async with for exception-safety and readability, so cleanup runs even when an error is raised mid-block:
close() and aclose() reset the internal stores, so re-entering a with db: ... block after close cleanly re-initializes instead of dispatching to closed handles:
For high-throughput async applications, implement native async methods:
All hooks accept optional metadata dictionaries:
Both sync and async context managers are supported:

Persistence Overview

Complete persistence system documentation

Agent Architecture

Learn about async agent patterns