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
RunContextto 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, eachcontext.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:- 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
PlatformContextStorefor 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. ThePlatformContextStore automatically handles conversation persistence, ensuring that users can continue their conversations even after agent restarts or deployments.
History Contents
Thecontext.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
- User messages: Messages sent by the user
- Assistant messages: Previous responses from your agent
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 byRunContext.load_history(load_history_items=True), Context.list_history or Context.list_all_history.
Message Storage Guidelines
Since messages are not automatically stored, you need to explicitly callcontext.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 incontext.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