title: “Async DB Hooks” sidebarTitle: “Async DB Hooks” description: “Async database lifecycle hooks for non-blocking persistence in async agents” icon: “database”
Async DB hooks enable non-blocking database operations in async agents through automatic async/sync detection andasyncio.to_thread fallback.
Quick Start
How It Works
| Method | Purpose | Async Detection |
|---|---|---|
aon_agent_start | Agent session start | Checks async_set_agent_state |
aon_user_message | User message logging | Checks async_add_message |
aon_agent_message | Agent response logging | Checks async_add_message |
aon_tool_call | Tool execution logging | Checks async_add_tool_call |
aon_agent_end | Agent session end | Checks async_set_agent_state |
Configuration Options
All async hooks support these signatures from the DB adapter:Common Patterns
Complete Async Lifecycle
Sync Store Compatibility
Native Async Store
Best Practices
Always use async with for cleanup
Always use async with for cleanup
The async context manager ensures proper connection cleanup and transaction handling:
Implement async_* methods for performance
Implement async_* methods for performance
For high-throughput async applications, implement native async methods:
Handle metadata consistently
Handle metadata consistently
All hooks accept optional metadata dictionaries:
Use sync context manager for mixed environments
Use sync context manager for mixed environments
Both sync and async context managers are supported:
Related
Persistence Overview
Complete persistence system documentation
Agent Architecture
Learn about async agent patterns

