Learn how to persist conversations for your agents and networks
HistoryConfig
interface.
The adapter is passed to createAgent()
or createNetwork()
and AgentKit automatically calls your adapter’s methods at the appropriate times during execution.
HistoryConfig
interface has three optional methods. Below is an expanded view of the interface showing the context and parameters passed to each method.
createThread
threadId
exists in the statethreadId
get
results
or messages
AgentResult[]
representing the conversation historyappendResults
history
property when creating an agent or network:
threadId
. AgentKit automatically loads the full conversation context from your database before the network runs.
threadId
. AgentKit fetches the history from your database using history.get()
. The application then hydrates the client-side state with this history.results
or messages
) along with the threadId
. AgentKit detects the client-provided history and skips the database read, resulting in a faster response.threadId
to use, in order of precedence:
threadId
(Highest Priority): The most direct method is to provide a threadId
when you create your state. This is the standard way to resume a specific, existing conversation. AgentKit will use this ID to load the relevant history via the history.get()
method.
createThread
: If you don’t provide a threadId
, AgentKit checks if your history adapter has a createThread
method. If so, AgentKit calls it to create a new conversation thread in your database. Your createThread
function is responsible for generating and returning the new unique threadId
. This is the recommended approach for starting new conversations, as it ensures a record is created in your backend from the very beginning.
threadId
and your history adapter does not have a createThread
method but does have a get
method, AgentKit provides a fallback. It will automatically generate a standard UUID and assign it as the threadId
for the current run. This convenience ensures the conversation can proceed with a unique identifier for saving and loading history, even without an explicit creation step.
Leverage Inngest's Durable Steps
step.run()
for automatic retries and durability.Handle Missing Threads Gracefully
Index Your Database Properly
thread_id
and created_at
columns for fast queries.