When building conversational AI agents, one of the key requirements is maintaining context across multiple interactions. While agent functions are designed to be stateless, Agent Stack provides mechanisms to access and manage conversation history and interactions with agents. The context memory system provided by Agent Stack enables the conversation continuity that users expect from AI assistants.
History Management Control
Simple History Access Example
Here’s an example agent that maintains conversation history and counts the number of interactions:
Steps
- Access conversation history: Use
RunContext to set up an instance of the conversation history to store and load previous messages.
- Store incoming messages: Use
await context.store(input) to store the current user message in the conversation history.
- Filter and process history: Retrieve the conversation history with
load_history() and filter to get the messages relevant to your agent’s logic.
- Store agent responses: Use
await context.store(response) to store your agent’s responses for future conversation context.
Streaming with Buffered History Example
Use this pattern when you want to stream partial outputs to users while keeping one clean assistant message in conversation history.
This is usecase-specific and one my opt for a combination of this and previous approach.
When to use buffering
- Use simple yield + store when your agent emits a single final response.
- Use stream + buffer + single store when your agent emits multiple partial chunks which are streamed to the user.
- With
PlatformContextStore, each context.store() call creates a persisted history item, so buffering prevents chunk-level history fragmentation.
Advanced BeeAI Framework Example
Here’s a sophisticated example using the BeeAI Framework to build a multi-turn chat agent that leverages conversation history and LLM capabilities:
This advanced example demonstrates several key concepts:
- LLM Integration: Uses the platform’s LLM service extension to get model access
- Framework Integration: Leverages the BeeAI Framework for sophisticated agent capabilities
- Memory Management: Converts conversation history to framework format and loads it into agent memory
- Tool Usage: Includes thinking tools and conditional requirements for better reasoning
- Persistent Storage: Uses
PlatformContextStore for conversation persistence
Using Content History
Persistent Storage Example
By default, conversation history is stored in memory and is lost when the agent process restarts. For production applications, you’ll want to use persistent context storage to maintain conversation history across agent restarts. The PlatformContextStore automatically handles conversation persistence, ensuring that users can continue their conversations even after agent restarts or deployments.
History Contents
The context.load_history() method returns an async iterator containing all items in the conversation, including the current message. This can include:
- A2A Messages: Both user and assistant messages from the conversation, including the current A2A message
- Artifacts: Any files, documents, or other artifacts shared during the conversation
For multi-turn conversations, you’ll primarily work with A2A messages, which include:
- User messages: Messages sent by the user
- Assistant messages: Previous responses from your agent
The history includes the current message, so if you want only previous messages, you may need to filter out the last message or use the current message separately.
The history iterator returns all message types. Always filter messages using isinstance(message, Message) to ensure you’re working with the correct message format.
Editing and Removing Messages from History
Sometimes you may need to edit a previous message in a conversation or remove messages that are no longer relevant.
The Agent Stack provides a mechanism to delete history items from a specific point onward, allowing you to effectively “rewind” the conversation and replace a message with an edited version.
Possible use cases include editing a previous message, clearing irrelevant exchanges, or removing messages that resulted from processing errors.
Here’s an example of a function for editing a user message in a conversation using the context API. This assumes you know the context message id, which can be obtained as an id field of an object returned by RunContext.load_history(load_history_items=True), Context.list_history or Context.list_all_history.
When you delete history from a specific message onwards, all messages created after that point (including the message itself) are removed. This effectively creates a new conversation branch starting from the message before the deleted one.
This operation is permanent. Once messages are deleted, they cannot be recovered. Consider informing users about this operation or implementing a confirmation step for important conversations.
Message Storage Guidelines
Since messages are not automatically stored, you need to explicitly call context.store() for any message you want to be available in future interactions. Here are the key guidelines:
Store Request Example
What to Store
Store all of the messages you may want to use later. If you don’t store messages, they won’t be available in context.load_history() for future interactions, causing your agent to lose conversation context. Stored material can include:
- User messages: Always store incoming user messages to maintain conversation context
- Agent responses: Store your agent’s responses so they’re available for future reference
- Important artifacts: Store any files, documents, or other artifacts that should persist
Storage Best Practices
- Store early: Store user messages at the beginning of your agent function
- Store after yielding: Store agent responses after yielding them to the user
- Be selective: Only store messages that are relevant for future conversation context
- Handle errors: Consider what happens if storage fails - your agent should still function